@adimm/x-injection 0.3.1

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.
@@ -0,0 +1,831 @@
1
+ import { BindingActivation, BindingDeactivation, BindingConstraints, Container, BindingScope, BindToFluentSyntax, GetOptions, IsBoundOptions, injectFromBase, MetadataName, MetadataTag, ServiceIdentifier } from 'inversify';
2
+ import { Class, Except } from 'type-fest';
3
+
4
+ /** The identifier of the `GlobalAppModule`. */
5
+ declare const GLOBAL_APP_MODULE_ID = "GlobalAppModule";
6
+ /** The default {@link IProviderModule.identifier}. */
7
+ declare const ANONYMOUSE_MODULE_DEFAULT_ID = "AnonymousModule";
8
+
9
+ declare enum InjectionScope {
10
+ /** When the service is resolved, the same cached resolved value will be used. */
11
+ Singleton = 0,
12
+ /** When the service is resolved, a new resolved value will be used each time. */
13
+ Transient = 1,
14
+ /** When the service is resolved within the same container request, the same resolved value will be used. */
15
+ Request = 2
16
+ }
17
+
18
+ /** See {@link https://inversify.io/docs/api/decorator/#injectable} for more details. */
19
+ declare function Injectable(scope?: InjectionScope): ClassDecorator;
20
+
21
+ interface OnEvent<T> {
22
+ /**
23
+ * Sets a binding activation handler. The activation handler is invoked after a dependency has been resolved
24
+ * and before it is added to a scope cache. The activation handler will not be
25
+ * invoked if the dependency is taken from a scope cache.
26
+ */
27
+ activation?: BindingActivation<T>;
28
+ /**
29
+ * Sets a binding deactivation handler on a singleton scope binding.
30
+ * The deactivation handler is called when the binding is unbound from a container.
31
+ */
32
+ deactivation?: BindingDeactivation<T>;
33
+ }
34
+
35
+ type ProviderToken<T = any> = ProviderIdentifier<T> | ProviderClassToken<T> | ProviderValueToken<T> | ProviderFactoryToken<T>;
36
+ type ProviderClassToken<T> = (ProviderOptions<T> & ProviderScopeOption) & {
37
+ /** The `class` to be injected. */
38
+ useClass: Class<T>;
39
+ };
40
+ type ProviderValueToken<T> = ProviderOptions<T> & {
41
+ /** The _(constant)_ `value` to be injected. */
42
+ useValue: T;
43
+ };
44
+ type ProviderFactoryToken<T> = (ProviderOptions<T> & ProviderScopeOption) & {
45
+ /**
46
+ * Factory `function` that returns an instance of the provider to be injected.
47
+ *
48
+ * @example
49
+ * ```ts
50
+ * const connectionProvider = {
51
+ * provide: 'CONNECTION',
52
+ * useFactory: (optionsService: OptionsService, secondService: SecondService) => {
53
+ * const options = optionsService.get();
54
+ *
55
+ * return new DatabaseConnection(options);
56
+ * },
57
+ * // `inject` is optional.
58
+ * inject: [OptionsService, SecondService]
59
+ * };
60
+ * ```
61
+ */
62
+ useFactory: (...providers: any[]) => T;
63
+ /**
64
+ * Optional list of providers to be injected into the context of the Factory `function`.
65
+ *
66
+ * See {@link https://inversify.io/docs/api/binding-syntax/#toresolvedvalue} for more details.
67
+ */
68
+ inject?: ProviderToken[];
69
+ };
70
+ type ProviderIdentifier<T = any> = Class<T> | Function | symbol | string;
71
+ interface ProviderOptions<T> {
72
+ /** The injection `token`. */
73
+ provide: ProviderIdentifier<T>;
74
+ /**
75
+ * Can be used to set a binding handler.
76
+ *
77
+ * See {@link https://inversify.io/docs/fundamentals/lifecycle/activation/ | Activation}
78
+ * and {@link https://inversify.io/docs/fundamentals/lifecycle/deactivation/ | Deactivation} lifecycle.
79
+ */
80
+ onEvent?: OnEvent<T>;
81
+ /**
82
+ * Specifies whether the binding is used to provide a resolved value for the given {@link ProviderToken | provider}.
83
+ *
84
+ * See {@link https://inversify.io/docs/api/binding-syntax/#bindwhenfluentsyntax} for more details.
85
+ */
86
+ when?: (metadata: BindingConstraints) => boolean;
87
+ }
88
+ interface ProviderScopeOption {
89
+ /**
90
+ * The scope determines the caching strategy used to decide whether the service should be resolved or a cached value should be provided.
91
+ *
92
+ * If not provided, it'll default to the `ProviderModule` default scope.
93
+ *
94
+ * See {@link https://inversify.io/docs/fundamentals/binding/#scope} for more details..
95
+ */
96
+ scope?: InjectionScope;
97
+ }
98
+
99
+ /**
100
+ * Class containing an internal set of `utils`.
101
+ *
102
+ * Each {@link IProviderModuleNaked.moduleUtils | ProviderModule} instance has its own {@link ProviderModuleUtils} property instance.
103
+ */
104
+ declare class ProviderModuleUtils {
105
+ /** The low-level InversifyJS {@link Container} owned by {@link ProviderModuleUtils.module | module}. */
106
+ get container(): Container;
107
+ /** The parent {@link IProviderModule | ProviderModule} of `this` instance. */
108
+ readonly module: IProviderModule;
109
+ readonly moduleNaked: IProviderModuleNaked;
110
+ constructor(module: IProviderModule);
111
+ /**
112
+ * Low-level method which can be used to manually register _(bind)_ a new {@link provider} into the {@link ProviderModuleUtils.module | module} container.
113
+ *
114
+ * **Note:** _You shouldn't directly use this to register providers as they will not appear_
115
+ * _into the module's `imports` and `exports` arrays! Therefore leading to unexpected bugs and confusion!_
116
+ *
117
+ * @param provider The {@link ProviderToken | provider} to register.
118
+ * @param defaultScope Optionally provide the default {@link InjectionScope} to use when applicable.
119
+ * @returns `true` when the {@link provider} has been bound otherwhise `false`.
120
+ */
121
+ bindToContainer<T>(provider: ProviderToken<T>, defaultScope: InjectionScope): boolean;
122
+ /** Low-level method which does exactly what {@link bindToContainer} does, however accepts a list of {@link providers}. */
123
+ bindManyToContainer(providers: ProviderToken[], defaultScope: InjectionScope): void;
124
+ private bindSelfTokenToContainer;
125
+ private bindClassTokenToContainer;
126
+ private bindValueTokenToContainer;
127
+ private bindFactoryTokenToContainer;
128
+ /** Sets the {@link InjectionScope} of a bound {@link provider}. */
129
+ private setBindingScope;
130
+ /** Sets the `when` clause of a bound {@link provider}. */
131
+ private setWhenBinding;
132
+ /** Sets the `activation` and `deactivation` events of a bound {@link provider}. */
133
+ private setBindingOnEvent;
134
+ }
135
+
136
+ /** Can be used to publicly expose internal properties and methods of an {@link IProviderModule} instance. */
137
+ interface IProviderModuleNaked extends IProviderModule {
138
+ /** It'll be true when the current module is the global `AppModule`. */
139
+ readonly isAppModule: boolean;
140
+ /** The low-level `InversifyJS` {@link https://inversify.io/docs/api/container/ | container} instance. */
141
+ readonly container: Container;
142
+ /** Instance of the {@link ProviderModuleUtils}. */
143
+ readonly moduleUtils: ProviderModuleUtils;
144
+ /** The default injection scope of this module. */
145
+ readonly defaultScope: {
146
+ /** Scope from `xInjection` {@link InjectionScope} enum. */
147
+ native: InjectionScope;
148
+ /** Scope from `InversifyJS` {@link BindingScope} string union. */
149
+ inversify: BindingScope;
150
+ };
151
+ /** The module dynamic exports method. */
152
+ readonly dynamicExports: DynamicExports | undefined;
153
+ /** The registered `callback` which will be invoked when the internal initialization process has been completed. */
154
+ readonly onReady: ProviderModuleOptions['onReady'];
155
+ /** The registered `callback` which will be invoked when the {@link _dispose} method is invoked. */
156
+ readonly onDispose: ProviderModuleOptions['onDispose'];
157
+ /** It'll _completely_ re-init the `module` with the provided {@link LazyInitOptions | options}. */
158
+ _lazyInit(options: LazyInitOptions): void;
159
+ /** Can be used to get the list of all the imported modules of this module. */
160
+ _getImportedModules(): IProviderModuleNaked[];
161
+ /** Can be used to get the list of all the providers of this module. */
162
+ _getProviders(): ProviderToken[];
163
+ /** Can be used to get the list of all the exportable modules and providers of this module. */
164
+ _getExportableModulesAndProviders(): StaticExports;
165
+ /**
166
+ * Can be used to execute the provided {@link cb | callback} whenever a _new_ {@link https://inversify.io/docs/fundamentals/binding/ | binding}
167
+ * is registered for the {@link provider}.
168
+ *
169
+ * **Note:** _This is not the same as the {@link onActivationEvent}!_
170
+ *
171
+ * @param provider The {@link ProviderToken}.
172
+ * @param cb The `callback` to be invoked.
173
+ */
174
+ _onBind<T>(provider: ProviderToken<T>, cb: () => Promise<void> | void): void;
175
+ /**
176
+ * Can be used to execute the provided {@link cb | callback} whenever an existing {@link https://inversify.io/docs/fundamentals/binding/ | binding}
177
+ * is re-registered for the {@link provider}.
178
+ *
179
+ * @param provider The {@link ProviderToken}.
180
+ * @param cb The `callback` to be invoked.
181
+ */
182
+ _onRebind<T>(provider: ProviderToken<T>, cb: () => Promise<void> | void): void;
183
+ /**
184
+ * Can be used to execute the provided {@link cb | callback} whenever a {@link provider} is `unbound`.
185
+ *
186
+ * **Note:** _This is not the same as the {@link onDeactivationEvent}!_
187
+ * _Also the {@link _onBind}, {@link __rebind} and {@link _onUnbind} registered callbacks will be removed._
188
+ *
189
+ * @param provider The {@link ProviderToken}.
190
+ * @param cb The `callback` to be invoked.
191
+ */
192
+ _onUnbind<T>(provider: ProviderToken<T>, cb: () => Promise<void> | void): void;
193
+ /**
194
+ * Internal method which can be used to completely overwrite the module {@link container}
195
+ * with the one provided.
196
+ *
197
+ * @param cb Callback which when invoked must return an instance of the {@link Container}.
198
+ */
199
+ _overwriteContainer(cb: () => Container): void;
200
+ /**
201
+ * Removes all the bindings from the {@link IProviderModuleNaked.container | container}.
202
+ *
203
+ * Then also sets to `null` these properties:
204
+ * - `container`
205
+ * - `imports`
206
+ * - `providers`
207
+ * - `exports`
208
+ * - `dynamicExports`
209
+ *
210
+ * **Note:** The module can be fully re-initialized by invoking the {@link _lazyInit} method.
211
+ */
212
+ _dispose(): Promise<void>;
213
+ /**
214
+ * Binds a {@link ProviderToken | provider}.
215
+ *
216
+ * See {@link https://inversify.io/docs/api/container/#bind | Container.bind} for more details.
217
+ */
218
+ __bind<T>(provider: ProviderToken<T>): BindToFluentSyntax<T>;
219
+ /**
220
+ * Resolves a dependency by its runtime identifier.
221
+ * The runtime identifier must be associated with only one binding and the binding must be synchronously resolved,
222
+ * otherwise an error is thrown.
223
+ *
224
+ * @param provider The {@link ProviderToken}.
225
+ * @param options The {@link GetOptions}.
226
+ * @returns Either the {@link T | dependency} or `undefined` if {@link GetOptions.optional} is set to `true`.
227
+ *
228
+ * See {@link https://inversify.io/docs/api/container/#get | Container.get} for more details.
229
+ */
230
+ __get<T>(provider: ProviderToken<T>, options?: GetOptions): T;
231
+ /**
232
+ * Resolves a dependency by its runtime identifier.
233
+ * The runtime identifier must be associated with only one binding,
234
+ * otherwise an error is thrown.
235
+ *
236
+ * @param provider The {@link ProviderToken}.
237
+ * @param options The {@link GetOptions}.
238
+ * @returns Either the {@link T | dependency} or `undefined` if {@link GetOptions.optional} is set to `true`.
239
+ *
240
+ * See {@link https://inversify.io/docs/api/container/#getasync | Container.getAsync} for more details.
241
+ */
242
+ __getAsync<T>(provider: ProviderToken<T>, options?: GetOptions): Promise<T>;
243
+ /** See {@link https://inversify.io/docs/api/container/#getall | Container.getAll} for more details. */
244
+ __getAll<T>(provider: ProviderToken<T>, options?: GetOptions): T[];
245
+ /** See {@link https://inversify.io/docs/api/container/#getallasync | Container.getAllAsync} for more details. */
246
+ __getAllAsync<T>(provider: ProviderToken<T>, options?: GetOptions): Promise<T[]>;
247
+ /**
248
+ * Can be used to check if there are registered bindings for the {@link provider | provider}.
249
+ *
250
+ * See {@link https://inversify.io/docs/api/container/#isbound | Container.isBound} for more details.
251
+ */
252
+ __isBound(provider: ProviderToken, options?: IsBoundOptions): boolean;
253
+ /**
254
+ * Can be useed to check if there are registered bindings for the {@link provider | provider} only in the current container.
255
+ *
256
+ * See {@link https://inversify.io/docs/api/container/#iscurrentbound | Container.isCurrentBound} for more details.
257
+ */
258
+ __isCurrentBound(provider: ProviderToken, options?: IsBoundOptions): boolean;
259
+ /**
260
+ * Save the state of the container to be later restored with the restore method.
261
+ *
262
+ * See {@link https://inversify.io/docs/api/container/#snapshot | Container.snapshot} for more details.
263
+ */
264
+ __takeSnapshot(): void;
265
+ /**
266
+ * Restore container state to last snapshot.
267
+ *
268
+ * See {@link https://inversify.io/docs/api/container/#restore | Container.restore} for more details.
269
+ */
270
+ __restoreSnapshot(): void;
271
+ /**
272
+ * Convenience method that unbinds a {@link ProviderToken | provider} and then creates a new binding for it.
273
+ * This is equivalent to calling await `container.unbind` followed by `container.bind`, but in a single method.
274
+ *
275
+ * @param provider The {@link ProviderToken}.
276
+ * @returns A {@link BindToFluentSyntax | binding builder} to continue configuring the new binding.
277
+ *
278
+ * See {@link https://inversify.io/docs/api/container/#rebind | Container.rebind} for more details.
279
+ */
280
+ __rebind<T>(provider: ProviderToken<T>): Promise<BindToFluentSyntax<T>>;
281
+ /**
282
+ * Synchronous version of {@link __rebindProvider}. Unbinds a {@link ProviderToken | provider} synchronously and then creates a new binding for it.
283
+ * Will throw an error if the unbind operation would be asynchronous.
284
+ *
285
+ * @param provider The {@link ProviderToken}.
286
+ * @returns A {@link BindToFluentSyntax | binding builder} to continue configuring the new binding.
287
+ *
288
+ * See {@link https://inversify.io/docs/api/container/#rebindsync | Container.rebindSync} for more details.
289
+ */
290
+ __rebindSync<T>(provider: ProviderToken<T>): BindToFluentSyntax<T>;
291
+ /**
292
+ * Removes **all** the associated bindings with the {@link provider} from the container.
293
+ *
294
+ * This will result in the {@link https://inversify.io/docs/fundamentals/lifecycle/deactivation/ | deactivation process}.
295
+ *
296
+ * See {@link https://inversify.io/docs/api/container/#unbind | Container.unbind} for more details.
297
+ */
298
+ __unbind(provider: ProviderToken): Promise<void>;
299
+ /**
300
+ * Removes **all** the associated bindings with the {@link provider} from the container synchronously.
301
+ * This method works like {@link __unbind} but does not return a Promise.
302
+ * If the unbinding operation would be asynchronous (e.g. due to deactivation handlers),
303
+ * it will throw an error. Use this method when you know the operation won't involve async deactivations.
304
+ *
305
+ * This will result in the {@link https://inversify.io/docs/fundamentals/lifecycle/deactivation/ | deactivation process}.
306
+ *
307
+ * See {@link https://inversify.io/docs/api/container/#unbindsync | Container.unbindSync} for more details.
308
+ */
309
+ __unbindSync(provider: ProviderToken): void;
310
+ /**
311
+ * Remove all bindings bound in this container.
312
+ *
313
+ * This will result in the {@link https://inversify.io/docs/fundamentals/lifecycle/deactivation/ | deactivation process}.
314
+ *
315
+ * See {@link https://inversify.io/docs/api/container/#unbindall | Container.unbindAll} for more details.
316
+ */
317
+ __unbindAll(): Promise<void>;
318
+ }
319
+ type LazyInitOptions = Except<ProviderModuleOptions & ProviderModuleOptionsInternal, 'identifier' | 'isAppModule'>;
320
+
321
+ interface IProviderModule {
322
+ /** The module unique ID. */
323
+ readonly identifier: symbol;
324
+ /**
325
+ * Can be used to retrieve a resolved `dependency` from the module container.
326
+ *
327
+ * @param providerOrIdentifier Either the {@link ProviderToken} or the {@link ProviderIdentifier}.
328
+ * @param isOptional When set to `false` _(default)_ an exception will be thrown when the {@link provider} isn't bound.
329
+ * @returns Either the {@link T | dependency} or `undefined` if {@link isOptional} is set to `true`.
330
+ */
331
+ get<T>(providerOrIdentifier: ProviderToken<T>, isOptional?: boolean): T;
332
+ /**
333
+ * Can be used to retrieve many resolved `dependencies` from the module container at once.
334
+ *
335
+ * @param deps Either one or more {@link ProviderToken} or {@link ProviderIdentifier}.
336
+ * @returns Tuple containing the {@link D | dependencies}.
337
+ *
338
+ * @example
339
+ * ```ts
340
+ * // When ProviderTokens are supplied, TS auto-inference works as expected.
341
+ * // `car` will infer the `Car` type, `engine` the `Engine` type and `dashboard` the `Dashboard` type.
342
+ * const [car, engine, dashboard] = AppModule.getMany(
343
+ * Car,
344
+ * Engine,
345
+ * { providerOrIdentifier: Dashboard, isOptional: true }
346
+ * );
347
+ *
348
+ * // When auto-inference is not possible, you can manually cast the types.
349
+ * const [configService, userService] = AppModule.getMany<[typeof ConfigService, typeof UserService]>('CONFIG_SERVICE', UserService);
350
+ * // Now the `configService` is of type `ConfigService` and the `userService` is of type `UserService`.
351
+ * ```
352
+ */
353
+ getMany<D extends (ProviderModuleGetManyParam<any> | ProviderToken)[]>(...deps: D | unknown[]): ProviderModuleGetManySignature<D>;
354
+ /**
355
+ * Adds an activation handler for the {@link provider}.
356
+ *
357
+ * See {@link https://inversify.io/docs/api/container/#onactivation} for more details.
358
+ */
359
+ onActivationEvent<T>(provider: ProviderToken<T>, cb: BindingActivation<T>): void;
360
+ /**
361
+ * Adds a deactivation handler for the {@link provider}.
362
+ *
363
+ * See {@link https://inversify.io/docs/api/container/#ondeactivation} for more details.
364
+ */
365
+ onDeactivationEvent<T>(provider: ProviderToken<T>, cb: BindingDeactivation<T>): void;
366
+ /**
367
+ * Casts the current module type to the {@link IProviderModuleNaked} type.
368
+ *
369
+ * **Internally used and for testing purposes!**
370
+ */
371
+ toNaked(): IProviderModuleNaked;
372
+ /** Returns the {@link IProviderModule.identifier} `symbol` description. */
373
+ toString(): string;
374
+ }
375
+ type ProviderModuleGetManySignature<Tokens extends (ProviderModuleGetManyParam<any> | ProviderToken)[]> = {
376
+ [K in keyof Tokens]: Tokens[K] extends ProviderModuleGetManyParam<infer U> ? U : Tokens[K] extends ProviderToken<infer T> ? T : Tokens[K] extends ProviderIdentifier<infer I> ? I : never;
377
+ };
378
+ type ProviderModuleGetManyParam<T> = {
379
+ /** The {@link ProviderToken}. */
380
+ provider: ProviderToken<T>;
381
+ /** When set to `false` _(default)_ an exception will be thrown when the {@link ProviderModuleGetManyParam.provider | provider} isn't bound. */
382
+ isOptional?: boolean;
383
+ };
384
+
385
+ interface ProviderModuleOptions {
386
+ /** The module unique ID. */
387
+ identifier?: symbol;
388
+ /** The list of imported {@link IProviderModule | modules} that export the {@link Provider | providers} which are required in this module. */
389
+ imports?: IProviderModule[];
390
+ /** The {@link ProviderToken | providers} that will be instantiated by the container and that may be shared at least across this module. */
391
+ providers?: ProviderToken[];
392
+ /**
393
+ * The subset of {@link ProviderToken | providers} or {@link IProviderModule | modules} that
394
+ * are provided by this module and should be available in other modules which import this module.
395
+ *
396
+ * _Check also the {@link dynamicExports} property._
397
+ */
398
+ exports?: StaticExports;
399
+ /**
400
+ * When provided, can be used to control which providers from the {@link ProviderModuleOptions.exports | exports}
401
+ * array should actually be exported into the importing module.
402
+ *
403
+ * **Note:** _Static {@link ProviderModuleOptions.exports | exports} should always be preferred as their static nature implies predictibility._
404
+ * _This is for advanced use cases only, and most probably you may never need to use a dynamic export!_
405
+ *
406
+ * To keep in mind in order to avoid nasty bugs:
407
+ * - You **must always** return only the providers/modules declared into the static {@link ProviderModuleOptions.exports | exports} array.
408
+ * - You **can** return _less_ providers/modules as long as they are still part of the static {@link ProviderModuleOptions.exports | exports} array.
409
+ * - You **cannot** return _more_ providers/modules than the static {@link ProviderModuleOptions.exports | exports} array!
410
+ *
411
+ * @example
412
+ * ```ts
413
+ * {
414
+ * exports: [ConfigModule, UserModule, PaymentService, ReviewService],
415
+ * dynamicExports: (importerModule, moduleExports) => {
416
+ * const shouldExportOnlyTheServices = true;
417
+ *
418
+ * if (shouldExportOnlyTheServices === false) return moduleExports;
419
+ *
420
+ * return moduleExports.flatMap((ex) => {
421
+ * // With `flatMap` we can map and filter out elements
422
+ * // from the sequence at the same time
423
+ * // by returning an empty array.
424
+ * return ex instanceof ProviderModule ? [] : ex;
425
+ * });
426
+ * }
427
+ * }
428
+ *
429
+ * // Or
430
+ *
431
+ * {
432
+ * exports: [ConfigModule, UserModule, PaymentService, ReviewService],
433
+ * dynamicExports: (importerModule, moduleExports) => {
434
+ * // We export all the providers only when the importer module is not the `BULLIED_MODULE`.
435
+ * if (importerModule.toNaked().name !== 'BULLIED_MODULE') return moduleExports;
436
+ *
437
+ * return [ConfigModule];
438
+ * }
439
+ * }
440
+ *
441
+ * ```
442
+ */
443
+ dynamicExports?: DynamicExports;
444
+ /**
445
+ * The default {@link InjectionScope} to be used when a {@link ProviderToken} does not have a defined `scope`.
446
+ *
447
+ * Defaults to {@link InjectionScope.Singleton}.
448
+ */
449
+ defaultScope?: InjectionScope;
450
+ /**
451
+ * Callback which will be invoked once the module container has been initialized
452
+ * and the providers resolved.
453
+ *
454
+ * **This happens only once, when the {@link IProviderModule | ProviderModule} has been bootstrapped!**
455
+ *
456
+ * @param module The instance of the {@link IProviderModule | module}.
457
+ */
458
+ onReady?: (module: IProviderModule) => Promise<void>;
459
+ /**
460
+ * Callback which will be invoked whenever the internal module dispose method is invoked.
461
+ *
462
+ * **The method will be invoked right _before_ the clean-up process.**
463
+ *
464
+ * @param module The instance of the {@link IProviderModule | module}.
465
+ */
466
+ onDispose?: (module: IProviderModule) => Promise<void>;
467
+ }
468
+ interface ProviderModuleOptionsInternal {
469
+ isAppModule?: boolean;
470
+ /** Can be used to manually provide a {@link Container} instance. */
471
+ container?: () => Container;
472
+ }
473
+ type StaticExports = (ProviderToken | IProviderModule)[];
474
+ type DynamicExports = (
475
+ /** The {@link IProviderModule} which is importing this module. */
476
+ importerModule: IProviderModule,
477
+ /** The {@link ProviderModuleOptions.exports | exports} array of this module. */
478
+ moduleExports: StaticExports) => StaticExports;
479
+
480
+ interface IAppModule extends IProviderModule {
481
+ /** Must be invoked _(only once during the application lifecycle)_ in order to provide the {@link options} to the module. */
482
+ register<AsNaked extends boolean = false>(options: AppModuleOptions): AsNaked extends false ? IAppModule : IAppModule & IProviderModuleNaked;
483
+ toNaked(): IAppModule & IProviderModuleNaked;
484
+ }
485
+ type AppModuleOptions = Except<LazyInitOptions, 'exports' | 'dynamicExports'>;
486
+
487
+ /** See {@link https://inversify.io/docs/api/decorator/#inject} for more details. */
488
+ declare function Inject(provider: ProviderToken): MethodDecorator & ParameterDecorator & PropertyDecorator;
489
+
490
+ /** See {@link https://inversify.io/docs/api/decorator/#multiinject} for more details. */
491
+ declare function MultiInject(provider: ProviderToken): MethodDecorator & ParameterDecorator & PropertyDecorator;
492
+
493
+ /** See {@link https://inversify.io/docs/api/decorator/#injectfrombase} for more details. */
494
+ declare function InjectFromBase(options?: Parameters<typeof injectFromBase>[0]): ClassDecorator;
495
+
496
+ /** See {@link https://inversify.io/docs/api/decorator/#named} for more details. */
497
+ declare function Named(name: MetadataName): MethodDecorator & ParameterDecorator & PropertyDecorator;
498
+
499
+ /** See {@link https://inversify.io/docs/api/decorator/#optional} for more details. */
500
+ declare function Optional(): MethodDecorator & ParameterDecorator & PropertyDecorator;
501
+
502
+ /** See {@link https://inversify.io/docs/api/decorator/#postconstruct} for more details. */
503
+ declare function PostConstruct(): MethodDecorator;
504
+
505
+ /** See {@link https://inversify.io/docs/api/decorator/#predestroy} for more details. */
506
+ declare function PreDestroy(): MethodDecorator;
507
+
508
+ /** See {@link https://inversify.io/docs/api/decorator/#tagged} for more details. */
509
+ declare function Tagged(key: MetadataTag, value: unknown): MethodDecorator & ParameterDecorator & PropertyDecorator;
510
+
511
+ /** See {@link https://inversify.io/docs/api/decorator/#unmanaged} for more details. */
512
+ declare function Unmanaged(): MethodDecorator & ParameterDecorator & PropertyDecorator;
513
+
514
+ /** Exception which indicates that there is a generic error. */
515
+ declare class XInjectionError extends Error {
516
+ name: string;
517
+ }
518
+
519
+ /** Exception which indicates that there is a generic error with an instance of {@link IProviderModule}. */
520
+ declare class XInjectionProviderModuleError extends Error {
521
+ name: string;
522
+ constructor(module: IProviderModule, message: string);
523
+ }
524
+
525
+ declare class XInjectionProviderModuleDisposedError extends XInjectionProviderModuleError {
526
+ name: string;
527
+ constructor(module: IProviderModule);
528
+ }
529
+
530
+ /**
531
+ * Exception which indicates that an instance of {@link IProviderModule}
532
+ * imports another module which dynamically exports its providers/modules and
533
+ * is trying to dynamically export more or different providers/modules than the
534
+ * ones declared into its static exports.
535
+ */
536
+ declare class XInjectionDynamicExportsOutOfRange extends XInjectionProviderModuleError {
537
+ name: string;
538
+ constructor(module: IProviderModule);
539
+ }
540
+
541
+ /**
542
+ * The low-level App Container.
543
+ *
544
+ * **Internally used, please use the `AppModule` to interact with it.**
545
+ */
546
+ declare const GlobalContainer: Container;
547
+
548
+ /**
549
+ * Modules are highly recommended as an effective way to organize your components.
550
+ * For most applications, you'll likely have multiple modules, each encapsulating a closely related set of capabilities.
551
+ *
552
+ * _See {@link ProviderModuleOptions | ProviderModuleOptions}_.
553
+ *
554
+ * @example
555
+ * ```ts
556
+ * import { ProviderModule } from '@adimm/x-injection';
557
+ *
558
+ * const EngineModule = new ProviderModule({
559
+ * providers: [EngineService],
560
+ * exports: [EngineService]
561
+ * });
562
+ *
563
+ * const DashboardModule = new ProviderModule({
564
+ * providers: [DashboardService],
565
+ * exports: [DashboardService]
566
+ * });
567
+ *
568
+ * const CarModule = new ProviderModule({
569
+ * imports: [EngineModule, DashboardModule],
570
+ * providers: [CarService],
571
+ * exports: [CarService]
572
+ * });
573
+ *
574
+ * // Run-time class replacement:
575
+ * const RedCarModule = new ProviderModule({
576
+ * imports: [CarModule],
577
+ * providers: [
578
+ * {
579
+ * provide: CarService,
580
+ * useClass: RedCarService,
581
+ * }
582
+ * ],
583
+ * });
584
+ *
585
+ * // Run-time factory example:
586
+ * const BlackCarModule = new ProviderModule({
587
+ * imports: [CarModule],
588
+ * providers: [
589
+ * {
590
+ * provide: CarService,
591
+ * useFactory: (carService: CarService) => {
592
+ * carService.setColor('black');
593
+ *
594
+ * return carService;
595
+ * },
596
+ * inject: [CarService]
597
+ * }
598
+ * ],
599
+ * });
600
+ * ```
601
+ */
602
+ declare class ProviderModule implements IProviderModule {
603
+ readonly identifier: symbol;
604
+ protected readonly isAppModule: boolean;
605
+ protected readonly container: Container;
606
+ protected readonly defaultScope: IProviderModuleNaked['defaultScope'];
607
+ protected readonly dynamicExports: IProviderModuleNaked['dynamicExports'];
608
+ protected readonly onReady: IProviderModuleNaked['onReady'];
609
+ protected readonly onDispose: IProviderModuleNaked['onDispose'];
610
+ protected readonly moduleUtils: IProviderModuleNaked['moduleUtils'];
611
+ private readonly providers;
612
+ private readonly exports;
613
+ private readonly imports;
614
+ private readonly registeredBindingSideEffects;
615
+ constructor({ identifier, imports, providers, exports, defaultScope, dynamicExports, onReady, onDispose, ..._internalParams }: ProviderModuleOptions);
616
+ get<T>(provider: ProviderToken<T>, isOptional?: boolean): T;
617
+ getMany<D extends (ProviderModuleGetManyParam<any> | ProviderToken)[]>(...deps: D | unknown[]): ProviderModuleGetManySignature<D>;
618
+ onActivationEvent<T>(provider: ProviderToken<T>, cb: BindingActivation<T>): void;
619
+ onDeactivationEvent<T>(provider: ProviderToken<T>, cb: BindingDeactivation<T>): void;
620
+ toNaked(): IProviderModuleNaked;
621
+ toString(): string;
622
+ private prepareContainer;
623
+ private injectImportedModules;
624
+ private injectProviders;
625
+ private registerBindingSideEffect;
626
+ private invokeRegisteredBindingSideEffects;
627
+ private removeRegisteredBindingSideEffects;
628
+ private shouldThrowWhenModuleDynamicExportsDontMatchTheStaticExports;
629
+ private shouldThrowIfDisposed;
630
+ /**
631
+ * **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
632
+ *
633
+ * See {@link IProviderModuleNaked._dispose}.
634
+ */
635
+ protected _dispose(): Promise<void>;
636
+ /**
637
+ * **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
638
+ *
639
+ * See {@link IProviderModuleNaked._lazyInit}.
640
+ */
641
+ protected _lazyInit({ imports, providers, exports, defaultScope, dynamicExports, onReady, onDispose, ..._internalParams }: LazyInitOptions): void;
642
+ /**
643
+ * **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
644
+ *
645
+ * See {@link IProviderModuleNaked._getImportedModules}.
646
+ */
647
+ protected _getImportedModules(): IProviderModuleNaked[];
648
+ /**
649
+ * **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
650
+ *
651
+ * See {@link IProviderModuleNaked._getProviders}.
652
+ */
653
+ protected _getProviders(): ProviderToken[];
654
+ /**
655
+ * **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
656
+ *
657
+ * See {@link IProviderModuleNaked._getExportableModulesAndProviders}.
658
+ */
659
+ protected _getExportableModulesAndProviders(): StaticExports;
660
+ /**
661
+ * **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
662
+ *
663
+ * See {@link IProviderModuleNaked._onBind}.
664
+ */
665
+ protected _onBind<T>(provider: ProviderToken<T>, cb: () => Promise<void> | void): void;
666
+ /**
667
+ * **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
668
+ *
669
+ * See {@link IProviderModuleNaked._onRebind}.
670
+ */
671
+ protected _onRebind<T>(provider: ProviderToken<T>, cb: () => Promise<void> | void): void;
672
+ /**
673
+ * **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
674
+ *
675
+ * See {@link IProviderModuleNaked._onUnbind}.
676
+ */
677
+ protected _onUnbind<T>(provider: ProviderToken<T>, cb: () => Promise<void> | void): void;
678
+ /**
679
+ * **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
680
+ *
681
+ * See {@link IProviderModuleNaked._overwriteContainer}.
682
+ */
683
+ protected _overwriteContainer(cb: () => Container): void;
684
+ /**
685
+ * **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
686
+ *
687
+ * See {@link IProviderModuleNaked.__bind}.
688
+ */
689
+ protected __bind<T>(provider: ProviderToken<T>): BindToFluentSyntax<T>;
690
+ /**
691
+ * **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
692
+ *
693
+ * See {@link IProviderModuleNaked.__get}.
694
+ */
695
+ protected __get<T>(provider: ProviderToken<T>, options?: GetOptions): T;
696
+ /**
697
+ * **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
698
+ *
699
+ * See {@link IProviderModuleNaked.__getAsync}.
700
+ */
701
+ protected __getAsync<T>(provider: ProviderToken<T>, options?: GetOptions): Promise<T>;
702
+ /**
703
+ * **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
704
+ *
705
+ * See {@link IProviderModuleNaked.__getAll}.
706
+ */
707
+ protected __getAll<T>(provider: ProviderToken<T>, options?: GetOptions): T[];
708
+ /**
709
+ * **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
710
+ *
711
+ * See {@link IProviderModuleNaked.__getAllAsync}.
712
+ */
713
+ protected __getAllAsync<T>(provider: ProviderToken<T>, options?: GetOptions): Promise<T[]>;
714
+ /**
715
+ * **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
716
+ *
717
+ * See {@link IProviderModuleNaked.__isBound}.
718
+ */
719
+ protected __isBound(provider: ProviderToken, options?: IsBoundOptions): boolean;
720
+ /**
721
+ * **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
722
+ *
723
+ * See {@link IProviderModuleNaked.__isCurrentBound}.
724
+ */
725
+ protected __isCurrentBound(provider: ProviderToken, options?: IsBoundOptions): boolean;
726
+ /**
727
+ * **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
728
+ *
729
+ * See {@link IProviderModuleNaked.__takeSnapshot}.
730
+ */
731
+ protected __takeSnapshot(): void;
732
+ /**
733
+ * **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
734
+ *
735
+ * See {@link IProviderModuleNaked.__restoreSnapshot}.
736
+ */
737
+ protected __restoreSnapshot(): void;
738
+ /**
739
+ * **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
740
+ *
741
+ * See {@link IProviderModuleNaked.__rebind}.
742
+ */
743
+ protected __rebind<T>(provider: ProviderToken<T>): Promise<BindToFluentSyntax<T>>;
744
+ /**
745
+ * **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
746
+ *
747
+ * See {@link IProviderModuleNaked.__rebindSync}.
748
+ */
749
+ protected __rebindSync<T>(provider: ProviderToken<T>): BindToFluentSyntax<T>;
750
+ /**
751
+ * **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
752
+ *
753
+ * See {@link IProviderModuleNaked.__unbind}.
754
+ */
755
+ protected __unbind(provider: ProviderToken): Promise<void>;
756
+ /**
757
+ * **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
758
+ *
759
+ * See {@link IProviderModuleNaked.__unbindSync}.
760
+ */
761
+ protected __unbindSync(provider: ProviderToken): void;
762
+ /**
763
+ * **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
764
+ *
765
+ * See {@link IProviderModuleNaked.__unbindAll}.
766
+ */
767
+ protected __unbindAll(): Promise<void>;
768
+ }
769
+
770
+ /**
771
+ * Class of the {@link AppModule} instance.
772
+ *
773
+ * **You shouldn't initialize a new instance of this class, please use the {@link AppModule} instance!**
774
+ */
775
+ declare class GlobalAppModule extends ProviderModule implements IAppModule {
776
+ private nakedModule;
777
+ private isLoaded;
778
+ constructor();
779
+ register<AsNaked extends boolean = false>(options: AppModuleOptions): AsNaked extends false ? IAppModule : IAppModule & IProviderModuleNaked;
780
+ toNaked(): IAppModule & IProviderModuleNaked;
781
+ protected _dispose(): Promise<void>;
782
+ }
783
+ /**
784
+ * Special instance of {@link ProviderModule} which acts as the global module of your application in which you can inject any provider
785
+ * which must be available through your entire application.
786
+ *
787
+ * The registered providers will automatically be available inside all the modules.
788
+ *
789
+ * @example
790
+ * ```ts
791
+ * import { AppModule } from '@adimm/x-injection';
792
+ *
793
+ * // The `register` method must be invoked only once during your application life cycle!
794
+ * AppModule.register({
795
+ * imports: [ConfigModule, ApiModule, UserModule, DatabaseModule],
796
+ * providers: [DummyService],
797
+ * });
798
+ * ```
799
+ */
800
+ declare const AppModule: GlobalAppModule;
801
+
802
+ declare function injectionScopeToBindingScope(injectionScope: InjectionScope): BindingScope;
803
+
804
+ declare namespace ProviderTokenHelpers {
805
+ function isClassToken<T>(provider: ProviderToken<T>): provider is ProviderClassToken<T>;
806
+ function isValueToken<T>(provider: ProviderToken<T>): provider is ProviderValueToken<T>;
807
+ function isFactoryToken<T>(provider: ProviderToken<T>): provider is ProviderFactoryToken<T>;
808
+ function isProviderIdentifier<T = any>(value: any): value is ProviderIdentifier<T>;
809
+ function toServiceIdentifier<T = any>(provider: ProviderToken<T>): ServiceIdentifier<T>;
810
+ function toServiceIdentifiers(providers: ProviderToken[]): ServiceIdentifier<unknown>[];
811
+ /**
812
+ * The priority order is as follows:
813
+ * 1. From the `ProviderToken.scope`
814
+ * 2. From the class `@Injectable(scope)` decorator
815
+ * 3. From the `ProviderModule` default scope.
816
+ *
817
+ * @param provider The {@link ProviderToken}.
818
+ * @param moduleDefaultScope The module default scope.
819
+ */
820
+ function getInjectionScopeByPriority(provider: ProviderToken, moduleDefaultScope: InjectionScope): InjectionScope;
821
+ function tryGetProviderOptions<T>(provider: ProviderToken<T>): (ProviderOptions<T> & ProviderScopeOption) | undefined;
822
+ function tryGetScopeFromProvider(provider: ProviderToken): InjectionScope | undefined;
823
+ function tryGetDecoratorScopeFromClass<T = any>(provider: ProviderToken<T>): InjectionScope | undefined;
824
+ }
825
+
826
+ declare namespace ProviderModuleHelpers {
827
+ function buildInternalConstructorParams(params: ProviderModuleOptions & ProviderModuleOptionsInternal): ProviderModuleOptions;
828
+ function isDynamicExport(exporter: StaticExports | DynamicExports): exporter is DynamicExports;
829
+ }
830
+
831
+ export { ANONYMOUSE_MODULE_DEFAULT_ID, AppModule, type AppModuleOptions, type DynamicExports, GLOBAL_APP_MODULE_ID, GlobalAppModule, GlobalContainer, type IAppModule, type IProviderModule, type IProviderModuleNaked, Inject, InjectFromBase, Injectable, InjectionScope, type LazyInitOptions, MultiInject, Named, type OnEvent, Optional, PostConstruct, PreDestroy, type ProviderClassToken, type ProviderFactoryToken, type ProviderIdentifier, ProviderModule, type ProviderModuleGetManyParam, type ProviderModuleGetManySignature, ProviderModuleHelpers, type ProviderModuleOptions, type ProviderModuleOptionsInternal, type ProviderOptions, type ProviderScopeOption, type ProviderToken, ProviderTokenHelpers, type ProviderValueToken, type StaticExports, Tagged, Unmanaged, XInjectionDynamicExportsOutOfRange, XInjectionError, XInjectionProviderModuleDisposedError, XInjectionProviderModuleError, injectionScopeToBindingScope };