ts-ioc-container 33.3.0 → 33.4.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
  ```
@@ -2,7 +2,14 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.register = exports.getTransformers = exports.scope = 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;
7
14
  const scope = (predicate) => (r) => r.when(predicate);
8
15
  exports.scope = scope;
@@ -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;
@@ -1,5 +1,12 @@
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
+ };
3
10
  export const scope = (predicate) => (r) => r.when(predicate);
4
11
  const METADATA_KEY = 'registration';
5
12
  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.4.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": "11b2d0ad78a1b6a6e800c94ac9439f6275c95342"
63
63
  }
@@ -6,9 +6,10 @@ 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>;
12
13
  export declare const scope: (predicate: ScopePredicate) => MapFn<IRegistration>;
13
14
  export declare const getTransformers: (Target: constructor<unknown>) => MapFn<IRegistration<any>>[];
14
15
  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;