Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(platform-fastify): add fastify custom log level feature #14470

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/platform-fastify/adapters/fastify-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import { parse as querystringParse } from 'querystring';
import {
FASTIFY_ROUTE_CONFIG_METADATA,
FASTIFY_ROUTE_CONSTRAINTS_METADATA,
FASTIFY_ROUTE_LOG_METADATA,
} from '../constants';
import { NestFastifyBodyParserOptions } from '../interfaces';
import {
Expand Down Expand Up @@ -750,8 +751,15 @@ export class FastifyAdapter<
handlerRef,
);

const routeLog = Reflect.getMetadata(
FASTIFY_ROUTE_LOG_METADATA,
handlerRef,
);


const hasConfig = !isUndefined(routeConfig);
const hasConstraints = !isUndefined(routeConstraints);
const hasLog = !isUndefined(routeLog);

const routeToInject: RouteOptions<TServer, TRawRequest, TRawResponse> &
RouteShorthandOptions = {
Expand All @@ -760,7 +768,7 @@ export class FastifyAdapter<
handler: handlerRef,
};

if (isVersioned || hasConstraints || hasConfig) {
if (isVersioned || hasConstraints || hasConfig || hasLog) {
const isPathAndRouteTuple = args.length === 2;
if (isPathAndRouteTuple) {
const constraints = {
Expand All @@ -777,6 +785,9 @@ export class FastifyAdapter<
...routeConfig,
},
}),
...(hasLog && {
...routeLog,
}),
};

const routeToInjectWithOptions = { ...routeToInject, ...options };
Expand Down
1 change: 1 addition & 0 deletions packages/platform-fastify/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const FASTIFY_ROUTE_CONFIG_METADATA = '__fastify_route_config__';
export const FASTIFY_ROUTE_CONSTRAINTS_METADATA =
'__fastify_route_constraints__';
export const FASTIFY_ROUTE_LOG_METADATA = '__fastify_route_log__';
2 changes: 2 additions & 0 deletions packages/platform-fastify/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './route-config.decorator';
export * from './route-constraints.decorator';
export * from './route-log.decorator';

11 changes: 11 additions & 0 deletions packages/platform-fastify/decorators/route-log.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { SetMetadata } from '@nestjs/common';
import { FASTIFY_ROUTE_LOG_METADATA } from '../constants';
import { LogLevel } from 'fastify';

/**
* @publicApi
*
* @param config See {@link https://fastify.dev/docs/latest/Reference/Routes/#custom-log-level}
*/
export const RouteLog = (config: { logLevel: LogLevel }) =>
SetMetadata(FASTIFY_ROUTE_LOG_METADATA, config);
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { expect } from 'chai';
import { FASTIFY_ROUTE_LOG_METADATA } from '../../constants';
import { RouteLog } from '../../decorators/route-log.decorator';

describe('@RouteConfig', () => {
const routeConfig = { logLevel: 'debug' };
class Test {
config;
@RouteLog(routeConfig)
public static test() {}
}

it('should enhance method with expected fastify route config', () => {
const path = Reflect.getMetadata(FASTIFY_ROUTE_LOG_METADATA, Test.test);
expect(path).to.be.eql(routeConfig);
});
});