ts-ioc-container 41.1.0 → 41.1.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.
package/README.md CHANGED
@@ -522,7 +522,6 @@ describe('ProxyInjector', function () {
522
522
  mockContainer.addRegistration(R.fromClass(Service).bindToKey('service'));
523
523
 
524
524
  const app = mockContainer.resolveOne(App);
525
- console.log('App loggers:', app.loggers);
526
525
  expect(app.loggers).toBeInstanceOf(Array);
527
526
  expect(app.loggers.length).toBe(1);
528
527
  expect(app.loggers[0]).toBe(mockLogger);
@@ -752,7 +751,7 @@ describe('Provider', () => {
752
751
  }
753
752
 
754
753
  const root = new Container({ tags: ['root'] }).addRegistration(R.fromClass(Logger));
755
- const main = root.resolveByClass(Main);
754
+ const main = root.resolveClass(Main);
756
755
 
757
756
  expect(isLoggerCreated).toBe(false);
758
757
 
@@ -780,7 +779,7 @@ describe('Provider', () => {
780
779
  }
781
780
 
782
781
  const root = new Container({ tags: ['root'] }).addRegistration(R.fromClass(Logger));
783
- const main = root.resolveByClass(Main);
782
+ const main = root.resolveClass(Main);
784
783
 
785
784
  expect(main.getChannel()).toBe('file');
786
785
  });
@@ -809,7 +808,7 @@ describe('Provider', () => {
809
808
  .addRegistration(R.fromValue('file').bindToKey('channel'))
810
809
  .addRegistration(R.fromClass(Logger));
811
810
 
812
- const main = root.resolveByClass(Main);
811
+ const main = root.resolveClass(Main);
813
812
 
814
813
  expect(main.getChannel()).toBe('file');
815
814
  });
@@ -1552,7 +1551,7 @@ export class MoqContainer extends AutoMockedContainer {
1552
1551
  return this.mocks.get(key) as IMock<T>;
1553
1552
  }
1554
1553
 
1555
- resolveByClass<T>(target: any, options?: { args?: unknown[] }): T {
1554
+ resolveClass<T>(target: any, options?: { args?: unknown[] }): T {
1556
1555
  throw new Error('Method not implemented.');
1557
1556
  }
1558
1557
 
@@ -20,12 +20,14 @@ class Container {
20
20
  onConstruct;
21
21
  onDispose;
22
22
  injector;
23
+ resolveOneStrategy;
23
24
  constructor(options = {}) {
24
25
  this.injector = options.injector ?? new MetadataInjector_1.MetadataInjector();
25
26
  this.parent = options.parent ?? new EmptyContainer_1.EmptyContainer();
26
27
  this.tags = new Set(options.tags ?? []);
27
28
  this.onConstruct = options.onConstruct ?? (() => { });
28
29
  this.onDispose = options.onDispose ?? (() => { });
30
+ this.resolveOneStrategy = options.resolveOneStrategy ?? IContainer_1.DEFAULT_CONTAINER_RESOLVER;
29
31
  }
30
32
  register(key, provider, { aliases = [] } = {}) {
31
33
  this.validateContainer();
@@ -42,7 +44,7 @@ class Container {
42
44
  getRegistrations() {
43
45
  return [...this.parent.getRegistrations(), ...this.registrations];
44
46
  }
45
- resolveByClass(token, { args = [] } = {}) {
47
+ resolveClass(token, { args = [] } = {}) {
46
48
  this.validateContainer();
47
49
  const instance = this.injector.resolve(this, token, { args });
48
50
  this.instances.add(instance);
@@ -50,7 +52,7 @@ class Container {
50
52
  return instance;
51
53
  }
52
54
  resolveOne(keyOrAlias, options) {
53
- return (0, IContainer_1.DEFAULT_CONTAINER_RESOLVER)(this, keyOrAlias, options);
55
+ return this.resolveOneStrategy(this, keyOrAlias, options);
54
56
  }
55
57
  resolveOneByKey(keyOrAlias, { args = [], child = this, lazy } = {}) {
56
58
  this.validateContainer();
@@ -10,7 +10,7 @@ class EmptyContainer {
10
10
  getParent() {
11
11
  return undefined;
12
12
  }
13
- resolveByClass(token, options) {
13
+ resolveClass(token, options) {
14
14
  throw new MethodNotImplementedError_1.MethodNotImplementedError();
15
15
  }
16
16
  getScopes() {
@@ -9,7 +9,7 @@ function isDependencyKey(token) {
9
9
  }
10
10
  const DEFAULT_CONTAINER_RESOLVER = (scope, keyOrAlias, options) => {
11
11
  if ((0, utils_1.isConstructor)(keyOrAlias)) {
12
- return scope.resolveByClass(keyOrAlias, options);
12
+ return scope.resolveClass(keyOrAlias, options);
13
13
  }
14
14
  try {
15
15
  return scope.resolveOneByKey(keyOrAlias, options);
@@ -17,7 +17,7 @@ const toInjectFn = (target) => {
17
17
  return (s) => target.resolve(s);
18
18
  }
19
19
  if ((0, utils_1.isConstructor)(target)) {
20
- return (scope) => scope.resolveByClass(target);
20
+ return (scope) => scope.resolveClass(target);
21
21
  }
22
22
  if ((0, IContainer_1.isDependencyKey)(target)) {
23
23
  return (scope) => scope.resolveOne(target);
@@ -6,7 +6,7 @@ const ProviderPipe_1 = require("./ProviderPipe");
6
6
  class Provider {
7
7
  resolveDependency;
8
8
  static fromClass(Target) {
9
- return new Provider((container, options) => container.resolveByClass(Target, options));
9
+ return new Provider((container, options) => container.resolveClass(Target, options));
10
10
  }
11
11
  static fromValue(value) {
12
12
  return new Provider(() => value);
@@ -1,4 +1,4 @@
1
- import { DEFAULT_CONTAINER_RESOLVER as resolveOne, } from './IContainer';
1
+ import { DEFAULT_CONTAINER_RESOLVER, } from './IContainer';
2
2
  import { EmptyContainer } from './EmptyContainer';
3
3
  import { ContainerDisposedError } from '../errors/ContainerDisposedError';
4
4
  import { MetadataInjector } from '../injector/MetadataInjector';
@@ -17,12 +17,14 @@ export class Container {
17
17
  onConstruct;
18
18
  onDispose;
19
19
  injector;
20
+ resolveOneStrategy;
20
21
  constructor(options = {}) {
21
22
  this.injector = options.injector ?? new MetadataInjector();
22
23
  this.parent = options.parent ?? new EmptyContainer();
23
24
  this.tags = new Set(options.tags ?? []);
24
25
  this.onConstruct = options.onConstruct ?? (() => { });
25
26
  this.onDispose = options.onDispose ?? (() => { });
27
+ this.resolveOneStrategy = options.resolveOneStrategy ?? DEFAULT_CONTAINER_RESOLVER;
26
28
  }
27
29
  register(key, provider, { aliases = [] } = {}) {
28
30
  this.validateContainer();
@@ -39,7 +41,7 @@ export class Container {
39
41
  getRegistrations() {
40
42
  return [...this.parent.getRegistrations(), ...this.registrations];
41
43
  }
42
- resolveByClass(token, { args = [] } = {}) {
44
+ resolveClass(token, { args = [] } = {}) {
43
45
  this.validateContainer();
44
46
  const instance = this.injector.resolve(this, token, { args });
45
47
  this.instances.add(instance);
@@ -47,7 +49,7 @@ export class Container {
47
49
  return instance;
48
50
  }
49
51
  resolveOne(keyOrAlias, options) {
50
- return resolveOne(this, keyOrAlias, options);
52
+ return this.resolveOneStrategy(this, keyOrAlias, options);
51
53
  }
52
54
  resolveOneByKey(keyOrAlias, { args = [], child = this, lazy } = {}) {
53
55
  this.validateContainer();
@@ -7,7 +7,7 @@ export class EmptyContainer {
7
7
  getParent() {
8
8
  return undefined;
9
9
  }
10
- resolveByClass(token, options) {
10
+ resolveClass(token, options) {
11
11
  throw new MethodNotImplementedError();
12
12
  }
13
13
  getScopes() {
@@ -5,7 +5,7 @@ export function isDependencyKey(token) {
5
5
  }
6
6
  export const DEFAULT_CONTAINER_RESOLVER = (scope, keyOrAlias, options) => {
7
7
  if (isConstructor(keyOrAlias)) {
8
- return scope.resolveByClass(keyOrAlias, options);
8
+ return scope.resolveClass(keyOrAlias, options);
9
9
  }
10
10
  try {
11
11
  return scope.resolveOneByKey(keyOrAlias, options);
@@ -13,7 +13,7 @@ export const toInjectFn = (target) => {
13
13
  return (s) => target.resolve(s);
14
14
  }
15
15
  if (isConstructor(target)) {
16
- return (scope) => scope.resolveByClass(target);
16
+ return (scope) => scope.resolveClass(target);
17
17
  }
18
18
  if (isDependencyKey(target)) {
19
19
  return (scope) => scope.resolveOne(target);
@@ -3,7 +3,7 @@ import { isProviderPipe } from './ProviderPipe';
3
3
  export class Provider {
4
4
  resolveDependency;
5
5
  static fromClass(Target) {
6
- return new Provider((container, options) => container.resolveByClass(Target, options));
6
+ return new Provider((container, options) => container.resolveClass(Target, options));
7
7
  }
8
8
  static fromValue(value) {
9
9
  return new Provider(() => value);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-ioc-container",
3
- "version": "41.1.0",
3
+ "version": "41.1.1",
4
4
  "description": "Typescript IoC container",
5
5
  "publishConfig": {
6
6
  "access": "public",
@@ -15,7 +15,7 @@ export declare abstract class AutoMockedContainer implements IContainer {
15
15
  getRegistrations(): never[];
16
16
  addRegistration(registration: IRegistration): this;
17
17
  abstract resolveMany<T>(alias: DependencyKey, options?: ResolveManyOptions): T[];
18
- abstract resolveByClass<T>(target: constructor<T>, options?: {
18
+ abstract resolveClass<T>(target: constructor<T>, options?: {
19
19
  args?: unknown[];
20
20
  }): T;
21
21
  abstract resolveOneByKey<T>(keyOrAlias: DependencyKey, options?: ResolveOneOptions): T;
@@ -3,6 +3,7 @@ import { type IInjector } from '../injector/IInjector';
3
3
  import { type IProvider } from '../provider/IProvider';
4
4
  import { type IRegistration } from '../registration/IRegistration';
5
5
  import { type constructor } from '../utils';
6
+ type ResolveOneStrategy = <T>(scope: IContainer, keyOrAlias: constructor<T> | DependencyKey, options?: ResolveOneOptions) => T;
6
7
  export declare class Container implements IContainer {
7
8
  isDisposed: boolean;
8
9
  private parent;
@@ -15,17 +16,19 @@ export declare class Container implements IContainer {
15
16
  private readonly onConstruct;
16
17
  private readonly onDispose;
17
18
  private readonly injector;
19
+ private readonly resolveOneStrategy;
18
20
  constructor(options?: {
19
21
  injector?: IInjector;
20
22
  parent?: IContainer;
21
23
  tags?: Tag[];
22
24
  onConstruct?: (instance: Instance, scope: IContainer) => void;
23
25
  onDispose?: (scope: IContainer) => void;
26
+ resolveOneStrategy?: ResolveOneStrategy;
24
27
  });
25
28
  register(key: DependencyKey, provider: IProvider, { aliases }?: RegisterOptions): this;
26
29
  addRegistration(registration: IRegistration): this;
27
30
  getRegistrations(): IRegistration[];
28
- resolveByClass<T>(token: constructor<T>, { args }?: {
31
+ resolveClass<T>(token: constructor<T>, { args }?: {
29
32
  args?: unknown[];
30
33
  }): T;
31
34
  resolveOne<T>(keyOrAlias: constructor<T> | DependencyKey, options?: ResolveOneOptions): T;
@@ -47,3 +50,4 @@ export declare class Container implements IContainer {
47
50
  private validateContainer;
48
51
  private findProviderByKeyOrFail;
49
52
  }
53
+ export {};
@@ -5,7 +5,7 @@ import { type constructor } from '../utils';
5
5
  export declare class EmptyContainer implements IContainer {
6
6
  get isDisposed(): boolean;
7
7
  getParent(): undefined;
8
- resolveByClass<T>(token: constructor<T>, options?: {
8
+ resolveClass<T>(token: constructor<T>, options?: {
9
9
  args?: [];
10
10
  }): T;
11
11
  getScopes(): never[];
@@ -36,7 +36,7 @@ export interface IContainer extends Tagged {
36
36
  register(key: DependencyKey, value: IProvider, options?: RegisterOptions): this;
37
37
  addRegistration(registration: IRegistration): this;
38
38
  getRegistrations(): IRegistration[];
39
- resolveByClass<T>(target: constructor<T>, options?: {
39
+ resolveClass<T>(target: constructor<T>, options?: {
40
40
  args?: unknown[];
41
41
  }): T;
42
42
  resolveOne<T>(alias: constructor<T> | DependencyKey, options?: ResolveManyOptions): T;