Skip to content

Commit cfa96f0

Browse files
bugfix(core) fix interceptors execution flow
1 parent f71613a commit cfa96f0

8 files changed

Lines changed: 48 additions & 32 deletions

File tree

packages/common/cache/interceptors/cache.interceptor.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Observable, of } from 'rxjs';
22
import { tap } from 'rxjs/operators';
33
import { Inject, Injectable, Optional } from '../../decorators';
44
import {
5+
CallHandler,
56
ExecutionContext,
67
HttpServer,
78
NestInterceptor,
@@ -30,20 +31,22 @@ export class CacheInterceptor implements NestInterceptor {
3031

3132
async intercept(
3233
context: ExecutionContext,
33-
call$: Observable<any>,
34+
next: CallHandler,
3435
): Promise<Observable<any>> {
3536
const key = this.trackBy(context);
3637
if (!key) {
37-
return call$;
38+
return next.handle();
3839
}
3940
try {
4041
const value = await this.cacheManager.get(key);
4142
if (value) {
4243
return of(value);
4344
}
44-
return call$.pipe(tap(response => this.cacheManager.set(key, response)));
45+
return next
46+
.handle()
47+
.pipe(tap(response => this.cacheManager.set(key, response)));
4548
} catch {
46-
return call$;
49+
return next.handle();
4750
}
4851
}
4952

packages/common/files/interceptors/file-fields.interceptor.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import {
77
MulterField,
88
MulterOptions,
99
} from '../../interfaces/external/multer-options.interface';
10-
import { NestInterceptor } from '../../interfaces/features/nest-interceptor.interface';
10+
import {
11+
CallHandler,
12+
NestInterceptor,
13+
} from '../../interfaces/features/nest-interceptor.interface';
1114
import { MULTER_MODULE_OPTIONS } from '../files.constants';
1215
import { MulterModuleOptions } from '../interfaces';
1316
import { transformException } from '../multer/multer.utils';
@@ -34,7 +37,7 @@ export function FileFieldsInterceptor(
3437

3538
async intercept(
3639
context: ExecutionContext,
37-
call$: Observable<any>,
40+
next: CallHandler,
3841
): Promise<Observable<any>> {
3942
const ctx = context.switchToHttp();
4043

@@ -51,7 +54,7 @@ export function FileFieldsInterceptor(
5154
},
5255
),
5356
);
54-
return call$;
57+
return next.handle();
5558
}
5659
}
5760
const Interceptor = mixin(MixinInterceptor);

packages/common/files/interceptors/file.interceptor.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { Inject, Optional } from '../../decorators';
44
import { mixin } from '../../decorators/core/component.decorator';
55
import { ExecutionContext } from '../../interfaces';
66
import { MulterOptions } from '../../interfaces/external/multer-options.interface';
7-
import { NestInterceptor } from '../../interfaces/features/nest-interceptor.interface';
7+
import {
8+
CallHandler,
9+
NestInterceptor,
10+
} from '../../interfaces/features/nest-interceptor.interface';
811
import { MULTER_MODULE_OPTIONS } from '../files.constants';
912
import { MulterModuleOptions } from '../interfaces';
1013
import { transformException } from '../multer/multer.utils';
@@ -31,7 +34,7 @@ export function FileInterceptor(
3134

3235
async intercept(
3336
context: ExecutionContext,
34-
call$: Observable<any>,
37+
next: CallHandler,
3538
): Promise<Observable<any>> {
3639
const ctx = context.switchToHttp();
3740

@@ -48,7 +51,7 @@ export function FileInterceptor(
4851
},
4952
),
5053
);
51-
return call$;
54+
return next.handle();
5255
}
5356
}
5457
const Interceptor = mixin(MixinInterceptor);

