1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.hasInjections = exports.inspectInjections = exports.describeInjectedProperties = exports.inspectTargetType = exports.describeInjectedArguments = exports.assertTargetType = exports.Getter = exports.inject = void 0;
|
8 | const metadata_1 = require("@loopback/metadata");
|
9 | const binding_1 = require("./binding");
|
10 | const binding_filter_1 = require("./binding-filter");
|
11 | const context_view_1 = require("./context-view");
|
12 | const resolution_session_1 = require("./resolution-session");
|
13 | const INJECT_PARAMETERS_KEY = metadata_1.MetadataAccessor.create('inject:parameters');
|
14 | const INJECT_PROPERTIES_KEY = metadata_1.MetadataAccessor.create('inject:properties');
|
15 |
|
16 | const INJECT_METHODS_KEY = metadata_1.MetadataAccessor.create('inject:methods');
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | function inject(bindingSelector, metadata, resolve) {
|
45 | if (typeof bindingSelector === 'function' && !resolve) {
|
46 | resolve = resolveValuesByFilter;
|
47 | }
|
48 | const injectionMetadata = Object.assign({ decorator: '@inject' }, metadata);
|
49 | if (injectionMetadata.bindingComparator && !resolve) {
|
50 | throw new Error('Binding comparator is only allowed with a binding filter');
|
51 | }
|
52 | if (!bindingSelector && typeof resolve !== 'function') {
|
53 | throw new Error('A non-empty binding selector or resolve function is required for @inject');
|
54 | }
|
55 | return function markParameterOrPropertyAsInjected(target, member, methodDescriptorOrParameterIndex) {
|
56 | if (typeof methodDescriptorOrParameterIndex === 'number') {
|
57 |
|
58 |
|
59 | const paramDecorator = metadata_1.ParameterDecoratorFactory.createDecorator(INJECT_PARAMETERS_KEY, {
|
60 | target,
|
61 | member,
|
62 | methodDescriptorOrParameterIndex,
|
63 | bindingSelector,
|
64 | metadata: injectionMetadata,
|
65 | resolve,
|
66 | },
|
67 |
|
68 |
|
69 | { cloneInputSpec: false, decoratorName: injectionMetadata.decorator });
|
70 | paramDecorator(target, member, methodDescriptorOrParameterIndex);
|
71 | }
|
72 | else if (member) {
|
73 |
|
74 | if (target instanceof Function) {
|
75 | throw new Error('@inject is not supported for a static property: ' +
|
76 | metadata_1.DecoratorFactory.getTargetName(target, member));
|
77 | }
|
78 | if (methodDescriptorOrParameterIndex) {
|
79 |
|
80 | throw new Error('@inject cannot be used on a method: ' +
|
81 | metadata_1.DecoratorFactory.getTargetName(target, member, methodDescriptorOrParameterIndex));
|
82 | }
|
83 | const propDecorator = metadata_1.PropertyDecoratorFactory.createDecorator(INJECT_PROPERTIES_KEY, {
|
84 | target,
|
85 | member,
|
86 | methodDescriptorOrParameterIndex,
|
87 | bindingSelector,
|
88 | metadata: injectionMetadata,
|
89 | resolve,
|
90 | },
|
91 |
|
92 |
|
93 | { cloneInputSpec: false, decoratorName: injectionMetadata.decorator });
|
94 | propDecorator(target, member);
|
95 | }
|
96 | else {
|
97 |
|
98 |
|
99 | throw new Error('@inject can only be used on a property or a method parameter');
|
100 | }
|
101 | };
|
102 | }
|
103 | exports.inject = inject;
|
104 | var Getter;
|
105 | (function (Getter) {
|
106 | |
107 |
|
108 |
|
109 |
|
110 | function fromValue(value) {
|
111 | return () => Promise.resolve(value);
|
112 | }
|
113 | Getter.fromValue = fromValue;
|
114 | })(Getter || (exports.Getter = Getter = {}));
|
115 | (function (inject) {
|
116 | |
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | inject.getter = function injectGetter(bindingSelector, metadata) {
|
131 | metadata = Object.assign({ decorator: '@inject.getter' }, metadata);
|
132 | return inject(bindingSelector, metadata, (0, binding_filter_1.isBindingAddress)(bindingSelector)
|
133 | ? resolveAsGetter
|
134 | : resolveAsGetterByFilter);
|
135 | };
|
136 | |
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | inject.setter = function injectSetter(bindingKey, metadata) {
|
150 | metadata = Object.assign({ decorator: '@inject.setter' }, metadata);
|
151 | return inject(bindingKey, metadata, resolveAsSetter);
|
152 | };
|
153 | |
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | inject.binding = function injectBinding(bindingKey, metadata) {
|
179 | metadata = Object.assign({ decorator: '@inject.binding' }, metadata);
|
180 | return inject(bindingKey !== null && bindingKey !== void 0 ? bindingKey : '', metadata, resolveAsBinding);
|
181 | };
|
182 | |
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 | inject.tag = function injectByTag(bindingTag, metadata) {
|
197 | metadata = Object.assign({ decorator: '@inject.tag', tag: bindingTag }, metadata);
|
198 | return inject((0, binding_filter_1.filterByTag)(bindingTag), metadata);
|
199 | };
|
200 | |
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 | inject.view = function injectContextView(bindingFilter, metadata) {
|
214 | metadata = Object.assign({ decorator: '@inject.view' }, metadata);
|
215 | return inject(bindingFilter, metadata, resolveAsContextView);
|
216 | };
|
217 | |
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 | inject.context = function injectContext() {
|
228 | return inject('', { decorator: '@inject.context' }, (ctx) => ctx);
|
229 | };
|
230 | })(inject || (exports.inject = inject = {}));
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 | function assertTargetType(injection, expectedType, expectedTypeName) {
|
240 | const targetName = resolution_session_1.ResolutionSession.describeInjection(injection).targetName;
|
241 | const targetType = inspectTargetType(injection);
|
242 | if (targetType && targetType !== expectedType) {
|
243 | expectedTypeName = expectedTypeName !== null && expectedTypeName !== void 0 ? expectedTypeName : expectedType.name;
|
244 | throw new Error(`The type of ${targetName} (${targetType.name}) is not ${expectedTypeName}`);
|
245 | }
|
246 | return targetName;
|
247 | }
|
248 | exports.assertTargetType = assertTargetType;
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 | function resolveAsGetter(ctx, injection, session) {
|
256 | assertTargetType(injection, Function, 'Getter function');
|
257 | const bindingSelector = injection.bindingSelector;
|
258 | const options = {
|
259 |
|
260 |
|
261 |
|
262 | session: undefined,
|
263 | ...injection.metadata,
|
264 | };
|
265 | return function getter() {
|
266 | return ctx.get(bindingSelector, options);
|
267 | };
|
268 | }
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 | function resolveAsSetter(ctx, injection) {
|
275 | const targetName = assertTargetType(injection, Function, 'Setter function');
|
276 | const bindingSelector = injection.bindingSelector;
|
277 | if (!(0, binding_filter_1.isBindingAddress)(bindingSelector)) {
|
278 | throw new Error(`@inject.setter (${targetName}) does not allow BindingFilter.`);
|
279 | }
|
280 | if (bindingSelector === '') {
|
281 | throw new Error('Binding key is not set for @inject.setter');
|
282 | }
|
283 |
|
284 | return function setter(value) {
|
285 | const binding = findOrCreateBindingForInjection(ctx, injection);
|
286 | binding.to(value);
|
287 | };
|
288 | }
|
289 | function resolveAsBinding(ctx, injection, session) {
|
290 | const targetName = assertTargetType(injection, binding_1.Binding);
|
291 | const bindingSelector = injection.bindingSelector;
|
292 | if (!(0, binding_filter_1.isBindingAddress)(bindingSelector)) {
|
293 | throw new Error(`@inject.binding (${targetName}) does not allow BindingFilter.`);
|
294 | }
|
295 | return findOrCreateBindingForInjection(ctx, injection, session);
|
296 | }
|
297 | function findOrCreateBindingForInjection(ctx, injection, session) {
|
298 | if (injection.bindingSelector === '')
|
299 | return session === null || session === void 0 ? void 0 : session.currentBinding;
|
300 | const bindingCreation = injection.metadata &&
|
301 | injection.metadata.bindingCreation;
|
302 | const binding = ctx.findOrCreateBinding(injection.bindingSelector, bindingCreation);
|
303 | return binding;
|
304 | }
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 | function shouldSkipBaseConstructorInjection(targetClass) {
|
312 |
|
313 | const classDef = targetClass.toString();
|
314 | return (
|
315 | |
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 | !classDef.match(/\s+constructor\s*\(\s*\)\s*\{\s*super\(\.\.\.arguments\)/) &&
|
342 | |
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 | classDef.match(/\s+constructor\s*\([^\)]*\)\s+\{/m));
|
362 | }
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 | function describeInjectedArguments(target, method) {
|
370 | var _a, _b;
|
371 | method = method !== null && method !== void 0 ? method : '';
|
372 |
|
373 | const cache = (_a = metadata_1.MetadataInspector.getAllMethodMetadata(INJECT_METHODS_KEY, target, {
|
374 | ownMetadataOnly: true,
|
375 | })) !== null && _a !== void 0 ? _a : {};
|
376 | let meta = cache[method];
|
377 | if (meta)
|
378 | return meta;
|
379 |
|
380 | const options = {};
|
381 | if (method === '') {
|
382 | if (shouldSkipBaseConstructorInjection(target)) {
|
383 | options.ownMetadataOnly = true;
|
384 | }
|
385 | }
|
386 | else if (Object.prototype.hasOwnProperty.call(target, method)) {
|
387 |
|
388 |
|
389 | options.ownMetadataOnly = true;
|
390 | }
|
391 | meta =
|
392 | (_b = metadata_1.MetadataInspector.getAllParameterMetadata(INJECT_PARAMETERS_KEY, target, method, options)) !== null && _b !== void 0 ? _b : [];
|
393 |
|
394 | cache[method] = meta;
|
395 | metadata_1.MetadataInspector.defineMetadata(INJECT_METHODS_KEY, cache, target);
|
396 | return meta;
|
397 | }
|
398 | exports.describeInjectedArguments = describeInjectedArguments;
|
399 |
|
400 |
|
401 |
|
402 |
|
403 |
|
404 | function inspectTargetType(injection) {
|
405 | var _a;
|
406 | if (typeof injection.methodDescriptorOrParameterIndex === 'number') {
|
407 | const designType = metadata_1.MetadataInspector.getDesignTypeForMethod(injection.target, injection.member);
|
408 | return (_a = designType === null || designType === void 0 ? void 0 : designType.parameterTypes) === null || _a === void 0 ? void 0 : _a[injection.methodDescriptorOrParameterIndex];
|
409 | }
|
410 | return metadata_1.MetadataInspector.getDesignTypeForProperty(injection.target, injection.member);
|
411 | }
|
412 | exports.inspectTargetType = inspectTargetType;
|
413 |
|
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 | function resolveValuesByFilter(ctx, injection, session) {
|
420 | assertTargetType(injection, Array);
|
421 | const bindingFilter = injection.bindingSelector;
|
422 | const view = new context_view_1.ContextView(ctx, bindingFilter, injection.metadata.bindingComparator);
|
423 | return view.resolve(session);
|
424 | }
|
425 |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 | function resolveAsGetterByFilter(ctx, injection, session) {
|
434 | assertTargetType(injection, Function, 'Getter function');
|
435 | const bindingFilter = injection.bindingSelector;
|
436 | return (0, context_view_1.createViewGetter)(ctx, bindingFilter, injection.metadata.bindingComparator, session);
|
437 | }
|
438 |
|
439 |
|
440 |
|
441 |
|
442 |
|
443 |
|
444 | function resolveAsContextView(ctx, injection) {
|
445 | assertTargetType(injection, context_view_1.ContextView);
|
446 | const bindingFilter = injection.bindingSelector;
|
447 | const view = new context_view_1.ContextView(ctx, bindingFilter, injection.metadata.bindingComparator);
|
448 | view.open();
|
449 | return view;
|
450 | }
|
451 |
|
452 |
|
453 |
|
454 |
|
455 |
|
456 | function describeInjectedProperties(target) {
|
457 | var _a;
|
458 | const metadata = (_a = metadata_1.MetadataInspector.getAllPropertyMetadata(INJECT_PROPERTIES_KEY, target)) !== null && _a !== void 0 ? _a : {};
|
459 | return metadata;
|
460 | }
|
461 | exports.describeInjectedProperties = describeInjectedProperties;
|
462 |
|
463 |
|
464 |
|
465 |
|
466 | function inspectInjections(binding) {
|
467 | var _a;
|
468 | const json = {};
|
469 | const ctor = (_a = binding.valueConstructor) !== null && _a !== void 0 ? _a : binding.providerConstructor;
|
470 | if (ctor == null)
|
471 | return json;
|
472 | const constructorInjections = describeInjectedArguments(ctor, '').map(inspectInjection);
|
473 | if (constructorInjections.length) {
|
474 | json.constructorArguments = constructorInjections;
|
475 | }
|
476 | const propertyInjections = describeInjectedProperties(ctor.prototype);
|
477 | const properties = {};
|
478 | for (const p in propertyInjections) {
|
479 | properties[p] = inspectInjection(propertyInjections[p]);
|
480 | }
|
481 | if (Object.keys(properties).length) {
|
482 | json.properties = properties;
|
483 | }
|
484 | return json;
|
485 | }
|
486 | exports.inspectInjections = inspectInjections;
|
487 |
|
488 |
|
489 |
|
490 |
|
491 | function inspectInjection(injection) {
|
492 | var _a, _b;
|
493 | const injectionInfo = resolution_session_1.ResolutionSession.describeInjection(injection);
|
494 | const descriptor = {};
|
495 | if (injectionInfo.targetName) {
|
496 | descriptor.targetName = injectionInfo.targetName;
|
497 | }
|
498 | if ((0, binding_filter_1.isBindingAddress)(injectionInfo.bindingSelector)) {
|
499 |
|
500 | descriptor.bindingKey = injectionInfo.bindingSelector.toString();
|
501 | }
|
502 | else if ((0, binding_filter_1.isBindingTagFilter)(injectionInfo.bindingSelector)) {
|
503 |
|
504 | descriptor.bindingTagPattern = JSON.parse(JSON.stringify(injectionInfo.bindingSelector.bindingTagPattern));
|
505 | }
|
506 | else {
|
507 |
|
508 | descriptor.bindingFilter =
|
509 | (_b = (_a = injectionInfo.bindingSelector) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : '<function>';
|
510 | }
|
511 |
|
512 | if (injectionInfo.metadata) {
|
513 | if (injectionInfo.metadata.decorator &&
|
514 | injectionInfo.metadata.decorator !== '@inject') {
|
515 | descriptor.decorator = injectionInfo.metadata.decorator;
|
516 | }
|
517 | if (injectionInfo.metadata.optional) {
|
518 | descriptor.optional = injectionInfo.metadata.optional;
|
519 | }
|
520 | }
|
521 | return descriptor;
|
522 | }
|
523 |
|
524 |
|
525 |
|
526 |
|
527 |
|
528 |
|
529 | function hasInjections(cls) {
|
530 | return (metadata_1.MetadataInspector.getClassMetadata(INJECT_PARAMETERS_KEY, cls) != null ||
|
531 | metadata_1.Reflector.getMetadata(INJECT_PARAMETERS_KEY.toString(), cls.prototype) !=
|
532 | null ||
|
533 | metadata_1.MetadataInspector.getAllPropertyMetadata(INJECT_PROPERTIES_KEY, cls.prototype) != null);
|
534 | }
|
535 | exports.hasInjections = hasInjections;
|
536 |
|
\ | No newline at end of file |