1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | import {DecoratorFactory} from '@loopback/metadata';
|
7 | import assert from 'assert';
|
8 | import debugModule from 'debug';
|
9 | import {isBindingAddress} from './binding-filter';
|
10 | import {BindingAddress} from './binding-key';
|
11 | import {Context} from './context';
|
12 | import {
|
13 | describeInjectedArguments,
|
14 | describeInjectedProperties,
|
15 | Injection,
|
16 | } from './inject';
|
17 | import {
|
18 | ResolutionError,
|
19 | ResolutionOptions,
|
20 | ResolutionSession,
|
21 | } from './resolution-session';
|
22 | import {
|
23 | BoundValue,
|
24 | Constructor,
|
25 | MapObject,
|
26 | resolveList,
|
27 | resolveMap,
|
28 | transformValueOrPromise,
|
29 | ValueOrPromise,
|
30 | } from './value-promise';
|
31 |
|
32 | const debug = debugModule('loopback:context:resolver');
|
33 | const getTargetName = DecoratorFactory.getTargetName;
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | export function instantiateClass<T extends object>(
|
48 | ctor: Constructor<T>,
|
49 | ctx: Context,
|
50 | session?: ResolutionSession,
|
51 |
|
52 | nonInjectedArgs?: any[],
|
53 | ): ValueOrPromise<T> {
|
54 |
|
55 | if (debug.enabled) {
|
56 | debug('Instantiating %s', getTargetName(ctor));
|
57 | if (nonInjectedArgs?.length) {
|
58 | debug('Non-injected arguments:', nonInjectedArgs);
|
59 | }
|
60 | }
|
61 | const argsOrPromise = resolveInjectedArguments(
|
62 | ctor,
|
63 | '',
|
64 | ctx,
|
65 | session,
|
66 | nonInjectedArgs,
|
67 | );
|
68 | const propertiesOrPromise = resolveInjectedProperties(ctor, ctx, session);
|
69 | const inst: ValueOrPromise<T> = transformValueOrPromise(
|
70 | argsOrPromise,
|
71 | args => {
|
72 |
|
73 | if (debug.enabled) {
|
74 | debug('Injected arguments for %s():', ctor.name, args);
|
75 | }
|
76 | return new ctor(...args);
|
77 | },
|
78 | );
|
79 | return transformValueOrPromise(propertiesOrPromise, props => {
|
80 |
|
81 | if (debug.enabled) {
|
82 | debug('Injected properties for %s:', ctor.name, props);
|
83 | }
|
84 | return transformValueOrPromise<T, T>(inst, obj =>
|
85 | Object.assign(obj, props),
|
86 | );
|
87 | });
|
88 | }
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | function resolveContext(
|
97 | ctx: Context,
|
98 | injection: Readonly<Injection>,
|
99 | session?: ResolutionSession,
|
100 | ) {
|
101 | const currentBinding = session?.currentBinding;
|
102 | if (currentBinding == null) {
|
103 |
|
104 | return ctx;
|
105 | }
|
106 |
|
107 | const isConstructorOrPropertyInjection =
|
108 |
|
109 | !injection.member ||
|
110 |
|
111 | typeof injection.methodDescriptorOrParameterIndex !== 'number';
|
112 |
|
113 | if (isConstructorOrPropertyInjection) {
|
114 |
|
115 |
|
116 | ctx = ctx.getResolutionContext(currentBinding)!;
|
117 | }
|
118 | return ctx;
|
119 | }
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | function resolve<T>(
|
128 | ctx: Context,
|
129 | injection: Readonly<Injection>,
|
130 | session?: ResolutionSession,
|
131 | ): ValueOrPromise<T> {
|
132 |
|
133 | if (debug.enabled) {
|
134 | debug(
|
135 | 'Resolving an injection:',
|
136 | ResolutionSession.describeInjection(injection),
|
137 | );
|
138 | }
|
139 |
|
140 | ctx = resolveContext(ctx, injection, session);
|
141 | const resolved = ResolutionSession.runWithInjection(
|
142 | s => {
|
143 | if (injection.resolve) {
|
144 |
|
145 | return injection.resolve(ctx, injection, s);
|
146 | } else {
|
147 |
|
148 | assert(
|
149 | isBindingAddress(injection.bindingSelector),
|
150 | 'The binding selector must be an address (string or BindingKey)',
|
151 | );
|
152 | const key = injection.bindingSelector as BindingAddress;
|
153 | const options: ResolutionOptions = {
|
154 | session: s,
|
155 | ...injection.metadata,
|
156 | };
|
157 | return ctx.getValueOrPromise(key, options);
|
158 | }
|
159 | },
|
160 | injection,
|
161 | session,
|
162 | );
|
163 | return resolved;
|
164 | }
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 | export function resolveInjectedArguments(
|
183 | target: object,
|
184 | method: string,
|
185 | ctx: Context,
|
186 | session?: ResolutionSession,
|
187 |
|
188 | nonInjectedArgs?: any[],
|
189 | ): ValueOrPromise<BoundValue[]> {
|
190 |
|
191 | if (debug.enabled) {
|
192 | debug('Resolving injected arguments for %s', getTargetName(target, method));
|
193 | }
|
194 | const targetWithMethods = <{[method: string]: Function}>target;
|
195 | if (method) {
|
196 | assert(
|
197 | typeof targetWithMethods[method] === 'function',
|
198 | `Method ${method} not found`,
|
199 | );
|
200 | }
|
201 |
|
202 |
|
203 |
|
204 |
|
205 | const injectedArgs = describeInjectedArguments(target, method);
|
206 | const extraArgs = nonInjectedArgs ?? [];
|
207 |
|
208 | let argLength = DecoratorFactory.getNumberOfParameters(target, method);
|
209 |
|
210 |
|
211 | const numberOfInjected = injectedArgs.filter(i => i != null).length;
|
212 | if (argLength < numberOfInjected + extraArgs.length) {
|
213 | |
214 |
|
215 |
|
216 |
|
217 |
|
218 | argLength = numberOfInjected + extraArgs.length;
|
219 | }
|
220 |
|
221 | let nonInjectedIndex = 0;
|
222 | return resolveList(new Array(argLength), (val, ix) => {
|
223 |
|
224 |
|
225 | const injection = ix < injectedArgs.length ? injectedArgs[ix] : undefined;
|
226 | if (
|
227 | injection == null ||
|
228 | (!injection.bindingSelector && !injection.resolve)
|
229 | ) {
|
230 | if (nonInjectedIndex < extraArgs.length) {
|
231 |
|
232 | return extraArgs[nonInjectedIndex++];
|
233 | } else {
|
234 | const name = getTargetName(target, method, ix);
|
235 | throw new ResolutionError(
|
236 | `The argument '${name}' is not decorated for dependency injection ` +
|
237 | 'but no value was supplied by the caller. Did you forget to apply ' +
|
238 | '@inject() to the argument?',
|
239 | {context: ctx, options: {session}},
|
240 | );
|
241 | }
|
242 | }
|
243 |
|
244 | return resolve(
|
245 | ctx,
|
246 | injection,
|
247 |
|
248 | ResolutionSession.fork(session),
|
249 | );
|
250 | });
|
251 | }
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 | export function resolveInjectedProperties(
|
266 | constructor: Function,
|
267 | ctx: Context,
|
268 | session?: ResolutionSession,
|
269 | ): ValueOrPromise<MapObject<BoundValue>> {
|
270 |
|
271 | if (debug.enabled) {
|
272 | debug('Resolving injected properties for %s', getTargetName(constructor));
|
273 | }
|
274 | const injectedProperties = describeInjectedProperties(constructor.prototype);
|
275 |
|
276 | return resolveMap(injectedProperties, injection =>
|
277 | resolve(
|
278 | ctx,
|
279 | injection,
|
280 | // Clone the session so that multiple properties can be resolved in parallel
|
281 | ResolutionSession.fork(session),
|
282 | ),
|
283 | );
|
284 | }
|