1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
4 | const rxjs_1 = require("rxjs");
|
5 | const operators_1 = require("rxjs/operators");
|
6 | const execution_context_host_1 = require("../helpers/execution-context-host");
|
7 | class InterceptorsConsumer {
|
8 | async intercept(interceptors, args, instance, callback, next, type) {
|
9 | if (shared_utils_1.isEmpty(interceptors)) {
|
10 | return next();
|
11 | }
|
12 | const context = this.createContext(args, instance, callback);
|
13 | context.setType(type);
|
14 | const start$ = rxjs_1.defer(() => this.transformDeffered(next));
|
15 | const nextFn = (i = 0) => async () => {
|
16 | if (i >= interceptors.length) {
|
17 | return start$;
|
18 | }
|
19 | const handler = {
|
20 | handle: () => rxjs_1.from(nextFn(i + 1)()).pipe(operators_1.mergeAll()),
|
21 | };
|
22 | return interceptors[i].intercept(context, handler);
|
23 | };
|
24 | return nextFn()();
|
25 | }
|
26 | createContext(args, instance, callback) {
|
27 | return new execution_context_host_1.ExecutionContextHost(args, instance.constructor, callback);
|
28 | }
|
29 | transformDeffered(next) {
|
30 | return rxjs_1.from(next()).pipe(operators_1.switchMap(res => {
|
31 | const isDeffered = res instanceof Promise || res instanceof rxjs_1.Observable;
|
32 | return isDeffered ? res : Promise.resolve(res);
|
33 | }));
|
34 | }
|
35 | }
|
36 | exports.InterceptorsConsumer = InterceptorsConsumer;
|