@loopback/context 4.0.0-alpha.6 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +25 -0
- package/README.md +116 -0
- package/dist/binding-config.d.ts +40 -0
- package/dist/binding-config.js +33 -0
- package/dist/binding-config.js.map +1 -0
- package/dist/binding-decorator.d.ts +45 -0
- package/dist/binding-decorator.js +118 -0
- package/dist/binding-decorator.js.map +1 -0
- package/dist/binding-filter.d.ts +108 -0
- package/dist/binding-filter.js +162 -0
- package/dist/binding-filter.js.map +1 -0
- package/dist/binding-inspector.d.ts +150 -0
- package/dist/binding-inspector.js +249 -0
- package/dist/binding-inspector.js.map +1 -0
- package/dist/binding-key.d.ts +66 -0
- package/dist/binding-key.js +121 -0
- package/dist/binding-key.js.map +1 -0
- package/dist/binding-sorter.d.ts +71 -0
- package/dist/binding-sorter.js +89 -0
- package/dist/binding-sorter.js.map +1 -0
- package/dist/binding.d.ts +577 -0
- package/dist/binding.js +788 -0
- package/dist/binding.js.map +1 -0
- package/dist/context-event.d.ts +23 -0
- package/dist/context-event.js +7 -0
- package/dist/context-event.js.map +1 -0
- package/dist/context-observer.d.ts +36 -0
- package/dist/context-observer.js +7 -0
- package/dist/context-observer.js.map +1 -0
- package/dist/context-subscription.d.ts +147 -0
- package/dist/context-subscription.js +317 -0
- package/dist/context-subscription.js.map +1 -0
- package/dist/context-tag-indexer.d.ts +42 -0
- package/dist/context-tag-indexer.js +135 -0
- package/dist/context-tag-indexer.js.map +1 -0
- package/dist/context-view.d.ts +209 -0
- package/dist/context-view.js +240 -0
- package/dist/context-view.js.map +1 -0
- package/dist/context.d.ts +513 -0
- package/dist/context.js +717 -0
- package/dist/context.js.map +1 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.js +60 -0
- package/dist/index.js.map +1 -0
- package/dist/inject-config.d.ts +67 -0
- package/dist/inject-config.js +181 -0
- package/dist/inject-config.js.map +1 -0
- package/dist/inject.d.ts +250 -0
- package/dist/inject.js +535 -0
- package/dist/inject.js.map +1 -0
- package/dist/interception-proxy.d.ts +76 -0
- package/dist/interception-proxy.js +67 -0
- package/dist/interception-proxy.js.map +1 -0
- package/dist/interceptor-chain.d.ts +121 -0
- package/dist/interceptor-chain.js +148 -0
- package/dist/interceptor-chain.js.map +1 -0
- package/dist/interceptor.d.ts +138 -0
- package/dist/interceptor.js +299 -0
- package/dist/interceptor.js.map +1 -0
- package/dist/invocation.d.ts +101 -0
- package/dist/invocation.js +163 -0
- package/dist/invocation.js.map +1 -0
- package/dist/json-types.d.ts +28 -0
- package/dist/json-types.js +7 -0
- package/dist/json-types.js.map +1 -0
- package/dist/keys.d.ts +65 -0
- package/dist/keys.js +74 -0
- package/dist/keys.js.map +1 -0
- package/dist/provider.d.ts +31 -0
- package/dist/provider.js +7 -0
- package/dist/provider.js.map +1 -0
- package/dist/resolution-session.d.ts +180 -0
- package/dist/resolution-session.js +274 -0
- package/dist/resolution-session.js.map +1 -0
- package/dist/resolver.d.ts +46 -0
- package/dist/resolver.js +203 -0
- package/dist/resolver.js.map +1 -0
- package/dist/unique-id.d.ts +14 -0
- package/dist/unique-id.js +26 -0
- package/dist/unique-id.js.map +1 -0
- package/dist/value-promise.d.ts +134 -0
- package/dist/value-promise.js +277 -0
- package/dist/value-promise.js.map +1 -0
- package/package.json +49 -36
- package/src/binding-config.ts +73 -0
- package/src/binding-decorator.ts +136 -0
- package/src/binding-filter.ts +250 -0
- package/src/binding-inspector.ts +371 -0
- package/src/binding-key.ts +136 -0
- package/src/binding-sorter.ts +124 -0
- package/src/binding.ts +1107 -0
- package/src/context-event.ts +30 -0
- package/src/context-observer.ts +50 -0
- package/src/context-subscription.ts +402 -0
- package/src/context-tag-indexer.ts +147 -0
- package/src/context-view.ts +440 -0
- package/src/context.ts +1079 -0
- package/src/index.ts +58 -0
- package/src/inject-config.ts +239 -0
- package/src/inject.ts +796 -0
- package/src/interception-proxy.ts +127 -0
- package/src/interceptor-chain.ts +268 -0
- package/src/interceptor.ts +430 -0
- package/src/invocation.ts +269 -0
- package/src/json-types.ts +35 -0
- package/src/keys.ts +85 -0
- package/src/provider.ts +37 -0
- package/src/resolution-session.ts +414 -0
- package/src/resolver.ts +282 -0
- package/src/unique-id.ts +24 -0
- package/src/value-promise.ts +318 -0
- package/index.d.ts +0 -6
- package/index.js +0 -9
- package/lib/binding.d.ts +0 -75
- package/lib/binding.js +0 -102
- package/lib/context.d.ts +0 -14
- package/lib/context.js +0 -96
- package/lib/index.d.ts +0 -5
- package/lib/index.js +0 -13
- package/lib/inject.d.ts +0 -24
- package/lib/inject.js +0 -43
- package/lib/isPromise.d.ts +0 -1
- package/lib/isPromise.js +0 -14
- package/lib/resolver.d.ts +0 -26
- package/lib/resolver.js +0 -72
- package/lib6/binding.d.ts +0 -75
- package/lib6/binding.js +0 -102
- package/lib6/context.d.ts +0 -14
- package/lib6/context.js +0 -96
- package/lib6/index.d.ts +0 -5
- package/lib6/index.js +0 -13
- package/lib6/inject.d.ts +0 -24
- package/lib6/inject.js +0 -43
- package/lib6/isPromise.d.ts +0 -1
- package/lib6/isPromise.js +0 -14
- package/lib6/resolver.d.ts +0 -26
- package/lib6/resolver.js +0 -72
package/dist/inject.js
ADDED
|
@@ -0,0 +1,535 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright IBM Corp. 2017,2020. All Rights Reserved.
|
|
3
|
+
// Node module: @loopback/context
|
|
4
|
+
// This file is licensed under the MIT License.
|
|
5
|
+
// License text available at https://opensource.org/licenses/MIT
|
|
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
|
+
// A key to cache described argument injections
|
|
16
|
+
const INJECT_METHODS_KEY = metadata_1.MetadataAccessor.create('inject:methods');
|
|
17
|
+
/**
|
|
18
|
+
* A decorator to annotate method arguments for automatic injection
|
|
19
|
+
* by LoopBack IoC container.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* Usage - Typescript:
|
|
23
|
+
*
|
|
24
|
+
* ```ts
|
|
25
|
+
* class InfoController {
|
|
26
|
+
* @inject('authentication.user') public userName: string;
|
|
27
|
+
*
|
|
28
|
+
* constructor(@inject('application.name') public appName: string) {
|
|
29
|
+
* }
|
|
30
|
+
* // ...
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* Usage - JavaScript:
|
|
35
|
+
*
|
|
36
|
+
* - TODO(bajtos)
|
|
37
|
+
*
|
|
38
|
+
* @param bindingSelector - What binding to use in order to resolve the value of the
|
|
39
|
+
* decorated constructor parameter or property.
|
|
40
|
+
* @param metadata - Optional metadata to help the injection
|
|
41
|
+
* @param resolve - Optional function to resolve the injection
|
|
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
|
+
// The decorator is applied to a method parameter
|
|
58
|
+
// Please note propertyKey is `undefined` for constructor
|
|
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
|
+
// Do not deep clone the spec as only metadata is mutable and it's
|
|
68
|
+
// shallowly cloned
|
|
69
|
+
{ cloneInputSpec: false, decoratorName: injectionMetadata.decorator });
|
|
70
|
+
paramDecorator(target, member, methodDescriptorOrParameterIndex);
|
|
71
|
+
}
|
|
72
|
+
else if (member) {
|
|
73
|
+
// Property or method
|
|
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
|
+
// Method
|
|
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
|
+
// Do not deep clone the spec as only metadata is mutable and it's
|
|
92
|
+
// shallowly cloned
|
|
93
|
+
{ cloneInputSpec: false, decoratorName: injectionMetadata.decorator });
|
|
94
|
+
propDecorator(target, member);
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
// It won't happen here as `@inject` is not compatible with ClassDecorator
|
|
98
|
+
/* istanbul ignore next */
|
|
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
|
+
* Convert a value into a Getter returning that value.
|
|
108
|
+
* @param value
|
|
109
|
+
*/
|
|
110
|
+
function fromValue(value) {
|
|
111
|
+
return () => Promise.resolve(value);
|
|
112
|
+
}
|
|
113
|
+
Getter.fromValue = fromValue;
|
|
114
|
+
})(Getter = exports.Getter || (exports.Getter = {}));
|
|
115
|
+
(function (inject) {
|
|
116
|
+
/**
|
|
117
|
+
* Inject a function for getting the actual bound value.
|
|
118
|
+
*
|
|
119
|
+
* This is useful when implementing Actions, where
|
|
120
|
+
* the action is instantiated for Sequence constructor, but some
|
|
121
|
+
* of action's dependencies become bound only after other actions
|
|
122
|
+
* have been executed by the sequence.
|
|
123
|
+
*
|
|
124
|
+
* See also `Getter<T>`.
|
|
125
|
+
*
|
|
126
|
+
* @param bindingSelector - The binding key or filter we want to eventually get
|
|
127
|
+
* value(s) from.
|
|
128
|
+
* @param metadata - Optional metadata to help the injection
|
|
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
|
+
* Inject a function for setting (binding) the given key to a given
|
|
138
|
+
* value. (Only static/constant values are supported, it's not possible
|
|
139
|
+
* to bind a key to a class or a provider.)
|
|
140
|
+
*
|
|
141
|
+
* This is useful e.g. when implementing Actions that are contributing
|
|
142
|
+
* new Elements.
|
|
143
|
+
*
|
|
144
|
+
* See also `Setter<T>`.
|
|
145
|
+
*
|
|
146
|
+
* @param bindingKey - The key of the value we want to set.
|
|
147
|
+
* @param metadata - Optional metadata to help the injection
|
|
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
|
+
* Inject the binding object for the given key. This is useful if a binding
|
|
155
|
+
* needs to be set up beyond just a constant value allowed by
|
|
156
|
+
* `@inject.setter`. The injected binding is found or created based on the
|
|
157
|
+
* `metadata.bindingCreation` option. See `BindingCreationPolicy` for more
|
|
158
|
+
* details.
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
*
|
|
162
|
+
* ```ts
|
|
163
|
+
* class MyAuthAction {
|
|
164
|
+
* @inject.binding('current-user', {
|
|
165
|
+
* bindingCreation: BindingCreationPolicy.ALWAYS_CREATE,
|
|
166
|
+
* })
|
|
167
|
+
* private userBinding: Binding<UserProfile>;
|
|
168
|
+
*
|
|
169
|
+
* async authenticate() {
|
|
170
|
+
* this.userBinding.toDynamicValue(() => {...});
|
|
171
|
+
* }
|
|
172
|
+
* }
|
|
173
|
+
* ```
|
|
174
|
+
*
|
|
175
|
+
* @param bindingKey - Binding key
|
|
176
|
+
* @param metadata - Metadata for the injection
|
|
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
|
+
* Inject an array of values by a tag pattern string or regexp
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```ts
|
|
187
|
+
* class AuthenticationManager {
|
|
188
|
+
* constructor(
|
|
189
|
+
* @inject.tag('authentication.strategy') public strategies: Strategy[],
|
|
190
|
+
* ) {}
|
|
191
|
+
* }
|
|
192
|
+
* ```
|
|
193
|
+
* @param bindingTag - Tag name, regex or object
|
|
194
|
+
* @param metadata - Optional metadata to help the injection
|
|
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
|
+
* Inject matching bound values by the filter function
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
* ```ts
|
|
205
|
+
* class MyControllerWithView {
|
|
206
|
+
* @inject.view(filterByTag('foo'))
|
|
207
|
+
* view: ContextView<string[]>;
|
|
208
|
+
* }
|
|
209
|
+
* ```
|
|
210
|
+
* @param bindingFilter - A binding filter function
|
|
211
|
+
* @param metadata
|
|
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
|
+
* Inject the context object.
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* ```ts
|
|
222
|
+
* class MyProvider {
|
|
223
|
+
* constructor(@inject.context() private ctx: Context) {}
|
|
224
|
+
* }
|
|
225
|
+
* ```
|
|
226
|
+
*/
|
|
227
|
+
inject.context = function injectContext() {
|
|
228
|
+
return inject('', { decorator: '@inject.context' }, (ctx) => ctx);
|
|
229
|
+
};
|
|
230
|
+
})(inject = exports.inject || (exports.inject = {}));
|
|
231
|
+
/**
|
|
232
|
+
* Assert the target type inspected from TypeScript for injection to be the
|
|
233
|
+
* expected type. If the types don't match, an error is thrown.
|
|
234
|
+
* @param injection - Injection information
|
|
235
|
+
* @param expectedType - Expected type
|
|
236
|
+
* @param expectedTypeName - Name of the expected type to be used in the error
|
|
237
|
+
* @returns The name of the target
|
|
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
|
+
* Resolver for `@inject.getter`
|
|
251
|
+
* @param ctx
|
|
252
|
+
* @param injection
|
|
253
|
+
* @param session
|
|
254
|
+
*/
|
|
255
|
+
function resolveAsGetter(ctx, injection, session) {
|
|
256
|
+
assertTargetType(injection, Function, 'Getter function');
|
|
257
|
+
const bindingSelector = injection.bindingSelector;
|
|
258
|
+
// We need to clone the session for the getter as it will be resolved later
|
|
259
|
+
const forkedSession = resolution_session_1.ResolutionSession.fork(session);
|
|
260
|
+
const options = {
|
|
261
|
+
session: forkedSession,
|
|
262
|
+
...injection.metadata,
|
|
263
|
+
};
|
|
264
|
+
return function getter() {
|
|
265
|
+
return ctx.get(bindingSelector, options);
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Resolver for `@inject.setter`
|
|
270
|
+
* @param ctx
|
|
271
|
+
* @param injection
|
|
272
|
+
*/
|
|
273
|
+
function resolveAsSetter(ctx, injection) {
|
|
274
|
+
const targetName = assertTargetType(injection, Function, 'Setter function');
|
|
275
|
+
const bindingSelector = injection.bindingSelector;
|
|
276
|
+
if (!(0, binding_filter_1.isBindingAddress)(bindingSelector)) {
|
|
277
|
+
throw new Error(`@inject.setter (${targetName}) does not allow BindingFilter.`);
|
|
278
|
+
}
|
|
279
|
+
if (bindingSelector === '') {
|
|
280
|
+
throw new Error('Binding key is not set for @inject.setter');
|
|
281
|
+
}
|
|
282
|
+
// No resolution session should be propagated into the setter
|
|
283
|
+
return function setter(value) {
|
|
284
|
+
const binding = findOrCreateBindingForInjection(ctx, injection);
|
|
285
|
+
binding.to(value);
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
function resolveAsBinding(ctx, injection, session) {
|
|
289
|
+
const targetName = assertTargetType(injection, binding_1.Binding);
|
|
290
|
+
const bindingSelector = injection.bindingSelector;
|
|
291
|
+
if (!(0, binding_filter_1.isBindingAddress)(bindingSelector)) {
|
|
292
|
+
throw new Error(`@inject.binding (${targetName}) does not allow BindingFilter.`);
|
|
293
|
+
}
|
|
294
|
+
return findOrCreateBindingForInjection(ctx, injection, session);
|
|
295
|
+
}
|
|
296
|
+
function findOrCreateBindingForInjection(ctx, injection, session) {
|
|
297
|
+
if (injection.bindingSelector === '')
|
|
298
|
+
return session === null || session === void 0 ? void 0 : session.currentBinding;
|
|
299
|
+
const bindingCreation = injection.metadata &&
|
|
300
|
+
injection.metadata.bindingCreation;
|
|
301
|
+
const binding = ctx.findOrCreateBinding(injection.bindingSelector, bindingCreation);
|
|
302
|
+
return binding;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Check if constructor injection should be applied to the base class
|
|
306
|
+
* of the given target class
|
|
307
|
+
*
|
|
308
|
+
* @param targetClass - Target class
|
|
309
|
+
*/
|
|
310
|
+
function shouldSkipBaseConstructorInjection(targetClass) {
|
|
311
|
+
// FXIME(rfeng): We use the class definition to check certain patterns
|
|
312
|
+
const classDef = targetClass.toString();
|
|
313
|
+
return (
|
|
314
|
+
/*
|
|
315
|
+
* See https://github.com/loopbackio/loopback-next/issues/2946
|
|
316
|
+
* A class decorator can return a new constructor that mixes in
|
|
317
|
+
* additional properties/methods.
|
|
318
|
+
*
|
|
319
|
+
* @example
|
|
320
|
+
* ```ts
|
|
321
|
+
* class extends baseConstructor {
|
|
322
|
+
* // The constructor calls `super(...arguments)`
|
|
323
|
+
* constructor() {
|
|
324
|
+
* super(...arguments);
|
|
325
|
+
* }
|
|
326
|
+
* classProperty = 'a classProperty';
|
|
327
|
+
* classFunction() {
|
|
328
|
+
* return 'a classFunction';
|
|
329
|
+
* }
|
|
330
|
+
* };
|
|
331
|
+
* ```
|
|
332
|
+
*
|
|
333
|
+
* We check the following pattern:
|
|
334
|
+
* ```ts
|
|
335
|
+
* constructor() {
|
|
336
|
+
* super(...arguments);
|
|
337
|
+
* }
|
|
338
|
+
* ```
|
|
339
|
+
*/
|
|
340
|
+
!classDef.match(/\s+constructor\s*\(\s*\)\s*\{\s*super\(\.\.\.arguments\)/) &&
|
|
341
|
+
/*
|
|
342
|
+
* See https://github.com/loopbackio/loopback-next/issues/1565
|
|
343
|
+
*
|
|
344
|
+
* @example
|
|
345
|
+
* ```ts
|
|
346
|
+
* class BaseClass {
|
|
347
|
+
* constructor(@inject('foo') protected foo: string) {}
|
|
348
|
+
* // ...
|
|
349
|
+
* }
|
|
350
|
+
*
|
|
351
|
+
* class SubClass extends BaseClass {
|
|
352
|
+
* // No explicit constructor is present
|
|
353
|
+
*
|
|
354
|
+
* @inject('bar')
|
|
355
|
+
* private bar: number;
|
|
356
|
+
* // ...
|
|
357
|
+
* };
|
|
358
|
+
*
|
|
359
|
+
*/
|
|
360
|
+
classDef.match(/\s+constructor\s*\([^\)]*\)\s+\{/m));
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Return an array of injection objects for parameters
|
|
364
|
+
* @param target - The target class for constructor or static methods,
|
|
365
|
+
* or the prototype for instance methods
|
|
366
|
+
* @param method - Method name, undefined for constructor
|
|
367
|
+
*/
|
|
368
|
+
function describeInjectedArguments(target, method) {
|
|
369
|
+
var _a, _b;
|
|
370
|
+
method = method !== null && method !== void 0 ? method : '';
|
|
371
|
+
// Try to read from cache
|
|
372
|
+
const cache = (_a = metadata_1.MetadataInspector.getAllMethodMetadata(INJECT_METHODS_KEY, target, {
|
|
373
|
+
ownMetadataOnly: true,
|
|
374
|
+
})) !== null && _a !== void 0 ? _a : {};
|
|
375
|
+
let meta = cache[method];
|
|
376
|
+
if (meta)
|
|
377
|
+
return meta;
|
|
378
|
+
// Build the description
|
|
379
|
+
const options = {};
|
|
380
|
+
if (method === '') {
|
|
381
|
+
if (shouldSkipBaseConstructorInjection(target)) {
|
|
382
|
+
options.ownMetadataOnly = true;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
else if (Object.prototype.hasOwnProperty.call(target, method)) {
|
|
386
|
+
// The method exists in the target, no injections on the super method
|
|
387
|
+
// should be honored
|
|
388
|
+
options.ownMetadataOnly = true;
|
|
389
|
+
}
|
|
390
|
+
meta =
|
|
391
|
+
(_b = metadata_1.MetadataInspector.getAllParameterMetadata(INJECT_PARAMETERS_KEY, target, method, options)) !== null && _b !== void 0 ? _b : [];
|
|
392
|
+
// Cache the result
|
|
393
|
+
cache[method] = meta;
|
|
394
|
+
metadata_1.MetadataInspector.defineMetadata(INJECT_METHODS_KEY, cache, target);
|
|
395
|
+
return meta;
|
|
396
|
+
}
|
|
397
|
+
exports.describeInjectedArguments = describeInjectedArguments;
|
|
398
|
+
/**
|
|
399
|
+
* Inspect the target type for the injection to find out the corresponding
|
|
400
|
+
* JavaScript type
|
|
401
|
+
* @param injection - Injection information
|
|
402
|
+
*/
|
|
403
|
+
function inspectTargetType(injection) {
|
|
404
|
+
var _a;
|
|
405
|
+
if (typeof injection.methodDescriptorOrParameterIndex === 'number') {
|
|
406
|
+
const designType = metadata_1.MetadataInspector.getDesignTypeForMethod(injection.target, injection.member);
|
|
407
|
+
return (_a = designType === null || designType === void 0 ? void 0 : designType.parameterTypes) === null || _a === void 0 ? void 0 : _a[injection.methodDescriptorOrParameterIndex];
|
|
408
|
+
}
|
|
409
|
+
return metadata_1.MetadataInspector.getDesignTypeForProperty(injection.target, injection.member);
|
|
410
|
+
}
|
|
411
|
+
exports.inspectTargetType = inspectTargetType;
|
|
412
|
+
/**
|
|
413
|
+
* Resolve an array of bound values matching the filter function for `@inject`.
|
|
414
|
+
* @param ctx - Context object
|
|
415
|
+
* @param injection - Injection information
|
|
416
|
+
* @param session - Resolution session
|
|
417
|
+
*/
|
|
418
|
+
function resolveValuesByFilter(ctx, injection, session) {
|
|
419
|
+
assertTargetType(injection, Array);
|
|
420
|
+
const bindingFilter = injection.bindingSelector;
|
|
421
|
+
const view = new context_view_1.ContextView(ctx, bindingFilter, injection.metadata.bindingComparator);
|
|
422
|
+
return view.resolve(session);
|
|
423
|
+
}
|
|
424
|
+
/**
|
|
425
|
+
* Resolve to a getter function that returns an array of bound values matching
|
|
426
|
+
* the filter function for `@inject.getter`.
|
|
427
|
+
*
|
|
428
|
+
* @param ctx - Context object
|
|
429
|
+
* @param injection - Injection information
|
|
430
|
+
* @param session - Resolution session
|
|
431
|
+
*/
|
|
432
|
+
function resolveAsGetterByFilter(ctx, injection, session) {
|
|
433
|
+
assertTargetType(injection, Function, 'Getter function');
|
|
434
|
+
const bindingFilter = injection.bindingSelector;
|
|
435
|
+
return (0, context_view_1.createViewGetter)(ctx, bindingFilter, injection.metadata.bindingComparator, session);
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* Resolve to an instance of `ContextView` by the binding filter function
|
|
439
|
+
* for `@inject.view`
|
|
440
|
+
* @param ctx - Context object
|
|
441
|
+
* @param injection - Injection information
|
|
442
|
+
*/
|
|
443
|
+
function resolveAsContextView(ctx, injection) {
|
|
444
|
+
assertTargetType(injection, context_view_1.ContextView);
|
|
445
|
+
const bindingFilter = injection.bindingSelector;
|
|
446
|
+
const view = new context_view_1.ContextView(ctx, bindingFilter, injection.metadata.bindingComparator);
|
|
447
|
+
view.open();
|
|
448
|
+
return view;
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* Return a map of injection objects for properties
|
|
452
|
+
* @param target - The target class for static properties or
|
|
453
|
+
* prototype for instance properties.
|
|
454
|
+
*/
|
|
455
|
+
function describeInjectedProperties(target) {
|
|
456
|
+
var _a;
|
|
457
|
+
const metadata = (_a = metadata_1.MetadataInspector.getAllPropertyMetadata(INJECT_PROPERTIES_KEY, target)) !== null && _a !== void 0 ? _a : {};
|
|
458
|
+
return metadata;
|
|
459
|
+
}
|
|
460
|
+
exports.describeInjectedProperties = describeInjectedProperties;
|
|
461
|
+
/**
|
|
462
|
+
* Inspect injections for a binding created with `toClass` or `toProvider`
|
|
463
|
+
* @param binding - Binding object
|
|
464
|
+
*/
|
|
465
|
+
function inspectInjections(binding) {
|
|
466
|
+
var _a;
|
|
467
|
+
const json = {};
|
|
468
|
+
const ctor = (_a = binding.valueConstructor) !== null && _a !== void 0 ? _a : binding.providerConstructor;
|
|
469
|
+
if (ctor == null)
|
|
470
|
+
return json;
|
|
471
|
+
const constructorInjections = describeInjectedArguments(ctor, '').map(inspectInjection);
|
|
472
|
+
if (constructorInjections.length) {
|
|
473
|
+
json.constructorArguments = constructorInjections;
|
|
474
|
+
}
|
|
475
|
+
const propertyInjections = describeInjectedProperties(ctor.prototype);
|
|
476
|
+
const properties = {};
|
|
477
|
+
for (const p in propertyInjections) {
|
|
478
|
+
properties[p] = inspectInjection(propertyInjections[p]);
|
|
479
|
+
}
|
|
480
|
+
if (Object.keys(properties).length) {
|
|
481
|
+
json.properties = properties;
|
|
482
|
+
}
|
|
483
|
+
return json;
|
|
484
|
+
}
|
|
485
|
+
exports.inspectInjections = inspectInjections;
|
|
486
|
+
/**
|
|
487
|
+
* Inspect an injection
|
|
488
|
+
* @param injection - Injection information
|
|
489
|
+
*/
|
|
490
|
+
function inspectInjection(injection) {
|
|
491
|
+
var _a, _b;
|
|
492
|
+
const injectionInfo = resolution_session_1.ResolutionSession.describeInjection(injection);
|
|
493
|
+
const descriptor = {};
|
|
494
|
+
if (injectionInfo.targetName) {
|
|
495
|
+
descriptor.targetName = injectionInfo.targetName;
|
|
496
|
+
}
|
|
497
|
+
if ((0, binding_filter_1.isBindingAddress)(injectionInfo.bindingSelector)) {
|
|
498
|
+
// Binding key
|
|
499
|
+
descriptor.bindingKey = injectionInfo.bindingSelector.toString();
|
|
500
|
+
}
|
|
501
|
+
else if ((0, binding_filter_1.isBindingTagFilter)(injectionInfo.bindingSelector)) {
|
|
502
|
+
// Binding tag filter
|
|
503
|
+
descriptor.bindingTagPattern = JSON.parse(JSON.stringify(injectionInfo.bindingSelector.bindingTagPattern));
|
|
504
|
+
}
|
|
505
|
+
else {
|
|
506
|
+
// Binding filter function
|
|
507
|
+
descriptor.bindingFilter =
|
|
508
|
+
(_b = (_a = injectionInfo.bindingSelector) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : '<function>';
|
|
509
|
+
}
|
|
510
|
+
// Inspect metadata
|
|
511
|
+
if (injectionInfo.metadata) {
|
|
512
|
+
if (injectionInfo.metadata.decorator &&
|
|
513
|
+
injectionInfo.metadata.decorator !== '@inject') {
|
|
514
|
+
descriptor.decorator = injectionInfo.metadata.decorator;
|
|
515
|
+
}
|
|
516
|
+
if (injectionInfo.metadata.optional) {
|
|
517
|
+
descriptor.optional = injectionInfo.metadata.optional;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
return descriptor;
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* Check if the given class has `@inject` or other decorations that map to
|
|
524
|
+
* `@inject`.
|
|
525
|
+
*
|
|
526
|
+
* @param cls - Class with possible `@inject` decorations
|
|
527
|
+
*/
|
|
528
|
+
function hasInjections(cls) {
|
|
529
|
+
return (metadata_1.MetadataInspector.getClassMetadata(INJECT_PARAMETERS_KEY, cls) != null ||
|
|
530
|
+
metadata_1.Reflector.getMetadata(INJECT_PARAMETERS_KEY.toString(), cls.prototype) !=
|
|
531
|
+
null ||
|
|
532
|
+
metadata_1.MetadataInspector.getAllPropertyMetadata(INJECT_PROPERTIES_KEY, cls.prototype) != null);
|
|
533
|
+
}
|
|
534
|
+
exports.hasInjections = hasInjections;
|
|
535
|
+
//# sourceMappingURL=inject.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inject.js","sourceRoot":"","sources":["../src/inject.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,iCAAiC;AACjC,+CAA+C;AAC/C,gEAAgE;;;AAEhE,iDAS4B;AAC5B,uCAA8C;AAC9C,qDAM0B;AAI1B,iDAA6D;AAE7D,6DAA0E;AAG1E,MAAM,qBAAqB,GAAG,2BAAgB,CAAC,MAAM,CAGnD,mBAAmB,CAAC,CAAC;AAEvB,MAAM,qBAAqB,GAAG,2BAAgB,CAAC,MAAM,CAGnD,mBAAmB,CAAC,CAAC;AAEvB,+CAA+C;AAC/C,MAAM,kBAAkB,GAAG,2BAAgB,CAAC,MAAM,CAChD,gBAAgB,CACjB,CAAC;AAwDF;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,SAAgB,MAAM,CACpB,eAAgC,EAChC,QAA4B,EAC5B,OAA0B;IAE1B,IAAI,OAAO,eAAe,KAAK,UAAU,IAAI,CAAC,OAAO,EAAE;QACrD,OAAO,GAAG,qBAAqB,CAAC;KACjC;IACD,MAAM,iBAAiB,GAAG,MAAM,CAAC,MAAM,CAAC,EAAC,SAAS,EAAE,SAAS,EAAC,EAAE,QAAQ,CAAC,CAAC;IAC1E,IAAI,iBAAiB,CAAC,iBAAiB,IAAI,CAAC,OAAO,EAAE;QACnD,MAAM,IAAI,KAAK,CAAC,0DAA0D,CAAC,CAAC;KAC7E;IACD,IAAI,CAAC,eAAe,IAAI,OAAO,OAAO,KAAK,UAAU,EAAE;QACrD,MAAM,IAAI,KAAK,CACb,0EAA0E,CAC3E,CAAC;KACH;IACD,OAAO,SAAS,iCAAiC,CAC/C,MAAc,EACd,MAA0B,EAC1B,gCAE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{ ResolutionSession } from './resolution-session';
|
|
4
|
+
import { ValueOrPromise } from './value-promise';
|
|
5
|
+
/**
|
|
6
|
+
* Create the Promise type for `T`. If `T` extends `Promise`, the type is `T`,
|
|
7
|
+
* otherwise the type is `ValueOrPromise<T>`.
|
|
8
|
+
*/
|
|
9
|
+
export declare type AsValueOrPromise<T> = T extends Promise<unknown> ? T : ValueOrPromise<T>;
|
|
10
|
+
/**
|
|
11
|
+
* The intercepted variant of a function to return `ValueOrPromise<T>`.
|
|
12
|
+
* If `T` is not a function, the type is `T`.
|
|
13
|
+
*/
|
|
14
|
+
export declare type AsInterceptedFunction<T> = T extends (...args: InvocationArgs) => infer R ? (...args: Parameters<T>) => AsValueOrPromise<R> : T;
|
|
15
|
+
/**
|
|
16
|
+
* The proxy type for `T`. The return type for any method of `T` with original
|
|
17
|
+
* return type `R` becomes `ValueOrPromise<R>` if `R` does not extend `Promise`.
|
|
18
|
+
* Property types stay untouched.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* class MyController {
|
|
23
|
+
* name: string;
|
|
24
|
+
*
|
|
25
|
+
* greet(name: string): string {
|
|
26
|
+
* return `Hello, ${name}`;
|
|
27
|
+
* }
|
|
28
|
+
*
|
|
29
|
+
* async hello(name: string) {
|
|
30
|
+
* return `Hello, ${name}`;
|
|
31
|
+
* }
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* `AsyncProxy<MyController>` will be:
|
|
36
|
+
* ```ts
|
|
37
|
+
* {
|
|
38
|
+
* name: string; // the same as MyController
|
|
39
|
+
* greet(name: string): ValueOrPromise<string>; // the return type becomes `ValueOrPromise<string>`
|
|
40
|
+
* hello(name: string): Promise<string>; // the same as MyController
|
|
41
|
+
* }
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare type AsyncProxy<T> = {
|
|
45
|
+
[P in keyof T]: AsInterceptedFunction<T[P]>;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Invocation source for injected proxies. It wraps a snapshot of the
|
|
49
|
+
* `ResolutionSession` that tracks the binding/injection stack.
|
|
50
|
+
*/
|
|
51
|
+
export declare class ProxySource implements InvocationSource<ResolutionSession> {
|
|
52
|
+
readonly value: ResolutionSession;
|
|
53
|
+
type: string;
|
|
54
|
+
constructor(value: ResolutionSession);
|
|
55
|
+
toString(): string;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* A proxy handler that applies interceptors
|
|
59
|
+
*
|
|
60
|
+
* See https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Proxy
|
|
61
|
+
*/
|
|
62
|
+
export declare class InterceptionHandler<T extends object> implements ProxyHandler<T> {
|
|
63
|
+
private context;
|
|
64
|
+
private session?;
|
|
65
|
+
private source?;
|
|
66
|
+
constructor(context?: Context, session?: ResolutionSession | undefined, source?: InvocationSource<unknown> | undefined);
|
|
67
|
+
get(target: T, propertyName: PropertyKey, receiver: unknown): any;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Create a proxy that applies interceptors for method invocations
|
|
71
|
+
* @param target - Target class or object
|
|
72
|
+
* @param context - Context object
|
|
73
|
+
* @param session - Resolution session
|
|
74
|
+
* @param source - Invocation source
|
|
75
|
+
*/
|
|
76
|
+
export declare function createProxyWithInterceptors<T extends object>(target: T, context?: Context, session?: ResolutionSession, source?: InvocationSource): AsyncProxy<T>;
|