Created
March 27, 2025 18:46
-
-
Save Vatsalya-singhi/4ba40ed998dbf67f8809f71bd6eafacf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common'; | |
import { Observable } from 'rxjs'; | |
import { tap } from 'rxjs/operators'; | |
@Injectable() | |
export class LoggingInterceptor implements NestInterceptor { | |
intercept(context: ExecutionContext, next: CallHandler): Observable<any> { | |
console.log('Before handler execution'); | |
const startTime = new Date(); | |
// Proceed with the request and log the response | |
return next.handle().pipe( | |
map((data)=> ({data:data, timeToCompletion: `${new Date() - startTime} ms` })), | |
tap(() => console.log('After handler execution')) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment