🦅 NestJS Beginner

What are Interceptors in NestJS?

Answer

Interceptors are classes decorated with @Injectable() that implement the NestInterceptor interface. They can: transform the result returned from a route handler, transform exceptions, extend behavior, or completely override behavior. They wrap the execution pipeline using RxJS Observables. The intercept() method receives the execution context and a CallHandler — call next.handle() to proceed, then pipe the observable to transform the response. Common uses: response transformation (wrap all responses in a standard envelope), logging execution time, caching, timeout, and serialization. Apply with @UseInterceptors() or globally in main.ts.