03 / 06

How do you use an interceptor to transform every response into a standard envelope format in NestJS?

Implement NestInterceptor with a generic type, use map() in the RxJS pipeline to wrap every handler return value in a standard object. Register globally via APP_INTERCEPTOR or app.useGlobalInterceptors() so every endpoint returns the same shape.

Response transform interceptor
Global registration options:
  1. 1

    app.useGlobalInterceptors(new TransformInterceptor()) — no DI support.

  2. 2

    { provide: APP_INTERCEPTOR, useClass: TransformInterceptor } — full DI support, preferred.

  3. 3

    The map() operator transforms every emitted value from the handler's observable.

  4. 4

    null and undefined handler returns are also passed through map() — handle them if needed.