@lppedd/di-wise-neo 0.25.0 → 0.26.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.
@@ -75,6 +75,10 @@ interface Type<T> extends ParameterDecorator {
75
75
  * An injectable type `T` with a default {@link Provider} and optional default registration options.
76
76
  */
77
77
  interface ProviderType<T> extends Type<T> {
78
+ /**
79
+ * The underlying `Type<T>`.
80
+ */
81
+ readonly type: Type<T>;
78
82
  /**
79
83
  * The type's default provider.
80
84
  */
@@ -399,7 +403,7 @@ interface Container {
399
403
  /**
400
404
  * Registers a {@link ClassProvider}, using the class itself as its token.
401
405
  *
402
- * Tokens provided via the {@link Injectable} decorator applied to the class
406
+ * Tokens provided via the {@link Alias} decorator applied to the class
403
407
  * are also registered as aliases.
404
408
  *
405
409
  * The scope is determined by the {@link Scoped} decorator - if present -
@@ -436,7 +440,10 @@ interface Container {
436
440
  */
437
441
  register<Value, ProviderValue extends Value>(token: Token<Value>, provider: ValueProvider<ProviderValue>): Container;
438
442
  /**
439
- * Removes all registrations for the given token from the container's internal registry.
443
+ * Removes registrations for the given token from the container's internal registry.
444
+ *
445
+ * When a `name` is provided, only the registration with that `name` is removed.
446
+ * Otherwise, all registrations are removed.
440
447
  *
441
448
  * Returns an array of the distinct values that were cached by this container for the
442
449
  * removed registrations. Values from {@link ValueProvider} registrations are not included,
@@ -731,25 +738,25 @@ declare function Inject<Value>(tokens: TokenRef<Value>): ParameterDecorator;
731
738
  *
732
739
  * Example:
733
740
  * ```ts
734
- * @Injectable(Weapon)
741
+ * @Alias(Weapon)
735
742
  * class Rifle {}
736
743
  * ```
737
744
  *
738
- * Note that `@Injectable` decorators can be stacked to add multiple aliases.
745
+ * Note that `@Alias` decorators can be stacked to add multiple aliases.
739
746
  *
740
747
  * Example:
741
748
  * ```ts
742
- * @Injectable(Weapon)
743
- * @Injectable(Gun) // Or just @Injectable(Weapon, Gun)
749
+ * @Alias(Weapon)
750
+ * @Alias(Gun) // Or just @Alias(Weapon, Gun)
744
751
  * class Rifle {}
745
752
  * ```
746
753
  */
747
- declare function Injectable<Value, This extends Value & object>(token: Token<Value>): ClassDecorator<This>;
748
- declare function Injectable<VA, VB, This extends VA & VB & object>(tokenA: Token<VA>, tokenB: Token<VB>): ClassDecorator<This>;
749
- declare function Injectable<VA, VB, VC, This extends VA & VB & VC & object>(tokenA: Token<VA>, tokenB: Token<VB>, tokenC: Token<VC>): ClassDecorator<This>;
750
- declare function Injectable<VA, VB, VC, VD, This extends VA & VB & VC & VD & object>(tokenA: Token<VA>, tokenB: Token<VB>, tokenC: Token<VC>, tokenD: Token<VD>): ClassDecorator<This>;
751
- declare function Injectable<VA, VB, VC, VD, VE, This extends VA & VB & VC & VD & VE & object>(tokenA: Token<VA>, tokenB: Token<VB>, tokenC: Token<VC>, tokenD: Token<VD>, tokenE: Token<VE>): ClassDecorator<This>;
752
- declare function Injectable<VA, VB, VC, VD, VE, VF, This extends VA & VB & VC & VD & VE & VF & object>(tokenA: Token<VA>, tokenB: Token<VB>, tokenC: Token<VC>, tokenD: Token<VD>, tokenE: Token<VE>, tokenF: Token<VF>): ClassDecorator<This>;
754
+ declare function Alias<Value, This extends Value & object>(token: Token<Value>): ClassDecorator<This>;
755
+ declare function Alias<VA, VB, This extends VA & VB & object>(tokenA: Token<VA>, tokenB: Token<VB>): ClassDecorator<This>;
756
+ declare function Alias<VA, VB, VC, This extends VA & VB & VC & object>(tokenA: Token<VA>, tokenB: Token<VB>, tokenC: Token<VC>): ClassDecorator<This>;
757
+ declare function Alias<VA, VB, VC, VD, This extends VA & VB & VC & VD & object>(tokenA: Token<VA>, tokenB: Token<VB>, tokenC: Token<VC>, tokenD: Token<VD>): ClassDecorator<This>;
758
+ declare function Alias<VA, VB, VC, VD, VE, This extends VA & VB & VC & VD & VE & object>(tokenA: Token<VA>, tokenB: Token<VB>, tokenC: Token<VC>, tokenD: Token<VD>, tokenE: Token<VE>): ClassDecorator<This>;
759
+ declare function Alias<VA, VB, VC, VD, VE, VF, This extends VA & VB & VC & VD & VE & VF & object>(tokenA: Token<VA>, tokenB: Token<VB>, tokenC: Token<VC>, tokenD: Token<VD>, tokenE: Token<VE>, tokenF: Token<VF>): ClassDecorator<This>;
753
760
  /**
754
761
  * Class decorator that registers an additional aliasing token for the decorated type.
755
762
  *
@@ -761,22 +768,26 @@ declare function Injectable<VA, VB, VC, VD, VE, VF, This extends VA & VB & VC &
761
768
  *
762
769
  * Example:
763
770
  * ```ts
764
- * @Injectable(tokenRef(() => Weapon)) // Weapon is declared after Rifle
771
+ * @Alias(tokenRef(() => Weapon)) // Weapon is declared after Rifle
765
772
  * class Rifle {}
766
773
  * // Other code...
767
774
  * const Weapon = createType("Weapon");
768
775
  * ```
769
776
  *
770
- * Note that `@Injectable` decorators can be stacked to add multiple aliases.
777
+ * Note that `@Alias` decorators can be stacked to add multiple aliases.
771
778
  *
772
779
  * Example:
773
780
  * ```ts
774
- * @Injectable(tokenRef(() => Weapon))
775
- * @Injectable(Gun)
781
+ * @Alias(tokenRef(() => Weapon))
782
+ * @Alias(Gun)
776
783
  * class Rifle {}
777
784
  * ```
778
785
  */
779
- declare function Injectable<Value, This extends Value & object>(tokens: TokenRef<Value>): ClassDecorator<This>;
786
+ declare function Alias<Value, This extends Value & object>(tokens: TokenRef<Value>): ClassDecorator<This>;
787
+ /**
788
+ * @deprecated Use {@link Alias} instead.
789
+ */
790
+ declare const Injectable: typeof Alias;
780
791
 
781
792
  /**
782
793
  * Parameter decorator that injects all instances provided by the registrations
@@ -1245,5 +1256,5 @@ declare function optionalBy<Instance extends object>(thisArg: object, Class: Con
1245
1256
  */
1246
1257
  declare function optionalBy<Value>(thisArg: object, token: Token<Value>, name?: string): Value | undefined;
1247
1258
 
1248
- export { AutoRegister, ContainerScoped, EagerInstantiate, Inject, InjectAll, Injectable, Injector, Named, Optional, OptionalAll, ResolutionScoped, Scope, Scoped, TransientScoped, assertInjectionContext, build, classRef, createContainer, createType, inject, injectAll, injectBy, optional, optionalAll, optionalBy, setClassIdentityMapping, tokenRef };
1259
+ export { Alias, AutoRegister, ContainerScoped, EagerInstantiate, Inject, InjectAll, Injectable, Injector, Named, Optional, OptionalAll, ResolutionScoped, Scope, Scoped, TransientScoped, assertInjectionContext, build, classRef, createContainer, createType, inject, injectAll, injectBy, optional, optionalAll, optionalBy, setClassIdentityMapping, tokenRef };
1249
1260
  export type { ChildContainerOptions, ClassProvider, ClassRef, Constructor, Container, ContainerHook, ContainerOptions, ExistingProvider, FactoryProvider, Provider, ProviderType, RegistrationOptions, Token, TokenRef, Tokens, Type, ValueProvider };
package/dist/cjs/index.js CHANGED
@@ -453,6 +453,7 @@ function createType(debugName, provider, options) {
453
453
  type.__type = undefined;
454
454
  type.toString = ()=>name;
455
455
  if (provider) {
456
+ type.type = type;
456
457
  type.provider = provider;
457
458
  type.options = options;
458
459
  }
@@ -790,10 +791,10 @@ function isDisposable(value) {
790
791
  };
791
792
  // Register the class itself
792
793
  this.myTokenRegistry.put(Class, registration);
793
- // Register the additional tokens specified via class decorators.
794
- // These tokens will point to the original Class token and will have the same scope.
795
- for (const token of metadata.tokenRef.getRefTokens()){
796
- this.myTokenRegistry.put(token, {
794
+ // Register additional aliasing tokens specified via class decorators.
795
+ // These tokens will point to the original class token and will have the same scope.
796
+ for (const aliasToken of metadata.tokenRef.getRefTokens()){
797
+ this.myTokenRegistry.put(aliasToken, {
797
798
  name: name,
798
799
  provider: {
799
800
  useExisting: [
@@ -815,9 +816,10 @@ function isDisposable(value) {
815
816
  check(name === undefined || name.trim(), `name qualifier for token ${getTokenName(token)} must not be empty`);
816
817
  if (isClassProvider(provider)) {
817
818
  const metadata = getMetadata(provider.useClass);
819
+ // An explicit provider name overrides what is specified via @Named
820
+ const effectiveName = name ?? metadata.name;
818
821
  const registration = {
819
- // An explicit provider name overrides what is specified via @Named
820
- name: metadata.name ?? name,
822
+ name: effectiveName,
821
823
  provider: metadata.provider,
822
824
  options: {
823
825
  // Explicit registration options override what is specified via class decorators (e.g., @Scoped)
@@ -826,7 +828,21 @@ function isDisposable(value) {
826
828
  },
827
829
  dependencies: metadata.dependencies
828
830
  };
831
+ // Register the class itself
829
832
  this.myTokenRegistry.put(token, registration);
833
+ // Register additional aliasing tokens specified via class decorators.
834
+ // These tokens will point to the original class token and will have the same scope.
835
+ for (const aliasToken of metadata.tokenRef.getRefTokens()){
836
+ this.myTokenRegistry.put(aliasToken, {
837
+ name: effectiveName,
838
+ provider: {
839
+ useExisting: [
840
+ token,
841
+ effectiveName
842
+ ]
843
+ }
844
+ });
845
+ }
830
846
  // Eager-instantiate only if the provided class is container-scoped.
831
847
  // Note that we are comparing the scope using the registration configured just above,
832
848
  // which takes into account both the metadata and the container option as a fallback.
@@ -1180,7 +1196,7 @@ function Inject(token) {
1180
1196
  }
1181
1197
 
1182
1198
  // @__NO_SIDE_EFFECTS__
1183
- function Injectable(...args) {
1199
+ function Alias(...args) {
1184
1200
  return (target)=>{
1185
1201
  const metadata = getMetadata(target);
1186
1202
  const arg0 = args[0];
@@ -1198,6 +1214,9 @@ function Injectable(...args) {
1198
1214
  };
1199
1215
  };
1200
1216
  }
1217
+ /**
1218
+ * @deprecated Use {@link Alias} instead.
1219
+ */ const Injectable = Alias;
1201
1220
 
1202
1221
  // @__NO_SIDE_EFFECTS__
1203
1222
  function InjectAll(token) {
@@ -1421,6 +1440,7 @@ const Scope = {
1421
1440
  */ Container: "Container"
1422
1441
  };
1423
1442
 
1443
+ exports.Alias = Alias;
1424
1444
  exports.AutoRegister = AutoRegister;
1425
1445
  exports.ContainerScoped = ContainerScoped;
1426
1446
  exports.EagerInstantiate = EagerInstantiate;