Skip to content

Commit d534c47

Browse files
sample() update examples with new API
1 parent cfa96f0 commit d534c47

8 files changed

Lines changed: 81 additions & 60 deletions

File tree

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import {
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';
89
import { Observable, throwError } from 'rxjs';
910
import { catchError } from 'rxjs/operators';
1011

1112
@Injectable()
1213
export 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
}
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
import { Injectable, NestInterceptor, ExecutionContext } from '@nestjs/common';
1+
import {
2+
CallHandler,
3+
ExecutionContext,
4+
Injectable,
5+
NestInterceptor,
6+
} from '@nestjs/common';
27
import { Observable } from 'rxjs';
38
import { tap } from 'rxjs/operators';
49

510
@Injectable()
611
export 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
}
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
import { Injectable, NestInterceptor, ExecutionContext } from '@nestjs/common';
1+
import {
2+
CallHandler,
3+
ExecutionContext,
4+
Injectable,
5+
NestInterceptor,
6+
} from '@nestjs/common';
27
import { Observable } from 'rxjs';
38
import { timeout } from 'rxjs/operators';
49

510
@Injectable()
611
export 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
}

sample/01-cats-app/src/common/interceptors/transform.interceptor.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { Injectable, NestInterceptor, ExecutionContext } from '@nestjs/common';
1+
import {
2+
CallHandler,
3+
ExecutionContext,
4+
Injectable,
5+
NestInterceptor,
6+
} from '@nestjs/common';
27
import { Observable } from 'rxjs';
38
import { 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
}
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
import { Injectable, NestInterceptor, ExecutionContext } from '@nestjs/common';
1+
import {
2+
CallHandler,
3+
ExecutionContext,
4+
Injectable,
5+
NestInterceptor,
6+
} from '@nestjs/common';
27
import { Observable } from 'rxjs';
38
import { tap } from 'rxjs/operators';
49

510
@Injectable()
611
export 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
}
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
import {
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';
89
import { Observable, throwError } from 'rxjs';
910
import { catchError } from 'rxjs/operators';
1011

1112
@Injectable()
1213
export 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
}
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
import { Injectable, NestInterceptor, ExecutionContext } from '@nestjs/common';
1+
import {
2+
CallHandler,
3+
ExecutionContext,
4+
Injectable,
5+
NestInterceptor,
6+
} from '@nestjs/common';
27
import { Observable } from 'rxjs';
38
import { tap } from 'rxjs/operators';
49

510
@Injectable()
611
export 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
}
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
import { Injectable, NestInterceptor, ExecutionContext } from '@nestjs/common';
1+
import {
2+
CallHandler,
3+
ExecutionContext,
4+
Injectable,
5+
NestInterceptor,
6+
} from '@nestjs/common';
27
import { Observable } from 'rxjs';
38
import { map } from 'rxjs/operators';
49

510
@Injectable()
611
export 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
}

0 commit comments

Comments
 (0)