@tstdl/base 0.86.0-beta9 → 0.86.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/injector/injector.js +1 -1
- package/module/modules/web-server.module.js +0 -2
- package/object-storage/object-storage-provider.d.ts +1 -1
- package/object-storage/s3/s3.object-storage-provider.d.ts +1 -6
- package/object-storage/s3/s3.object-storage-provider.js +2 -12
- package/object-storage/s3/s3.object-storage.js +4 -1
- package/package.json +2 -2
- package/_container/container.d.ts +0 -99
- package/_container/container.js +0 -443
- package/_container/decorators.d.ts +0 -76
- package/_container/decorators.js +0 -110
- package/_container/index.d.ts +0 -10
- package/_container/index.js +0 -27
- package/_container/interfaces.d.ts +0 -16
- package/_container/interfaces.js +0 -26
- package/_container/provider.d.ts +0 -35
- package/_container/provider.js +0 -60
- package/_container/resolve-chain.d.ts +0 -27
- package/_container/resolve-chain.js +0 -105
- package/_container/resolve.error.d.ts +0 -5
- package/_container/resolve.error.js +0 -36
- package/_container/token.d.ts +0 -18
- package/_container/token.js +0 -41
- package/_container/type-info.d.ts +0 -18
- package/_container/type-info.js +0 -16
- package/_container/types.d.ts +0 -9
- package/_container/types.js +0 -16
- package/_container/utils.d.ts +0 -3
- package/_container/utils.js +0 -44
package/_container/container.js
DELETED
|
@@ -1,443 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var container_exports = {};
|
|
20
|
-
__export(container_exports, {
|
|
21
|
-
Container: () => Container,
|
|
22
|
-
container: () => container,
|
|
23
|
-
injectMetadataSymbol: () => injectMetadataSymbol
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(container_exports);
|
|
26
|
-
var import_circular_buffer = require("../data-structures/circular-buffer.js");
|
|
27
|
-
var import_multi_key_map = require("../data-structures/multi-key-map.js");
|
|
28
|
-
var import_reflection = require("../reflection/index.js");
|
|
29
|
-
var import_map = require("../utils/async-iterable-helpers/map.js");
|
|
30
|
-
var import_to_array = require("../utils/async-iterable-helpers/to-array.js");
|
|
31
|
-
var import_forward_ref = require("../utils/object/forward-ref.js");
|
|
32
|
-
var import_object = require("../utils/object/object.js");
|
|
33
|
-
var import_type_guards = require("../utils/type-guards.js");
|
|
34
|
-
var import_rxjs = require("rxjs");
|
|
35
|
-
var import_interfaces = require("./interfaces.js");
|
|
36
|
-
var import_provider = require("./provider.js");
|
|
37
|
-
var import_resolve_chain = require("./resolve-chain.js");
|
|
38
|
-
var import_resolve_error = require("./resolve.error.js");
|
|
39
|
-
var import_token = require("./token.js");
|
|
40
|
-
var import_utils = require("./utils.js");
|
|
41
|
-
const injectMetadataSymbol = Symbol("Inject metadata");
|
|
42
|
-
class Container {
|
|
43
|
-
registrationMap;
|
|
44
|
-
registrationSubject;
|
|
45
|
-
/** emits on new registration */
|
|
46
|
-
registration$;
|
|
47
|
-
/** all registrations */
|
|
48
|
-
get registrations() {
|
|
49
|
-
return this.registrationMap.values();
|
|
50
|
-
}
|
|
51
|
-
constructor() {
|
|
52
|
-
this.registrationMap = /* @__PURE__ */ new Map();
|
|
53
|
-
this.registrationSubject = new import_rxjs.Subject();
|
|
54
|
-
this.registration$ = this.registrationSubject.asObservable();
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* register a provider for a token
|
|
58
|
-
* @param token token to register
|
|
59
|
-
* @param provider provider used to resolve the token
|
|
60
|
-
* @param options registration options
|
|
61
|
-
*/
|
|
62
|
-
register(token, provider, options = {}) {
|
|
63
|
-
if ((0, import_provider.isClassProvider)(provider) && !(0, import_utils.isStubClass)(provider.useClass)) {
|
|
64
|
-
const injectable = import_reflection.reflectionRegistry.getMetadata(provider.useClass)?.data.has(injectMetadataSymbol) ?? false;
|
|
65
|
-
if (!injectable) {
|
|
66
|
-
throw new Error(`${provider.useClass.name} is not injectable.`);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
const registration = {
|
|
70
|
-
token,
|
|
71
|
-
provider,
|
|
72
|
-
options,
|
|
73
|
-
instances: /* @__PURE__ */ new Map()
|
|
74
|
-
};
|
|
75
|
-
this.registrationMap.set(token, registration);
|
|
76
|
-
this.registrationSubject.next(registration);
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* register a provider for a token as a singleton. Alias for {@link register} with `singleton` lifecycle
|
|
80
|
-
* @param token token to register
|
|
81
|
-
* @param provider provider used to resolve the token
|
|
82
|
-
* @param options registration options
|
|
83
|
-
*/
|
|
84
|
-
registerSingleton(token, provider, options) {
|
|
85
|
-
this.register(token, provider, { ...options, lifecycle: "singleton" });
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* check if token has a registered provider
|
|
89
|
-
* @param token token check
|
|
90
|
-
*/
|
|
91
|
-
hasRegistration(token) {
|
|
92
|
-
return this.registrationMap.has(token);
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* get registration
|
|
96
|
-
* @param token token to get registration for
|
|
97
|
-
*/
|
|
98
|
-
getRegistration(token) {
|
|
99
|
-
const registration = this.registrationMap.get(token);
|
|
100
|
-
if ((0, import_type_guards.isUndefined)(registration)) {
|
|
101
|
-
const tokenName = (0, import_token.getTokenName)(token);
|
|
102
|
-
throw new Error(`No provider for ${tokenName} registered.`);
|
|
103
|
-
}
|
|
104
|
-
return registration;
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* try to get registration
|
|
108
|
-
* @param token token to get registration for
|
|
109
|
-
*/
|
|
110
|
-
tryGetRegistration(token) {
|
|
111
|
-
return this.registrationMap.get(token);
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* resolve a token
|
|
115
|
-
* @param token token to resolve
|
|
116
|
-
* @param argument argument used for resolving (overrides token and default arguments)
|
|
117
|
-
* @returns
|
|
118
|
-
*/
|
|
119
|
-
resolve(token, argument, instances) {
|
|
120
|
-
const context = {
|
|
121
|
-
isAsync: false,
|
|
122
|
-
resolves: 0,
|
|
123
|
-
forwardRefQueue: new import_circular_buffer.CircularBuffer(),
|
|
124
|
-
resolutions: [],
|
|
125
|
-
forwardRefs: /* @__PURE__ */ new Set(),
|
|
126
|
-
providedInstances: new Map(instances),
|
|
127
|
-
instances: new import_multi_key_map.MultiKeyMap(),
|
|
128
|
-
resolving: new import_multi_key_map.MultiKeyMap()
|
|
129
|
-
};
|
|
130
|
-
return this._resolve(token, false, argument, context, import_resolve_chain.ResolveChain.startWith(token), true);
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* resolve a token
|
|
134
|
-
* @param token token to resolve
|
|
135
|
-
* @param argument argument used for resolving (overrides token and default arguments)
|
|
136
|
-
* @returns
|
|
137
|
-
*/
|
|
138
|
-
async resolveAsync(token, argument, instances) {
|
|
139
|
-
const context = {
|
|
140
|
-
isAsync: true,
|
|
141
|
-
resolves: 0,
|
|
142
|
-
forwardRefQueue: new import_circular_buffer.CircularBuffer(),
|
|
143
|
-
resolutions: [],
|
|
144
|
-
forwardRefs: /* @__PURE__ */ new Set(),
|
|
145
|
-
providedInstances: new Map(instances),
|
|
146
|
-
instances: new import_multi_key_map.MultiKeyMap(),
|
|
147
|
-
resolving: new import_multi_key_map.MultiKeyMap()
|
|
148
|
-
};
|
|
149
|
-
return this._resolveAsync(token, false, argument, context, import_resolve_chain.ResolveChain.startWith(token), true);
|
|
150
|
-
}
|
|
151
|
-
// eslint-disable-next-line max-statements, max-lines-per-function, complexity
|
|
152
|
-
_resolve(token, optional, _argument, context, chain, isFirst) {
|
|
153
|
-
if (chain.length > 750 || ++context.resolves > 750) {
|
|
154
|
-
throw new import_resolve_error.ResolveError("Resolve stack overflow. This can happen on circular dependencies with transient lifecycles and self reference. Use scoped or singleton lifecycle instead or @forwardRef().", chain);
|
|
155
|
-
}
|
|
156
|
-
if ((0, import_type_guards.isUndefined)(token)) {
|
|
157
|
-
throw new import_resolve_error.ResolveError("Token is undefined - this might be because of circular dependencies, use alias or forwardRef in this case.", chain);
|
|
158
|
-
}
|
|
159
|
-
if (context.providedInstances.has(token)) {
|
|
160
|
-
return context.providedInstances.get(token);
|
|
161
|
-
}
|
|
162
|
-
const registration = this.tryGetRegistration(token);
|
|
163
|
-
if ((0, import_type_guards.isUndefined)(registration)) {
|
|
164
|
-
if (optional == true) {
|
|
165
|
-
return void 0;
|
|
166
|
-
}
|
|
167
|
-
throw new import_resolve_error.ResolveError(`No provider for ${(0, import_token.getTokenName)(token)} registered.`, chain);
|
|
168
|
-
}
|
|
169
|
-
const resolveArgument = _argument ?? registration.options.defaultArgument ?? registration.options.defaultArgumentProvider?.(this.getResolveContext(context, chain));
|
|
170
|
-
if ((0, import_type_guards.isPromise)(resolveArgument)) {
|
|
171
|
-
throw new import_resolve_error.ResolveError(`Cannot evaluate async argument provider for token ${(0, import_token.getTokenName)(token)} in synchronous resolve, use resolveAsync instead.`, chain);
|
|
172
|
-
}
|
|
173
|
-
const argumentIdentity = (0, import_type_guards.isDefined)(registration.options.argumentIdentityProvider) && (registration.options.lifecycle == "resolution" || registration.options.lifecycle == "singleton") ? registration.options.argumentIdentityProvider(resolveArgument) : resolveArgument;
|
|
174
|
-
if ((0, import_type_guards.isPromise)(argumentIdentity)) {
|
|
175
|
-
throw new import_resolve_error.ResolveError(`Cannot evaluate async argument identity provider for token ${(0, import_token.getTokenName)(token)} in synchronous resolve, use resolveAsync instead.`, chain);
|
|
176
|
-
}
|
|
177
|
-
if (registration.options.lifecycle == "resolution" && context.instances.has([token, argumentIdentity])) {
|
|
178
|
-
return context.instances.get([token, argumentIdentity]);
|
|
179
|
-
}
|
|
180
|
-
if (registration.options.lifecycle == "singleton" && registration.instances.has(argumentIdentity)) {
|
|
181
|
-
return registration.instances.get(argumentIdentity);
|
|
182
|
-
}
|
|
183
|
-
if (context.resolving.has([token, argumentIdentity])) {
|
|
184
|
-
throw new import_resolve_error.ResolveError("Circular dependency to itself detected. Please check your registrations and providers. @forwardRef might be a solution.", chain);
|
|
185
|
-
}
|
|
186
|
-
context.resolving.set([token, argumentIdentity], true);
|
|
187
|
-
let instance;
|
|
188
|
-
if ((0, import_provider.isClassProvider)(registration.provider)) {
|
|
189
|
-
const typeMetadata = import_reflection.reflectionRegistry.getMetadata(registration.provider.useClass);
|
|
190
|
-
if ((0, import_type_guards.isUndefined)(typeMetadata) || !typeMetadata.data.has(injectMetadataSymbol)) {
|
|
191
|
-
throw new import_resolve_error.ResolveError(`${registration.provider.useClass.name} is not injectable.`, chain);
|
|
192
|
-
}
|
|
193
|
-
if ((0, import_utils.isStubClass)(typeMetadata.constructor)) {
|
|
194
|
-
throw new import_resolve_error.ResolveError(`No provider for ${(0, import_token.getTokenName)(token)} registered.`, chain);
|
|
195
|
-
}
|
|
196
|
-
const parameters = (typeMetadata.parameters ?? []).map((metadata) => this.resolveInjection(token, context, typeMetadata, metadata, resolveArgument, chain));
|
|
197
|
-
instance = new typeMetadata.constructor(...parameters);
|
|
198
|
-
for (const [property, metadata] of typeMetadata.properties) {
|
|
199
|
-
if (!metadata.data.has(injectMetadataSymbol)) {
|
|
200
|
-
continue;
|
|
201
|
-
}
|
|
202
|
-
instance[property] = this.resolveInjection(token, context, typeMetadata, metadata, resolveArgument, chain);
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
if ((0, import_provider.isValueProvider)(registration.provider)) {
|
|
206
|
-
instance = registration.provider.useValue;
|
|
207
|
-
}
|
|
208
|
-
if ((0, import_provider.isTokenProvider)(registration.provider)) {
|
|
209
|
-
const arg = resolveArgument ?? registration.provider.argument ?? registration.provider.argumentProvider?.();
|
|
210
|
-
const innerToken = registration.provider.useToken ?? registration.provider.useTokenProvider();
|
|
211
|
-
if ((0, import_type_guards.isPromise)(arg)) {
|
|
212
|
-
throw new import_resolve_error.ResolveError(`Cannot evaluate async argument provider for token ${(0, import_token.getTokenName)(token)} in synchronous resolve, use resolveAsync instead.`, chain);
|
|
213
|
-
}
|
|
214
|
-
if ((0, import_type_guards.isPromise)(innerToken)) {
|
|
215
|
-
throw new import_resolve_error.ResolveError(`Cannot evaluate async token provider for token ${(0, import_token.getTokenName)(token)} in synchronous resolve, use resolveAsync instead.`, chain);
|
|
216
|
-
}
|
|
217
|
-
instance = this._resolve(innerToken, false, arg, context, chain.addToken(innerToken), false);
|
|
218
|
-
}
|
|
219
|
-
if ((0, import_provider.isFactoryProvider)(registration.provider)) {
|
|
220
|
-
try {
|
|
221
|
-
const result = registration.provider.useFactory(resolveArgument, this.getResolveContext(context, chain));
|
|
222
|
-
if ((0, import_type_guards.isPromise)(result)) {
|
|
223
|
-
throw new import_resolve_error.ResolveError(`Cannot evaluate async factory provider for token ${(0, import_token.getTokenName)(token)} in synchronous resolve, use resolveAsync instead.`, chain);
|
|
224
|
-
}
|
|
225
|
-
instance = result;
|
|
226
|
-
} catch (error) {
|
|
227
|
-
throw new import_resolve_error.ResolveError("Error in factory.", chain, error);
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
if (registration.options.lifecycle == "resolution") {
|
|
231
|
-
context.instances.set([token, argumentIdentity], instance);
|
|
232
|
-
}
|
|
233
|
-
if (registration.options.lifecycle == "singleton") {
|
|
234
|
-
registration.instances.set(argumentIdentity, instance);
|
|
235
|
-
}
|
|
236
|
-
context.resolutions.push({ instance, registration });
|
|
237
|
-
context.resolving.delete([token, argumentIdentity]);
|
|
238
|
-
if (isFirst) {
|
|
239
|
-
for (const fn of context.forwardRefQueue.consume()) {
|
|
240
|
-
fn();
|
|
241
|
-
}
|
|
242
|
-
derefForwardRefs(context);
|
|
243
|
-
for (let i = context.resolutions.length - 1; i >= 0; i--) {
|
|
244
|
-
const resolution = context.resolutions[i];
|
|
245
|
-
if ((0, import_type_guards.isFunction)(resolution.instance?.[import_interfaces.afterResolve])) {
|
|
246
|
-
const returnValue = resolution.instance[import_interfaces.afterResolve]();
|
|
247
|
-
if ((0, import_type_guards.isPromise)(returnValue)) {
|
|
248
|
-
throw new import_resolve_error.ResolveError(`Cannot execute async [afterResolve] for token ${(0, import_token.getTokenName)(token)} in synchronous resolve, use resolveAsync instead.`, chain);
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
if ((0, import_type_guards.isDefined)(resolution.registration.options.initializer)) {
|
|
252
|
-
const returnValue = resolution.registration.options.initializer(resolution.instance);
|
|
253
|
-
if ((0, import_type_guards.isPromise)(returnValue)) {
|
|
254
|
-
throw new import_resolve_error.ResolveError(`Cannot execute async initializer for token ${(0, import_token.getTokenName)(token)} in synchronous resolve, use resolveAsync instead.`, chain);
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
return instance;
|
|
260
|
-
}
|
|
261
|
-
// eslint-disable-next-line max-statements, max-lines-per-function, complexity
|
|
262
|
-
async _resolveAsync(token, optional, _argument, context, chain, isFirst) {
|
|
263
|
-
if (chain.length > 750 || ++context.resolves > 750) {
|
|
264
|
-
throw new import_resolve_error.ResolveError("Resolve stack overflow. This can happen on circular dependencies with transient lifecycles and self reference. Use scoped or singleton lifecycle instead or @forwardRef().", chain);
|
|
265
|
-
}
|
|
266
|
-
if ((0, import_type_guards.isUndefined)(token)) {
|
|
267
|
-
throw new import_resolve_error.ResolveError("Token is undefined - this might be because of circular dependencies, use alias or forwardRef in this case.", chain);
|
|
268
|
-
}
|
|
269
|
-
if (context.providedInstances.has(token)) {
|
|
270
|
-
return context.providedInstances.get(token);
|
|
271
|
-
}
|
|
272
|
-
const registration = this.tryGetRegistration(token);
|
|
273
|
-
if ((0, import_type_guards.isUndefined)(registration)) {
|
|
274
|
-
if (optional == true) {
|
|
275
|
-
return void 0;
|
|
276
|
-
}
|
|
277
|
-
throw new import_resolve_error.ResolveError(`No provider for ${(0, import_token.getTokenName)(token)} registered.`, chain);
|
|
278
|
-
}
|
|
279
|
-
const resolveArgument = _argument ?? registration.options.defaultArgument ?? await registration.options.defaultArgumentProvider?.(this.getResolveContext(context, chain));
|
|
280
|
-
const argumentIdentity = (0, import_type_guards.isDefined)(registration.options.argumentIdentityProvider) && (registration.options.lifecycle == "resolution" || registration.options.lifecycle == "singleton") ? await registration.options.argumentIdentityProvider(resolveArgument) : resolveArgument;
|
|
281
|
-
if (registration.options.lifecycle == "resolution" && context.instances.has([token, argumentIdentity])) {
|
|
282
|
-
return context.instances.get([token, argumentIdentity]);
|
|
283
|
-
}
|
|
284
|
-
if (registration.options.lifecycle == "singleton" && registration.instances.has(argumentIdentity)) {
|
|
285
|
-
return registration.instances.get(argumentIdentity);
|
|
286
|
-
}
|
|
287
|
-
if (context.resolving.has([token, argumentIdentity])) {
|
|
288
|
-
throw new import_resolve_error.ResolveError("Circular dependency to itself detected. Please check your registrations and providers. @forwardRef might be a solution.", chain);
|
|
289
|
-
}
|
|
290
|
-
context.resolving.set([token, argumentIdentity], true);
|
|
291
|
-
let instance;
|
|
292
|
-
if ((0, import_provider.isClassProvider)(registration.provider)) {
|
|
293
|
-
const typeMetadata = import_reflection.reflectionRegistry.getMetadata(registration.provider.useClass);
|
|
294
|
-
if ((0, import_type_guards.isUndefined)(typeMetadata) || !typeMetadata.data.has(injectMetadataSymbol)) {
|
|
295
|
-
throw new import_resolve_error.ResolveError(`${registration.provider.useClass.name} is not injectable.`, chain);
|
|
296
|
-
}
|
|
297
|
-
if ((0, import_utils.isStubClass)(typeMetadata.constructor)) {
|
|
298
|
-
throw new import_resolve_error.ResolveError(`No provider for ${(0, import_token.getTokenName)(token)} registered.`, chain);
|
|
299
|
-
}
|
|
300
|
-
const boxedParameters = await (0, import_to_array.toArrayAsync)((0, import_map.mapAsync)(typeMetadata.parameters ?? [], async (metadata) => this.resolveInjectionAsync(context, typeMetadata, metadata, resolveArgument, chain)));
|
|
301
|
-
const parameters = boxedParameters.map((box) => box.resolved);
|
|
302
|
-
instance = new typeMetadata.constructor(...parameters);
|
|
303
|
-
for (const [property, metadata] of typeMetadata.properties) {
|
|
304
|
-
if (!metadata.data.has(injectMetadataSymbol)) {
|
|
305
|
-
continue;
|
|
306
|
-
}
|
|
307
|
-
instance[property] = (await this.resolveInjectionAsync(context, typeMetadata, metadata, resolveArgument, chain)).resolved;
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
if ((0, import_provider.isValueProvider)(registration.provider)) {
|
|
311
|
-
instance = registration.provider.useValue;
|
|
312
|
-
}
|
|
313
|
-
if ((0, import_provider.isTokenProvider)(registration.provider)) {
|
|
314
|
-
const arg = resolveArgument ?? registration.provider.argument ?? await registration.provider.argumentProvider?.();
|
|
315
|
-
const innerToken = registration.provider.useToken ?? await registration.provider.useTokenProvider();
|
|
316
|
-
instance = await this._resolveAsync(innerToken, false, arg, context, chain.addToken(innerToken), false);
|
|
317
|
-
}
|
|
318
|
-
if ((0, import_provider.isFactoryProvider)(registration.provider)) {
|
|
319
|
-
try {
|
|
320
|
-
instance = await registration.provider.useFactory(resolveArgument, this.getResolveContext(context, chain));
|
|
321
|
-
} catch (error) {
|
|
322
|
-
throw new import_resolve_error.ResolveError("Error in factory.", chain, error);
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
if (registration.options.lifecycle == "resolution") {
|
|
326
|
-
context.instances.set([token, argumentIdentity], instance);
|
|
327
|
-
}
|
|
328
|
-
if (registration.options.lifecycle == "singleton") {
|
|
329
|
-
registration.instances.set(argumentIdentity, instance);
|
|
330
|
-
}
|
|
331
|
-
context.resolutions.push({ instance, registration });
|
|
332
|
-
context.resolving.delete([token, argumentIdentity]);
|
|
333
|
-
if (isFirst) {
|
|
334
|
-
for (const fn of context.forwardRefQueue.consume()) {
|
|
335
|
-
await fn();
|
|
336
|
-
}
|
|
337
|
-
derefForwardRefs(context);
|
|
338
|
-
for (let i = context.resolutions.length - 1; i >= 0; i--) {
|
|
339
|
-
const resolution = context.resolutions[i];
|
|
340
|
-
if ((0, import_type_guards.isFunction)(resolution.instance?.[import_interfaces.afterResolve])) {
|
|
341
|
-
await resolution.instance[import_interfaces.afterResolve]();
|
|
342
|
-
}
|
|
343
|
-
if ((0, import_type_guards.isDefined)(resolution.registration.options.initializer)) {
|
|
344
|
-
await resolution.registration.options.initializer(resolution.instance);
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
return instance;
|
|
349
|
-
}
|
|
350
|
-
resolveInjection(token, context, typeMetadata, metadata, resolveArgument, chain) {
|
|
351
|
-
const getChain = metadata.metadataType == "constructor-parameter" ? (injectToken2) => chain.addParameter(typeMetadata.constructor, metadata.index, injectToken2).addToken(injectToken2) : (injectToken2) => chain.addProperty(typeMetadata.constructor, metadata.key, injectToken2).addToken(injectToken2);
|
|
352
|
-
const injectMetadata = metadata.data.tryGet(injectMetadataSymbol) ?? {};
|
|
353
|
-
const injectToken = injectMetadata.injectToken ?? metadata.type;
|
|
354
|
-
if ((0, import_type_guards.isDefined)(injectMetadata.injectArgumentMapper) && (!this.hasRegistration(injectToken) || (0, import_type_guards.isDefined)(resolveArgument) || (0, import_type_guards.isUndefined)(injectToken))) {
|
|
355
|
-
const mapped = injectMetadata.injectArgumentMapper(resolveArgument);
|
|
356
|
-
if ((0, import_type_guards.isPromise)(mapped)) {
|
|
357
|
-
throw new import_resolve_error.ResolveError(`Cannot evaluate async argument mapper for token ${(0, import_token.getTokenName)(token)} in synchronous resolve, use resolveAsync instead.`, getChain(injectToken));
|
|
358
|
-
}
|
|
359
|
-
return mapped;
|
|
360
|
-
}
|
|
361
|
-
const parameterResolveArgument = injectMetadata.forwardArgumentMapper?.(resolveArgument) ?? injectMetadata.resolveArgumentProvider?.(this.getResolveContext(context, getChain(injectToken)));
|
|
362
|
-
if ((0, import_type_guards.isPromise)(parameterResolveArgument)) {
|
|
363
|
-
throw new import_resolve_error.ResolveError(`Cannot evaluate async argument provider for token ${(0, import_token.getTokenName)(token)} in synchronous resolve, use resolveAsync instead.`, getChain(injectToken));
|
|
364
|
-
}
|
|
365
|
-
if ((0, import_type_guards.isDefined)(injectMetadata.forwardRefToken)) {
|
|
366
|
-
const forwardRef = import_forward_ref.ForwardRef.create();
|
|
367
|
-
const forwardRefToken = injectMetadata.forwardRefToken;
|
|
368
|
-
context.forwardRefQueue.add(() => {
|
|
369
|
-
const forwardToken = (0, import_type_guards.isFunction)(forwardRefToken) ? forwardRefToken() : forwardRefToken;
|
|
370
|
-
if ((0, import_type_guards.isDefined)(injectMetadata.mapper)) {
|
|
371
|
-
throw new import_resolve_error.ResolveError("Cannot use inject mapper with forwardRef.", getChain(forwardToken));
|
|
372
|
-
}
|
|
373
|
-
const resolved2 = this._resolve(forwardToken, injectMetadata.optional, parameterResolveArgument, context, getChain(forwardToken), false);
|
|
374
|
-
import_forward_ref.ForwardRef.setRef(forwardRef, resolved2);
|
|
375
|
-
});
|
|
376
|
-
context.forwardRefs.add(forwardRef);
|
|
377
|
-
return forwardRef;
|
|
378
|
-
}
|
|
379
|
-
const resolved = this._resolve(injectToken, injectMetadata.optional, parameterResolveArgument, context, getChain(injectToken), false);
|
|
380
|
-
return (0, import_type_guards.isDefined)(injectMetadata.mapper) ? injectMetadata.mapper(resolved) : resolved;
|
|
381
|
-
}
|
|
382
|
-
async resolveInjectionAsync(context, typeMetadata, metadata, resolveArgument, chain) {
|
|
383
|
-
const getChain = metadata.metadataType == "constructor-parameter" ? (injectToken2) => chain.addParameter(typeMetadata.constructor, metadata.index, injectToken2).addToken(injectToken2) : (injectToken2) => chain.addProperty(typeMetadata.constructor, metadata.key, injectToken2).addToken(injectToken2);
|
|
384
|
-
const injectMetadata = metadata.data.tryGet(injectMetadataSymbol) ?? {};
|
|
385
|
-
const injectToken = injectMetadata.injectToken ?? metadata.type;
|
|
386
|
-
if ((0, import_type_guards.isDefined)(injectMetadata.injectArgumentMapper) && (!this.hasRegistration(injectToken) || (0, import_type_guards.isDefined)(resolveArgument) || (0, import_type_guards.isUndefined)(injectToken))) {
|
|
387
|
-
return { resolved: injectMetadata.injectArgumentMapper(resolveArgument) };
|
|
388
|
-
}
|
|
389
|
-
const parameterResolveArgument = await (injectMetadata.forwardArgumentMapper?.(resolveArgument) ?? injectMetadata.resolveArgumentProvider?.(this.getResolveContext(context, getChain(injectToken))));
|
|
390
|
-
if ((0, import_type_guards.isDefined)(injectMetadata.forwardRefToken)) {
|
|
391
|
-
const forwardRef = import_forward_ref.ForwardRef.create();
|
|
392
|
-
const forwardRefToken = injectMetadata.forwardRefToken;
|
|
393
|
-
context.forwardRefQueue.add(async () => {
|
|
394
|
-
const forwardToken = (0, import_type_guards.isFunction)(forwardRefToken) ? forwardRefToken() : forwardRefToken;
|
|
395
|
-
if ((0, import_type_guards.isDefined)(injectMetadata.mapper)) {
|
|
396
|
-
throw new import_resolve_error.ResolveError("Cannot use inject mapper with forwardRef.", getChain(forwardToken));
|
|
397
|
-
}
|
|
398
|
-
const resolved2 = await this._resolveAsync(forwardToken, injectMetadata.optional, parameterResolveArgument, context, getChain(forwardToken), false);
|
|
399
|
-
import_forward_ref.ForwardRef.setRef(forwardRef, resolved2);
|
|
400
|
-
});
|
|
401
|
-
context.forwardRefs.add(forwardRef);
|
|
402
|
-
return { resolved: forwardRef };
|
|
403
|
-
}
|
|
404
|
-
const resolved = await this._resolveAsync(injectToken, injectMetadata.optional, parameterResolveArgument, context, getChain(injectToken), false);
|
|
405
|
-
return { resolved: (0, import_type_guards.isDefined)(injectMetadata.mapper) ? injectMetadata.mapper(resolved) : resolved };
|
|
406
|
-
}
|
|
407
|
-
getResolveContext(resolveContext, chain) {
|
|
408
|
-
const context = {
|
|
409
|
-
isAsync: resolveContext.isAsync,
|
|
410
|
-
resolve: (token, argument, instances) => {
|
|
411
|
-
this.addInstancesToResolveContext(instances, resolveContext);
|
|
412
|
-
return this._resolve(token, false, argument, resolveContext, chain.addToken(token), false);
|
|
413
|
-
},
|
|
414
|
-
resolveAsync: async (token, argument, instances) => {
|
|
415
|
-
this.addInstancesToResolveContext(instances, resolveContext);
|
|
416
|
-
return this._resolveAsync(token, false, argument, resolveContext, chain.addToken(token), false);
|
|
417
|
-
}
|
|
418
|
-
};
|
|
419
|
-
return context;
|
|
420
|
-
}
|
|
421
|
-
addInstancesToResolveContext(instances, resolveContext) {
|
|
422
|
-
if ((0, import_type_guards.isUndefined)(instances)) {
|
|
423
|
-
return;
|
|
424
|
-
}
|
|
425
|
-
for (const [instanceToken, instance] of instances) {
|
|
426
|
-
resolveContext.providedInstances.set(instanceToken, instance);
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
function derefForwardRefs(context) {
|
|
431
|
-
for (const resolution of context.resolutions) {
|
|
432
|
-
if (!(typeof resolution.instance == "object")) {
|
|
433
|
-
continue;
|
|
434
|
-
}
|
|
435
|
-
for (const [key, value] of (0, import_object.objectEntries)(resolution.instance)) {
|
|
436
|
-
if (!context.forwardRefs.has(value)) {
|
|
437
|
-
continue;
|
|
438
|
-
}
|
|
439
|
-
resolution.instance[key] = import_forward_ref.ForwardRef.deref(value);
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
const container = new Container();
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import type { Decorator } from '../reflection/index.js';
|
|
2
|
-
import type { Constructor, OneOrMany, Simplify, TypedOmit } from '../types.js';
|
|
3
|
-
import type { RegistrationOptions } from './container.js';
|
|
4
|
-
import type { Provider } from './provider.js';
|
|
5
|
-
import type { InjectionToken } from './token.js';
|
|
6
|
-
import type { ArgumentProvider, ForwardRefInjectionToken, Mapper } from './types.js';
|
|
7
|
-
type InjectDecorator = Decorator<'property' | 'accessor' | 'constructorParameter'>;
|
|
8
|
-
export type InjectableOptions<T, A> = RegistrationOptions<T> & {
|
|
9
|
-
/** aliases (tokens) for the class. Useful for example for circular dependencies when you can't use the class itself as a token */
|
|
10
|
-
alias?: OneOrMany<InjectionToken>;
|
|
11
|
-
/** custom provider. Useful for example if initialization is required */
|
|
12
|
-
provider?: Provider<T, A>;
|
|
13
|
-
};
|
|
14
|
-
export type InjectableOptionsWithoutLifecycle<T, A> = Simplify<TypedOmit<InjectableOptions<T, A>, 'lifecycle'>>;
|
|
15
|
-
/**
|
|
16
|
-
* Helper decorator to replace a class definition with an other
|
|
17
|
-
* can be used for example to type external classes with the {@link Injectable} interface
|
|
18
|
-
* @param constructor class to replace with
|
|
19
|
-
*/
|
|
20
|
-
export declare function replaceClass<T>(constructor: Constructor<T>): ClassDecorator;
|
|
21
|
-
/**
|
|
22
|
-
* registers the class in the global container. Decorated class is not modified in any way
|
|
23
|
-
* @param options registration options
|
|
24
|
-
*/
|
|
25
|
-
export declare function injectable<T = any, A = any>(options?: InjectableOptions<T, A>): ClassDecorator;
|
|
26
|
-
/**
|
|
27
|
-
* registers the class in the global container with singleton lifecycle. Decorated class is not modified in any way
|
|
28
|
-
* @param options registration options
|
|
29
|
-
*/
|
|
30
|
-
export declare function singleton<T = any, A = any>(options?: InjectableOptionsWithoutLifecycle<T, A>): ClassDecorator;
|
|
31
|
-
/**
|
|
32
|
-
* registers the class in the global container with scoped lifecycle. Decorated class is not modified in any way
|
|
33
|
-
* @param options registration options
|
|
34
|
-
*/
|
|
35
|
-
export declare function scoped<T = any, A = any>(lifecycle: 'resolution', options?: InjectableOptionsWithoutLifecycle<T, A>): ClassDecorator;
|
|
36
|
-
/**
|
|
37
|
-
* sets the token used to resolve the parameter
|
|
38
|
-
* @param token token used for resolving
|
|
39
|
-
* @param argument resolve argument
|
|
40
|
-
* @param mapperOrKey map the resolved value. If {@link PropertyKey} is provided, that property of the resolved value will be injected
|
|
41
|
-
*/
|
|
42
|
-
export declare function inject<T, A>(token?: InjectionToken<T, A>, argument?: A, mapperOrKey?: Mapper<T> | keyof T): InjectDecorator;
|
|
43
|
-
/**
|
|
44
|
-
* sets the argument used for resolving the parameter
|
|
45
|
-
* @param argument
|
|
46
|
-
*/
|
|
47
|
-
export declare function resolveArg<T>(argument: T): InjectDecorator;
|
|
48
|
-
/**
|
|
49
|
-
* sets the argument provider used for resolving the parameter
|
|
50
|
-
* @param argumentProvider
|
|
51
|
-
*/
|
|
52
|
-
export declare function resolveArgProvider<T>(argumentProvider: ArgumentProvider<T>): InjectDecorator;
|
|
53
|
-
/**
|
|
54
|
-
* injects the argument used for resolving the class instead of resolving the parameter
|
|
55
|
-
* @param argument
|
|
56
|
-
* @param mapperOrKey map the resolved value. If {@link PropertyKey} is provided, that property of the resolved value will be injected
|
|
57
|
-
*/
|
|
58
|
-
export declare function injectArg<T>(mapperOrKey?: Mapper<T> | keyof T): InjectDecorator;
|
|
59
|
-
/**
|
|
60
|
-
* sets the argument used for resolving the decorated parameter to the the argument provided for parent resolve
|
|
61
|
-
* @param mapper map the argument (for example to select a property instead of forwarding the whole object)
|
|
62
|
-
*/
|
|
63
|
-
export declare function forwardArg(): InjectDecorator;
|
|
64
|
-
export declare function forwardArg<T, U>(mapper: Mapper<T, U>): InjectDecorator;
|
|
65
|
-
/**
|
|
66
|
-
* marks the argument as optional
|
|
67
|
-
* @param argument
|
|
68
|
-
*/
|
|
69
|
-
export declare function optional(): InjectDecorator;
|
|
70
|
-
/**
|
|
71
|
-
* resolve using ForwardRef to handle circular dependencies. Resolve logic derefs all ForwardRefs which are direct properties of resolved instances automatically
|
|
72
|
-
* @param token token to resolve
|
|
73
|
-
* @param argument resolve argument
|
|
74
|
-
*/
|
|
75
|
-
export declare function forwardRef<T extends object, A>(token: ForwardRefInjectionToken<T>, argument?: A): InjectDecorator;
|
|
76
|
-
export {};
|
package/_container/decorators.js
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var decorators_exports = {};
|
|
20
|
-
__export(decorators_exports, {
|
|
21
|
-
forwardArg: () => forwardArg,
|
|
22
|
-
forwardRef: () => forwardRef,
|
|
23
|
-
inject: () => inject,
|
|
24
|
-
injectArg: () => injectArg,
|
|
25
|
-
injectable: () => injectable,
|
|
26
|
-
optional: () => optional,
|
|
27
|
-
replaceClass: () => replaceClass,
|
|
28
|
-
resolveArg: () => resolveArg,
|
|
29
|
-
resolveArgProvider: () => resolveArgProvider,
|
|
30
|
-
scoped: () => scoped,
|
|
31
|
-
singleton: () => singleton
|
|
32
|
-
});
|
|
33
|
-
module.exports = __toCommonJS(decorators_exports);
|
|
34
|
-
var import_reflection = require("../reflection/index.js");
|
|
35
|
-
var import_array = require("../utils/array/array.js");
|
|
36
|
-
var import_type_guards = require("../utils/type-guards.js");
|
|
37
|
-
var import_container = require("./container.js");
|
|
38
|
-
function replaceClass(constructor) {
|
|
39
|
-
return (0, import_reflection.createClassDecorator)({ handler: () => constructor });
|
|
40
|
-
}
|
|
41
|
-
function injectable(options = {}) {
|
|
42
|
-
return (0, import_reflection.createClassDecorator)({
|
|
43
|
-
data: { [import_container.injectMetadataSymbol]: {} },
|
|
44
|
-
mergeData: true,
|
|
45
|
-
handler: (data) => {
|
|
46
|
-
const { alias: aliases, provider, ...registrationOptions } = options;
|
|
47
|
-
const targetProvider = provider ?? { useClass: data.constructor };
|
|
48
|
-
import_container.container.register(data.constructor, targetProvider, registrationOptions);
|
|
49
|
-
if ((0, import_type_guards.isDefined)(aliases)) {
|
|
50
|
-
for (const alias of (0, import_array.toArray)(aliases)) {
|
|
51
|
-
import_container.container.register(alias, { useToken: data.constructor }, registrationOptions);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
function singleton(options = {}) {
|
|
58
|
-
return injectable({ ...options, lifecycle: "singleton" });
|
|
59
|
-
}
|
|
60
|
-
function scoped(lifecycle, options = {}) {
|
|
61
|
-
return injectable({ ...options, lifecycle });
|
|
62
|
-
}
|
|
63
|
-
function inject(token, argument, mapperOrKey) {
|
|
64
|
-
const injectMetadata = {};
|
|
65
|
-
if ((0, import_type_guards.isDefined)(token)) {
|
|
66
|
-
injectMetadata.injectToken = token;
|
|
67
|
-
}
|
|
68
|
-
if ((0, import_type_guards.isDefined)(argument)) {
|
|
69
|
-
injectMetadata.resolveArgumentProvider = () => argument;
|
|
70
|
-
}
|
|
71
|
-
if ((0, import_type_guards.isDefined)(mapperOrKey)) {
|
|
72
|
-
injectMetadata.mapper = (0, import_type_guards.isFunction)(mapperOrKey) ? mapperOrKey : (value) => value[mapperOrKey];
|
|
73
|
-
}
|
|
74
|
-
return createInjectDecorator(injectMetadata);
|
|
75
|
-
}
|
|
76
|
-
function resolveArg(argument) {
|
|
77
|
-
return resolveArgProvider(() => argument);
|
|
78
|
-
}
|
|
79
|
-
function resolveArgProvider(argumentProvider) {
|
|
80
|
-
return createInjectDecorator({ resolveArgumentProvider: argumentProvider });
|
|
81
|
-
}
|
|
82
|
-
function injectArg(mapperOrKey) {
|
|
83
|
-
return createInjectDecorator({
|
|
84
|
-
injectArgumentMapper: (0, import_type_guards.isFunction)(mapperOrKey) ? mapperOrKey : (0, import_type_guards.isDefined)(mapperOrKey) ? (value) => value[mapperOrKey] : (value) => value
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
function forwardArg(mapper = (value) => value) {
|
|
88
|
-
return createInjectDecorator({ forwardArgumentMapper: mapper });
|
|
89
|
-
}
|
|
90
|
-
function optional() {
|
|
91
|
-
return createInjectDecorator({ optional: true });
|
|
92
|
-
}
|
|
93
|
-
function forwardRef(token, argument) {
|
|
94
|
-
const injectMetadata = {
|
|
95
|
-
forwardRefToken: token
|
|
96
|
-
};
|
|
97
|
-
if ((0, import_type_guards.isDefined)(argument)) {
|
|
98
|
-
injectMetadata.resolveArgumentProvider = () => argument;
|
|
99
|
-
}
|
|
100
|
-
return createInjectDecorator(injectMetadata);
|
|
101
|
-
}
|
|
102
|
-
function createInjectDecorator(metadata) {
|
|
103
|
-
return (0, import_reflection.createDecorator)({
|
|
104
|
-
property: true,
|
|
105
|
-
accessor: true,
|
|
106
|
-
constructorParameter: true,
|
|
107
|
-
data: { [import_container.injectMetadataSymbol]: metadata },
|
|
108
|
-
mergeData: true
|
|
109
|
-
});
|
|
110
|
-
}
|
package/_container/index.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export * from './container.js';
|
|
2
|
-
export * from './decorators.js';
|
|
3
|
-
export * from './interfaces.js';
|
|
4
|
-
export * from './provider.js';
|
|
5
|
-
export * from './resolve-chain.js';
|
|
6
|
-
export * from './resolve.error.js';
|
|
7
|
-
export * from './token.js';
|
|
8
|
-
export * from './type-info.js';
|
|
9
|
-
export * from './types.js';
|
|
10
|
-
export * from './utils.js';
|
package/_container/index.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __copyProps = (to, from, except, desc) => {
|
|
7
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
-
for (let key of __getOwnPropNames(from))
|
|
9
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
-
}
|
|
12
|
-
return to;
|
|
13
|
-
};
|
|
14
|
-
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
|
-
var container_exports = {};
|
|
17
|
-
module.exports = __toCommonJS(container_exports);
|
|
18
|
-
__reExport(container_exports, require("./container.js"), module.exports);
|
|
19
|
-
__reExport(container_exports, require("./decorators.js"), module.exports);
|
|
20
|
-
__reExport(container_exports, require("./interfaces.js"), module.exports);
|
|
21
|
-
__reExport(container_exports, require("./provider.js"), module.exports);
|
|
22
|
-
__reExport(container_exports, require("./resolve-chain.js"), module.exports);
|
|
23
|
-
__reExport(container_exports, require("./resolve.error.js"), module.exports);
|
|
24
|
-
__reExport(container_exports, require("./token.js"), module.exports);
|
|
25
|
-
__reExport(container_exports, require("./type-info.js"), module.exports);
|
|
26
|
-
__reExport(container_exports, require("./types.js"), module.exports);
|
|
27
|
-
__reExport(container_exports, require("./utils.js"), module.exports);
|