1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.composeInterceptors = exports.invokeInterceptors = exports.GenericInterceptorChain = void 0;
|
8 | const tslib_1 = require("tslib");
|
9 | const debug_1 = tslib_1.__importDefault(require("debug"));
|
10 | const value_promise_1 = require("./value-promise");
|
11 | const debug = (0, debug_1.default)('loopback:context:interceptor-chain');
|
12 |
|
13 |
|
14 |
|
15 | class InterceptorChainState {
|
16 | |
17 |
|
18 |
|
19 |
|
20 |
|
21 | constructor(interceptors, finalHandler = () => undefined) {
|
22 | this.interceptors = interceptors;
|
23 | this.finalHandler = finalHandler;
|
24 | this._index = 0;
|
25 | }
|
26 | |
27 |
|
28 |
|
29 | get index() {
|
30 | return this._index;
|
31 | }
|
32 | |
33 |
|
34 |
|
35 | done() {
|
36 | return this._index === this.interceptors.length;
|
37 | }
|
38 | |
39 |
|
40 |
|
41 | next() {
|
42 | if (this.done()) {
|
43 | throw new Error('No more interceptor is in the chain');
|
44 | }
|
45 | return this.interceptors[this._index++];
|
46 | }
|
47 | }
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | class GenericInterceptorChain {
|
54 |
|
55 | constructor(context, interceptors, comparator) {
|
56 | this.context = context;
|
57 | if (typeof interceptors === 'function') {
|
58 | const interceptorsView = context.createView(interceptors, comparator);
|
59 | this.getInterceptors = () => {
|
60 | const bindings = interceptorsView.bindings;
|
61 | if (comparator) {
|
62 | bindings.sort(comparator);
|
63 | }
|
64 | return bindings.map(b => b.key);
|
65 | };
|
66 | }
|
67 | else if (Array.isArray(interceptors)) {
|
68 | this.getInterceptors = () => interceptors;
|
69 | }
|
70 | }
|
71 | |
72 |
|
73 |
|
74 | invokeInterceptors(finalHandler) {
|
75 |
|
76 | const state = new InterceptorChainState(this.getInterceptors(), finalHandler);
|
77 | return this.next(state);
|
78 | }
|
79 | |
80 |
|
81 |
|
82 | asInterceptor() {
|
83 | return (ctx, next) => {
|
84 | return this.invokeInterceptors(next);
|
85 | };
|
86 | }
|
87 | |
88 |
|
89 |
|
90 | next(state) {
|
91 | if (state.done()) {
|
92 |
|
93 | return state.finalHandler();
|
94 | }
|
95 |
|
96 | return this.invokeNextInterceptor(state);
|
97 | }
|
98 | |
99 |
|
100 |
|
101 | invokeNextInterceptor(state) {
|
102 | const index = state.index;
|
103 | const interceptor = state.next();
|
104 | const interceptorFn = this.loadInterceptor(interceptor);
|
105 | return (0, value_promise_1.transformValueOrPromise)(interceptorFn, fn => {
|
106 |
|
107 | if (debug.enabled) {
|
108 | debug('Invoking interceptor %d (%s) on %s', index, fn.name);
|
109 | }
|
110 | return fn(this.context, () => this.next(state));
|
111 | });
|
112 | }
|
113 | |
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | loadInterceptor(interceptor) {
|
120 | if (typeof interceptor === 'function')
|
121 | return interceptor;
|
122 | debug('Resolving interceptor binding %s', interceptor);
|
123 | return this.context.getValueOrPromise(interceptor);
|
124 | }
|
125 | }
|
126 | exports.GenericInterceptorChain = GenericInterceptorChain;
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | function invokeInterceptors(context, interceptors) {
|
133 | const chain = new GenericInterceptorChain(context, interceptors);
|
134 | return chain.invokeInterceptors();
|
135 | }
|
136 | exports.invokeInterceptors = invokeInterceptors;
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | function composeInterceptors(...interceptors) {
|
142 | return (ctx, next) => {
|
143 | const interceptor = new GenericInterceptorChain(ctx, interceptors).asInterceptor();
|
144 | return interceptor(ctx, next);
|
145 | };
|
146 | }
|
147 | exports.composeInterceptors = composeInterceptors;
|
148 |
|
\ | No newline at end of file |