NestInterceptor

Interface for interceptors that wrap handler execution with RxJS-based logic.

Since NestJS 10/11 Spec ↗

Syntax

intercept(context: ExecutionContext, next: CallHandler): Observable<any>

Parameters

NameTypeRequiredDescription
context ExecutionContext No The execution context for the current request.
next CallHandler No Provides next.handle() returning the handler result stream.

Returns

Observable<any> — The (possibly transformed) response stream.

Examples

import {
  Injectable, NestInterceptor, ExecutionContext, CallHandler,
} from '@nestjs/common';
import { map, Observable } from 'rxjs';

@Injectable()
export class WrapInterceptor implements NestInterceptor {
  intercept(_ctx: ExecutionContext, next: CallHandler): Observable<any> {
    return next.handle().pipe(map((data) => ({ data, ok: true })));
  }
}

Notes

Code before next.handle() runs before the route handler; operators piped after run on its result. Use tap() for logging, map() for response shaping, timeout() for deadlines, and catchError() for error mapping.