diff --git a/README.md b/README.md index 1b87c1b0..b50cd071 100644 --- a/README.md +++ b/README.md @@ -229,6 +229,14 @@ interface Params { * {"level":30, ... "RENAME_CONTEXT_VALUE_HERE":"AppController" } */ renameContext?: string; + + /** + * Optional parameter to use legacy express wildcard route handling. If you + * are using a version of NestJS that does not support the new wildcard + * routes, you can enable this option to use a legacy implementation. This + * option is not recommended for new projects. + */ + useLegacyWildcardRoute?: boolean; } ``` @@ -457,7 +465,7 @@ class TestController { ## Expose stack trace and error class in `err` property -By default, `pino-http` exposes `err` property with a stack trace and error details, however, this `err` property contains default error details, which do not tell anything about actual error. To expose actual error details you need you to use a NestJS interceptor which captures exceptions and assigns them to the response object `err` property which is later processed by pino-http: +By default, `pino-http` exposes `err` property with a stack trace and error details, however, this `err` property contains default error details, which do not tell anything about actual error. To expose actual error details you need you to use a NestJS interceptor which captures exceptions and assigns them to the response object `err` property which is later processed by pino-http: ```typescript import { LoggerErrorInterceptor } from 'nestjs-pino'; diff --git a/__tests__/use-legacy-wildcard-route.spec.ts b/__tests__/use-legacy-wildcard-route.spec.ts new file mode 100644 index 00000000..f9193a54 --- /dev/null +++ b/__tests__/use-legacy-wildcard-route.spec.ts @@ -0,0 +1,53 @@ +import { MiddlewareConsumer } from '@nestjs/common'; +import { Test, TestingModule } from '@nestjs/testing'; + +import { LoggerModule, Params } from '../src'; + +describe('use legacy wildcard route', () => { + let forRoutesSpy: jest.Mock; + let moduleRef: TestingModule; + + beforeEach(async () => { + forRoutesSpy = jest.fn(); + }); + + function createMockConsumer(): MiddlewareConsumer { + return { + apply: jest.fn().mockReturnThis(), + exclude: jest.fn().mockReturnThis(), + forRoutes: forRoutesSpy, + } as MiddlewareConsumer; + } + + async function createModule(params?: Params) { + const module = await Test.createTestingModule({ + imports: [LoggerModule.forRoot(params)], + }).compile(); + + return module; + } + + it('should use `/{*splat}` when useLegacyWildcardRoute is false', async () => { + moduleRef = await createModule({ useLegacyWildcardRoute: false }); + const loggerModule = moduleRef.get(LoggerModule); + + const consumer = createMockConsumer(); + loggerModule.configure(consumer); + + expect(forRoutesSpy).toHaveBeenCalledWith( + expect.objectContaining({ path: '/{*splat}' }), + ); + }); + + it('should use `*` when useLegacyWildcardRoute is true', async () => { + moduleRef = await createModule({ useLegacyWildcardRoute: true }); + const loggerModule = moduleRef.get(LoggerModule); + + const consumer = createMockConsumer(); + loggerModule.configure(consumer); + + expect(forRoutesSpy).toHaveBeenCalledWith( + expect.objectContaining({ path: '*' }), + ); + }); +}); diff --git a/src/LoggerModule.ts b/src/LoggerModule.ts index f7fe940a..a499ff2c 100644 --- a/src/LoggerModule.ts +++ b/src/LoggerModule.ts @@ -23,12 +23,8 @@ import { import { PinoLogger } from './PinoLogger'; import { Store, storage } from './storage'; -/** - * As NestJS@11 still supports express@4 `*`-style routing by itself let's keep - * it for the backward compatibility. On the next major NestJS release `*` we - * can replace it with `/{*splat}`, and drop the support for NestJS@9 and below. - */ -const DEFAULT_ROUTES = [{ path: '*', method: RequestMethod.ALL }]; +const DEFAULT_ROUTES = [{ path: '/{*splat}', method: RequestMethod.ALL }]; +const LEGACY_DEFAULT_ROUTES = [{ path: '*', method: RequestMethod.ALL }]; @Global() @Module({ providers: [Logger], exports: [Logger] }) @@ -78,7 +74,10 @@ export class LoggerModule implements NestModule { configure(consumer: MiddlewareConsumer) { const { exclude, - forRoutes = DEFAULT_ROUTES, + useLegacyWildcardRoute, + forRoutes = useLegacyWildcardRoute + ? LEGACY_DEFAULT_ROUTES + : DEFAULT_ROUTES, pinoHttp, useExisting, assignResponse, diff --git a/src/params.ts b/src/params.ts index 049605c2..6db9e1da 100644 --- a/src/params.ts +++ b/src/params.ts @@ -61,6 +61,14 @@ export interface Params { * (e.g.`Request completed`). */ assignResponse?: boolean; + + /** + * Optional parameter to use legacy express wildcard route handling. If you + * are using a version of NestJS that does not support the new wildcard + * routes, you can enable this option to use a legacy implementation. This + * option is not recommended for new projects. + */ + useLegacyWildcardRoute?: boolean; } // for support of nestjs@8 we don't use