@adimm/x-injection 0.3.2 → 0.5.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/dist/index.d.ts CHANGED
@@ -3,8 +3,6 @@ import { Class, Except } from 'type-fest';
3
3
 
4
4
  /** The identifier of the `GlobalAppModule`. */
5
5
  declare const GLOBAL_APP_MODULE_ID = "GlobalAppModule";
6
- /** The default {@link IProviderModule.identifier}. */
7
- declare const ANONYMOUS_MODULE_DEFAULT_ID = "AnonymousModule";
8
6
 
9
7
  declare enum InjectionScope {
10
8
  /** When the service is resolved, the same cached resolved value will be used. */
@@ -119,9 +117,7 @@ declare class ProviderModuleUtils {
119
117
  * @param defaultScope Optionally provide the default {@link InjectionScope} to use when applicable.
120
118
  * @returns `true` when the {@link provider} has been bound otherwhise `false`.
121
119
  */
122
- bindToContainer<T>(provider: ProviderToken<T>, defaultScope: InjectionScope): boolean;
123
- /** Low-level method which does exactly what {@link bindToContainer} does, however accepts a list of {@link providers}. */
124
- bindManyToContainer(providers: ProviderToken[], defaultScope: InjectionScope): void;
120
+ bindToContainer<T>(provider: DependencyProvider<T>, defaultScope: InjectionScope): boolean;
125
121
  private bindSelfTokenToContainer;
126
122
  private bindClassTokenToContainer;
127
123
  private bindValueTokenToContainer;
@@ -140,8 +136,6 @@ interface IProviderModuleNaked extends IProviderModule {
140
136
  readonly isAppModule: boolean;
141
137
  /** The low-level `InversifyJS` {@link https://inversify.io/docs/api/container/ | container} instance. */
142
138
  readonly container: Container;
143
- /** Instance of the {@link ProviderModuleUtils}. */
144
- readonly moduleUtils: ProviderModuleUtils;
145
139
  /** The default injection scope of this module. */
146
140
  readonly defaultScope: {
147
141
  /** Scope from `xInjection` {@link InjectionScope} enum. */
@@ -149,14 +143,31 @@ interface IProviderModuleNaked extends IProviderModule {
149
143
  /** Scope from `InversifyJS` {@link BindingScope} string union. */
150
144
  inversify: BindingScope;
151
145
  };
146
+ /** Instance of the {@link ProviderModuleUtils}. */
147
+ readonly moduleUtils: ProviderModuleUtils;
148
+ /** The {@link DependencyProvider | providers} resolved by this module. */
149
+ readonly providers: DependencyProvider[];
150
+ /**
151
+ * The imported {@link DependencyProvider | providers} resolved by this module.
152
+ *
153
+ * _It is a `dictionary` where the key is the {@link IProviderModule | module} and the value an_
154
+ * _array containing the imported dependencies from that module_
155
+ */
156
+ readonly importedProviders: Map<IProviderModuleNaked, DependencyProvider[]>;
157
+ /** What is exported from this module. */
158
+ readonly exports: StaticExports;
159
+ /** What is exported into this module. */
160
+ readonly imports: IProviderModuleNaked[];
152
161
  /** The module dynamic exports method. */
153
162
  readonly dynamicExports: DynamicExports | undefined;
154
163
  /** The registered `callback` which will be invoked when the internal initialization process has been completed. */
155
164
  readonly onReady: ProviderModuleOptions['onReady'];
156
165
  /** The registered `callback` which will be invoked when the {@link _dispose} method is invoked. */
157
166
  readonly onDispose: ProviderModuleOptions['onDispose'];
167
+ /** Can be used to override all the _imported_ providers _before_ the binding process. */
168
+ readonly importedProvidersMap: ProviderModuleOptionsInternal['importedProvidersMap'];
158
169
  /** It'll _completely_ re-init the `module` with the provided {@link LazyInitOptions | options}. */
159
- _lazyInit(options: LazyInitOptions): void;
170
+ _lazyInit(options: LazyInitOptions): IProviderModule;
160
171
  /** Can be used to get the list of all the imported modules of this module. */
161
172
  _getImportedModules(): IProviderModuleNaked[];
162
173
  /** Can be used to get the list of all the providers of this module. */
@@ -319,74 +330,9 @@ interface IProviderModuleNaked extends IProviderModule {
319
330
  }
320
331
  type LazyInitOptions = Except<ProviderModuleOptions & ProviderModuleOptionsInternal, 'identifier' | 'isAppModule'>;
321
332
 
322
- interface IProviderModule {
323
- /** The module unique ID. */
324
- readonly identifier: symbol;
325
- readonly isDisposed: boolean;
326
- /**
327
- * Can be used to retrieve a resolved `dependency` from the module container.
328
- *
329
- * @param provider The {@link ProviderToken}.
330
- * @param isOptional When set to `false` _(default)_ an exception will be thrown when the {@link provider} isn't bound.
331
- * @returns Either the {@link T | dependency} or `undefined` if {@link isOptional} is set to `true`.
332
- */
333
- get<T>(provider: ProviderToken<T>, isOptional?: boolean): T;
334
- /**
335
- * Can be used to retrieve many resolved `dependencies` from the module container at once.
336
- *
337
- * @param deps Either one or more {@link ProviderToken}.
338
- * @returns Tuple containing the {@link D | dependencies}.
339
- *
340
- * @example
341
- * ```ts
342
- * // When ProviderTokens are supplied, TS auto-inference works as expected.
343
- * // `car` will infer the `Car` type, `engine` the `Engine` type and `dashboard` the `Dashboard` type.
344
- * const [car, engine, dashboard] = AppModule.getMany(
345
- * Car,
346
- * Engine,
347
- * { provider: Dashboard, isOptional: true }
348
- * );
349
- *
350
- * // When auto-inference is not possible, you can manually cast the types.
351
- * const [configService, userService] = AppModule.getMany<[typeof ConfigService, typeof UserService]>('CONFIG_SERVICE', UserService);
352
- * // Now the `configService` is of type `ConfigService` and the `userService` is of type `UserService`.
353
- * ```
354
- */
355
- getMany<D extends (ProviderModuleGetManyParam<any> | ProviderToken)[]>(...deps: D | unknown[]): ProviderModuleGetManySignature<D>;
356
- /**
357
- * Adds an activation handler for the {@link provider}.
358
- *
359
- * See {@link https://inversify.io/docs/api/container/#onactivation} for more details.
360
- */
361
- onActivationEvent<T>(provider: ProviderToken<T>, cb: BindingActivation<T>): void;
362
- /**
363
- * Adds a deactivation handler for the {@link provider}.
364
- *
365
- * See {@link https://inversify.io/docs/api/container/#ondeactivation} for more details.
366
- */
367
- onDeactivationEvent<T>(provider: ProviderToken<T>, cb: BindingDeactivation<T>): void;
368
- /**
369
- * Casts the current module type to the {@link IProviderModuleNaked} type.
370
- *
371
- * **Internally used and for testing purposes!**
372
- */
373
- toNaked(): IProviderModuleNaked;
374
- /** Returns the {@link IProviderModule.identifier} `symbol` description. */
375
- toString(): string;
376
- }
377
- type ProviderModuleGetManySignature<Tokens extends (ProviderModuleGetManyParam<any> | ProviderToken)[]> = {
378
- [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;
379
- };
380
- type ProviderModuleGetManyParam<T> = {
381
- /** The {@link ProviderToken}. */
382
- provider: ProviderToken<T>;
383
- /** When set to `false` _(default)_ an exception will be thrown when the {@link ProviderModuleGetManyParam.provider | provider} isn't bound. */
384
- isOptional?: boolean;
385
- };
386
-
387
333
  interface ProviderModuleOptions {
388
334
  /** The module unique ID. */
389
- identifier?: symbol;
335
+ identifier: symbol;
390
336
  /** The list of imported {@link IProviderModule | modules} that export the {@link Provider | providers} which are required in this module. */
391
337
  imports?: IProviderModule[];
392
338
  /** The {@link DependencyProvider | providers} that will be instantiated by the container and that may be shared at least across this module. */
@@ -469,8 +415,17 @@ interface ProviderModuleOptions {
469
415
  }
470
416
  interface ProviderModuleOptionsInternal {
471
417
  isAppModule?: boolean;
418
+ isDisposed?: boolean;
472
419
  /** Can be used to manually provide a {@link Container} instance. */
473
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>;
474
429
  }
475
430
  type StaticExports = (ProviderToken | IProviderModule)[];
476
431
  type DynamicExports = (
@@ -479,6 +434,93 @@ importerModule: IProviderModule,
479
434
  /** The {@link ProviderModuleOptions.exports | exports} array of this module. */
480
435
  moduleExports: StaticExports) => StaticExports;
481
436
 
437
+ interface IProviderModule {
438
+ /** The module unique ID. */
439
+ readonly identifier: symbol;
440
+ readonly isDisposed: boolean;
441
+ /**
442
+ * Can be used to retrieve a resolved `dependency` from the module container.
443
+ *
444
+ * @param provider The {@link ProviderToken}.
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`.
447
+ */
448
+ get<T>(provider: ProviderToken<T>, isOptional?: boolean): T;
449
+ /**
450
+ * Can be used to retrieve many resolved `dependencies` from the module container at once.
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
+ * );
464
+ *
465
+ * // When auto-inference is not possible, you can manually cast the types.
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
+ * ```
469
+ */
470
+ getMany<D extends (ProviderModuleGetManyParam<any> | ProviderToken)[]>(...deps: D | unknown[]): ProviderModuleGetManySignature<D>;
471
+ /**
472
+ * Adds an activation handler for the {@link provider}.
473
+ *
474
+ * See {@link https://inversify.io/docs/api/container/#onactivation} for more details.
475
+ */
476
+ onActivationEvent<T>(provider: ProviderToken<T>, cb: BindingActivation<T>): void;
477
+ /**
478
+ * Adds a deactivation handler for the {@link provider}.
479
+ *
480
+ * See {@link https://inversify.io/docs/api/container/#ondeactivation} for more details.
481
+ */
482
+ onDeactivationEvent<T>(provider: ProviderToken<T>, cb: BindingDeactivation<T>): void;
483
+ /**
484
+ * Casts the current module type to the {@link IProviderModuleNaked} type.
485
+ *
486
+ * **Internally used and for testing purposes!**
487
+ */
488
+ toNaked(): IProviderModuleNaked;
489
+ /**
490
+ * Can be used to create a new instance of the current {@link IProviderModule | module}.
491
+ *
492
+ * **Note:** _All the providers will be registered again within the new module!_
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}.
497
+ */
498
+ clone(options?: CloneParams): IProviderModule;
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,
518
+ /** Flag indicating if the {@link provider} has been imported from the {@link module}. */
519
+ isImported: boolean) => DependencyProvider<any>;
520
+ /** Can be used to override all the _imported_ providers _before_ the binding process. */
521
+ importedProvidersMap?: ProviderModuleOptionsInternal['importedProvidersMap'];
522
+ }
523
+
482
524
  interface IAppModule extends IProviderModule {
483
525
  /** Must be invoked _(only once during the application lifecycle)_ in order to provide the {@link options} to the module. */
484
526
  register<AsNaked extends boolean = false>(options: AppModuleOptions): AsNaked extends false ? IAppModule : IAppModule & IProviderModuleNaked;
@@ -524,6 +566,7 @@ declare class XInjectionProviderModuleError extends Error {
524
566
  constructor(module: IProviderModule, message: string);
525
567
  }
526
568
 
569
+ /** Exception which indicates an invokation of a disposed module. */
527
570
  declare class XInjectionProviderModuleDisposedError extends XInjectionProviderModuleError {
528
571
  name: string;
529
572
  constructor(module: IProviderModule);
@@ -540,6 +583,12 @@ declare class XInjectionDynamicExportsOutOfRange extends XInjectionProviderModul
540
583
  constructor(module: IProviderModule);
541
584
  }
542
585
 
586
+ /** Exception which indicates that a module has been initialized without an `identifier`. */
587
+ declare class XInjectionProviderModuleMissingIdentifierError extends XInjectionProviderModuleError {
588
+ name: string;
589
+ constructor(module: IProviderModule);
590
+ }
591
+
543
592
  declare function injectionScopeToBindingScope(injectionScope: InjectionScope): BindingScope;
544
593
  declare function bindingScopeToInjectionScope(bindingScope: BindingScope): InjectionScope;
545
594
 
@@ -550,6 +599,7 @@ declare namespace ProviderTokenHelpers {
550
599
  function isProviderIdentifier<T = any>(value: any): value is ProviderIdentifier<T>;
551
600
  function toServiceIdentifier<T = any>(provider: ProviderToken<T>): ServiceIdentifier<T>;
552
601
  function toServiceIdentifiers(providers: ProviderToken[]): ServiceIdentifier<unknown>[];
602
+ function toDependencyProviderWithOptions<T extends DependencyProvider<any>>(original: T, override?: Partial<T>): T;
553
603
  /**
554
604
  * The priority order is as follows:
555
605
  * 1. From the `ProviderToken.scope`
@@ -574,6 +624,8 @@ declare function isPlainObject(o: any): o is object;
574
624
 
575
625
  declare function isClass(v: any): boolean;
576
626
 
627
+ declare function isFunction(v: any): boolean;
628
+
577
629
  declare function isClassOrFunction(value: any): value is Function | Class<any>;
578
630
 
579
631
  /**
@@ -594,16 +646,19 @@ declare const GlobalContainer: Container;
594
646
  * import { ProviderModule } from '@adimm/x-injection';
595
647
  *
596
648
  * const EngineModule = new ProviderModule({
649
+ * identifier: Symbol('EngineModule'),
597
650
  * providers: [EngineService],
598
651
  * exports: [EngineService]
599
652
  * });
600
653
  *
601
654
  * const DashboardModule = new ProviderModule({
655
+ * identifier: Symbol('DashboardModule'),
602
656
  * providers: [DashboardService],
603
657
  * exports: [DashboardService]
604
658
  * });
605
659
  *
606
660
  * const CarModule = new ProviderModule({
661
+ * identifier: Symbol('CarModule'),
607
662
  * imports: [EngineModule, DashboardModule],
608
663
  * providers: [CarService],
609
664
  * exports: [CarService]
@@ -611,6 +666,7 @@ declare const GlobalContainer: Container;
611
666
  *
612
667
  * // Run-time class replacement:
613
668
  * const RedCarModule = new ProviderModule({
669
+ * identifier: Symbol('RedCarModule'),
614
670
  * imports: [CarModule],
615
671
  * providers: [
616
672
  * {
@@ -622,6 +678,7 @@ declare const GlobalContainer: Container;
622
678
  *
623
679
  * // Run-time factory example:
624
680
  * const BlackCarModule = new ProviderModule({
681
+ * identifier: Symbol('BlackCarModule'),
625
682
  * imports: [CarModule],
626
683
  * providers: [
627
684
  * {
@@ -639,17 +696,19 @@ declare const GlobalContainer: Container;
639
696
  */
640
697
  declare class ProviderModule implements IProviderModule {
641
698
  readonly identifier: symbol;
642
- readonly isDisposed = false;
699
+ readonly isDisposed: boolean;
643
700
  protected readonly isAppModule: boolean;
644
701
  protected readonly container: Container;
645
702
  protected readonly defaultScope: IProviderModuleNaked['defaultScope'];
646
703
  protected readonly dynamicExports: IProviderModuleNaked['dynamicExports'];
647
704
  protected readonly onReady: IProviderModuleNaked['onReady'];
648
705
  protected readonly onDispose: IProviderModuleNaked['onDispose'];
706
+ protected readonly importedProvidersMap: IProviderModuleNaked['importedProvidersMap'];
649
707
  protected readonly moduleUtils: IProviderModuleNaked['moduleUtils'];
650
- private readonly providers;
651
- private readonly exports;
652
- private readonly imports;
708
+ protected readonly imports: IProviderModuleNaked['imports'];
709
+ protected readonly providers: IProviderModuleNaked['providers'];
710
+ protected readonly importedProviders: IProviderModuleNaked['importedProviders'];
711
+ protected readonly exports: IProviderModuleNaked['exports'];
653
712
  private readonly registeredBindingSideEffects;
654
713
  constructor({ identifier, imports, providers, exports, defaultScope, dynamicExports, onReady, onDispose, ..._internalParams }: ProviderModuleOptions);
655
714
  get<T>(provider: ProviderToken<T>, isOptional?: boolean): T;
@@ -657,7 +716,9 @@ declare class ProviderModule implements IProviderModule {
657
716
  onActivationEvent<T>(provider: ProviderToken<T>, cb: BindingActivation<T>): void;
658
717
  onDeactivationEvent<T>(provider: ProviderToken<T>, cb: BindingDeactivation<T>): void;
659
718
  toNaked(): IProviderModuleNaked;
719
+ clone(options?: CloneParams): IProviderModule;
660
720
  toString(): string;
721
+ private setIdentifier;
661
722
  private prepareContainer;
662
723
  private injectImportedModules;
663
724
  private injectProviders;
@@ -677,7 +738,7 @@ declare class ProviderModule implements IProviderModule {
677
738
  *
678
739
  * See {@link IProviderModuleNaked._lazyInit}.
679
740
  */
680
- protected _lazyInit({ imports, providers, exports, defaultScope, dynamicExports, onReady, onDispose, ..._internalParams }: LazyInitOptions): void;
741
+ protected _lazyInit({ imports, providers, exports, defaultScope, dynamicExports, onReady, onDispose, ..._internalParams }: LazyInitOptions): IProviderModule;
681
742
  /**
682
743
  * **Publicly visible when the instance is casted to {@link IProviderModuleNaked}.**
683
744
  *
@@ -838,4 +899,4 @@ declare class GlobalAppModule extends ProviderModule implements IAppModule {
838
899
  */
839
900
  declare const AppModule: GlobalAppModule;
840
901
 
841
- export { ANONYMOUS_MODULE_DEFAULT_ID, AppModule, type AppModuleOptions, 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, XInjectionDynamicExportsOutOfRange, XInjectionError, XInjectionProviderModuleDisposedError, XInjectionProviderModuleError, bindingScopeToInjectionScope, injectionScopeToBindingScope, isClass, isClassOrFunction, isPlainObject };
902
+ 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, XInjectionDynamicExportsOutOfRange, XInjectionError, XInjectionProviderModuleDisposedError, XInjectionProviderModuleError, XInjectionProviderModuleMissingIdentifierError, bindingScopeToInjectionScope, injectionScopeToBindingScope, isClass, isClassOrFunction, isFunction, isPlainObject };