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
- Describe and validate a request body
- Enable validation globally
- Protect routes with a guard
- Customize error responses
- Enable CORS
- Generate OpenAPI docs
All validation, guards and interceptors commands · NestJS 11 cheatsheet