ts-ioc-container 33.3.0 → 33.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/README.md CHANGED
@@ -1083,6 +1083,17 @@ describe('Registration module', function () {
1083
1083
 
1084
1084
  expect(root.resolve('FileLogger')).toBeInstanceOf(FileLogger);
1085
1085
  });
1086
+
1087
+ it('should assign additional key which redirects to original one', function () {
1088
+ @register(key('ILogger', 'Logger'))
1089
+ @provider(singleton())
1090
+ class Logger {}
1091
+
1092
+ const root = createContainer().add(R.fromClass(Logger));
1093
+
1094
+ expect(root.resolve('Logger')).toBeInstanceOf(Logger);
1095
+ expect(root.resolve('Logger')).toBe(root.resolve('ILogger'));
1096
+ });
1086
1097
  });
1087
1098
 
1088
1099
  ```
package/cjm/by.js CHANGED
@@ -52,17 +52,18 @@ const depKey = (key = (0, utils_1.generateUUID)()) => {
52
52
  let isValidWhen;
53
53
  const mappers = [];
54
54
  return {
55
+ key,
55
56
  assignTo: (registration) => {
56
- registration.pipe(...mappers).to(key);
57
+ let reg = registration.pipe(...mappers).to(key);
57
58
  if (isValidWhen) {
58
- registration.when(isValidWhen);
59
+ reg = registration.when(isValidWhen);
59
60
  }
60
- return registration;
61
+ return reg;
61
62
  },
62
63
  register: (fn) => {
63
- const registration = new Registration_1.Registration(() => new Provider_1.Provider(fn), key).pipe(...mappers);
64
+ let registration = new Registration_1.Registration(() => new Provider_1.Provider(fn), key).pipe(...mappers);
64
65
  if (isValidWhen) {
65
- registration.when(isValidWhen);
66
+ registration = registration.when(isValidWhen);
66
67
  }
67
68
  return registration;
68
69
  },
@@ -79,6 +80,7 @@ const depKey = (key = (0, utils_1.generateUUID)()) => {
79
80
  isValidWhen = value;
80
81
  return this;
81
82
  },
83
+ redirectFrom: (registration) => registration.redirectFrom(key),
82
84
  };
83
85
  };
84
86
  exports.depKey = depKey;
package/cjm/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getParameterMetadata = exports.getMethodMetadata = exports.setMethodMetadata = exports.setParameterMetadata = exports.getMetadata = exports.setMetadata = exports.depKey = exports.byAliases = exports.byAlias = exports.IMemoKey = exports.by = exports.HookContext = exports.injectProp = exports.runHooksAsync = exports.runHooks = exports.hasHooks = exports.hook = exports.getHooks = exports.ContainerDisposedError = exports.MethodNotImplementedError = exports.DependencyNotFoundError = exports.Registration = exports.register = exports.scope = exports.key = exports.decorate = exports.multiCache = exports.MultiCache = exports.SingletonProvider = exports.singleton = exports.Provider = exports.ProviderDecorator = exports.args = exports.argsFn = exports.alias = exports.visible = exports.provider = exports.ProxyInjector = exports.SimpleInjector = exports.MetadataInjector = exports.resolveArgs = exports.inject = exports.AutoMockedContainer = exports.EmptyContainer = exports.Container = exports.isDependencyKey = void 0;
3
+ exports.getParameterMetadata = exports.getMethodMetadata = exports.setMethodMetadata = exports.setParameterMetadata = exports.getMetadata = exports.setMetadata = exports.depKey = exports.byAliases = exports.byAlias = exports.IMemoKey = exports.by = exports.HookContext = exports.injectProp = exports.runHooksAsync = exports.runHooks = exports.hasHooks = exports.hook = exports.getHooks = exports.ContainerDisposedError = exports.MethodNotImplementedError = exports.DependencyNotFoundError = exports.Registration = exports.redirectFrom = exports.register = exports.scope = exports.key = exports.decorate = exports.multiCache = exports.MultiCache = exports.SingletonProvider = exports.singleton = exports.Provider = exports.ProviderDecorator = exports.args = exports.argsFn = exports.alias = exports.visible = exports.provider = exports.ProxyInjector = exports.SimpleInjector = exports.MetadataInjector = exports.resolveArgs = exports.inject = exports.AutoMockedContainer = exports.EmptyContainer = exports.Container = exports.isDependencyKey = void 0;
4
4
  // Containers
5
5
  var IContainer_1 = require("./container/IContainer");
6
6
  Object.defineProperty(exports, "isDependencyKey", { enumerable: true, get: function () { return IContainer_1.isDependencyKey; } });
@@ -43,6 +43,7 @@ var IRegistration_1 = require("./registration/IRegistration");
43
43
  Object.defineProperty(exports, "key", { enumerable: true, get: function () { return IRegistration_1.key; } });
44
44
  Object.defineProperty(exports, "scope", { enumerable: true, get: function () { return IRegistration_1.scope; } });
45
45
  Object.defineProperty(exports, "register", { enumerable: true, get: function () { return IRegistration_1.register; } });
46
+ Object.defineProperty(exports, "redirectFrom", { enumerable: true, get: function () { return IRegistration_1.redirectFrom; } });
46
47
  var Registration_1 = require("./registration/Registration");
47
48
  Object.defineProperty(exports, "Registration", { enumerable: true, get: function () { return Registration_1.Registration; } });
48
49
  // Errors
@@ -1,9 +1,20 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.register = exports.getTransformers = exports.scope = exports.key = void 0;
3
+ exports.register = exports.getTransformers = exports.scope = exports.redirectFrom = exports.key = void 0;
4
4
  const metadata_1 = require("../metadata");
5
- const key = (key) => (r) => r.to(key);
5
+ const key = (...keys) => (r) => {
6
+ const [originalKey, ...redirectKeys] = keys;
7
+ let registration = r.to(originalKey);
8
+ for (const key of redirectKeys) {
9
+ registration = registration.redirectFrom(key);
10
+ }
11
+ return registration;
12
+ };
6
13
  exports.key = key;
14
+ const redirectFrom = (redirectKey) => (r) => {
15
+ return r.redirectFrom(redirectKey);
16
+ };
17
+ exports.redirectFrom = redirectFrom;
7
18
  const scope = (predicate) => (r) => r.when(predicate);
8
19
  exports.scope = scope;
9
20
  const METADATA_KEY = 'registration';
@@ -24,12 +24,17 @@ class Registration {
24
24
  this.createProvider = createProvider;
25
25
  this.key = key;
26
26
  this.matchScope = matchScope;
27
+ this.redirectKeys = new Set();
27
28
  this.mappers = [];
28
29
  }
29
30
  to(key) {
30
31
  this.key = key;
31
32
  return this;
32
33
  }
34
+ redirectFrom(key) {
35
+ this.redirectKeys.add(key);
36
+ return this;
37
+ }
33
38
  pipe(...mappers) {
34
39
  this.mappers.push(...mappers);
35
40
  return this;
@@ -45,7 +50,11 @@ class Registration {
45
50
  if (!this.key) {
46
51
  throw new DependencyMissingKeyError_1.DependencyMissingKeyError('No key provided for registration');
47
52
  }
48
- container.register(this.key, this.createProvider(this.key).pipe(...this.mappers));
53
+ const key = this.key;
54
+ container.register(key, this.createProvider(key).pipe(...this.mappers));
55
+ for (const redirectKey of this.redirectKeys) {
56
+ container.register(redirectKey, new Provider_1.Provider((s) => s.resolve(key)));
57
+ }
49
58
  }
50
59
  }
51
60
  exports.Registration = Registration;
package/esm/by.js CHANGED
@@ -46,17 +46,18 @@ export const depKey = (key = generateUUID()) => {
46
46
  let isValidWhen;
47
47
  const mappers = [];
48
48
  return {
49
+ key,
49
50
  assignTo: (registration) => {
50
- registration.pipe(...mappers).to(key);
51
+ let reg = registration.pipe(...mappers).to(key);
51
52
  if (isValidWhen) {
52
- registration.when(isValidWhen);
53
+ reg = registration.when(isValidWhen);
53
54
  }
54
- return registration;
55
+ return reg;
55
56
  },
56
57
  register: (fn) => {
57
- const registration = new Registration(() => new Provider(fn), key).pipe(...mappers);
58
+ let registration = new Registration(() => new Provider(fn), key).pipe(...mappers);
58
59
  if (isValidWhen) {
59
- registration.when(isValidWhen);
60
+ registration = registration.when(isValidWhen);
60
61
  }
61
62
  return registration;
62
63
  },
@@ -73,5 +74,6 @@ export const depKey = (key = generateUUID()) => {
73
74
  isValidWhen = value;
74
75
  return this;
75
76
  },
77
+ redirectFrom: (registration) => registration.redirectFrom(key),
76
78
  };
77
79
  };
package/esm/index.js CHANGED
@@ -15,7 +15,7 @@ export { singleton, SingletonProvider } from './provider/singleton/SingletonProv
15
15
  export { MultiCache, multiCache } from './provider/singleton/MultiCache';
16
16
  export { decorate } from './provider/DecoratorProvider';
17
17
  // Registrations
18
- export { key, scope, register } from './registration/IRegistration';
18
+ export { key, scope, register, redirectFrom, } from './registration/IRegistration';
19
19
  export { Registration } from './registration/Registration';
20
20
  // Errors
21
21
  export { DependencyNotFoundError } from './errors/DependencyNotFoundError';
@@ -1,5 +1,15 @@
1
1
  import { getMetadata, setMetadata } from '../metadata';
2
- export const key = (key) => (r) => r.to(key);
2
+ export const key = (...keys) => (r) => {
3
+ const [originalKey, ...redirectKeys] = keys;
4
+ let registration = r.to(originalKey);
5
+ for (const key of redirectKeys) {
6
+ registration = registration.redirectFrom(key);
7
+ }
8
+ return registration;
9
+ };
10
+ export const redirectFrom = (redirectKey) => (r) => {
11
+ return r.redirectFrom(redirectKey);
12
+ };
3
13
  export const scope = (predicate) => (r) => r.when(predicate);
4
14
  const METADATA_KEY = 'registration';
5
15
  export const getTransformers = (Target) => getMetadata(Target, METADATA_KEY) ?? [];
@@ -21,12 +21,17 @@ export class Registration {
21
21
  this.createProvider = createProvider;
22
22
  this.key = key;
23
23
  this.matchScope = matchScope;
24
+ this.redirectKeys = new Set();
24
25
  this.mappers = [];
25
26
  }
26
27
  to(key) {
27
28
  this.key = key;
28
29
  return this;
29
30
  }
31
+ redirectFrom(key) {
32
+ this.redirectKeys.add(key);
33
+ return this;
34
+ }
30
35
  pipe(...mappers) {
31
36
  this.mappers.push(...mappers);
32
37
  return this;
@@ -42,6 +47,10 @@ export class Registration {
42
47
  if (!this.key) {
43
48
  throw new DependencyMissingKeyError('No key provided for registration');
44
49
  }
45
- container.register(this.key, this.createProvider(this.key).pipe(...this.mappers));
50
+ const key = this.key;
51
+ container.register(key, this.createProvider(key).pipe(...this.mappers));
52
+ for (const redirectKey of this.redirectKeys) {
53
+ container.register(redirectKey, new Provider((s) => s.resolve(key)));
54
+ }
46
55
  }
47
56
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-ioc-container",
3
- "version": "33.3.0",
3
+ "version": "33.5.0",
4
4
  "description": "Typescript IoC container",
5
5
  "publishConfig": {
6
6
  "access": "public",
@@ -59,5 +59,5 @@
59
59
  "ts-node": "^10.9.1",
60
60
  "typescript": "5.4.3"
61
61
  },
62
- "gitHead": "5313997379db49d84f62a6acee5b7ae42c51b32f"
62
+ "gitHead": "554d11b827eeaea495cc7024ade9b1e7fd6c8649"
63
63
  }
package/typings/by.d.ts CHANGED
@@ -27,12 +27,14 @@ export declare const by: {
27
27
  };
28
28
  };
29
29
  export type DepKey<T> = {
30
+ key: DependencyKey;
30
31
  assignTo: (registration: IRegistration<T>) => IRegistration<T>;
31
32
  register: (fn: (s: IContainer, ...args: unknown[]) => T) => IRegistration<T>;
32
33
  resolve: (s: IContainer, ...args: unknown[]) => T;
33
34
  pipe(...values: MapFn<IProvider<T>>[]): DepKey<T>;
34
35
  to(target: DependencyKey): DepKey<T>;
35
36
  when(value: ScopePredicate): DepKey<T>;
37
+ redirectFrom: (registration: IRegistration<T>) => IRegistration<T>;
36
38
  };
37
39
  export declare const depKey: <T>(key?: DependencyKey) => DepKey<T>;
38
40
  export {};
@@ -13,7 +13,7 @@ export { singleton, SingletonProvider } from './provider/singleton/SingletonProv
13
13
  export { MultiCache, multiCache } from './provider/singleton/MultiCache';
14
14
  export { Cache } from './provider/singleton/Cache';
15
15
  export { decorate, DecorateFn } from './provider/DecoratorProvider';
16
- export { key, IRegistration, ReturnTypeOfRegistration, scope, register } from './registration/IRegistration';
16
+ export { key, IRegistration, ReturnTypeOfRegistration, scope, register, redirectFrom, } from './registration/IRegistration';
17
17
  export { Registration } from './registration/Registration';
18
18
  export { DependencyNotFoundError } from './errors/DependencyNotFoundError';
19
19
  export { MethodNotImplementedError } from './errors/MethodNotImplementedError';
@@ -6,9 +6,11 @@ export interface IRegistration<T = any> extends IContainerModule {
6
6
  when(isValidWhen: ScopePredicate): this;
7
7
  to(key: DependencyKey): this;
8
8
  pipe(...mappers: MapFn<IProvider<T>>[]): this;
9
+ redirectFrom(key: DependencyKey): this;
9
10
  }
10
11
  export type ReturnTypeOfRegistration<T> = T extends IRegistration<infer R> ? R : never;
11
- export declare const key: (key: DependencyKey) => MapFn<IRegistration>;
12
+ export declare const key: (...keys: DependencyKey[]) => MapFn<IRegistration>;
13
+ export declare const redirectFrom: (redirectKey: DependencyKey) => MapFn<IRegistration>;
12
14
  export declare const scope: (predicate: ScopePredicate) => MapFn<IRegistration>;
13
15
  export declare const getTransformers: (Target: constructor<unknown>) => MapFn<IRegistration<any>>[];
14
16
  export declare const register: (...mappers: MapFn<IRegistration>[]) => ClassDecorator;
@@ -6,12 +6,14 @@ export declare class Registration<T = any> implements IRegistration<T> {
6
6
  private createProvider;
7
7
  private key?;
8
8
  private matchScope;
9
+ private redirectKeys;
9
10
  static fromClass<T>(Target: constructor<T>): IRegistration<any>;
10
11
  static fromValue<T>(value: T): IRegistration<any>;
11
12
  static fromFn<T>(fn: ResolveDependency<T>): Registration<T>;
12
13
  private mappers;
13
14
  constructor(createProvider: (key: DependencyKey) => IProvider<T>, key?: DependencyKey | undefined, matchScope?: ScopePredicate);
14
15
  to(key: DependencyKey): this;
16
+ redirectFrom(key: DependencyKey): this;
15
17
  pipe(...mappers: MapFn<IProvider<T>>[]): this;
16
18
  when(isValidWhen: ScopePredicate): this;
17
19
  applyTo(container: IContainer): void;