File tree Expand file tree Collapse file tree
01-cats-app/src/common/interceptors
03-microservices/src/common/interceptors
10-fastify/src/common/interceptors Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import {
2- Injectable ,
3- NestInterceptor ,
2+ CallHandler ,
43 ExecutionContext ,
4+ HttpException ,
55 HttpStatus ,
6+ Injectable ,
7+ NestInterceptor ,
68} from '@nestjs/common' ;
7- import { HttpException } from '@nestjs/common' ;
89import { Observable , throwError } from 'rxjs' ;
910import { catchError } from 'rxjs/operators' ;
1011
1112@Injectable ( )
1213export class ErrorsInterceptor implements NestInterceptor {
13- intercept (
14- context : ExecutionContext ,
15- call$ : Observable < any > ,
16- ) : Observable < any > {
17- return call$ . pipe (
18- catchError ( err =>
19- throwError ( new HttpException ( 'Message' , HttpStatus . BAD_GATEWAY ) ) ,
20- ) ,
21- ) ;
14+ intercept ( context : ExecutionContext , next : CallHandler ) : Observable < any > {
15+ return next
16+ . handle ( )
17+ . pipe (
18+ catchError ( err =>
19+ throwError ( new HttpException ( 'New message' , HttpStatus . BAD_GATEWAY ) ) ,
20+ ) ,
21+ ) ;
2222 }
2323}
Original file line number Diff line number Diff line change 1- import { Injectable , NestInterceptor , ExecutionContext } from '@nestjs/common' ;
1+ import {
2+ CallHandler ,
3+ ExecutionContext ,
4+ Injectable ,
5+ NestInterceptor ,
6+ } from '@nestjs/common' ;
27import { Observable } from 'rxjs' ;
38import { tap } from 'rxjs/operators' ;
49
510@Injectable ( )
611export class LoggingInterceptor implements NestInterceptor {
7- intercept (
8- context : ExecutionContext ,
9- call$ : Observable < any > ,
10- ) : Observable < any > {
12+ intercept ( context : ExecutionContext , next : CallHandler ) : Observable < any > {
1113 console . log ( 'Before...' ) ;
1214
1315 const now = Date . now ( ) ;
14- return call$ . pipe ( tap ( ( ) => console . log ( `After... ${ Date . now ( ) - now } ms` ) ) ) ;
16+ return next
17+ . handle ( )
18+ . pipe ( tap ( ( ) => console . log ( `After... ${ Date . now ( ) - now } ms` ) ) ) ;
1519 }
1620}
Original file line number Diff line number Diff line change 1- import { Injectable , NestInterceptor , ExecutionContext } from '@nestjs/common' ;
1+ import {
2+ CallHandler ,
3+ ExecutionContext ,
4+ Injectable ,
5+ NestInterceptor ,
6+ } from '@nestjs/common' ;
27import { Observable } from 'rxjs' ;
38import { timeout } from 'rxjs/operators' ;
49
510@Injectable ( )
611export class TimeoutInterceptor implements NestInterceptor {
7- intercept (
8- context : ExecutionContext ,
9- call$ : Observable < any > ,
10- ) : Observable < any > {
11- return call$ . pipe ( timeout ( 5000 ) ) ;
12+ intercept ( context : ExecutionContext , next : CallHandler ) : Observable < any > {
13+ return next . handle ( ) . pipe ( timeout ( 5000 ) ) ;
1214 }
1315}
Original file line number Diff line number Diff line change 1- import { Injectable , NestInterceptor , ExecutionContext } from '@nestjs/common' ;
1+ import {
2+ CallHandler ,
3+ ExecutionContext ,
4+ Injectable ,
5+ NestInterceptor ,
6+ } from '@nestjs/common' ;
27import { Observable } from 'rxjs' ;
38import { map } from 'rxjs/operators' ;
49
@@ -11,8 +16,8 @@ export class TransformInterceptor<T>
1116 implements NestInterceptor < T , Response < T > > {
1217 intercept (
1318 context : ExecutionContext ,
14- call$ : Observable < T > ,
19+ next : CallHandler < T > ,
1520 ) : Observable < Response < T > > {
16- return call$ . pipe ( map ( data => ( { data } ) ) ) ;
21+ return next . handle ( ) . pipe ( map ( data => ( { data } ) ) ) ;
1722 }
1823}
Original file line number Diff line number Diff line change 1- import { Injectable , NestInterceptor , ExecutionContext } from '@nestjs/common' ;
1+ import {
2+ CallHandler ,
3+ ExecutionContext ,
4+ Injectable ,
5+ NestInterceptor ,
6+ } from '@nestjs/common' ;
27import { Observable } from 'rxjs' ;
38import { tap } from 'rxjs/operators' ;
49
510@Injectable ( )
611export class LoggingInterceptor implements NestInterceptor {
7- intercept (
8- context : ExecutionContext ,
9- call$ : Observable < any > ,
10- ) : Observable < any > {
12+ intercept ( context : ExecutionContext , next : CallHandler ) : Observable < any > {
1113 console . log ( 'Before...' ) ;
1214
1315 const now = Date . now ( ) ;
14- return call$ . pipe ( tap ( ( ) => console . log ( `After... ${ Date . now ( ) - now } ms` ) ) ) ;
16+ return next
17+ . handle ( )
18+ . pipe ( tap ( ( ) => console . log ( `After... ${ Date . now ( ) - now } ms` ) ) ) ;
1519 }
1620}
Original file line number Diff line number Diff line change 11import {
2- Injectable ,
3- NestInterceptor ,
2+ CallHandler ,
43 ExecutionContext ,
4+ HttpException ,
55 HttpStatus ,
6+ Injectable ,
7+ NestInterceptor ,
68} from '@nestjs/common' ;
7- import { HttpException } from '@nestjs/common' ;
89import { Observable , throwError } from 'rxjs' ;
910import { catchError } from 'rxjs/operators' ;
1011
1112@Injectable ( )
1213export class ExceptionInterceptor implements NestInterceptor {
13- intercept (
14- context : ExecutionContext ,
15- call$ : Observable < any > ,
16- ) : Observable < any > {
17- return call$ . pipe (
18- catchError ( err =>
19- throwError (
20- new HttpException (
21- 'Exception interceptor message' ,
22- HttpStatus . BAD_GATEWAY ,
14+ intercept ( context : ExecutionContext , next : CallHandler ) : Observable < any > {
15+ return next
16+ . handle ( )
17+ . pipe (
18+ catchError ( err =>
19+ throwError (
20+ new HttpException (
21+ 'Exception interceptor message' ,
22+ HttpStatus . BAD_GATEWAY ,
23+ ) ,
2324 ) ,
2425 ) ,
25- ) ,
26- ) ;
26+ ) ;
2727 }
2828}
Original file line number Diff line number Diff line change 1- import { Injectable , NestInterceptor , ExecutionContext } from '@nestjs/common' ;
1+ import {
2+ CallHandler ,
3+ ExecutionContext ,
4+ Injectable ,
5+ NestInterceptor ,
6+ } from '@nestjs/common' ;
27import { Observable } from 'rxjs' ;
38import { tap } from 'rxjs/operators' ;
49
510@Injectable ( )
611export class LoggingInterceptor implements NestInterceptor {
7- intercept (
8- context : ExecutionContext ,
9- call$ : Observable < any > ,
10- ) : Observable < any > {
12+ intercept ( context : ExecutionContext , next : CallHandler ) : Observable < any > {
1113 console . log ( 'Before...' ) ;
1214
1315 const now = Date . now ( ) ;
14- return call$ . pipe ( tap ( ( ) => console . log ( `After... ${ Date . now ( ) - now } ms` ) ) ) ;
16+ return next
17+ . handle ( )
18+ . pipe ( tap ( ( ) => console . log ( `After... ${ Date . now ( ) - now } ms` ) ) ) ;
1519 }
1620}
Original file line number Diff line number Diff line change 1- import { Injectable , NestInterceptor , ExecutionContext } from '@nestjs/common' ;
1+ import {
2+ CallHandler ,
3+ ExecutionContext ,
4+ Injectable ,
5+ NestInterceptor ,
6+ } from '@nestjs/common' ;
27import { Observable } from 'rxjs' ;
38import { map } from 'rxjs/operators' ;
49
510@Injectable ( )
611export class TransformInterceptor implements NestInterceptor {
7- intercept (
8- context : ExecutionContext ,
9- call$ : Observable < any > ,
10- ) : Observable < any > {
11- return call$ . pipe ( map ( data => ( { data } ) ) ) ;
12+ intercept ( context : ExecutionContext , next : CallHandler ) : Observable < any > {
13+ return next . handle ( ) . pipe ( map ( data => ( { data } ) ) ) ;
1214 }
1315}
You can’t perform that action at this time.
0 commit comments