NestJS - Interceptors
You want to measure the time taken by any request handler in your NestJS app. Which interceptor approach correctly adds this cross-cutting timing logic?
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable, tap } from 'rxjs';
@Injectable()
export class TimingInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable {
const start = Date.now();
return next.handle().pipe(
tap(() => {
const elapsed = Date.now() - start;
console.log(`Request took ${elapsed}ms`);
})
);
}
} 