03 / 06

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

Difficulty: 5/10
interceptors, global providers, response formatting

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.

Scenario Questions

0-2 years experience

  1. 1We have a simple NestJS controller that returns a plain object. How would you add an interceptor so that every response is wrapped in { data: ..., success: true }?
  2. 2If you applied your interceptor globally, what would happen to a 404 error response? How can you ensure error responses keep their original shape?
  3. 3Where do you register an interceptor in NestJS so that it runs for all routes?

2-5 years experience

  1. 1You need to add pagination metadata to every list endpoint without changing each controller. Walk me through how you'd implement an interceptor to inject { meta: { page, limit }, data: [...] } and discuss any trade‑offs.
  2. 2During a code review you notice that after adding a response‑enveloping interceptor, a third‑party library that expects raw JSON is failing. How would you debug and fix the issue?
  3. 3Explain why using @UseInterceptors at the controller level versus setting the interceptor as a global provider might affect performance or testability.

5-8 years experience

  1. 1Our service handles high‑throughput streaming responses. How would you design an interceptor that wraps responses without introducing significant latency or breaking streaming?
  2. 2Consider a microservices architecture where some services return binary files (e.g., PDFs). How would you adapt a global response‑enveloping interceptor to handle both JSON and binary payloads correctly?
  3. 3What are the implications of using an interceptor that mutates the response object for NestJS’s built‑in exception filters and logging mechanisms? How would you mitigate any conflicts?

8+ years experience

  1. 1We are migrating a legacy monolith to a NestJS‑based platform and want to enforce a unified response envelope across all teams. What governance, tooling, and architectural patterns would you put in place to ensure consistency and allow teams to opt‑out when necessary?
  2. 2Discuss the long‑term maintenance impact of a global response‑enveloping interceptor on version upgrades of NestJS and third‑party libraries. How would you structure the code to minimize breakage?
  3. 3If multiple cross‑functional teams need different envelope schemas (e.g., one for internal APIs, another for public APIs), how would you design a flexible interceptor strategy that supports per‑module configuration while keeping a single source of truth?

Follow-up Questions

  • How would you test that the interceptor correctly wraps both success and error payloads?
  • What adjustments would you make if different modules need slightly different envelope structures?
  • Can you talk about any performance or memory considerations when applying this globally?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.