UNPKG

1.53 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
4const rxjs_1 = require("rxjs");
5const operators_1 = require("rxjs/operators");
6const execution_context_host_1 = require("../helpers/execution-context-host");
7class 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}
36exports.InterceptorsConsumer = InterceptorsConsumer;