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 +4 -5
- package/cjm/container/Container.js +4 -2
- package/cjm/container/EmptyContainer.js +1 -1
- package/cjm/container/IContainer.js +1 -1
- package/cjm/injector/inject.js +1 -1
- package/cjm/provider/Provider.js +1 -1
- package/esm/container/Container.js +5 -3
- package/esm/container/EmptyContainer.js +1 -1
- package/esm/container/IContainer.js +1 -1
- package/esm/injector/inject.js +1 -1
- package/esm/provider/Provider.js +1 -1
- package/package.json +1 -1
- package/typings/container/AutoMockedContainer.d.ts +1 -1
- package/typings/container/Container.d.ts +5 -1
- package/typings/container/EmptyContainer.d.ts +1 -1
- package/typings/container/IContainer.d.ts +1 -1
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.
|
|
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.
|
|
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.
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
55
|
+
return this.resolveOneStrategy(this, keyOrAlias, options);
|
|
54
56
|
}
|
|
55
57
|
resolveOneByKey(keyOrAlias, { args = [], child = this, lazy } = {}) {
|
|
56
58
|
this.validateContainer();
|
|
@@ -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.
|
|
12
|
+
return scope.resolveClass(keyOrAlias, options);
|
|
13
13
|
}
|
|
14
14
|
try {
|
|
15
15
|
return scope.resolveOneByKey(keyOrAlias, options);
|
package/cjm/injector/inject.js
CHANGED
|
@@ -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.
|
|
20
|
+
return (scope) => scope.resolveClass(target);
|
|
21
21
|
}
|
|
22
22
|
if ((0, IContainer_1.isDependencyKey)(target)) {
|
|
23
23
|
return (scope) => scope.resolveOne(target);
|
package/cjm/provider/Provider.js
CHANGED
|
@@ -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.
|
|
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
|
|
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
|
-
|
|
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
|
|
52
|
+
return this.resolveOneStrategy(this, keyOrAlias, options);
|
|
51
53
|
}
|
|
52
54
|
resolveOneByKey(keyOrAlias, { args = [], child = this, lazy } = {}) {
|
|
53
55
|
this.validateContainer();
|
|
@@ -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.
|
|
8
|
+
return scope.resolveClass(keyOrAlias, options);
|
|
9
9
|
}
|
|
10
10
|
try {
|
|
11
11
|
return scope.resolveOneByKey(keyOrAlias, options);
|
package/esm/injector/inject.js
CHANGED
|
@@ -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.
|
|
16
|
+
return (scope) => scope.resolveClass(target);
|
|
17
17
|
}
|
|
18
18
|
if (isDependencyKey(target)) {
|
|
19
19
|
return (scope) => scope.resolveOne(target);
|
package/esm/provider/Provider.js
CHANGED
|
@@ -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.
|
|
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
|
@@ -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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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;
|