Skip to content
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
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
```

Expand Down Expand Up @@ -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';
Expand Down
53 changes: 53 additions & 0 deletions __tests__/use-legacy-wildcard-route.spec.ts
Original file line number Diff line number Diff line change
@@ -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>(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>(LoggerModule);

const consumer = createMockConsumer();
loggerModule.configure(consumer);

expect(forRoutesSpy).toHaveBeenCalledWith(
expect.objectContaining({ path: '*' }),
);
});
});
13 changes: 6 additions & 7 deletions src/LoggerModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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] })
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions src/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down