NestJS 11 · Validation, guards and interceptors

Run code around handlers

An interceptor can log, transform responses or time requests.

@Injectable()
export class TimingInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler) {
    const start = Date.now();
    return next.handle().pipe(tap(() => console.log(`took ${Date.now() - start}ms`)));
  }
}

Official NestJS 11 documentation

More validation, guards and interceptors commands

All validation, guards and interceptors commands · NestJS 11 cheatsheet

Was this page helpful?