packages/common/files/interceptors/files.interceptor.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { Inject, Optional } from '../../decorators';
44
import { mixin } from '../../decorators/core/component.decorator';
55
import { ExecutionContext } from '../../interfaces';
66
import { MulterOptions } from '../../interfaces/external/multer-options.interface';
7-
import { NestInterceptor } from '../../interfaces/features/nest-interceptor.interface';
7+
import {
8+
CallHandler,
9+
NestInterceptor,
10+
} from '../../interfaces/features/nest-interceptor.interface';
811
import { MULTER_MODULE_OPTIONS } from '../files.constants';
912
import { MulterModuleOptions } from '../interfaces';
1013
import { transformException } from '../multer/multer.utils';
@@ -32,7 +35,7 @@ export function FilesInterceptor(
3235

3336
async intercept(
3437
context: ExecutionContext,
35-
call$: Observable<any>,
38+
next: CallHandler,
3639
): Promise<Observable<any>> {
3740
const ctx = context.switchToHttp();
3841

@@ -49,7 +52,7 @@ export function FilesInterceptor(
4952
},
5053
),
5154
);
52-
return call$;
55+
return next.handle();
5356
}
5457
}
5558
const Interceptor = mixin(MixinInterceptor);

packages/common/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export * from './http';
1515
export {
1616
ArgumentMetadata,
1717
ArgumentsHost,
18+
CallHandler,
1819
CanActivate,
1920
DynamicModule,
2021
ExceptionFilter,
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { Observable } from 'rxjs';
22
import { ExecutionContext } from './execution-context.interface';
33

4+
export interface CallHandler<T = any> {
5+
handle(): Observable<T>;
6+
}
7+
48
export interface NestInterceptor<T = any, R = any> {
59
intercept(
610
context: ExecutionContext,
7-
call$: Observable<T>,
11+
next: CallHandler<T>,
812
): Observable<R> | Promise<Observable<R>>;
913
}

packages/common/serializer/class-serializer.interceptor.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import { Inject, Injectable } from '../decorators/core';
44
import { ClassTransformOptions } from '../interfaces/external/class-transform-options.interface';
55
import { loadPackage } from '../utils/load-package.util';
66
import { isObject } from '../utils/shared.utils';
7-
import { ExecutionContext, NestInterceptor } from './../interfaces';
7+
import {
8+
CallHandler,
9+
ExecutionContext,
10+
NestInterceptor,
11+
} from './../interfaces';
812
import { CLASS_SERIALIZER_OPTIONS } from './class-serializer.constants';
913

1014
let classTransformer: any = {};
@@ -25,16 +29,15 @@ export class ClassSerializerInterceptor implements NestInterceptor {
2529
classTransformer = loadPkg('class-transformer');
2630
}
2731

28-
intercept(
29-
context: ExecutionContext,
30-
call$: Observable<any>,
31-
): Observable<any> {
32+
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
3233
const options = this.getContextOptions(context);
33-
return call$.pipe(
34-
map((res: PlainLiteralObject | Array<PlainLiteralObject>) =>
35-
this.serialize(res, options),
36-
),
37-
);
34+
return next
35+
.handle()
36+
.pipe(
37+
map((res: PlainLiteralObject | Array<PlainLiteralObject>) =>
38+
this.serialize(res, options),
39+
),
40+
);
3841
}
3942

4043
serialize(

packages/core/interceptors/interceptors-consumer.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,14 @@ export class InterceptorsConsumer {
1818
}
1919
const context = this.createContext(args, instance, callback);
2020
const start$ = defer(() => this.transformDeffered(next));
21-
/***
22-
const nextFn = (i: number) => async () => {
21+
22+
const nextFn = (i = 0) => async () => {
2323
if (i <= interceptors.length) {
2424
return start$;
2525
}
26-
return await interceptors[i].intercept(context, nextFn(i + 1) as any);
26+
return interceptors[i].intercept(context, nextFn(i + 1) as any);
2727
};
28-
*/
29-
const result$ = await interceptors.reduce(
30-
async (stream$, interceptor) => interceptor.intercept(context, await stream$),
31-
Promise.resolve(start$),
32-
);
28+
const result$ = await nextFn()();
3329
return result$.toPromise();
3430
}
3531

0 commit comments

Comments
 (0)