ts-ioc-container 43.4.0 → 43.5.2
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 +10 -66
- package/cjm/container/Container.js +6 -22
- package/cjm/container/EmptyContainer.js +1 -1
- package/cjm/container/IContainer.js +4 -0
- package/cjm/index.js +7 -6
- package/cjm/injector/IInjector.js +1 -1
- package/cjm/registration/IRegistration.js +2 -2
- package/cjm/registration/Registration.js +4 -3
- package/cjm/token/{UniqToken.js → IDToken.js} +6 -6
- package/cjm/token/toToken.js +4 -3
- package/cjm/types.js +2 -0
- package/cjm/utils.js +0 -1
- package/esm/container/Container.js +6 -22
- package/esm/container/EmptyContainer.js +1 -1
- package/esm/container/IContainer.js +3 -1
- package/esm/index.js +3 -2
- package/esm/injector/IInjector.js +1 -1
- package/esm/registration/IRegistration.js +2 -2
- package/esm/registration/Registration.js +4 -3
- package/esm/token/{UniqToken.js → IDToken.js} +4 -4
- package/esm/token/toToken.js +4 -3
- package/esm/types.js +1 -0
- package/esm/utils.js +0 -1
- package/package.json +1 -1
- package/typings/container/Container.d.ts +4 -8
- package/typings/container/EmptyContainer.d.ts +3 -3
- package/typings/container/IContainer.d.ts +12 -17
- package/typings/hooks/HookContext.d.ts +0 -1
- package/typings/hooks/hook.d.ts +3 -1
- package/typings/hooks/injectProp.d.ts +2 -3
- package/typings/hooks/onConstruct.d.ts +2 -1
- package/typings/index.d.ts +7 -5
- package/typings/injector/IInjector.d.ts +1 -1
- package/typings/injector/MetadataInjector.d.ts +1 -1
- package/typings/injector/ProxyInjector.d.ts +1 -1
- package/typings/injector/SimpleInjector.d.ts +1 -1
- package/typings/injector/inject.d.ts +2 -2
- package/typings/metadata.d.ts +1 -1
- package/typings/provider/IProvider.d.ts +1 -1
- package/typings/provider/Provider.d.ts +1 -1
- package/typings/registration/IRegistration.d.ts +1 -1
- package/typings/registration/Registration.d.ts +2 -2
- package/typings/select.d.ts +1 -1
- package/typings/token/ClassToken.d.ts +1 -1
- package/typings/token/FunctionToken.d.ts +1 -1
- package/typings/token/{UniqToken.d.ts → IDToken.d.ts} +4 -4
- package/typings/token/InstanceListToken.d.ts +2 -1
- package/typings/token/toToken.d.ts +2 -2
- package/typings/types.d.ts +8 -0
- package/typings/utils.d.ts +1 -7
- package/cjm/container/AutoMockedContainer.js +0 -38
- package/esm/container/AutoMockedContainer.js +0 -34
- package/typings/container/AutoMockedContainer.d.ts +0 -21
package/README.md
CHANGED
|
@@ -861,13 +861,13 @@ import {
|
|
|
861
861
|
argsFn,
|
|
862
862
|
bindTo,
|
|
863
863
|
Container,
|
|
864
|
+
IDToken,
|
|
864
865
|
inject,
|
|
865
866
|
MultiCache,
|
|
866
867
|
register,
|
|
867
868
|
Registration as R,
|
|
868
869
|
resolveByArgs,
|
|
869
870
|
singleton,
|
|
870
|
-
UniqToken,
|
|
871
871
|
} from 'ts-ioc-container';
|
|
872
872
|
|
|
873
873
|
@register(bindTo('logger'))
|
|
@@ -911,8 +911,8 @@ describe('ArgsProvider', function () {
|
|
|
911
911
|
name: string;
|
|
912
912
|
}
|
|
913
913
|
|
|
914
|
-
const IUserRepositoryKey = new
|
|
915
|
-
const ITodoRepositoryKey = new
|
|
914
|
+
const IUserRepositoryKey = new IDToken<IRepository>('IUserRepository');
|
|
915
|
+
const ITodoRepositoryKey = new IDToken<IRepository>('ITodoRepository');
|
|
916
916
|
|
|
917
917
|
@register(bindTo(IUserRepositoryKey))
|
|
918
918
|
class UserRepository implements IRepository {
|
|
@@ -928,7 +928,7 @@ describe('ArgsProvider', function () {
|
|
|
928
928
|
repository: IRepository;
|
|
929
929
|
}
|
|
930
930
|
|
|
931
|
-
const IEntityManagerKey = new
|
|
931
|
+
const IEntityManagerKey = new IDToken<IEntityManager>('IEntityManager');
|
|
932
932
|
|
|
933
933
|
@register(bindTo(IEntityManagerKey), argsFn(resolveByArgs))
|
|
934
934
|
class EntityManager {
|
|
@@ -957,8 +957,8 @@ describe('ArgsProvider', function () {
|
|
|
957
957
|
name: string;
|
|
958
958
|
}
|
|
959
959
|
|
|
960
|
-
const IUserRepositoryKey = new
|
|
961
|
-
const ITodoRepositoryKey = new
|
|
960
|
+
const IUserRepositoryKey = new IDToken<IRepository>('IUserRepository');
|
|
961
|
+
const ITodoRepositoryKey = new IDToken<IRepository>('ITodoRepository');
|
|
962
962
|
|
|
963
963
|
@register(bindTo(IUserRepositoryKey))
|
|
964
964
|
class UserRepository implements IRepository {
|
|
@@ -974,7 +974,7 @@ describe('ArgsProvider', function () {
|
|
|
974
974
|
repository: IRepository;
|
|
975
975
|
}
|
|
976
976
|
|
|
977
|
-
const IEntityManagerKey = new
|
|
977
|
+
const IEntityManagerKey = new IDToken<IEntityManager>('IEntityManager');
|
|
978
978
|
|
|
979
979
|
@register(bindTo(IEntityManagerKey), argsFn(resolveByArgs), singleton(MultiCache.fromFirstArg))
|
|
980
980
|
class EntityManager {
|
|
@@ -1130,7 +1130,7 @@ describe('alias', () => {
|
|
|
1130
1130
|
|
|
1131
1131
|
const container = new Container().addRegistration(R.fromClass(FileLogger));
|
|
1132
1132
|
|
|
1133
|
-
expect(container.
|
|
1133
|
+
expect(container.resolveOneByAlias('ILogger')).toBeInstanceOf(FileLogger);
|
|
1134
1134
|
expect(() => container.resolve('logger')).toThrowError(DependencyNotFoundError);
|
|
1135
1135
|
});
|
|
1136
1136
|
|
|
@@ -1145,9 +1145,9 @@ describe('alias', () => {
|
|
|
1145
1145
|
.addRegistration(R.fromClass(FileLogger))
|
|
1146
1146
|
.addRegistration(R.fromClass(DbLogger));
|
|
1147
1147
|
|
|
1148
|
-
const
|
|
1148
|
+
const result1 = container.resolveOneByAlias('ILogger');
|
|
1149
1149
|
const child = container.createScope({ tags: ['child'] });
|
|
1150
|
-
const
|
|
1150
|
+
const result2 = child.resolveOneByAlias('ILogger');
|
|
1151
1151
|
|
|
1152
1152
|
expect(result1).toBeInstanceOf(FileLogger);
|
|
1153
1153
|
expect(result2).toBeInstanceOf(DbLogger);
|
|
@@ -1536,62 +1536,6 @@ describe('inject property', () => {
|
|
|
1536
1536
|
|
|
1537
1537
|
```
|
|
1538
1538
|
|
|
1539
|
-
## Mock
|
|
1540
|
-
Sometimes you need to automatically mock all dependencies in container. This is what `AutoMockedContainer` is for.
|
|
1541
|
-
|
|
1542
|
-
```typescript
|
|
1543
|
-
import {
|
|
1544
|
-
AutoMockedContainer,
|
|
1545
|
-
Container,
|
|
1546
|
-
type DependencyKey,
|
|
1547
|
-
MethodNotImplementedError,
|
|
1548
|
-
ResolveOneOptions,
|
|
1549
|
-
} from 'ts-ioc-container';
|
|
1550
|
-
import { type IMock, Mock } from 'moq.ts';
|
|
1551
|
-
|
|
1552
|
-
export class MoqContainer extends AutoMockedContainer {
|
|
1553
|
-
private mocks = new Map<DependencyKey, IMock<any>>();
|
|
1554
|
-
|
|
1555
|
-
resolve<T>(key: DependencyKey): T {
|
|
1556
|
-
return this.resolveMock<T>(key).object();
|
|
1557
|
-
}
|
|
1558
|
-
|
|
1559
|
-
resolveOneByAlias<T>(alias: DependencyKey, options?: ResolveOneOptions): T {
|
|
1560
|
-
throw new MethodNotImplementedError('resolveOneByAlias');
|
|
1561
|
-
}
|
|
1562
|
-
|
|
1563
|
-
resolveMock<T>(key: DependencyKey): IMock<T> {
|
|
1564
|
-
if (!this.mocks.has(key)) {
|
|
1565
|
-
this.mocks.set(key, new Mock());
|
|
1566
|
-
}
|
|
1567
|
-
return this.mocks.get(key) as IMock<T>;
|
|
1568
|
-
}
|
|
1569
|
-
|
|
1570
|
-
resolveByAlias<T>(alias: DependencyKey): T[] {
|
|
1571
|
-
throw new Error('Method not implemented.');
|
|
1572
|
-
}
|
|
1573
|
-
}
|
|
1574
|
-
|
|
1575
|
-
interface IEngine {
|
|
1576
|
-
getRegistrationNumber(): string;
|
|
1577
|
-
}
|
|
1578
|
-
|
|
1579
|
-
describe('Mocking', () => {
|
|
1580
|
-
it('should auto-mock dependencies', () => {
|
|
1581
|
-
const mockContainer = new MoqContainer();
|
|
1582
|
-
const container = new Container({ parent: mockContainer });
|
|
1583
|
-
|
|
1584
|
-
const engineMock = mockContainer.resolveMock<IEngine>('IEngine');
|
|
1585
|
-
engineMock.setup((i) => i.getRegistrationNumber()).returns('123');
|
|
1586
|
-
|
|
1587
|
-
const engine = container.resolve<IEngine>('IEngine');
|
|
1588
|
-
|
|
1589
|
-
expect(engine.getRegistrationNumber()).toBe('123');
|
|
1590
|
-
});
|
|
1591
|
-
});
|
|
1592
|
-
|
|
1593
|
-
```
|
|
1594
|
-
|
|
1595
1539
|
## Error
|
|
1596
1540
|
|
|
1597
1541
|
- [DependencyNotFoundError.ts](..%2F..%2Flib%2Ferrors%2FDependencyNotFoundError.ts)
|
|
@@ -32,7 +32,7 @@ class Container {
|
|
|
32
32
|
this.onDisposeHookList.push(...hooks);
|
|
33
33
|
return this;
|
|
34
34
|
}
|
|
35
|
-
|
|
35
|
+
addInstance(instance) {
|
|
36
36
|
this.instances.add(instance);
|
|
37
37
|
// Execute onConstruct hooks
|
|
38
38
|
for (const onConstruct of this.onConstructHookList) {
|
|
@@ -64,9 +64,8 @@ class Container {
|
|
|
64
64
|
? provider.resolve(this, { args, lazy })
|
|
65
65
|
: this.parent.resolve(keyOrAlias, { args, child, lazy });
|
|
66
66
|
}
|
|
67
|
-
resolveByAlias(alias, { args = [], child = this, lazy, excludedKeys = []
|
|
67
|
+
resolveByAlias(alias, { args = [], child = this, lazy, excludedKeys = [] } = {}) {
|
|
68
68
|
this.validateContainer();
|
|
69
|
-
let left = takeFirst;
|
|
70
69
|
const keys = [];
|
|
71
70
|
const deps = [];
|
|
72
71
|
for (const key of this.aliases.getKeysByAlias(alias).filter(utils_1.Filter.exclude(excludedKeys))) {
|
|
@@ -76,11 +75,6 @@ class Container {
|
|
|
76
75
|
}
|
|
77
76
|
keys.push(key);
|
|
78
77
|
deps.push(provider.resolve(this, { args, lazy }));
|
|
79
|
-
if (left < 0 || left > 0) {
|
|
80
|
-
left--;
|
|
81
|
-
continue;
|
|
82
|
-
}
|
|
83
|
-
break;
|
|
84
78
|
}
|
|
85
79
|
const parentDeps = this.parent.resolveByAlias(alias, {
|
|
86
80
|
args,
|
|
@@ -100,14 +94,12 @@ class Container {
|
|
|
100
94
|
}
|
|
101
95
|
createScope({ tags = [] } = {}) {
|
|
102
96
|
this.validateContainer();
|
|
103
|
-
const scope = new Container({
|
|
104
|
-
injector: this.injector,
|
|
105
|
-
parent: this,
|
|
106
|
-
tags,
|
|
107
|
-
})
|
|
97
|
+
const scope = new Container({ injector: this.injector, parent: this, tags })
|
|
108
98
|
.addOnConstructHook(...this.onConstructHookList)
|
|
109
99
|
.addOnDisposeHook(...this.onDisposeHookList);
|
|
110
|
-
|
|
100
|
+
for (const registration of this.getRegistrations()) {
|
|
101
|
+
registration.applyTo(scope);
|
|
102
|
+
}
|
|
111
103
|
this.scopes.add(scope);
|
|
112
104
|
return scope;
|
|
113
105
|
}
|
|
@@ -149,14 +141,6 @@ class Container {
|
|
|
149
141
|
onDispose(this);
|
|
150
142
|
}
|
|
151
143
|
}
|
|
152
|
-
/**
|
|
153
|
-
* @private
|
|
154
|
-
*/
|
|
155
|
-
applyRegistrationsFrom(source) {
|
|
156
|
-
for (const registration of source.getRegistrations()) {
|
|
157
|
-
registration.applyTo(this);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
144
|
validateContainer() {
|
|
161
145
|
if (this.isDisposed) {
|
|
162
146
|
throw new ContainerDisposedError_1.ContainerDisposedError('Container is already disposed');
|
package/cjm/index.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AliasUniqToken = exports.toAlias = exports.AliasToken = exports.InjectionToken = exports.getParameterMetadata = exports.getMethodMetadata = exports.setMethodMetadata = exports.setParameterMetadata = exports.getMetadata = exports.setMetadata = exports.SyncHooksRunner = exports.AsyncHooksRunner = exports.onDispose = exports.onDisposeHooksRunner = exports.onConstruct = exports.onConstructHooksRunner = exports.injectProp = exports.HookContext = exports.hasHooks = exports.hook = exports.getHooks = exports.UnexpectedHookResultError = exports.ContainerDisposedError = exports.MethodNotImplementedError = exports.DependencyMissingKeyError = exports.DependencyNotFoundError = exports.Registration = exports.bindTo = exports.register = exports.scope = exports.decorate = exports.MultiCache = exports.multiCache = exports.SingletonProvider = exports.singleton = exports.Provider = exports.resolveByArgs = exports.ProviderDecorator = exports.args = exports.argsFn = exports.lazy = exports.scopeAccess = exports.ProxyInjector = exports.SimpleInjector = exports.MetadataInjector = exports.resolveArgs = exports.inject = exports.
|
|
4
|
-
exports.Is = exports.select = exports.toToken = exports.InstanceListToken = exports.ConstantToken = exports.FunctionToken = exports.
|
|
3
|
+
exports.AliasUniqToken = exports.toAlias = exports.AliasToken = exports.InjectionToken = exports.getParameterMetadata = exports.getMethodMetadata = exports.setMethodMetadata = exports.setParameterMetadata = exports.getMetadata = exports.setMetadata = exports.SyncHooksRunner = exports.AsyncHooksRunner = exports.onDispose = exports.onDisposeHooksRunner = exports.onConstruct = exports.onConstructHooksRunner = exports.injectProp = exports.HookContext = exports.hasHooks = exports.hook = exports.getHooks = exports.UnexpectedHookResultError = exports.ContainerDisposedError = exports.MethodNotImplementedError = exports.DependencyMissingKeyError = exports.DependencyNotFoundError = exports.Registration = exports.bindTo = exports.register = exports.scope = exports.decorate = exports.MultiCache = exports.multiCache = exports.SingletonProvider = exports.singleton = exports.Provider = exports.resolveByArgs = exports.ProviderDecorator = exports.args = exports.argsFn = exports.lazy = exports.scopeAccess = exports.ProxyInjector = exports.SimpleInjector = exports.MetadataInjector = exports.resolveArgs = exports.inject = exports.EmptyContainer = exports.Container = exports.isDependencyKey = void 0;
|
|
4
|
+
exports.Is = exports.select = exports.toToken = exports.InstanceListToken = exports.ConstantToken = exports.FunctionToken = exports.IDToken = exports.ClassToken = exports.toAliasUniq = void 0;
|
|
5
|
+
// Containers
|
|
6
|
+
var IContainer_1 = require("./container/IContainer");
|
|
7
|
+
Object.defineProperty(exports, "isDependencyKey", { enumerable: true, get: function () { return IContainer_1.isDependencyKey; } });
|
|
5
8
|
var Container_1 = require("./container/Container");
|
|
6
9
|
Object.defineProperty(exports, "Container", { enumerable: true, get: function () { return Container_1.Container; } });
|
|
7
10
|
var EmptyContainer_1 = require("./container/EmptyContainer");
|
|
8
11
|
Object.defineProperty(exports, "EmptyContainer", { enumerable: true, get: function () { return EmptyContainer_1.EmptyContainer; } });
|
|
9
|
-
var AutoMockedContainer_1 = require("./container/AutoMockedContainer");
|
|
10
|
-
Object.defineProperty(exports, "AutoMockedContainer", { enumerable: true, get: function () { return AutoMockedContainer_1.AutoMockedContainer; } });
|
|
11
12
|
// Injectors
|
|
12
13
|
var inject_1 = require("./injector/inject");
|
|
13
14
|
Object.defineProperty(exports, "inject", { enumerable: true, get: function () { return inject_1.inject; } });
|
|
@@ -92,8 +93,8 @@ Object.defineProperty(exports, "AliasUniqToken", { enumerable: true, get: functi
|
|
|
92
93
|
Object.defineProperty(exports, "toAliasUniq", { enumerable: true, get: function () { return AliasUniqToken_1.toAliasUniq; } });
|
|
93
94
|
var ClassToken_1 = require("./token/ClassToken");
|
|
94
95
|
Object.defineProperty(exports, "ClassToken", { enumerable: true, get: function () { return ClassToken_1.ClassToken; } });
|
|
95
|
-
var
|
|
96
|
-
Object.defineProperty(exports, "
|
|
96
|
+
var IDToken_1 = require("./token/IDToken");
|
|
97
|
+
Object.defineProperty(exports, "IDToken", { enumerable: true, get: function () { return IDToken_1.IDToken; } });
|
|
97
98
|
var FunctionToken_1 = require("./token/FunctionToken");
|
|
98
99
|
Object.defineProperty(exports, "FunctionToken", { enumerable: true, get: function () { return FunctionToken_1.FunctionToken; } });
|
|
99
100
|
var ConstantToken_1 = require("./token/ConstantToken");
|
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.bindTo = exports.register = exports.getTransformers = exports.scope = void 0;
|
|
4
4
|
const metadata_1 = require("../metadata");
|
|
5
5
|
const ProviderPipe_1 = require("../provider/ProviderPipe");
|
|
6
|
-
const
|
|
6
|
+
const IDToken_1 = require("../token/IDToken");
|
|
7
7
|
const BindToken_1 = require("../token/BindToken");
|
|
8
8
|
const scope = (...predicates) => (r) => r.when(...predicates);
|
|
9
9
|
exports.scope = scope;
|
|
@@ -14,7 +14,7 @@ const register = (...mappers) => (0, metadata_1.setMetadata)(METADATA_KEY, mappe
|
|
|
14
14
|
exports.register = register;
|
|
15
15
|
const bindTo = (...tokens) => (r) => {
|
|
16
16
|
for (const token of tokens) {
|
|
17
|
-
const targetToken = (0, BindToken_1.isBindToken)(token) ? token : new
|
|
17
|
+
const targetToken = (0, BindToken_1.isBindToken)(token) ? token : new IDToken_1.IDToken(token);
|
|
18
18
|
targetToken.bindTo(r);
|
|
19
19
|
}
|
|
20
20
|
return r;
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Registration = void 0;
|
|
4
|
+
const IContainer_1 = require("../container/IContainer");
|
|
4
5
|
const utils_1 = require("../utils");
|
|
5
6
|
const Provider_1 = require("../provider/Provider");
|
|
6
7
|
const DependencyMissingKeyError_1 = require("../errors/DependencyMissingKeyError");
|
|
7
8
|
const IRegistration_1 = require("./IRegistration");
|
|
8
9
|
const ProviderPipe_1 = require("../provider/ProviderPipe");
|
|
9
|
-
const
|
|
10
|
+
const IDToken_1 = require("../token/IDToken");
|
|
10
11
|
class Registration {
|
|
11
12
|
createProvider;
|
|
12
13
|
key;
|
|
@@ -53,8 +54,8 @@ class Registration {
|
|
|
53
54
|
return this;
|
|
54
55
|
}
|
|
55
56
|
bindTo(key) {
|
|
56
|
-
if (
|
|
57
|
-
new
|
|
57
|
+
if ((0, IContainer_1.isDependencyKey)(key)) {
|
|
58
|
+
new IDToken_1.IDToken(key).bindTo(this);
|
|
58
59
|
return this;
|
|
59
60
|
}
|
|
60
61
|
key.bindTo(this);
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.IDToken = void 0;
|
|
4
4
|
const InjectionToken_1 = require("./InjectionToken");
|
|
5
|
-
class
|
|
5
|
+
class IDToken extends InjectionToken_1.InjectionToken {
|
|
6
6
|
token;
|
|
7
7
|
options;
|
|
8
8
|
constructor(token, options = {}) {
|
|
@@ -22,20 +22,20 @@ class UniqToken extends InjectionToken_1.InjectionToken {
|
|
|
22
22
|
}
|
|
23
23
|
args(...args) {
|
|
24
24
|
const argsFn = this.options.argsFn ?? (0, InjectionToken_1.setArgs)();
|
|
25
|
-
return new
|
|
25
|
+
return new IDToken(this.token, {
|
|
26
26
|
...this.options,
|
|
27
27
|
argsFn: (s) => [...argsFn(s), ...args],
|
|
28
28
|
});
|
|
29
29
|
}
|
|
30
30
|
argsFn(getArgsFn) {
|
|
31
31
|
const argsFn = this.options.argsFn ?? (0, InjectionToken_1.setArgs)();
|
|
32
|
-
return new
|
|
32
|
+
return new IDToken(this.token, {
|
|
33
33
|
...this.options,
|
|
34
34
|
argsFn: (s) => [...argsFn(s), ...getArgsFn(s)],
|
|
35
35
|
});
|
|
36
36
|
}
|
|
37
37
|
lazy() {
|
|
38
|
-
return new
|
|
38
|
+
return new IDToken(this.token, { ...this.options, lazy: true });
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
|
-
exports.
|
|
41
|
+
exports.IDToken = IDToken;
|
package/cjm/token/toToken.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.toToken = void 0;
|
|
4
|
+
const IContainer_1 = require("../container/IContainer");
|
|
4
5
|
const utils_1 = require("../utils");
|
|
5
|
-
const
|
|
6
|
+
const IDToken_1 = require("./IDToken");
|
|
6
7
|
const ClassToken_1 = require("./ClassToken");
|
|
7
8
|
const FunctionToken_1 = require("./FunctionToken");
|
|
8
9
|
const UnsupportedTokenTypeError_1 = require("../errors/UnsupportedTokenTypeError");
|
|
@@ -11,8 +12,8 @@ const toToken = (token) => {
|
|
|
11
12
|
if (token instanceof InjectionToken_1.InjectionToken) {
|
|
12
13
|
return token;
|
|
13
14
|
}
|
|
14
|
-
if (
|
|
15
|
-
return new
|
|
15
|
+
if ((0, IContainer_1.isDependencyKey)(token)) {
|
|
16
|
+
return new IDToken_1.IDToken(token);
|
|
16
17
|
}
|
|
17
18
|
if (utils_1.Is.constructor(token)) {
|
|
18
19
|
return new ClassToken_1.ClassToken(token);
|
package/cjm/types.js
ADDED
package/cjm/utils.js
CHANGED
|
@@ -45,5 +45,4 @@ exports.Is = {
|
|
|
45
45
|
object: (target) => target !== null && typeof target === 'object',
|
|
46
46
|
instance: (target) => Object.prototype.hasOwnProperty.call(target, 'constructor'),
|
|
47
47
|
constructor: (target) => typeof target === 'function' && !!target.prototype,
|
|
48
|
-
dependencyKey: (target) => ['string', 'symbol'].includes(typeof target),
|
|
49
48
|
};
|
|
@@ -29,7 +29,7 @@ export class Container {
|
|
|
29
29
|
this.onDisposeHookList.push(...hooks);
|
|
30
30
|
return this;
|
|
31
31
|
}
|
|
32
|
-
|
|
32
|
+
addInstance(instance) {
|
|
33
33
|
this.instances.add(instance);
|
|
34
34
|
// Execute onConstruct hooks
|
|
35
35
|
for (const onConstruct of this.onConstructHookList) {
|
|
@@ -61,9 +61,8 @@ export class Container {
|
|
|
61
61
|
? provider.resolve(this, { args, lazy })
|
|
62
62
|
: this.parent.resolve(keyOrAlias, { args, child, lazy });
|
|
63
63
|
}
|
|
64
|
-
resolveByAlias(alias, { args = [], child = this, lazy, excludedKeys = []
|
|
64
|
+
resolveByAlias(alias, { args = [], child = this, lazy, excludedKeys = [] } = {}) {
|
|
65
65
|
this.validateContainer();
|
|
66
|
-
let left = takeFirst;
|
|
67
66
|
const keys = [];
|
|
68
67
|
const deps = [];
|
|
69
68
|
for (const key of this.aliases.getKeysByAlias(alias).filter(F.exclude(excludedKeys))) {
|
|
@@ -73,11 +72,6 @@ export class Container {
|
|
|
73
72
|
}
|
|
74
73
|
keys.push(key);
|
|
75
74
|
deps.push(provider.resolve(this, { args, lazy }));
|
|
76
|
-
if (left < 0 || left > 0) {
|
|
77
|
-
left--;
|
|
78
|
-
continue;
|
|
79
|
-
}
|
|
80
|
-
break;
|
|
81
75
|
}
|
|
82
76
|
const parentDeps = this.parent.resolveByAlias(alias, {
|
|
83
77
|
args,
|
|
@@ -97,14 +91,12 @@ export class Container {
|
|
|
97
91
|
}
|
|
98
92
|
createScope({ tags = [] } = {}) {
|
|
99
93
|
this.validateContainer();
|
|
100
|
-
const scope = new Container({
|
|
101
|
-
injector: this.injector,
|
|
102
|
-
parent: this,
|
|
103
|
-
tags,
|
|
104
|
-
})
|
|
94
|
+
const scope = new Container({ injector: this.injector, parent: this, tags })
|
|
105
95
|
.addOnConstructHook(...this.onConstructHookList)
|
|
106
96
|
.addOnDisposeHook(...this.onDisposeHookList);
|
|
107
|
-
|
|
97
|
+
for (const registration of this.getRegistrations()) {
|
|
98
|
+
registration.applyTo(scope);
|
|
99
|
+
}
|
|
108
100
|
this.scopes.add(scope);
|
|
109
101
|
return scope;
|
|
110
102
|
}
|
|
@@ -146,14 +138,6 @@ export class Container {
|
|
|
146
138
|
onDispose(this);
|
|
147
139
|
}
|
|
148
140
|
}
|
|
149
|
-
/**
|
|
150
|
-
* @private
|
|
151
|
-
*/
|
|
152
|
-
applyRegistrationsFrom(source) {
|
|
153
|
-
for (const registration of source.getRegistrations()) {
|
|
154
|
-
registration.applyTo(this);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
141
|
validateContainer() {
|
|
158
142
|
if (this.isDisposed) {
|
|
159
143
|
throw new ContainerDisposedError('Container is already disposed');
|
package/esm/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
// Containers
|
|
2
|
+
export { isDependencyKey, } from './container/IContainer';
|
|
1
3
|
export { Container } from './container/Container';
|
|
2
4
|
export { EmptyContainer } from './container/EmptyContainer';
|
|
3
|
-
export { AutoMockedContainer } from './container/AutoMockedContainer';
|
|
4
5
|
// Injectors
|
|
5
6
|
export { inject, resolveArgs } from './injector/inject';
|
|
6
7
|
export { MetadataInjector } from './injector/MetadataInjector';
|
|
@@ -36,7 +37,7 @@ export { InjectionToken } from './token/InjectionToken';
|
|
|
36
37
|
export { AliasToken, toAlias } from './token/AliasToken';
|
|
37
38
|
export { AliasUniqToken, toAliasUniq } from './token/AliasUniqToken';
|
|
38
39
|
export { ClassToken } from './token/ClassToken';
|
|
39
|
-
export {
|
|
40
|
+
export { IDToken } from './token/IDToken';
|
|
40
41
|
export { FunctionToken } from './token/FunctionToken';
|
|
41
42
|
export { ConstantToken } from './token/ConstantToken';
|
|
42
43
|
export { InstanceListToken } from './token/InstanceListToken';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { getMetadata, setMetadata } from '../metadata';
|
|
2
2
|
import { isProviderPipe } from '../provider/ProviderPipe';
|
|
3
|
-
import {
|
|
3
|
+
import { IDToken } from '../token/IDToken';
|
|
4
4
|
import { isBindToken } from '../token/BindToken';
|
|
5
5
|
export const scope = (...predicates) => (r) => r.when(...predicates);
|
|
6
6
|
const METADATA_KEY = 'registration';
|
|
@@ -8,7 +8,7 @@ export const getTransformers = (Target) => getMetadata(Target, METADATA_KEY) ??
|
|
|
8
8
|
export const register = (...mappers) => setMetadata(METADATA_KEY, mappers.map((m) => (isProviderPipe(m) ? (r) => m.mapRegistration(r) : m)));
|
|
9
9
|
export const bindTo = (...tokens) => (r) => {
|
|
10
10
|
for (const token of tokens) {
|
|
11
|
-
const targetToken = isBindToken(token) ? token : new
|
|
11
|
+
const targetToken = isBindToken(token) ? token : new IDToken(token);
|
|
12
12
|
targetToken.bindTo(r);
|
|
13
13
|
}
|
|
14
14
|
return r;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import { isDependencyKey } from '../container/IContainer';
|
|
1
2
|
import { Is, pipe } from '../utils';
|
|
2
3
|
import { Provider } from '../provider/Provider';
|
|
3
4
|
import { DependencyMissingKeyError } from '../errors/DependencyMissingKeyError';
|
|
4
5
|
import { getTransformers } from './IRegistration';
|
|
5
6
|
import { isProviderPipe } from '../provider/ProviderPipe';
|
|
6
|
-
import {
|
|
7
|
+
import { IDToken } from '../token/IDToken';
|
|
7
8
|
export class Registration {
|
|
8
9
|
createProvider;
|
|
9
10
|
key;
|
|
@@ -50,8 +51,8 @@ export class Registration {
|
|
|
50
51
|
return this;
|
|
51
52
|
}
|
|
52
53
|
bindTo(key) {
|
|
53
|
-
if (
|
|
54
|
-
new
|
|
54
|
+
if (isDependencyKey(key)) {
|
|
55
|
+
new IDToken(key).bindTo(this);
|
|
55
56
|
return this;
|
|
56
57
|
}
|
|
57
58
|
key.bindTo(this);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { InjectionToken, setArgs } from './InjectionToken';
|
|
2
|
-
export class
|
|
2
|
+
export class IDToken extends InjectionToken {
|
|
3
3
|
token;
|
|
4
4
|
options;
|
|
5
5
|
constructor(token, options = {}) {
|
|
@@ -19,19 +19,19 @@ export class UniqToken extends InjectionToken {
|
|
|
19
19
|
}
|
|
20
20
|
args(...args) {
|
|
21
21
|
const argsFn = this.options.argsFn ?? setArgs();
|
|
22
|
-
return new
|
|
22
|
+
return new IDToken(this.token, {
|
|
23
23
|
...this.options,
|
|
24
24
|
argsFn: (s) => [...argsFn(s), ...args],
|
|
25
25
|
});
|
|
26
26
|
}
|
|
27
27
|
argsFn(getArgsFn) {
|
|
28
28
|
const argsFn = this.options.argsFn ?? setArgs();
|
|
29
|
-
return new
|
|
29
|
+
return new IDToken(this.token, {
|
|
30
30
|
...this.options,
|
|
31
31
|
argsFn: (s) => [...argsFn(s), ...getArgsFn(s)],
|
|
32
32
|
});
|
|
33
33
|
}
|
|
34
34
|
lazy() {
|
|
35
|
-
return new
|
|
35
|
+
return new IDToken(this.token, { ...this.options, lazy: true });
|
|
36
36
|
}
|
|
37
37
|
}
|
package/esm/token/toToken.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { isDependencyKey } from '../container/IContainer';
|
|
1
2
|
import { Is } from '../utils';
|
|
2
|
-
import {
|
|
3
|
+
import { IDToken } from './IDToken';
|
|
3
4
|
import { ClassToken } from './ClassToken';
|
|
4
5
|
import { FunctionToken } from './FunctionToken';
|
|
5
6
|
import { UnsupportedTokenTypeError } from '../errors/UnsupportedTokenTypeError';
|
|
@@ -8,8 +9,8 @@ export const toToken = (token) => {
|
|
|
8
9
|
if (token instanceof InjectionToken) {
|
|
9
10
|
return token;
|
|
10
11
|
}
|
|
11
|
-
if (
|
|
12
|
-
return new
|
|
12
|
+
if (isDependencyKey(token)) {
|
|
13
|
+
return new IDToken(token);
|
|
13
14
|
}
|
|
14
15
|
if (Is.constructor(token)) {
|
|
15
16
|
return new ClassToken(token);
|
package/esm/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/esm/utils.js
CHANGED
|
@@ -37,5 +37,4 @@ export const Is = {
|
|
|
37
37
|
object: (target) => target !== null && typeof target === 'object',
|
|
38
38
|
instance: (target) => Object.prototype.hasOwnProperty.call(target, 'constructor'),
|
|
39
39
|
constructor: (target) => typeof target === 'function' && !!target.prototype,
|
|
40
|
-
dependencyKey: (target) => ['string', 'symbol'].includes(typeof target),
|
|
41
40
|
};
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { type CreateScopeOptions, type DependencyKey, type IContainer, type IContainerModule, type
|
|
1
|
+
import { type CreateScopeOptions, type DependencyKey, type IContainer, type IContainerModule, type RegisterOptions, ResolveManyOptions, type ResolveOneOptions, type Tag } from './IContainer';
|
|
2
2
|
import { type IInjector } from '../injector/IInjector';
|
|
3
3
|
import { type IProvider } from '../provider/IProvider';
|
|
4
4
|
import { type IRegistration } from '../registration/IRegistration';
|
|
5
|
-
import { type constructor } from '../utils';
|
|
6
5
|
import { OnConstructHook } from '../hooks/onConstruct';
|
|
7
6
|
import { OnDisposeHook } from '../hooks/onDispose';
|
|
7
|
+
import { constructor, Instance } from '../types';
|
|
8
8
|
export declare class Container implements IContainer {
|
|
9
9
|
isDisposed: boolean;
|
|
10
10
|
private parent;
|
|
@@ -24,12 +24,12 @@ export declare class Container implements IContainer {
|
|
|
24
24
|
});
|
|
25
25
|
addOnConstructHook(...hooks: OnConstructHook[]): this;
|
|
26
26
|
addOnDisposeHook(...hooks: OnDisposeHook[]): this;
|
|
27
|
-
|
|
27
|
+
addInstance(instance: Instance): void;
|
|
28
28
|
register(key: DependencyKey, provider: IProvider, { aliases }?: RegisterOptions): this;
|
|
29
29
|
addRegistration(registration: IRegistration): this;
|
|
30
30
|
getRegistrations(): IRegistration[];
|
|
31
31
|
resolve<T>(keyOrAlias: constructor<T> | DependencyKey, { args, child, lazy }?: ResolveOneOptions): T;
|
|
32
|
-
resolveByAlias<T>(alias: DependencyKey, { args, child, lazy, excludedKeys
|
|
32
|
+
resolveByAlias<T>(alias: DependencyKey, { args, child, lazy, excludedKeys }?: ResolveManyOptions): T[];
|
|
33
33
|
resolveOneByAlias<T>(alias: DependencyKey, { args, child, lazy }?: ResolveOneOptions): T;
|
|
34
34
|
createScope({ tags }?: CreateScopeOptions): IContainer;
|
|
35
35
|
getScopes(): IContainer[];
|
|
@@ -39,10 +39,6 @@ export declare class Container implements IContainer {
|
|
|
39
39
|
getInstances(): Instance<unknown>[];
|
|
40
40
|
hasTag(tag: Tag): boolean;
|
|
41
41
|
dispose(): void;
|
|
42
|
-
/**
|
|
43
|
-
* @private
|
|
44
|
-
*/
|
|
45
|
-
applyRegistrationsFrom(source: Container): void;
|
|
46
42
|
private validateContainer;
|
|
47
43
|
private findProviderByKeyOrFail;
|
|
48
44
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { type DependencyKey, type IContainer, type IContainerModule,
|
|
1
|
+
import { type DependencyKey, type IContainer, type IContainerModule, type ResolveManyOptions, type ResolveOneOptions, type Tag } from './IContainer';
|
|
2
2
|
import { type IProvider } from '../provider/IProvider';
|
|
3
3
|
import { type IRegistration } from '../registration/IRegistration';
|
|
4
|
-
import {
|
|
4
|
+
import { constructor, Instance } from '../types';
|
|
5
5
|
export declare class EmptyContainer implements IContainer {
|
|
6
6
|
get isDisposed(): boolean;
|
|
7
|
-
|
|
7
|
+
addInstance(instance: Instance): void;
|
|
8
8
|
getParent(): undefined;
|
|
9
9
|
getScopes(): never[];
|
|
10
10
|
getInstances(): never[];
|
|
@@ -1,35 +1,30 @@
|
|
|
1
1
|
import { type IProvider, ProviderOptions } from '../provider/IProvider';
|
|
2
|
-
import { type constructor } from '../utils';
|
|
3
2
|
import { type IRegistration } from '../registration/IRegistration';
|
|
4
|
-
|
|
3
|
+
import { constructor, Instance } from '../types';
|
|
5
4
|
export type DependencyKey = string | symbol;
|
|
5
|
+
export declare function isDependencyKey(target: unknown): target is DependencyKey;
|
|
6
|
+
export type Tag = string;
|
|
7
|
+
type WithTags = {
|
|
8
|
+
tags: Tag[];
|
|
9
|
+
};
|
|
6
10
|
type WithChild = {
|
|
7
11
|
child: Tagged;
|
|
8
12
|
};
|
|
9
|
-
export type ResolveOneOptions = ProviderOptions & Partial<WithChild>;
|
|
10
13
|
type WithExcludedKeys = {
|
|
11
14
|
excludedKeys: DependencyKey[];
|
|
12
15
|
};
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
export type
|
|
16
|
+
export interface Tagged {
|
|
17
|
+
hasTag(tag: Tag): boolean;
|
|
18
|
+
}
|
|
19
|
+
export type ResolveOneOptions = ProviderOptions & Partial<WithChild>;
|
|
20
|
+
export type ResolveManyOptions = ResolveOneOptions & Partial<WithExcludedKeys>;
|
|
17
21
|
export interface Resolvable {
|
|
18
22
|
resolve<T>(key: constructor<T> | DependencyKey, options?: ResolveOneOptions): T;
|
|
19
23
|
}
|
|
20
24
|
export interface IContainerModule {
|
|
21
25
|
applyTo(container: IContainer): void;
|
|
22
26
|
}
|
|
23
|
-
export interface Tagged {
|
|
24
|
-
hasTag(tag: Tag): boolean;
|
|
25
|
-
}
|
|
26
|
-
type WithTags = {
|
|
27
|
-
tags: Tag[];
|
|
28
|
-
};
|
|
29
27
|
export type CreateScopeOptions = Partial<WithTags>;
|
|
30
|
-
export interface Instance<T = unknown> {
|
|
31
|
-
new (...args: unknown[]): T;
|
|
32
|
-
}
|
|
33
28
|
export type RegisterOptions = {
|
|
34
29
|
aliases?: DependencyKey[];
|
|
35
30
|
};
|
|
@@ -48,6 +43,6 @@ export interface IContainer extends Tagged {
|
|
|
48
43
|
getParent(): IContainer | undefined;
|
|
49
44
|
getInstances(): Instance[];
|
|
50
45
|
dispose(): void;
|
|
51
|
-
|
|
46
|
+
addInstance(instance: Instance): void;
|
|
52
47
|
}
|
|
53
48
|
export {};
|
package/typings/hooks/hook.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { type IHookContext } from './HookContext';
|
|
2
|
-
import {
|
|
2
|
+
import type { IContainer } from '../container/IContainer';
|
|
3
|
+
import { constructor } from '../types';
|
|
4
|
+
export type InjectFn<T = unknown> = (s: IContainer) => T;
|
|
3
5
|
export type HookFn<T extends IHookContext = IHookContext> = (context: T) => void | Promise<void>;
|
|
4
6
|
export interface HookClass<T extends IHookContext = IHookContext> {
|
|
5
7
|
execute(context: Omit<T, 'scope'>): void | Promise<void>;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type { InjectFn } from './HookContext';
|
|
2
1
|
import { DependencyKey } from '../container/IContainer';
|
|
3
|
-
import
|
|
4
|
-
import { HookFn } from './hook';
|
|
2
|
+
import { HookFn, InjectFn } from './hook';
|
|
5
3
|
import { InjectionToken } from '../token/InjectionToken';
|
|
4
|
+
import { constructor } from '../types';
|
|
6
5
|
export declare const injectProp: (fn: InjectFn | InjectionToken | DependencyKey | constructor<unknown>) => HookFn;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { HookFn } from './hook';
|
|
2
2
|
import { SyncHooksRunner } from './runner/SyncHooksRunner';
|
|
3
|
-
import type { IContainer
|
|
3
|
+
import type { IContainer } from '../container/IContainer';
|
|
4
|
+
import { Instance } from '../types';
|
|
4
5
|
export declare const onConstructHooksRunner: SyncHooksRunner;
|
|
5
6
|
export declare const onConstruct: (fn: HookFn) => (target: object, propertyKey: string | symbol) => void;
|
|
6
7
|
export type OnConstructHook = (instance: Instance, scope: IContainer) => void;
|
package/typings/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
export { type IContainer, type Resolvable, type IContainerModule, type DependencyKey, type Tag, type Tagged, type
|
|
1
|
+
export { type IContainer, type Resolvable, type IContainerModule, type DependencyKey, type Tag, type Tagged, type ResolveOneOptions, type ResolveManyOptions, isDependencyKey, } from './container/IContainer';
|
|
2
2
|
export { Container } from './container/Container';
|
|
3
3
|
export { EmptyContainer } from './container/EmptyContainer';
|
|
4
|
-
export { AutoMockedContainer } from './container/AutoMockedContainer';
|
|
5
4
|
export { inject, resolveArgs } from './injector/inject';
|
|
6
5
|
export { type IInjector, type InjectOptions, type IInjectFnResolver } from './injector/IInjector';
|
|
7
6
|
export { MetadataInjector } from './injector/MetadataInjector';
|
|
@@ -21,7 +20,7 @@ export { MethodNotImplementedError } from './errors/MethodNotImplementedError';
|
|
|
21
20
|
export { ContainerDisposedError } from './errors/ContainerDisposedError';
|
|
22
21
|
export { UnexpectedHookResultError } from './errors/UnexpectedHookResultError';
|
|
23
22
|
export { getHooks, hook, hasHooks, type HookFn, type HookClass } from './hooks/hook';
|
|
24
|
-
export { HookContext, type
|
|
23
|
+
export { HookContext, type IHookContext } from './hooks/HookContext';
|
|
25
24
|
export { injectProp } from './hooks/injectProp';
|
|
26
25
|
export { onConstructHooksRunner, onConstruct } from './hooks/onConstruct';
|
|
27
26
|
export { onDisposeHooksRunner, onDispose } from './hooks/onDispose';
|
|
@@ -33,10 +32,13 @@ export { InjectionToken } from './token/InjectionToken';
|
|
|
33
32
|
export { AliasToken, toAlias } from './token/AliasToken';
|
|
34
33
|
export { AliasUniqToken, toAliasUniq } from './token/AliasUniqToken';
|
|
35
34
|
export { ClassToken } from './token/ClassToken';
|
|
36
|
-
export {
|
|
35
|
+
export { IDToken } from './token/IDToken';
|
|
37
36
|
export { FunctionToken } from './token/FunctionToken';
|
|
38
37
|
export { ConstantToken } from './token/ConstantToken';
|
|
39
38
|
export { type InstancePredicate, InstanceListToken } from './token/InstanceListToken';
|
|
40
39
|
export { toToken } from './token/toToken';
|
|
41
40
|
export { select } from './select';
|
|
42
|
-
export {
|
|
41
|
+
export { Is } from './utils';
|
|
42
|
+
export { InjectFn } from './hooks/hook';
|
|
43
|
+
export { Instance } from './types';
|
|
44
|
+
export { constructor } from './types';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { IInjector, InjectOptions, Injector } from './IInjector';
|
|
2
2
|
import type { IContainer } from '../container/IContainer';
|
|
3
|
-
import
|
|
3
|
+
import { constructor } from '../types';
|
|
4
4
|
export declare class MetadataInjector extends Injector implements IInjector {
|
|
5
5
|
protected createInstance<T>(scope: IContainer, Target: constructor<T>, { args: deps }?: InjectOptions): T;
|
|
6
6
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { IInjector, InjectOptions, Injector } from './IInjector';
|
|
2
2
|
import type { IContainer } from '../container/IContainer';
|
|
3
|
-
import
|
|
3
|
+
import { constructor } from '../types';
|
|
4
4
|
export declare class ProxyInjector extends Injector implements IInjector {
|
|
5
5
|
protected createInstance<T>(scope: IContainer, Target: constructor<T>, { args: deps }?: InjectOptions): T;
|
|
6
6
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { IInjector, InjectOptions, Injector } from './IInjector';
|
|
2
2
|
import type { IContainer } from '../container/IContainer';
|
|
3
|
-
import { constructor } from '../
|
|
3
|
+
import { constructor } from '../types';
|
|
4
4
|
export declare class SimpleInjector extends Injector implements IInjector {
|
|
5
5
|
protected createInstance<T>(container: IContainer, Target: constructor<T>, { args }?: InjectOptions): T;
|
|
6
6
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { type constructor } from '../utils';
|
|
2
1
|
import { type IContainer } from '../container/IContainer';
|
|
3
|
-
import { type InjectFn } from '../hooks/HookContext';
|
|
4
2
|
import { InjectionToken } from '../token/InjectionToken';
|
|
3
|
+
import { InjectFn } from '../hooks/hook';
|
|
4
|
+
import { constructor } from '../types';
|
|
5
5
|
export declare const inject: <T>(fn: InjectionToken<T> | InjectFn<T> | symbol | string | constructor<T>) => ParameterDecorator;
|
|
6
6
|
export declare const resolveArgs: (Target: constructor<unknown>, methodName?: string) => (scope: IContainer, ...deps: unknown[]) => unknown[];
|
package/typings/metadata.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { constructor } from './types';
|
|
2
2
|
export declare const setMetadata: <T>(key: string | symbol, value: T) => ClassDecorator;
|
|
3
3
|
export declare function getMetadata<T>(target: object, key: string | symbol): T | undefined;
|
|
4
4
|
export declare const setParameterMetadata: (key: string | symbol, value: unknown) => ParameterDecorator;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { IContainer, Tagged } from '../container/IContainer';
|
|
2
|
-
import { MapFn } from '../utils';
|
|
3
2
|
import { ProviderPipe } from './ProviderPipe';
|
|
4
3
|
import { InjectOptions } from '../injector/IInjector';
|
|
4
|
+
import { MapFn } from '../types';
|
|
5
5
|
export type WithLazy = {
|
|
6
6
|
lazy: boolean;
|
|
7
7
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ArgsFn, IProvider, ProviderOptions, ResolveDependency, ScopeAccessFn, ScopeAccessOptions } from './IProvider';
|
|
2
2
|
import type { DependencyKey, IContainer } from '../container/IContainer';
|
|
3
|
-
import { constructor, MapFn } from '../utils';
|
|
4
3
|
import type { ProviderPipe } from './ProviderPipe';
|
|
4
|
+
import { constructor, MapFn } from '../types';
|
|
5
5
|
export declare class Provider<T = any> implements IProvider<T> {
|
|
6
6
|
private readonly resolveDependency;
|
|
7
7
|
static fromClass<T>(Target: constructor<T>): IProvider<T>;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { DependencyKey, IContainer, IContainerModule } from '../container/IContainer';
|
|
2
|
-
import { constructor, MapFn } from '../utils';
|
|
3
2
|
import type { IProvider } from '../provider/IProvider';
|
|
4
3
|
import { ProviderPipe } from '../provider/ProviderPipe';
|
|
5
4
|
import { BindToken } from '../token/BindToken';
|
|
5
|
+
import { constructor, MapFn } from '../types';
|
|
6
6
|
export type ScopePredicate = (s: IContainer, prev?: boolean) => boolean;
|
|
7
7
|
export interface IRegistration<T = any> extends IContainerModule {
|
|
8
8
|
when(...predicates: ScopePredicate[]): this;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { constructor, MapFn } from '../utils';
|
|
1
|
+
import { DependencyKey, IContainer } from '../container/IContainer';
|
|
3
2
|
import type { IProvider, ResolveDependency } from '../provider/IProvider';
|
|
4
3
|
import type { IRegistration, ScopePredicate } from './IRegistration';
|
|
5
4
|
import type { ProviderPipe } from '../provider/ProviderPipe';
|
|
6
5
|
import { BindToken } from '../token/BindToken';
|
|
6
|
+
import { constructor, MapFn } from '../types';
|
|
7
7
|
export declare class Registration<T = any> implements IRegistration<T> {
|
|
8
8
|
private createProvider;
|
|
9
9
|
private key?;
|
package/typings/select.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { FunctionToken } from './token/FunctionToken';
|
|
|
3
3
|
import { InstanceListToken, InstancePredicate } from './token/InstanceListToken';
|
|
4
4
|
export declare const select: {
|
|
5
5
|
alias: (token: import("./container/IContainer").DependencyKey) => import("./token/AliasToken").AliasToken<any>;
|
|
6
|
-
token: <T = any>(token: import(".").InjectFn<T> | import(".").InjectionToken<T> | import("./container/IContainer").DependencyKey | import("./
|
|
6
|
+
token: <T = any>(token: import(".").InjectFn<T> | import(".").InjectionToken<T> | import("./container/IContainer").DependencyKey | import("./types").constructor<T>) => import(".").InjectionToken<T>;
|
|
7
7
|
instances: (predicate?: InstancePredicate) => InstanceListToken;
|
|
8
8
|
scope: {
|
|
9
9
|
current: FunctionToken<IContainer>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { IContainer } from '../container/IContainer';
|
|
2
2
|
import { InjectionToken, TokenOptions } from './InjectionToken';
|
|
3
|
-
import { constructor } from '../
|
|
3
|
+
import { constructor } from '../types';
|
|
4
4
|
export declare class ClassToken<T = any> extends InjectionToken<T> {
|
|
5
5
|
private readonly token;
|
|
6
6
|
private options;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { InjectFn } from '../hooks/HookContext';
|
|
2
1
|
import type { IContainer } from '../container/IContainer';
|
|
3
2
|
import { InjectionToken } from './InjectionToken';
|
|
3
|
+
import { InjectFn } from '../hooks/hook';
|
|
4
4
|
export declare class FunctionToken<T = any> extends InjectionToken<T> {
|
|
5
5
|
private readonly fn;
|
|
6
6
|
constructor(fn: InjectFn<T>);
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import type { IContainer } from '../container/IContainer';
|
|
2
2
|
import { InjectionToken, TokenOptions } from './InjectionToken';
|
|
3
3
|
import { IRegistration } from '../registration/IRegistration';
|
|
4
|
-
export declare class
|
|
4
|
+
export declare class IDToken<T = any> extends InjectionToken {
|
|
5
5
|
token: string | symbol;
|
|
6
6
|
private options;
|
|
7
7
|
constructor(token: string | symbol, options?: TokenOptions);
|
|
8
8
|
resolve(s: IContainer): T;
|
|
9
9
|
bindTo(r: IRegistration<T>): void;
|
|
10
|
-
args(...args: unknown[]):
|
|
11
|
-
argsFn(getArgsFn: (s: IContainer) => unknown[]):
|
|
12
|
-
lazy():
|
|
10
|
+
args(...args: unknown[]): IDToken<T>;
|
|
11
|
+
argsFn(getArgsFn: (s: IContainer) => unknown[]): IDToken<T>;
|
|
12
|
+
lazy(): IDToken<T>;
|
|
13
13
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { InjectionToken } from './InjectionToken';
|
|
2
|
-
import type { IContainer
|
|
2
|
+
import type { IContainer } from '../container/IContainer';
|
|
3
|
+
import { Instance } from '../types';
|
|
3
4
|
export type InstancePredicate = (dep: unknown) => boolean;
|
|
4
5
|
export declare class InstanceListToken extends InjectionToken<Instance[]> {
|
|
5
6
|
private predicate;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { InjectFn } from '../hooks/HookContext';
|
|
2
1
|
import { DependencyKey } from '../container/IContainer';
|
|
3
|
-
import { constructor } from '../utils';
|
|
4
2
|
import { InjectionToken } from './InjectionToken';
|
|
3
|
+
import { InjectFn } from '../hooks/hook';
|
|
4
|
+
import { constructor } from '../types';
|
|
5
5
|
export declare const toToken: <T = any>(token: InjectFn<T> | InjectionToken<T> | DependencyKey | constructor<T>) => InjectionToken<T>;
|
package/typings/utils.d.ts
CHANGED
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export type constructor<T> = new (...args: any[]) => T;
|
|
3
|
-
export interface InstanceOfClass<T = unknown> {
|
|
4
|
-
new (...args: unknown[]): T;
|
|
5
|
-
}
|
|
6
|
-
export type MapFn<T> = (value: T) => T;
|
|
1
|
+
import { constructor, InstanceOfClass, MapFn } from './types';
|
|
7
2
|
export declare const pipe: <T>(...mappers: MapFn<T>[]) => MapFn<T>;
|
|
8
3
|
export declare function fillEmptyIndexes<T>(baseArr: (T | undefined)[], insertArr: T[]): T[];
|
|
9
4
|
export declare function lazyProxy<T>(resolveInstance: () => T): T;
|
|
@@ -17,5 +12,4 @@ export declare const Is: {
|
|
|
17
12
|
object: (target: unknown) => target is object;
|
|
18
13
|
instance: (target: unknown) => target is InstanceOfClass;
|
|
19
14
|
constructor: (target: unknown) => target is constructor<unknown>;
|
|
20
|
-
dependencyKey: (target: unknown) => target is DependencyKey;
|
|
21
15
|
};
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AutoMockedContainer = void 0;
|
|
4
|
-
const MethodNotImplementedError_1 = require("../errors/MethodNotImplementedError");
|
|
5
|
-
class AutoMockedContainer {
|
|
6
|
-
isDisposed = false;
|
|
7
|
-
onInstanceCreated(instance) { }
|
|
8
|
-
createScope() {
|
|
9
|
-
throw new MethodNotImplementedError_1.MethodNotImplementedError();
|
|
10
|
-
}
|
|
11
|
-
dispose() { }
|
|
12
|
-
register() {
|
|
13
|
-
return this;
|
|
14
|
-
}
|
|
15
|
-
getParent() {
|
|
16
|
-
return undefined;
|
|
17
|
-
}
|
|
18
|
-
getScopes() {
|
|
19
|
-
return [];
|
|
20
|
-
}
|
|
21
|
-
getInstances() {
|
|
22
|
-
return [];
|
|
23
|
-
}
|
|
24
|
-
hasTag(tag) {
|
|
25
|
-
return false;
|
|
26
|
-
}
|
|
27
|
-
removeScope() { }
|
|
28
|
-
useModule() {
|
|
29
|
-
return this;
|
|
30
|
-
}
|
|
31
|
-
getRegistrations() {
|
|
32
|
-
return [];
|
|
33
|
-
}
|
|
34
|
-
addRegistration(registration) {
|
|
35
|
-
return this;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
exports.AutoMockedContainer = AutoMockedContainer;
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { MethodNotImplementedError } from '../errors/MethodNotImplementedError';
|
|
2
|
-
export class AutoMockedContainer {
|
|
3
|
-
isDisposed = false;
|
|
4
|
-
onInstanceCreated(instance) { }
|
|
5
|
-
createScope() {
|
|
6
|
-
throw new MethodNotImplementedError();
|
|
7
|
-
}
|
|
8
|
-
dispose() { }
|
|
9
|
-
register() {
|
|
10
|
-
return this;
|
|
11
|
-
}
|
|
12
|
-
getParent() {
|
|
13
|
-
return undefined;
|
|
14
|
-
}
|
|
15
|
-
getScopes() {
|
|
16
|
-
return [];
|
|
17
|
-
}
|
|
18
|
-
getInstances() {
|
|
19
|
-
return [];
|
|
20
|
-
}
|
|
21
|
-
hasTag(tag) {
|
|
22
|
-
return false;
|
|
23
|
-
}
|
|
24
|
-
removeScope() { }
|
|
25
|
-
useModule() {
|
|
26
|
-
return this;
|
|
27
|
-
}
|
|
28
|
-
getRegistrations() {
|
|
29
|
-
return [];
|
|
30
|
-
}
|
|
31
|
-
addRegistration(registration) {
|
|
32
|
-
return this;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { type DependencyKey, type IContainer, type Instance, type ResolveManyOptions, ResolveOneOptions, type Tag } from './IContainer';
|
|
2
|
-
import { type IRegistration } from '../registration/IRegistration';
|
|
3
|
-
import { type constructor } from '../utils';
|
|
4
|
-
export declare abstract class AutoMockedContainer implements IContainer {
|
|
5
|
-
isDisposed: boolean;
|
|
6
|
-
onInstanceCreated(instance: Instance): void;
|
|
7
|
-
createScope(): IContainer;
|
|
8
|
-
dispose(): void;
|
|
9
|
-
register(): this;
|
|
10
|
-
getParent(): undefined;
|
|
11
|
-
getScopes(): never[];
|
|
12
|
-
getInstances(): Instance[];
|
|
13
|
-
hasTag(tag: Tag): boolean;
|
|
14
|
-
removeScope(): void;
|
|
15
|
-
useModule(): this;
|
|
16
|
-
getRegistrations(): never[];
|
|
17
|
-
addRegistration(registration: IRegistration): this;
|
|
18
|
-
abstract resolveByAlias<T>(alias: DependencyKey, options?: ResolveManyOptions): T[];
|
|
19
|
-
abstract resolve<T>(alias: constructor<T> | DependencyKey, options?: ResolveManyOptions): T;
|
|
20
|
-
abstract resolveOneByAlias<T>(alias: DependencyKey, options?: ResolveOneOptions): T;
|
|
21
|
-
}
|