Skip to content

CallHandler

reference
1 min readUpdated

Kind: Interface

Source: packages/common/interfaces/features/nest-interceptor.interface.ts

Part of: Common

Interface providing access to the response stream.

CallHandler represents the downstream execution pipeline in a NestJS interceptor. It provides access to the response stream through handle(), allowing interceptors to transform, observe, or replace values returned by route handlers.

Diagram

mermaid
graph LR
  Client[Client Request] --> Interceptor[Nest Interceptor]
  Interceptor --> Handler[CallHandler]
  Handler --> Controller[Route Handler]
  Controller --> Stream[Observable Response Stream]
  Stream --> Interceptor
  Interceptor --> Client

Usage

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

@Injectable()
export class ResponseWrapperInterceptor implements NestInterceptor {
  intercept(
    context: ExecutionContext,
    next: CallHandler,
  ): Observable<{ data: unknown }> {
    return next.handle().pipe(
      map((response) => ({
        data: response,
      })),
    );
  }
}

AI Coding Instructions

  • Call next.handle() to continue execution to the controller or next interceptor and receive an Observable response stream.
  • Use RxJS operators such as map, tap, catchError, and timeout to modify or monitor responses without subscribing manually.
  • Return the transformed Observable; do not call subscribe() inside an interceptor.
  • Treat CallHandler as the boundary between interceptor logic and the downstream route handler execution.

Used by

32 references from 32 files. Each is a place in this repository where the symbol is actually used — go read one rather than trusting an example.

Imported by (32)

  • DataInterceptorintegration/graphql-code-first/src/common/interceptors/data.interceptor.ts:10
  • Interceptorintegration/inspector/src/circular-hello/interceptors/logging.interceptor.ts:10
  • TimeoutInterceptorintegration/inspector/src/common/interceptors/timeout.interceptor.ts:10
  • LoggingInterceptorintegration/inspector/src/core/interceptors/logging.interceptor.ts:10
  • TransformInterceptorintegration/inspector/src/core/interceptors/transform.interceptor.ts:14
  • LoggingInterceptorintegration/inspector/src/request-chain/interceptors/logging.interceptor.ts:10
  • PassthroughInterceptorintegration/nest-application/sse/src/app.controller.ts:28
  • Interceptorintegration/scopes/src/circular-hello/interceptors/logging.interceptor.ts:10

…and 24 more.

Was this page helpful?

Download as PDF
CallHandler — NestJS head-to-head