@adimm/x-injection 0.5.1 → 0.6.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/README.md +123 -4
- package/dist/index.cjs +184 -148
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +468 -445
- package/dist/index.d.ts +468 -445
- package/dist/index.js +164 -129
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -106,7 +106,8 @@ declare class ProviderModuleUtils {
|
|
|
106
106
|
/** The parent {@link IProviderModule | ProviderModule} of `this` instance. */
|
|
107
107
|
readonly module: IProviderModule;
|
|
108
108
|
readonly moduleNaked: IProviderModuleNaked;
|
|
109
|
-
|
|
109
|
+
private readonly appModule;
|
|
110
|
+
constructor(module: IProviderModule, internalOptions: ProviderModuleOptionsInternal);
|
|
110
111
|
/**
|
|
111
112
|
* Low-level method which can be used to manually register _(bind)_ a new {@link provider} into the {@link ProviderModuleUtils.module | module} container.
|
|
112
113
|
*
|
|
@@ -118,6 +119,7 @@ declare class ProviderModuleUtils {
|
|
|
118
119
|
* @returns `true` when the {@link provider} has been bound otherwhise `false`.
|
|
119
120
|
*/
|
|
120
121
|
bindToContainer<T>(provider: DependencyProvider<T>, defaultScope: InjectionScope): boolean;
|
|
122
|
+
private checkIfShouldBeImportedIntoAppModule;
|
|
121
123
|
private bindSelfTokenToContainer;
|
|
122
124
|
private bindClassTokenToContainer;
|
|
123
125
|
private bindValueTokenToContainer;
|
|
@@ -330,571 +332,592 @@ interface IProviderModuleNaked extends IProviderModule {
|
|
|
330
332
|
}
|
|
331
333
|
type LazyInitOptions = Except<ProviderModuleOptions & ProviderModuleOptionsInternal, 'identifier' | 'isAppModule'>;
|
|
332
334
|
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
335
|
+
/**
|
|
336
|
+
* The low-level App Container.
|
|
337
|
+
*
|
|
338
|
+
* **Internally used, please use the `AppModule` to interact with it.**
|
|
339
|
+
*/
|
|
340
|
+
declare const GlobalContainer: Container;
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Modules are highly recommended as an effective way to organize your components.
|
|
344
|
+
* For most applications, you'll likely have multiple modules, each encapsulating a closely related set of capabilities.
|
|
345
|
+
*
|
|
346
|
+
* _See {@link ProviderModuleOptions | ProviderModuleOptions}_.
|
|
347
|
+
*
|
|
348
|
+
* @example
|
|
349
|
+
* ```ts
|
|
350
|
+
* const EngineModule = new ProviderModule({
|
|
351
|
+
* identifier: Symbol('EngineModule'),
|
|
352
|
+
* providers: [EngineService],
|
|
353
|
+
* exports: [EngineService]
|
|
354
|
+
* });
|
|
355
|
+
*
|
|
356
|
+
* const DashboardModule = new ProviderModule({
|
|
357
|
+
* identifier: Symbol('DashboardModule'),
|
|
358
|
+
* providers: [DashboardService],
|
|
359
|
+
* exports: [DashboardService]
|
|
360
|
+
* });
|
|
361
|
+
*
|
|
362
|
+
* const CarModule = new ProviderModule({
|
|
363
|
+
* identifier: Symbol('CarModule'),
|
|
364
|
+
* imports: [EngineModule, DashboardModule],
|
|
365
|
+
* providers: [CarService],
|
|
366
|
+
* exports: [CarService]
|
|
367
|
+
* });
|
|
368
|
+
*
|
|
369
|
+
* // Run-time class replacement:
|
|
370
|
+
* const RedCarModule = new ProviderModule({
|
|
371
|
+
* identifier: Symbol('RedCarModule'),
|
|
372
|
+
* imports: [CarModule],
|
|
373
|
+
* providers: [
|
|
374
|
+
* {
|
|
375
|
+
* provide: CarService,
|
|
376
|
+
* useClass: RedCarService,
|
|
377
|
+
* }
|
|
378
|
+
* ],
|
|
379
|
+
* });
|
|
380
|
+
*
|
|
381
|
+
* // Run-time factory example:
|
|
382
|
+
* const BlackCarModule = new ProviderModule({
|
|
383
|
+
* identifier: Symbol('BlackCarModule'),
|
|
384
|
+
* imports: [CarModule],
|
|
385
|
+
* providers: [
|
|
386
|
+
* {
|
|
387
|
+
* provide: CarService,
|
|
388
|
+
* useFactory: (carService: CarService) => {
|
|
389
|
+
* carService.setColor('black');
|
|
390
|
+
*
|
|
391
|
+
* return carService;
|
|
392
|
+
* },
|
|
393
|
+
* inject: [CarService]
|
|
394
|
+
* }
|
|
395
|
+
* ],
|
|
396
|
+
* });
|
|
397
|
+
* ```
|
|
398
|
+
*/
|
|
399
|
+
declare class ProviderModule implements IProviderModule {
|
|
400
|
+
readonly identifier: symbol;
|
|
401
|
+
readonly isMarkedAsGlobal: boolean;
|
|
402
|
+
readonly isDisposed: boolean;
|
|
403
|
+
protected readonly isAppModule: boolean;
|
|
404
|
+
protected readonly container: Container;
|
|
405
|
+
protected readonly defaultScope: IProviderModuleNaked['defaultScope'];
|
|
406
|
+
protected readonly dynamicExports: IProviderModuleNaked['dynamicExports'];
|
|
407
|
+
protected readonly onReady: IProviderModuleNaked['onReady'];
|
|
408
|
+
protected readonly onDispose: IProviderModuleNaked['onDispose'];
|
|
409
|
+
protected readonly importedProvidersMap: IProviderModuleNaked['importedProvidersMap'];
|
|
410
|
+
protected readonly moduleUtils: IProviderModuleNaked['moduleUtils'];
|
|
411
|
+
protected readonly imports: IProviderModuleNaked['imports'];
|
|
412
|
+
protected readonly providers: IProviderModuleNaked['providers'];
|
|
413
|
+
protected readonly importedProviders: IProviderModuleNaked['importedProviders'];
|
|
414
|
+
protected readonly exports: IProviderModuleNaked['exports'];
|
|
415
|
+
private readonly registeredBindingSideEffects;
|
|
416
|
+
constructor({ identifier, imports, providers, exports, defaultScope, markAsGlobal, dynamicExports, onReady, onDispose, ..._internalParams }: ProviderModuleOptions);
|
|
417
|
+
get<T>(provider: ProviderToken<T>, isOptional?: boolean): T;
|
|
418
|
+
getMany<D extends (ProviderModuleGetManyParam<any> | ProviderToken)[]>(...deps: D | unknown[]): ProviderModuleGetManySignature<D>;
|
|
419
|
+
onActivationEvent<T>(provider: ProviderToken<T>, cb: BindingActivation<T>): void;
|
|
420
|
+
onDeactivationEvent<T>(provider: ProviderToken<T>, cb: BindingDeactivation<T>): void;
|
|
421
|
+
toNaked(): IProviderModuleNaked;
|
|
422
|
+
clone(options?: CloneParams): IProviderModule;
|
|
423
|
+
toString(): string;
|
|
424
|
+
private setIdentifier;
|
|
425
|
+
private prepareContainer;
|
|
426
|
+
private injectImportedModules;
|
|
427
|
+
private injectProviders;
|
|
428
|
+
private registerBindingSideEffect;
|
|
429
|
+
private invokeRegisteredBindingSideEffects;
|
|
430
|
+
private removeRegisteredBindingSideEffects;
|
|
431
|
+
private shouldThrowWhenModuleDynamicExportsDontMatchTheStaticExports;
|
|
432
|
+
private shouldThrowIfDisposed;
|
|
340
433
|
/**
|
|
341
|
-
*
|
|
342
|
-
* are provided by this module and should be available in other modules which import this module.
|
|
434
|
+
* **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
|
|
343
435
|
*
|
|
344
|
-
*
|
|
436
|
+
* See {@link IProviderModuleNaked._dispose}.
|
|
345
437
|
*/
|
|
346
|
-
|
|
438
|
+
protected _dispose(): Promise<void>;
|
|
347
439
|
/**
|
|
348
|
-
*
|
|
349
|
-
* array should actually be exported into the importing module.
|
|
350
|
-
*
|
|
351
|
-
* **Note:** _Static {@link ProviderModuleOptions.exports | exports} should always be preferred as their static nature implies predictibility._
|
|
352
|
-
* _This is for advanced use cases only, and most probably you may never need to use a dynamic export!_
|
|
353
|
-
*
|
|
354
|
-
* To keep in mind in order to avoid nasty bugs:
|
|
355
|
-
* - You **must always** return only the providers/modules declared into the static {@link ProviderModuleOptions.exports | exports} array.
|
|
356
|
-
* - You **can** return _less_ providers/modules as long as they are still part of the static {@link ProviderModuleOptions.exports | exports} array.
|
|
357
|
-
* - You **cannot** return _more_ providers/modules than the static {@link ProviderModuleOptions.exports | exports} array!
|
|
358
|
-
*
|
|
359
|
-
* @example
|
|
360
|
-
* ```ts
|
|
361
|
-
* {
|
|
362
|
-
* exports: [ConfigModule, UserModule, PaymentService, ReviewService],
|
|
363
|
-
* dynamicExports: (importerModule, moduleExports) => {
|
|
364
|
-
* const shouldExportOnlyTheServices = true;
|
|
365
|
-
*
|
|
366
|
-
* if (shouldExportOnlyTheServices === false) return moduleExports;
|
|
367
|
-
*
|
|
368
|
-
* return moduleExports.flatMap((ex) => {
|
|
369
|
-
* // With `flatMap` we can map and filter out elements
|
|
370
|
-
* // from the sequence at the same time
|
|
371
|
-
* // by returning an empty array.
|
|
372
|
-
* return ex instanceof ProviderModule ? [] : ex;
|
|
373
|
-
* });
|
|
374
|
-
* }
|
|
375
|
-
* }
|
|
376
|
-
*
|
|
377
|
-
* // Or
|
|
378
|
-
*
|
|
379
|
-
* {
|
|
380
|
-
* exports: [ConfigModule, UserModule, PaymentService, ReviewService],
|
|
381
|
-
* dynamicExports: (importerModule, moduleExports) => {
|
|
382
|
-
* // We export all the providers only when the importer module is not the `BULLIED_MODULE`.
|
|
383
|
-
* if (importerModule.toNaked().name !== 'BULLIED_MODULE') return moduleExports;
|
|
384
|
-
*
|
|
385
|
-
* return [ConfigModule];
|
|
386
|
-
* }
|
|
387
|
-
* }
|
|
440
|
+
* **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
|
|
388
441
|
*
|
|
389
|
-
*
|
|
442
|
+
* See {@link IProviderModuleNaked._lazyInit}.
|
|
390
443
|
*/
|
|
391
|
-
dynamicExports
|
|
444
|
+
protected _lazyInit({ markAsGlobal, imports, providers, exports, defaultScope, dynamicExports, onReady, onDispose, ..._internalParams }: LazyInitOptions): IProviderModule;
|
|
392
445
|
/**
|
|
393
|
-
*
|
|
446
|
+
* **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
|
|
394
447
|
*
|
|
395
|
-
*
|
|
448
|
+
* See {@link IProviderModuleNaked._getImportedModules}.
|
|
396
449
|
*/
|
|
397
|
-
|
|
450
|
+
protected _getImportedModules(): IProviderModuleNaked[];
|
|
398
451
|
/**
|
|
399
|
-
*
|
|
400
|
-
* and the providers resolved.
|
|
401
|
-
*
|
|
402
|
-
* **This happens only once, when the {@link IProviderModule | ProviderModule} has been bootstrapped!**
|
|
452
|
+
* **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
|
|
403
453
|
*
|
|
404
|
-
*
|
|
454
|
+
* See {@link IProviderModuleNaked._getProviders}.
|
|
405
455
|
*/
|
|
406
|
-
|
|
456
|
+
protected _getProviders(): DependencyProvider[];
|
|
407
457
|
/**
|
|
408
|
-
*
|
|
409
|
-
*
|
|
410
|
-
* **The method will be invoked right _before_ the clean-up process.**
|
|
458
|
+
* **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
|
|
411
459
|
*
|
|
412
|
-
*
|
|
460
|
+
* See {@link IProviderModuleNaked._getExportableModulesAndProviders}.
|
|
413
461
|
*/
|
|
414
|
-
|
|
415
|
-
}
|
|
416
|
-
interface ProviderModuleOptionsInternal {
|
|
417
|
-
isAppModule?: boolean;
|
|
418
|
-
isDisposed?: boolean;
|
|
419
|
-
/** Can be used to manually provide a {@link Container} instance. */
|
|
420
|
-
container?: () => Container;
|
|
421
|
-
/** Can be used to override all the _imported_ providers _before_ the binding process. */
|
|
422
|
-
importedProvidersMap?: (
|
|
423
|
-
/** The current imported {@link DependencyProvider | provider} altered to use the module from where was imported for resolution. */
|
|
424
|
-
factorizedProvider: DependencyProvider<any>,
|
|
425
|
-
/** The current imported {@link DependencyProvider | provider}. */
|
|
426
|
-
provider: DependencyProvider<any>,
|
|
427
|
-
/** The {@link IProviderModule | module} from where the {@link DependencyProvider | provider} originated. */
|
|
428
|
-
module: IProviderModule) => DependencyProvider<any>;
|
|
429
|
-
}
|
|
430
|
-
type StaticExports = (ProviderToken | IProviderModule)[];
|
|
431
|
-
type DynamicExports = (
|
|
432
|
-
/** The {@link IProviderModule} which is importing this module. */
|
|
433
|
-
importerModule: IProviderModule,
|
|
434
|
-
/** The {@link ProviderModuleOptions.exports | exports} array of this module. */
|
|
435
|
-
moduleExports: StaticExports) => StaticExports;
|
|
436
|
-
|
|
437
|
-
interface IProviderModule {
|
|
438
|
-
/** The module unique ID. */
|
|
439
|
-
readonly identifier: symbol;
|
|
440
|
-
readonly isDisposed: boolean;
|
|
462
|
+
protected _getExportableModulesAndProviders(): StaticExports;
|
|
441
463
|
/**
|
|
442
|
-
*
|
|
464
|
+
* **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
|
|
443
465
|
*
|
|
444
|
-
*
|
|
445
|
-
* @param isOptional When set to `false` _(default)_ an exception will be thrown when the {@link provider} isn't bound.
|
|
446
|
-
* @returns Either the {@link T | dependency} or `undefined` if {@link isOptional} is set to `true`.
|
|
466
|
+
* See {@link IProviderModuleNaked._onBind}.
|
|
447
467
|
*/
|
|
448
|
-
|
|
468
|
+
protected _onBind<T>(provider: ProviderToken<T>, cb: () => Promise<void> | void): void;
|
|
449
469
|
/**
|
|
450
|
-
*
|
|
451
|
-
*
|
|
452
|
-
* @param deps Either one or more {@link ProviderToken}.
|
|
453
|
-
* @returns Tuple containing the {@link D | dependencies}.
|
|
454
|
-
*
|
|
455
|
-
* @example
|
|
456
|
-
* ```ts
|
|
457
|
-
* // When ProviderTokens are supplied, TS auto-inference works as expected.
|
|
458
|
-
* // `car` will infer the `Car` type, `engine` the `Engine` type and `dashboard` the `Dashboard` type.
|
|
459
|
-
* const [car, engine, dashboard] = AppModule.getMany(
|
|
460
|
-
* Car,
|
|
461
|
-
* Engine,
|
|
462
|
-
* { provider: Dashboard, isOptional: true }
|
|
463
|
-
* );
|
|
470
|
+
* **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
|
|
464
471
|
*
|
|
465
|
-
*
|
|
466
|
-
* const [configService, userService] = AppModule.getMany<[typeof ConfigService, typeof UserService]>('CONFIG_SERVICE', UserService);
|
|
467
|
-
* // Now the `configService` is of type `ConfigService` and the `userService` is of type `UserService`.
|
|
468
|
-
* ```
|
|
472
|
+
* See {@link IProviderModuleNaked._onRebind}.
|
|
469
473
|
*/
|
|
470
|
-
|
|
474
|
+
protected _onRebind<T>(provider: ProviderToken<T>, cb: () => Promise<void> | void): void;
|
|
471
475
|
/**
|
|
472
|
-
*
|
|
476
|
+
* **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
|
|
473
477
|
*
|
|
474
|
-
* See {@link
|
|
478
|
+
* See {@link IProviderModuleNaked._onUnbind}.
|
|
475
479
|
*/
|
|
476
|
-
|
|
480
|
+
protected _onUnbind<T>(provider: ProviderToken<T>, cb: () => Promise<void> | void): void;
|
|
477
481
|
/**
|
|
478
|
-
*
|
|
482
|
+
* **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
|
|
479
483
|
*
|
|
480
|
-
* See {@link
|
|
484
|
+
* See {@link IProviderModuleNaked._overwriteContainer}.
|
|
481
485
|
*/
|
|
482
|
-
|
|
486
|
+
protected _overwriteContainer(cb: () => Container): void;
|
|
483
487
|
/**
|
|
484
|
-
*
|
|
488
|
+
* **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
|
|
485
489
|
*
|
|
486
|
-
*
|
|
490
|
+
* See {@link IProviderModuleNaked.__bind}.
|
|
487
491
|
*/
|
|
488
|
-
|
|
492
|
+
protected __bind<T>(provider: ProviderToken<T>): BindToFluentSyntax<T>;
|
|
489
493
|
/**
|
|
490
|
-
*
|
|
494
|
+
* **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
|
|
491
495
|
*
|
|
492
|
-
*
|
|
493
|
-
* _And also the new module will still refrain values by reference to its parent module because of_
|
|
494
|
-
* _JS limitation in deeply/truly cloning an instance._
|
|
495
|
-
*
|
|
496
|
-
* @param options See {@link CloneParams}.
|
|
496
|
+
* See {@link IProviderModuleNaked.__get}.
|
|
497
497
|
*/
|
|
498
|
-
|
|
499
|
-
/** Returns the {@link IProviderModule.identifier} `symbol` description. */
|
|
500
|
-
toString(): string;
|
|
501
|
-
}
|
|
502
|
-
type ProviderModuleGetManySignature<Tokens extends (ProviderModuleGetManyParam<any> | ProviderToken)[]> = {
|
|
503
|
-
[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;
|
|
504
|
-
};
|
|
505
|
-
type ProviderModuleGetManyParam<T> = {
|
|
506
|
-
/** The {@link ProviderToken}. */
|
|
507
|
-
provider: ProviderToken<T>;
|
|
508
|
-
/** When set to `false` _(default)_ an exception will be thrown when the {@link ProviderModuleGetManyParam.provider | provider} isn't bound. */
|
|
509
|
-
isOptional?: boolean;
|
|
510
|
-
};
|
|
511
|
-
interface CloneParams {
|
|
512
|
-
/** Can be used to override all the providers _before_ the binding process. */
|
|
513
|
-
providersMap?: (
|
|
514
|
-
/** The current {@link DependencyProvider | provider}. */
|
|
515
|
-
provider: DependencyProvider<any>,
|
|
516
|
-
/** The {@link IProviderModule | module} from where the {@link DependencyProvider | provider} originated. */
|
|
517
|
-
module: IProviderModule) => DependencyProvider<any>;
|
|
518
|
-
/** Can be used to override all the _imported_ providers _before_ the binding process. */
|
|
519
|
-
importedProvidersMap?: ProviderModuleOptionsInternal['importedProvidersMap'];
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
interface IAppModule extends IProviderModule {
|
|
523
|
-
/** Must be invoked _(only once during the application lifecycle)_ in order to provide the {@link options} to the module. */
|
|
524
|
-
register<AsNaked extends boolean = false>(options: AppModuleOptions): AsNaked extends false ? IAppModule : IAppModule & IProviderModuleNaked;
|
|
525
|
-
toNaked(): IAppModule & IProviderModuleNaked;
|
|
526
|
-
}
|
|
527
|
-
type AppModuleOptions = Except<LazyInitOptions, 'exports' | 'dynamicExports'>;
|
|
528
|
-
|
|
529
|
-
/** See {@link https://inversify.io/docs/api/decorator/#inject} for more details. */
|
|
530
|
-
declare function Inject(provider: ProviderToken): MethodDecorator & ParameterDecorator & PropertyDecorator;
|
|
531
|
-
|
|
532
|
-
/** See {@link https://inversify.io/docs/api/decorator/#multiinject} for more details. */
|
|
533
|
-
declare function MultiInject(provider: ProviderToken): MethodDecorator & ParameterDecorator & PropertyDecorator;
|
|
534
|
-
|
|
535
|
-
/** See {@link https://inversify.io/docs/api/decorator/#injectfrombase} for more details. */
|
|
536
|
-
declare function InjectFromBase(options?: Parameters<typeof injectFromBase>[0]): ClassDecorator;
|
|
537
|
-
|
|
538
|
-
/** See {@link https://inversify.io/docs/api/decorator/#named} for more details. */
|
|
539
|
-
declare function Named(name: MetadataName): MethodDecorator & ParameterDecorator & PropertyDecorator;
|
|
540
|
-
|
|
541
|
-
/** See {@link https://inversify.io/docs/api/decorator/#optional} for more details. */
|
|
542
|
-
declare function Optional(): MethodDecorator & ParameterDecorator & PropertyDecorator;
|
|
543
|
-
|
|
544
|
-
/** See {@link https://inversify.io/docs/api/decorator/#postconstruct} for more details. */
|
|
545
|
-
declare function PostConstruct(): MethodDecorator;
|
|
546
|
-
|
|
547
|
-
/** See {@link https://inversify.io/docs/api/decorator/#predestroy} for more details. */
|
|
548
|
-
declare function PreDestroy(): MethodDecorator;
|
|
549
|
-
|
|
550
|
-
/** See {@link https://inversify.io/docs/api/decorator/#tagged} for more details. */
|
|
551
|
-
declare function Tagged(key: MetadataTag, value: unknown): MethodDecorator & ParameterDecorator & PropertyDecorator;
|
|
552
|
-
|
|
553
|
-
/** See {@link https://inversify.io/docs/api/decorator/#unmanaged} for more details. */
|
|
554
|
-
declare function Unmanaged(): MethodDecorator & ParameterDecorator & PropertyDecorator;
|
|
555
|
-
|
|
556
|
-
/** Exception which indicates that there is a generic error. */
|
|
557
|
-
declare class XInjectionError extends Error {
|
|
558
|
-
name: string;
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
/** Exception which indicates that there is a generic error with an instance of {@link IProviderModule}. */
|
|
562
|
-
declare class XInjectionProviderModuleError extends Error {
|
|
563
|
-
name: string;
|
|
564
|
-
constructor(module: IProviderModule, message: string);
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
/** Exception which indicates an invokation of a disposed module. */
|
|
568
|
-
declare class XInjectionProviderModuleDisposedError extends XInjectionProviderModuleError {
|
|
569
|
-
name: string;
|
|
570
|
-
constructor(module: IProviderModule);
|
|
571
|
-
}
|
|
572
|
-
|
|
573
|
-
/**
|
|
574
|
-
* Exception which indicates that an instance of {@link IProviderModule}
|
|
575
|
-
* imports another module which dynamically exports its providers/modules and
|
|
576
|
-
* is trying to dynamically export more or different providers/modules than the
|
|
577
|
-
* ones declared into its static exports.
|
|
578
|
-
*/
|
|
579
|
-
declare class XInjectionDynamicExportsOutOfRange extends XInjectionProviderModuleError {
|
|
580
|
-
name: string;
|
|
581
|
-
constructor(module: IProviderModule);
|
|
582
|
-
}
|
|
583
|
-
|
|
584
|
-
/** Exception which indicates that a module has been initialized without an `identifier`. */
|
|
585
|
-
declare class XInjectionProviderModuleMissingIdentifierError extends XInjectionProviderModuleError {
|
|
586
|
-
name: string;
|
|
587
|
-
constructor(module: IProviderModule);
|
|
588
|
-
}
|
|
589
|
-
|
|
590
|
-
declare function injectionScopeToBindingScope(injectionScope: InjectionScope): BindingScope;
|
|
591
|
-
declare function bindingScopeToInjectionScope(bindingScope: BindingScope): InjectionScope;
|
|
592
|
-
|
|
593
|
-
declare namespace ProviderTokenHelpers {
|
|
594
|
-
function isClassToken<T>(provider: ProviderToken<T>): provider is ProviderClassToken<T>;
|
|
595
|
-
function isValueToken<T>(provider: ProviderToken<T>): provider is ProviderValueToken<T>;
|
|
596
|
-
function isFactoryToken<T>(provider: ProviderToken<T>): provider is ProviderFactoryToken<T>;
|
|
597
|
-
function isProviderIdentifier<T = any>(value: any): value is ProviderIdentifier<T>;
|
|
598
|
-
function toServiceIdentifier<T = any>(provider: ProviderToken<T>): ServiceIdentifier<T>;
|
|
599
|
-
function toServiceIdentifiers(providers: ProviderToken[]): ServiceIdentifier<unknown>[];
|
|
600
|
-
function toDependencyProviderWithOptions<T extends DependencyProvider<any>>(original: T, override?: Partial<T>): T;
|
|
498
|
+
protected __get<T>(provider: ProviderToken<T>, options?: GetOptions): T;
|
|
601
499
|
/**
|
|
602
|
-
*
|
|
603
|
-
* 1. From the `ProviderToken.scope`
|
|
604
|
-
* 2. From the class `@Injectable(scope)` decorator
|
|
605
|
-
* 3. From the `ProviderModule` default scope.
|
|
500
|
+
* **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
|
|
606
501
|
*
|
|
607
|
-
*
|
|
608
|
-
* @param moduleDefaultScope The module default scope.
|
|
502
|
+
* See {@link IProviderModuleNaked.__getAsync}.
|
|
609
503
|
*/
|
|
610
|
-
|
|
611
|
-
function tryGetProviderOptions<T>(provider: ProviderToken<T>): (ProviderOptions<T> & ProviderScopeOption) | undefined;
|
|
612
|
-
function tryGetScopeFromProvider(provider: ProviderToken): InjectionScope | undefined;
|
|
613
|
-
function tryGetDecoratorScopeFromClass<T = any>(provider: ProviderToken<T>): InjectionScope | undefined;
|
|
614
|
-
}
|
|
615
|
-
|
|
616
|
-
declare namespace ProviderModuleHelpers {
|
|
617
|
-
function buildInternalConstructorParams(params: ProviderModuleOptions & ProviderModuleOptionsInternal): ProviderModuleOptions;
|
|
618
|
-
function isDynamicExport(exporter: StaticExports | DynamicExports): exporter is DynamicExports;
|
|
619
|
-
}
|
|
620
|
-
|
|
621
|
-
declare function isPlainObject(o: any): o is object;
|
|
622
|
-
|
|
623
|
-
declare function isClass(v: any): boolean;
|
|
624
|
-
|
|
625
|
-
declare function isFunction(v: any): boolean;
|
|
626
|
-
|
|
627
|
-
declare function isClassOrFunction(value: any): value is Function | Class<any>;
|
|
628
|
-
|
|
629
|
-
/**
|
|
630
|
-
* The low-level App Container.
|
|
631
|
-
*
|
|
632
|
-
* **Internally used, please use the `AppModule` to interact with it.**
|
|
633
|
-
*/
|
|
634
|
-
declare const GlobalContainer: Container;
|
|
635
|
-
|
|
636
|
-
/**
|
|
637
|
-
* Modules are highly recommended as an effective way to organize your components.
|
|
638
|
-
* For most applications, you'll likely have multiple modules, each encapsulating a closely related set of capabilities.
|
|
639
|
-
*
|
|
640
|
-
* _See {@link ProviderModuleOptions | ProviderModuleOptions}_.
|
|
641
|
-
*
|
|
642
|
-
* @example
|
|
643
|
-
* ```ts
|
|
644
|
-
* import { ProviderModule } from '@adimm/x-injection';
|
|
645
|
-
*
|
|
646
|
-
* const EngineModule = new ProviderModule({
|
|
647
|
-
* identifier: Symbol('EngineModule'),
|
|
648
|
-
* providers: [EngineService],
|
|
649
|
-
* exports: [EngineService]
|
|
650
|
-
* });
|
|
651
|
-
*
|
|
652
|
-
* const DashboardModule = new ProviderModule({
|
|
653
|
-
* identifier: Symbol('DashboardModule'),
|
|
654
|
-
* providers: [DashboardService],
|
|
655
|
-
* exports: [DashboardService]
|
|
656
|
-
* });
|
|
657
|
-
*
|
|
658
|
-
* const CarModule = new ProviderModule({
|
|
659
|
-
* identifier: Symbol('CarModule'),
|
|
660
|
-
* imports: [EngineModule, DashboardModule],
|
|
661
|
-
* providers: [CarService],
|
|
662
|
-
* exports: [CarService]
|
|
663
|
-
* });
|
|
664
|
-
*
|
|
665
|
-
* // Run-time class replacement:
|
|
666
|
-
* const RedCarModule = new ProviderModule({
|
|
667
|
-
* identifier: Symbol('RedCarModule'),
|
|
668
|
-
* imports: [CarModule],
|
|
669
|
-
* providers: [
|
|
670
|
-
* {
|
|
671
|
-
* provide: CarService,
|
|
672
|
-
* useClass: RedCarService,
|
|
673
|
-
* }
|
|
674
|
-
* ],
|
|
675
|
-
* });
|
|
676
|
-
*
|
|
677
|
-
* // Run-time factory example:
|
|
678
|
-
* const BlackCarModule = new ProviderModule({
|
|
679
|
-
* identifier: Symbol('BlackCarModule'),
|
|
680
|
-
* imports: [CarModule],
|
|
681
|
-
* providers: [
|
|
682
|
-
* {
|
|
683
|
-
* provide: CarService,
|
|
684
|
-
* useFactory: (carService: CarService) => {
|
|
685
|
-
* carService.setColor('black');
|
|
686
|
-
*
|
|
687
|
-
* return carService;
|
|
688
|
-
* },
|
|
689
|
-
* inject: [CarService]
|
|
690
|
-
* }
|
|
691
|
-
* ],
|
|
692
|
-
* });
|
|
693
|
-
* ```
|
|
694
|
-
*/
|
|
695
|
-
declare class ProviderModule implements IProviderModule {
|
|
696
|
-
readonly identifier: symbol;
|
|
697
|
-
readonly isDisposed: boolean;
|
|
698
|
-
protected readonly isAppModule: boolean;
|
|
699
|
-
protected readonly container: Container;
|
|
700
|
-
protected readonly defaultScope: IProviderModuleNaked['defaultScope'];
|
|
701
|
-
protected readonly dynamicExports: IProviderModuleNaked['dynamicExports'];
|
|
702
|
-
protected readonly onReady: IProviderModuleNaked['onReady'];
|
|
703
|
-
protected readonly onDispose: IProviderModuleNaked['onDispose'];
|
|
704
|
-
protected readonly importedProvidersMap: IProviderModuleNaked['importedProvidersMap'];
|
|
705
|
-
protected readonly moduleUtils: IProviderModuleNaked['moduleUtils'];
|
|
706
|
-
protected readonly imports: IProviderModuleNaked['imports'];
|
|
707
|
-
protected readonly providers: IProviderModuleNaked['providers'];
|
|
708
|
-
protected readonly importedProviders: IProviderModuleNaked['importedProviders'];
|
|
709
|
-
protected readonly exports: IProviderModuleNaked['exports'];
|
|
710
|
-
private readonly registeredBindingSideEffects;
|
|
711
|
-
constructor({ identifier, imports, providers, exports, defaultScope, dynamicExports, onReady, onDispose, ..._internalParams }: ProviderModuleOptions);
|
|
712
|
-
get<T>(provider: ProviderToken<T>, isOptional?: boolean): T;
|
|
713
|
-
getMany<D extends (ProviderModuleGetManyParam<any> | ProviderToken)[]>(...deps: D | unknown[]): ProviderModuleGetManySignature<D>;
|
|
714
|
-
onActivationEvent<T>(provider: ProviderToken<T>, cb: BindingActivation<T>): void;
|
|
715
|
-
onDeactivationEvent<T>(provider: ProviderToken<T>, cb: BindingDeactivation<T>): void;
|
|
716
|
-
toNaked(): IProviderModuleNaked;
|
|
717
|
-
clone(options?: CloneParams): IProviderModule;
|
|
718
|
-
toString(): string;
|
|
719
|
-
private setIdentifier;
|
|
720
|
-
private prepareContainer;
|
|
721
|
-
private injectImportedModules;
|
|
722
|
-
private injectProviders;
|
|
723
|
-
private registerBindingSideEffect;
|
|
724
|
-
private invokeRegisteredBindingSideEffects;
|
|
725
|
-
private removeRegisteredBindingSideEffects;
|
|
726
|
-
private shouldThrowWhenModuleDynamicExportsDontMatchTheStaticExports;
|
|
727
|
-
private shouldThrowIfDisposed;
|
|
504
|
+
protected __getAsync<T>(provider: ProviderToken<T>, options?: GetOptions): Promise<T>;
|
|
728
505
|
/**
|
|
729
506
|
* **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
|
|
730
507
|
*
|
|
731
|
-
* See {@link IProviderModuleNaked.
|
|
508
|
+
* See {@link IProviderModuleNaked.__getAll}.
|
|
732
509
|
*/
|
|
733
|
-
protected
|
|
510
|
+
protected __getAll<T>(provider: ProviderToken<T>, options?: GetOptions): T[];
|
|
734
511
|
/**
|
|
735
512
|
* **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
|
|
736
513
|
*
|
|
737
|
-
* See {@link IProviderModuleNaked.
|
|
514
|
+
* See {@link IProviderModuleNaked.__getAllAsync}.
|
|
738
515
|
*/
|
|
739
|
-
protected
|
|
516
|
+
protected __getAllAsync<T>(provider: ProviderToken<T>, options?: GetOptions): Promise<T[]>;
|
|
740
517
|
/**
|
|
741
518
|
* **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
|
|
742
519
|
*
|
|
743
|
-
* See {@link IProviderModuleNaked.
|
|
520
|
+
* See {@link IProviderModuleNaked.__isBound}.
|
|
744
521
|
*/
|
|
745
|
-
protected
|
|
522
|
+
protected __isBound(provider: ProviderToken, options?: IsBoundOptions): boolean;
|
|
746
523
|
/**
|
|
747
524
|
* **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
|
|
748
525
|
*
|
|
749
|
-
* See {@link IProviderModuleNaked.
|
|
526
|
+
* See {@link IProviderModuleNaked.__isCurrentBound}.
|
|
750
527
|
*/
|
|
751
|
-
protected
|
|
528
|
+
protected __isCurrentBound(provider: ProviderToken, options?: IsBoundOptions): boolean;
|
|
752
529
|
/**
|
|
753
530
|
* **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
|
|
754
531
|
*
|
|
755
|
-
* See {@link IProviderModuleNaked.
|
|
532
|
+
* See {@link IProviderModuleNaked.__takeSnapshot}.
|
|
756
533
|
*/
|
|
757
|
-
protected
|
|
534
|
+
protected __takeSnapshot(): void;
|
|
758
535
|
/**
|
|
759
536
|
* **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
|
|
760
537
|
*
|
|
761
|
-
* See {@link IProviderModuleNaked.
|
|
538
|
+
* See {@link IProviderModuleNaked.__restoreSnapshot}.
|
|
762
539
|
*/
|
|
763
|
-
protected
|
|
540
|
+
protected __restoreSnapshot(): void;
|
|
764
541
|
/**
|
|
765
542
|
* **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
|
|
766
543
|
*
|
|
767
|
-
* See {@link IProviderModuleNaked.
|
|
544
|
+
* See {@link IProviderModuleNaked.__rebind}.
|
|
768
545
|
*/
|
|
769
|
-
protected
|
|
546
|
+
protected __rebind<T>(provider: ProviderToken<T>): Promise<BindToFluentSyntax<T>>;
|
|
770
547
|
/**
|
|
771
548
|
* **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
|
|
772
549
|
*
|
|
773
|
-
* See {@link IProviderModuleNaked.
|
|
550
|
+
* See {@link IProviderModuleNaked.__rebindSync}.
|
|
774
551
|
*/
|
|
775
|
-
protected
|
|
552
|
+
protected __rebindSync<T>(provider: ProviderToken<T>): BindToFluentSyntax<T>;
|
|
776
553
|
/**
|
|
777
554
|
* **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
|
|
778
555
|
*
|
|
779
|
-
* See {@link IProviderModuleNaked.
|
|
556
|
+
* See {@link IProviderModuleNaked.__unbind}.
|
|
780
557
|
*/
|
|
781
|
-
protected
|
|
558
|
+
protected __unbind(provider: ProviderToken): Promise<void>;
|
|
782
559
|
/**
|
|
783
560
|
* **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
|
|
784
561
|
*
|
|
785
|
-
* See {@link IProviderModuleNaked.
|
|
562
|
+
* See {@link IProviderModuleNaked.__unbindSync}.
|
|
786
563
|
*/
|
|
787
|
-
protected
|
|
564
|
+
protected __unbindSync(provider: ProviderToken): void;
|
|
788
565
|
/**
|
|
789
566
|
* **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
|
|
790
567
|
*
|
|
791
|
-
* See {@link IProviderModuleNaked.
|
|
568
|
+
* See {@link IProviderModuleNaked.__unbindAll}.
|
|
792
569
|
*/
|
|
793
|
-
protected
|
|
570
|
+
protected __unbindAll(): Promise<void>;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
/**
|
|
574
|
+
* Class of the {@link AppModule} instance.
|
|
575
|
+
*
|
|
576
|
+
* **You shouldn't initialize a new instance of this class, please use the {@link AppModule} instance!**
|
|
577
|
+
*/
|
|
578
|
+
declare class GlobalAppModule extends ProviderModule implements IAppModule {
|
|
579
|
+
isMarkedAsGlobal: boolean;
|
|
580
|
+
private nakedModule;
|
|
581
|
+
private isLoaded;
|
|
582
|
+
constructor();
|
|
583
|
+
register<AsNaked extends boolean = false>(options: AppModuleOptions): AsNaked extends false ? IAppModule : IAppModule & IProviderModuleNaked;
|
|
584
|
+
toNaked(): IAppModule & IProviderModuleNaked;
|
|
585
|
+
protected _dispose(): Promise<void>;
|
|
586
|
+
private checkIfImportsHaveGlobalMark;
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* Special instance of {@link ProviderModule} which acts as the global module of your application in which you can inject any provider
|
|
590
|
+
* which must be available through your entire application.
|
|
591
|
+
*
|
|
592
|
+
* The registered providers will automatically be available inside all the modules.
|
|
593
|
+
*
|
|
594
|
+
* @example
|
|
595
|
+
* ```ts
|
|
596
|
+
* // The `register` method must be invoked only once during your application life cycle!
|
|
597
|
+
* AppModule.register({
|
|
598
|
+
* imports: [ConfigModule, ApiModule, UserModule, DatabaseModule],
|
|
599
|
+
* providers: [DummyService],
|
|
600
|
+
* });
|
|
601
|
+
* ```
|
|
602
|
+
*/
|
|
603
|
+
declare const AppModule: GlobalAppModule;
|
|
604
|
+
|
|
605
|
+
interface ProviderModuleOptions {
|
|
606
|
+
/** The module unique ID. */
|
|
607
|
+
identifier: symbol;
|
|
608
|
+
/** The list of imported {@link IProviderModule | modules} that export the {@link Provider | providers} which are required in this module. */
|
|
609
|
+
imports?: IProviderModule[];
|
|
610
|
+
/** The {@link DependencyProvider | providers} that will be instantiated by the container and that may be shared at least across this module. */
|
|
611
|
+
providers?: DependencyProvider[];
|
|
794
612
|
/**
|
|
795
|
-
*
|
|
613
|
+
* The subset of {@link ProviderToken | providers} or {@link IProviderModule | modules} that
|
|
614
|
+
* are provided by this module and should be available in other modules which import this module.
|
|
796
615
|
*
|
|
797
|
-
*
|
|
616
|
+
* _Check also the {@link dynamicExports} property._
|
|
798
617
|
*/
|
|
799
|
-
|
|
618
|
+
exports?: StaticExports;
|
|
800
619
|
/**
|
|
801
|
-
*
|
|
620
|
+
* The default {@link InjectionScope} to be used when a {@link ProviderToken} does not have a defined `scope`.
|
|
802
621
|
*
|
|
803
|
-
*
|
|
622
|
+
* Defaults to {@link InjectionScope.Singleton}.
|
|
804
623
|
*/
|
|
805
|
-
|
|
624
|
+
defaultScope?: InjectionScope;
|
|
806
625
|
/**
|
|
807
|
-
*
|
|
626
|
+
* This option is only a _marker_, per se it doesn't do anything
|
|
627
|
+
* apart from marking this module as `global`.
|
|
628
|
+
* When a module is marked as `global`, it means that it is expected to be _imported_ into the `AppModule`.
|
|
808
629
|
*
|
|
809
|
-
*
|
|
630
|
+
* Expect an exception to be thrown:
|
|
631
|
+
* - If a `module` marked as global is **not** imported into the `AppModule`.
|
|
632
|
+
* - If a `module` **not** marked as global _is_ imported into the `AppModule`
|
|
633
|
+
*
|
|
634
|
+
* Defaults to `false`.
|
|
810
635
|
*/
|
|
811
|
-
|
|
636
|
+
markAsGlobal?: true;
|
|
812
637
|
/**
|
|
813
|
-
*
|
|
638
|
+
* When provided, can be used to control which providers from the {@link ProviderModuleOptions.exports | exports}
|
|
639
|
+
* array should actually be exported into the importing module.
|
|
814
640
|
*
|
|
815
|
-
*
|
|
641
|
+
* **Note:** _Static {@link ProviderModuleOptions.exports | exports} should always be preferred as their static nature implies predictibility._
|
|
642
|
+
* _This is for advanced use cases only, and most probably you may never need to use a dynamic export!_
|
|
643
|
+
*
|
|
644
|
+
* To keep in mind in order to avoid nasty bugs:
|
|
645
|
+
* - You **must always** return only the providers/modules declared into the static {@link ProviderModuleOptions.exports | exports} array.
|
|
646
|
+
* - You **can** return _less_ providers/modules as long as they are still part of the static {@link ProviderModuleOptions.exports | exports} array.
|
|
647
|
+
* - You **cannot** return _more_ providers/modules than the static {@link ProviderModuleOptions.exports | exports} array!
|
|
648
|
+
*
|
|
649
|
+
* @example
|
|
650
|
+
* ```ts
|
|
651
|
+
* {
|
|
652
|
+
* exports: [ConfigModule, UserModule, PaymentService, ReviewService],
|
|
653
|
+
* dynamicExports: (importerModule, moduleExports) => {
|
|
654
|
+
* const shouldExportOnlyTheServices = true;
|
|
655
|
+
*
|
|
656
|
+
* if (shouldExportOnlyTheServices === false) return moduleExports;
|
|
657
|
+
*
|
|
658
|
+
* return moduleExports.flatMap((ex) => {
|
|
659
|
+
* // With `flatMap` we can map and filter out elements
|
|
660
|
+
* // from the sequence at the same time
|
|
661
|
+
* // by returning an empty array.
|
|
662
|
+
* return ex instanceof ProviderModule ? [] : ex;
|
|
663
|
+
* });
|
|
664
|
+
* }
|
|
665
|
+
* }
|
|
666
|
+
*
|
|
667
|
+
* // Or
|
|
668
|
+
*
|
|
669
|
+
* {
|
|
670
|
+
* exports: [ConfigModule, UserModule, PaymentService, ReviewService],
|
|
671
|
+
* dynamicExports: (importerModule, moduleExports) => {
|
|
672
|
+
* // We export all the providers only when the importer module is not the `BULLIED_MODULE`.
|
|
673
|
+
* if (importerModule.toNaked().name !== 'BULLIED_MODULE') return moduleExports;
|
|
674
|
+
*
|
|
675
|
+
* return [ConfigModule];
|
|
676
|
+
* }
|
|
677
|
+
* }
|
|
678
|
+
*
|
|
679
|
+
* ```
|
|
816
680
|
*/
|
|
817
|
-
|
|
681
|
+
dynamicExports?: DynamicExports;
|
|
818
682
|
/**
|
|
819
|
-
*
|
|
683
|
+
* Callback which will be invoked once the module container has been initialized
|
|
684
|
+
* and the providers resolved.
|
|
820
685
|
*
|
|
821
|
-
*
|
|
686
|
+
* **This happens only once, when the {@link IProviderModule | ProviderModule} has been bootstrapped!**
|
|
687
|
+
*
|
|
688
|
+
* @param module The instance of the {@link IProviderModule | module}.
|
|
822
689
|
*/
|
|
823
|
-
|
|
690
|
+
onReady?: (module: IProviderModule) => Promise<void>;
|
|
824
691
|
/**
|
|
825
|
-
*
|
|
692
|
+
* Callback which will be invoked whenever the internal module dispose method is invoked.
|
|
826
693
|
*
|
|
827
|
-
*
|
|
694
|
+
* **The method will be invoked right _before_ the clean-up process.**
|
|
695
|
+
*
|
|
696
|
+
* @param module The instance of the {@link IProviderModule | module}.
|
|
828
697
|
*/
|
|
829
|
-
|
|
698
|
+
onDispose?: (module: IProviderModule) => Promise<void>;
|
|
699
|
+
}
|
|
700
|
+
interface ProviderModuleOptionsInternal {
|
|
701
|
+
isAppModule?: boolean;
|
|
702
|
+
isDisposed?: boolean;
|
|
703
|
+
/** Can be used to manually provide the {@link IAppModule} instance. */
|
|
704
|
+
appModule?: () => GlobalAppModule;
|
|
705
|
+
/** Can be used to manually provide a {@link Container} instance. */
|
|
706
|
+
container?: () => Container;
|
|
707
|
+
/** Can be used to override all the _imported_ providers _before_ the binding process. */
|
|
708
|
+
importedProvidersMap?: (
|
|
709
|
+
/** The current imported {@link DependencyProvider | provider} altered to use the module from where was imported for resolution. */
|
|
710
|
+
factorizedProvider: DependencyProvider<any>,
|
|
711
|
+
/** The current imported {@link DependencyProvider | provider}. */
|
|
712
|
+
provider: DependencyProvider<any>,
|
|
713
|
+
/** The {@link IProviderModule | module} from where the {@link DependencyProvider | provider} originated. */
|
|
714
|
+
module: IProviderModule) => DependencyProvider<any>;
|
|
715
|
+
}
|
|
716
|
+
type StaticExports = (ProviderToken | IProviderModule)[];
|
|
717
|
+
type DynamicExports = (
|
|
718
|
+
/** The {@link IProviderModule} which is importing this module. */
|
|
719
|
+
importerModule: IProviderModule,
|
|
720
|
+
/** The {@link ProviderModuleOptions.exports | exports} array of this module. */
|
|
721
|
+
moduleExports: StaticExports) => StaticExports;
|
|
722
|
+
|
|
723
|
+
interface IProviderModule {
|
|
724
|
+
/** The module unique ID. */
|
|
725
|
+
readonly identifier: symbol;
|
|
726
|
+
/** See {@link ProviderModuleOptions.markAsGlobal}. */
|
|
727
|
+
readonly isMarkedAsGlobal: boolean;
|
|
728
|
+
readonly isDisposed: boolean;
|
|
830
729
|
/**
|
|
831
|
-
*
|
|
730
|
+
* Can be used to retrieve a resolved `dependency` from the module container.
|
|
832
731
|
*
|
|
833
|
-
*
|
|
732
|
+
* @param provider The {@link ProviderToken}.
|
|
733
|
+
* @param isOptional When set to `false` _(default)_ an exception will be thrown when the {@link provider} isn't bound.
|
|
734
|
+
* @returns Either the {@link T | dependency} or `undefined` if {@link isOptional} is set to `true`.
|
|
834
735
|
*/
|
|
835
|
-
|
|
736
|
+
get<T>(provider: ProviderToken<T>, isOptional?: boolean): T;
|
|
836
737
|
/**
|
|
837
|
-
*
|
|
738
|
+
* Can be used to retrieve many resolved `dependencies` from the module container at once.
|
|
838
739
|
*
|
|
839
|
-
*
|
|
740
|
+
* @param deps Either one or more {@link ProviderToken}.
|
|
741
|
+
* @returns Tuple containing the {@link D | dependencies}.
|
|
742
|
+
*
|
|
743
|
+
* @example
|
|
744
|
+
* ```ts
|
|
745
|
+
* // When ProviderTokens are supplied, TS auto-inference works as expected.
|
|
746
|
+
* // `car` will infer the `Car` type, `engine` the `Engine` type and `dashboard` the `Dashboard` type.
|
|
747
|
+
* const [car, engine, dashboard] = AppModule.getMany(
|
|
748
|
+
* Car,
|
|
749
|
+
* Engine,
|
|
750
|
+
* { provider: Dashboard, isOptional: true }
|
|
751
|
+
* );
|
|
752
|
+
*
|
|
753
|
+
* // When auto-inference is not possible, you can manually cast the types.
|
|
754
|
+
* const [configService, userService] = AppModule.getMany<[typeof ConfigService, typeof UserService]>('CONFIG_SERVICE', UserService);
|
|
755
|
+
* // Now the `configService` is of type `ConfigService` and the `userService` is of type `UserService`.
|
|
756
|
+
* ```
|
|
840
757
|
*/
|
|
841
|
-
|
|
758
|
+
getMany<D extends (ProviderModuleGetManyParam<any> | ProviderToken)[]>(...deps: D | unknown[]): ProviderModuleGetManySignature<D>;
|
|
842
759
|
/**
|
|
843
|
-
*
|
|
760
|
+
* Adds an activation handler for the {@link provider}.
|
|
844
761
|
*
|
|
845
|
-
* See {@link
|
|
762
|
+
* See {@link https://inversify.io/docs/api/container/#onactivation} for more details.
|
|
846
763
|
*/
|
|
847
|
-
|
|
764
|
+
onActivationEvent<T>(provider: ProviderToken<T>, cb: BindingActivation<T>): void;
|
|
848
765
|
/**
|
|
849
|
-
*
|
|
766
|
+
* Adds a deactivation handler for the {@link provider}.
|
|
850
767
|
*
|
|
851
|
-
* See {@link
|
|
768
|
+
* See {@link https://inversify.io/docs/api/container/#ondeactivation} for more details.
|
|
852
769
|
*/
|
|
853
|
-
|
|
770
|
+
onDeactivationEvent<T>(provider: ProviderToken<T>, cb: BindingDeactivation<T>): void;
|
|
854
771
|
/**
|
|
855
|
-
*
|
|
772
|
+
* Casts the current module type to the {@link IProviderModuleNaked} type.
|
|
856
773
|
*
|
|
857
|
-
*
|
|
774
|
+
* **Internally used and for testing purposes!**
|
|
858
775
|
*/
|
|
859
|
-
|
|
776
|
+
toNaked(): IProviderModuleNaked;
|
|
860
777
|
/**
|
|
861
|
-
*
|
|
778
|
+
* Can be used to create a new instance of the current {@link IProviderModule | module}.
|
|
862
779
|
*
|
|
863
|
-
*
|
|
780
|
+
* **Note:** _All the providers will be registered again within the new module!_
|
|
781
|
+
* _And also the new module will still refrain values by reference to its parent module because of_
|
|
782
|
+
* _JS limitation in deeply/truly cloning an instance._
|
|
783
|
+
*
|
|
784
|
+
* @param options See {@link CloneParams}.
|
|
864
785
|
*/
|
|
865
|
-
|
|
786
|
+
clone(options?: CloneParams): IProviderModule;
|
|
787
|
+
/** Returns the {@link IProviderModule.identifier} `symbol` description. */
|
|
788
|
+
toString(): string;
|
|
789
|
+
}
|
|
790
|
+
type ProviderModuleGetManySignature<Tokens extends (ProviderModuleGetManyParam<any> | ProviderToken)[]> = {
|
|
791
|
+
[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;
|
|
792
|
+
};
|
|
793
|
+
type ProviderModuleGetManyParam<T> = {
|
|
794
|
+
/** The {@link ProviderToken}. */
|
|
795
|
+
provider: ProviderToken<T>;
|
|
796
|
+
/** When set to `false` _(default)_ an exception will be thrown when the {@link ProviderModuleGetManyParam.provider | provider} isn't bound. */
|
|
797
|
+
isOptional?: boolean;
|
|
798
|
+
};
|
|
799
|
+
interface CloneParams {
|
|
800
|
+
/** Can be used to override all the providers _before_ the binding process. */
|
|
801
|
+
providersMap?: (
|
|
802
|
+
/** The current {@link DependencyProvider | provider}. */
|
|
803
|
+
provider: DependencyProvider<any>,
|
|
804
|
+
/** The {@link IProviderModule | module} from where the {@link DependencyProvider | provider} originated. */
|
|
805
|
+
module: IProviderModule) => DependencyProvider<any>;
|
|
806
|
+
/** Can be used to override all the _imported_ providers _before_ the binding process. */
|
|
807
|
+
importedProvidersMap?: ProviderModuleOptionsInternal['importedProvidersMap'];
|
|
866
808
|
}
|
|
867
809
|
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
*
|
|
871
|
-
* **You shouldn't initialize a new instance of this class, please use the {@link AppModule} instance!**
|
|
872
|
-
*/
|
|
873
|
-
declare class GlobalAppModule extends ProviderModule implements IAppModule {
|
|
874
|
-
private nakedModule;
|
|
875
|
-
private isLoaded;
|
|
876
|
-
constructor();
|
|
810
|
+
interface IAppModule extends Except<IProviderModule, 'isMarkedAsGlobal'> {
|
|
811
|
+
/** Must be invoked _(only once during the application lifecycle)_ in order to provide the {@link options} to the module. */
|
|
877
812
|
register<AsNaked extends boolean = false>(options: AppModuleOptions): AsNaked extends false ? IAppModule : IAppModule & IProviderModuleNaked;
|
|
878
813
|
toNaked(): IAppModule & IProviderModuleNaked;
|
|
879
|
-
protected _dispose(): Promise<void>;
|
|
880
814
|
}
|
|
815
|
+
type AppModuleOptions = Except<LazyInitOptions, 'exports' | 'dynamicExports'>;
|
|
816
|
+
|
|
817
|
+
/** See {@link https://inversify.io/docs/api/decorator/#inject} for more details. */
|
|
818
|
+
declare function Inject(provider: ProviderToken): MethodDecorator & ParameterDecorator & PropertyDecorator;
|
|
819
|
+
|
|
820
|
+
/** See {@link https://inversify.io/docs/api/decorator/#multiinject} for more details. */
|
|
821
|
+
declare function MultiInject(provider: ProviderToken): MethodDecorator & ParameterDecorator & PropertyDecorator;
|
|
822
|
+
|
|
823
|
+
/** See {@link https://inversify.io/docs/api/decorator/#injectfrombase} for more details. */
|
|
824
|
+
declare function InjectFromBase(options?: Parameters<typeof injectFromBase>[0]): ClassDecorator;
|
|
825
|
+
|
|
826
|
+
/** See {@link https://inversify.io/docs/api/decorator/#named} for more details. */
|
|
827
|
+
declare function Named(name: MetadataName): MethodDecorator & ParameterDecorator & PropertyDecorator;
|
|
828
|
+
|
|
829
|
+
/** See {@link https://inversify.io/docs/api/decorator/#optional} for more details. */
|
|
830
|
+
declare function Optional(): MethodDecorator & ParameterDecorator & PropertyDecorator;
|
|
831
|
+
|
|
832
|
+
/** See {@link https://inversify.io/docs/api/decorator/#postconstruct} for more details. */
|
|
833
|
+
declare function PostConstruct(): MethodDecorator;
|
|
834
|
+
|
|
835
|
+
/** See {@link https://inversify.io/docs/api/decorator/#predestroy} for more details. */
|
|
836
|
+
declare function PreDestroy(): MethodDecorator;
|
|
837
|
+
|
|
838
|
+
/** See {@link https://inversify.io/docs/api/decorator/#tagged} for more details. */
|
|
839
|
+
declare function Tagged(key: MetadataTag, value: unknown): MethodDecorator & ParameterDecorator & PropertyDecorator;
|
|
840
|
+
|
|
841
|
+
/** See {@link https://inversify.io/docs/api/decorator/#unmanaged} for more details. */
|
|
842
|
+
declare function Unmanaged(): MethodDecorator & ParameterDecorator & PropertyDecorator;
|
|
843
|
+
|
|
844
|
+
/** Exception which indicates that there is a generic error. */
|
|
845
|
+
declare class InjectionError extends Error {
|
|
846
|
+
name: string;
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
/** Exception which indicates that there is a generic error with an instance of {@link IProviderModule}. */
|
|
850
|
+
declare class InjectionProviderModuleError extends Error {
|
|
851
|
+
name: string;
|
|
852
|
+
constructor(module: IProviderModule, message: string);
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
/** Exception which indicates an invokation of a disposed module. */
|
|
856
|
+
declare class InjectionProviderModuleDisposedError extends InjectionProviderModuleError {
|
|
857
|
+
name: string;
|
|
858
|
+
constructor(module: IProviderModule);
|
|
859
|
+
}
|
|
860
|
+
|
|
881
861
|
/**
|
|
882
|
-
*
|
|
883
|
-
*
|
|
884
|
-
*
|
|
885
|
-
*
|
|
886
|
-
*
|
|
887
|
-
* @example
|
|
888
|
-
* ```ts
|
|
889
|
-
* import { AppModule } from '@adimm/x-injection';
|
|
890
|
-
*
|
|
891
|
-
* // The `register` method must be invoked only once during your application life cycle!
|
|
892
|
-
* AppModule.register({
|
|
893
|
-
* imports: [ConfigModule, ApiModule, UserModule, DatabaseModule],
|
|
894
|
-
* providers: [DummyService],
|
|
895
|
-
* });
|
|
896
|
-
* ```
|
|
862
|
+
* Exception which indicates that an instance of {@link IProviderModule}
|
|
863
|
+
* imports another module which dynamically exports its providers/modules and
|
|
864
|
+
* is trying to dynamically export more or different providers/modules than the
|
|
865
|
+
* ones declared into its static exports.
|
|
897
866
|
*/
|
|
898
|
-
declare
|
|
867
|
+
declare class InjectionDynamicExportsOutOfRange extends InjectionProviderModuleError {
|
|
868
|
+
name: string;
|
|
869
|
+
constructor(module: IProviderModule);
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
/** Exception which indicates that a module has been initialized without an `identifier`. */
|
|
873
|
+
declare class InjectionProviderModuleMissingIdentifierError extends InjectionProviderModuleError {
|
|
874
|
+
name: string;
|
|
875
|
+
constructor(module: IProviderModule);
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
/** Exception which indicates an error with regards to the `markAsGlobal` option. */
|
|
879
|
+
declare class InjectionProviderModuleGlobalMarkError extends InjectionProviderModuleError {
|
|
880
|
+
name: string;
|
|
881
|
+
constructor(module: IProviderModule, message: string);
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
declare function injectionScopeToBindingScope(injectionScope: InjectionScope): BindingScope;
|
|
885
|
+
declare function bindingScopeToInjectionScope(bindingScope: BindingScope): InjectionScope;
|
|
886
|
+
|
|
887
|
+
declare namespace ProviderTokenHelpers {
|
|
888
|
+
function isClassToken<T>(provider: ProviderToken<T>): provider is ProviderClassToken<T>;
|
|
889
|
+
function isValueToken<T>(provider: ProviderToken<T>): provider is ProviderValueToken<T>;
|
|
890
|
+
function isFactoryToken<T>(provider: ProviderToken<T>): provider is ProviderFactoryToken<T>;
|
|
891
|
+
function isProviderIdentifier<T = any>(value: any): value is ProviderIdentifier<T>;
|
|
892
|
+
function toServiceIdentifier<T = any>(provider: ProviderToken<T>): ServiceIdentifier<T>;
|
|
893
|
+
function toServiceIdentifiers(providers: ProviderToken[]): ServiceIdentifier<unknown>[];
|
|
894
|
+
function toDependencyProviderWithOptions<T extends DependencyProvider<any>>(original: T, override?: Partial<T>): T;
|
|
895
|
+
/**
|
|
896
|
+
* The priority order is as follows:
|
|
897
|
+
* 1. From the `ProviderToken.scope`
|
|
898
|
+
* 2. From the class `@Injectable(scope)` decorator
|
|
899
|
+
* 3. From the `ProviderModule` default scope.
|
|
900
|
+
*
|
|
901
|
+
* @param provider The {@link ProviderToken}.
|
|
902
|
+
* @param moduleDefaultScope The module default scope.
|
|
903
|
+
*/
|
|
904
|
+
function getInjectionScopeByPriority(provider: ProviderToken, moduleDefaultScope: InjectionScope): InjectionScope;
|
|
905
|
+
function tryGetProviderOptions<T>(provider: ProviderToken<T>): (ProviderOptions<T> & ProviderScopeOption) | undefined;
|
|
906
|
+
function tryGetScopeFromProvider(provider: ProviderToken): InjectionScope | undefined;
|
|
907
|
+
function tryGetDecoratorScopeFromClass<T = any>(provider: ProviderToken<T>): InjectionScope | undefined;
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
declare namespace ProviderModuleHelpers {
|
|
911
|
+
function buildInternalConstructorParams(params: ProviderModuleOptions & ProviderModuleOptionsInternal): ProviderModuleOptions;
|
|
912
|
+
function isDynamicExport(exporter: StaticExports | DynamicExports): exporter is DynamicExports;
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
declare function isPlainObject(o: any): o is object;
|
|
916
|
+
|
|
917
|
+
declare function isClass(v: any): boolean;
|
|
918
|
+
|
|
919
|
+
declare function isFunction(v: any): boolean;
|
|
920
|
+
|
|
921
|
+
declare function isClassOrFunction(value: any): value is Function | Class<any>;
|
|
899
922
|
|
|
900
|
-
export { AppModule, type AppModuleOptions, type CloneParams, type DependencyProvider, 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,
|
|
923
|
+
export { AppModule, type AppModuleOptions, type CloneParams, type DependencyProvider, type DynamicExports, GLOBAL_APP_MODULE_ID, GlobalAppModule, GlobalContainer, type IAppModule, type IProviderModule, type IProviderModuleNaked, Inject, InjectFromBase, Injectable, InjectionDynamicExportsOutOfRange, InjectionError, InjectionProviderModuleDisposedError, InjectionProviderModuleError, InjectionProviderModuleGlobalMarkError, InjectionProviderModuleMissingIdentifierError, 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, bindingScopeToInjectionScope, injectionScopeToBindingScope, isClass, isClassOrFunction, isFunction, isPlainObject };
|