ts-ioc-container 31.4.0 → 31.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 +85 -85
- package/cjm/by.js +3 -22
- package/cjm/container/AutoMockedContainer.js +6 -5
- package/cjm/container/Container.js +11 -16
- package/cjm/container/EmptyContainer.js +4 -4
- package/cjm/index.js +7 -7
- package/cjm/provider/IProvider.js +3 -1
- package/cjm/provider/Provider.js +10 -0
- package/cjm/provider/ProviderDecorator.js +7 -0
- package/cjm/registration/IRegistration.js +1 -3
- package/cjm/registration/Registration.js +1 -8
- package/esm/by.js +2 -19
- package/esm/container/AutoMockedContainer.js +6 -5
- package/esm/container/Container.js +11 -16
- package/esm/container/EmptyContainer.js +4 -4
- package/esm/index.js +2 -2
- package/esm/provider/IProvider.js +1 -0
- package/esm/provider/Provider.js +10 -0
- package/esm/provider/ProviderDecorator.js +7 -0
- package/esm/registration/IRegistration.js +0 -1
- package/esm/registration/Registration.js +1 -8
- package/package.json +2 -2
- package/typings/by.d.ts +2 -4
- package/typings/container/AutoMockedContainer.d.ts +2 -2
- package/typings/container/Container.d.ts +4 -5
- package/typings/container/EmptyContainer.d.ts +2 -2
- package/typings/container/IContainer.d.ts +3 -3
- package/typings/index.d.ts +2 -2
- package/typings/provider/IProvider.d.ts +4 -1
- package/typings/provider/Provider.d.ts +4 -1
- package/typings/provider/ProviderDecorator.d.ts +3 -1
- package/typings/registration/IRegistration.d.ts +1 -3
- package/typings/registration/Registration.d.ts +1 -3
package/README.md
CHANGED
|
@@ -33,9 +33,9 @@
|
|
|
33
33
|
- [Singleton](#singleton) `singleton`
|
|
34
34
|
- [Arguments](#arguments) `args`
|
|
35
35
|
- [Visibility](#visibility) `visible`
|
|
36
|
+
- [Alias](#alias) `alias`
|
|
36
37
|
- [Registration](#registration) `@register(key('someKey'))`
|
|
37
38
|
- [Scope](#scope) `scope`
|
|
38
|
-
- [Alias](#alias) `alias`
|
|
39
39
|
- [Module](#module)
|
|
40
40
|
- [Hook](#hook) `@hook`
|
|
41
41
|
- [OnConstruct](#onconstruct) `@onConstruct`
|
|
@@ -657,105 +657,28 @@ describe('Visibility', function () {
|
|
|
657
657
|
|
|
658
658
|
```
|
|
659
659
|
|
|
660
|
-
## Registration
|
|
661
|
-
Registration is provider factory which registers provider in container.
|
|
662
|
-
- `@register(key('logger'))`
|
|
663
|
-
- `Registration.fromClass(Logger).to('logger')`
|
|
664
|
-
- `Registration.fromClass(Logger)`
|
|
665
|
-
- `Registration.fromValue(Logger)`
|
|
666
|
-
- `Registration.fromFn((container, ...args) => container.resolve(Logger, {args}))`
|
|
667
|
-
|
|
668
|
-
```typescript
|
|
669
|
-
import 'reflect-metadata';
|
|
670
|
-
import { Container, key, MetadataInjector, provider, register, Registration as R, scope, singleton } from 'ts-ioc-container';
|
|
671
|
-
import { DependencyMissingKeyError } from '../../lib/errors/DependencyMissingKeyError';
|
|
672
|
-
|
|
673
|
-
describe('Registration module', function () {
|
|
674
|
-
const createContainer = () => new Container(new MetadataInjector(), { tags: ['root'] });
|
|
675
|
-
|
|
676
|
-
it('should register class', function () {
|
|
677
|
-
@register(key('ILogger'), scope((s) => s.hasTag('root')))
|
|
678
|
-
@provider(singleton())
|
|
679
|
-
class Logger {}
|
|
680
|
-
|
|
681
|
-
const root = createContainer().add(R.fromClass(Logger));
|
|
682
|
-
|
|
683
|
-
expect(root.resolve('ILogger')).toBeInstanceOf(Logger);
|
|
684
|
-
});
|
|
685
|
-
|
|
686
|
-
it('should register value', function () {
|
|
687
|
-
const root = createContainer().add(R.fromValue('smth').to('ISmth'));
|
|
688
|
-
|
|
689
|
-
expect(root.resolve('ISmth')).toBe('smth');
|
|
690
|
-
});
|
|
691
|
-
|
|
692
|
-
it('should register fn', function () {
|
|
693
|
-
const root = createContainer().add(R.fromFn(() => 'smth').to('ISmth'));
|
|
694
|
-
|
|
695
|
-
expect(root.resolve('ISmth')).toBe('smth');
|
|
696
|
-
});
|
|
697
|
-
|
|
698
|
-
it('should raise an error if key is not provider', () => {
|
|
699
|
-
expect(() => {
|
|
700
|
-
createContainer().add(R.fromValue('smth'));
|
|
701
|
-
}).toThrowError(DependencyMissingKeyError);
|
|
702
|
-
});
|
|
703
|
-
|
|
704
|
-
it('should register dependency by class name if @key is not provided', function () {
|
|
705
|
-
class FileLogger {}
|
|
706
|
-
|
|
707
|
-
const root = createContainer().add(R.fromClass(FileLogger));
|
|
708
|
-
|
|
709
|
-
expect(root.resolve('FileLogger')).toBeInstanceOf(FileLogger);
|
|
710
|
-
});
|
|
711
|
-
});
|
|
712
|
-
|
|
713
|
-
```
|
|
714
|
-
|
|
715
|
-
### Scope
|
|
716
|
-
Sometimes you need to register provider only in scope which matches to certain condition and their sub scopes. Especially if you want to register dependency as singleton for some tags, for example `root`
|
|
717
|
-
- `@register(scope((container) => container.hasTag('root'))` - register provider only in root scope
|
|
718
|
-
- `Registration.fromClass(Logger).when((container) => container.hasTag('root'))`
|
|
719
|
-
|
|
720
|
-
```typescript
|
|
721
|
-
import 'reflect-metadata';
|
|
722
|
-
import { singleton, Container, key, provider, MetadataInjector, Registration as R, scope, register } from 'ts-ioc-container';
|
|
723
|
-
|
|
724
|
-
@register(key('ILogger'), scope((s) => s.hasTag('root')))
|
|
725
|
-
@provider(singleton()) // the same as .pipe(singleton(), scope((s) => s.hasTag('root')))
|
|
726
|
-
class Logger {}
|
|
727
|
-
describe('ScopeProvider', function () {
|
|
728
|
-
it('should return the same instance', function () {
|
|
729
|
-
const root = new Container(new MetadataInjector(), { tags: ['root'] }).add(R.fromClass(Logger));
|
|
730
|
-
const child = root.createScope();
|
|
731
|
-
expect(root.resolve('ILogger')).toBe(child.resolve('ILogger'));
|
|
732
|
-
});
|
|
733
|
-
});
|
|
734
|
-
|
|
735
|
-
```
|
|
736
|
-
|
|
737
660
|
### Alias
|
|
738
661
|
Alias is needed to group keys
|
|
739
|
-
- `@
|
|
662
|
+
- `@provider(alias('logger'))` helper assigns `logger` alias to registration.
|
|
740
663
|
- `by.aliases((it) => it.has('logger') || it.has('a'))` resolves dependencies which have `logger` or `a` aliases
|
|
741
|
-
- `
|
|
664
|
+
- `Provider.fromClass(Logger).pipe(alias('logger'))`
|
|
742
665
|
|
|
743
666
|
```typescript
|
|
744
667
|
import 'reflect-metadata';
|
|
745
668
|
import {
|
|
669
|
+
alias,
|
|
746
670
|
by,
|
|
747
671
|
Container,
|
|
672
|
+
DependencyNotFoundError,
|
|
748
673
|
inject,
|
|
749
674
|
MetadataInjector,
|
|
675
|
+
provider,
|
|
750
676
|
Registration as R,
|
|
751
|
-
register,
|
|
752
|
-
alias,
|
|
753
|
-
DependencyNotFoundError,
|
|
754
677
|
} from 'ts-ioc-container';
|
|
755
678
|
|
|
756
679
|
describe('alias', () => {
|
|
757
680
|
const IMiddlewareKey = 'IMiddleware';
|
|
758
|
-
const middleware =
|
|
681
|
+
const middleware = provider(alias(IMiddlewareKey));
|
|
759
682
|
|
|
760
683
|
interface IMiddleware {
|
|
761
684
|
applyTo(application: IApplication): void;
|
|
@@ -816,7 +739,7 @@ describe('alias', () => {
|
|
|
816
739
|
});
|
|
817
740
|
|
|
818
741
|
it('should resolve by some alias', () => {
|
|
819
|
-
@
|
|
742
|
+
@provider(alias('ILogger'))
|
|
820
743
|
class FileLogger {}
|
|
821
744
|
|
|
822
745
|
const container = new Container(new MetadataInjector()).add(R.fromClass(FileLogger));
|
|
@@ -829,6 +752,83 @@ describe('alias', () => {
|
|
|
829
752
|
```
|
|
830
753
|
|
|
831
754
|
|
|
755
|
+
## Registration
|
|
756
|
+
Registration is provider factory which registers provider in container.
|
|
757
|
+
- `@register(key('logger'))`
|
|
758
|
+
- `Registration.fromClass(Logger).to('logger')`
|
|
759
|
+
- `Registration.fromClass(Logger)`
|
|
760
|
+
- `Registration.fromValue(Logger)`
|
|
761
|
+
- `Registration.fromFn((container, ...args) => container.resolve(Logger, {args}))`
|
|
762
|
+
|
|
763
|
+
```typescript
|
|
764
|
+
import 'reflect-metadata';
|
|
765
|
+
import { Container, key, MetadataInjector, provider, register, Registration as R, scope, singleton } from 'ts-ioc-container';
|
|
766
|
+
import { DependencyMissingKeyError } from '../../lib/errors/DependencyMissingKeyError';
|
|
767
|
+
|
|
768
|
+
describe('Registration module', function () {
|
|
769
|
+
const createContainer = () => new Container(new MetadataInjector(), { tags: ['root'] });
|
|
770
|
+
|
|
771
|
+
it('should register class', function () {
|
|
772
|
+
@register(key('ILogger'), scope((s) => s.hasTag('root')))
|
|
773
|
+
@provider(singleton())
|
|
774
|
+
class Logger {}
|
|
775
|
+
|
|
776
|
+
const root = createContainer().add(R.fromClass(Logger));
|
|
777
|
+
|
|
778
|
+
expect(root.resolve('ILogger')).toBeInstanceOf(Logger);
|
|
779
|
+
});
|
|
780
|
+
|
|
781
|
+
it('should register value', function () {
|
|
782
|
+
const root = createContainer().add(R.fromValue('smth').to('ISmth'));
|
|
783
|
+
|
|
784
|
+
expect(root.resolve('ISmth')).toBe('smth');
|
|
785
|
+
});
|
|
786
|
+
|
|
787
|
+
it('should register fn', function () {
|
|
788
|
+
const root = createContainer().add(R.fromFn(() => 'smth').to('ISmth'));
|
|
789
|
+
|
|
790
|
+
expect(root.resolve('ISmth')).toBe('smth');
|
|
791
|
+
});
|
|
792
|
+
|
|
793
|
+
it('should raise an error if key is not provider', () => {
|
|
794
|
+
expect(() => {
|
|
795
|
+
createContainer().add(R.fromValue('smth'));
|
|
796
|
+
}).toThrowError(DependencyMissingKeyError);
|
|
797
|
+
});
|
|
798
|
+
|
|
799
|
+
it('should register dependency by class name if @key is not provided', function () {
|
|
800
|
+
class FileLogger {}
|
|
801
|
+
|
|
802
|
+
const root = createContainer().add(R.fromClass(FileLogger));
|
|
803
|
+
|
|
804
|
+
expect(root.resolve('FileLogger')).toBeInstanceOf(FileLogger);
|
|
805
|
+
});
|
|
806
|
+
});
|
|
807
|
+
|
|
808
|
+
```
|
|
809
|
+
|
|
810
|
+
### Scope
|
|
811
|
+
Sometimes you need to register provider only in scope which matches to certain condition and their sub scopes. Especially if you want to register dependency as singleton for some tags, for example `root`
|
|
812
|
+
- `@register(scope((container) => container.hasTag('root'))` - register provider only in root scope
|
|
813
|
+
- `Registration.fromClass(Logger).when((container) => container.hasTag('root'))`
|
|
814
|
+
|
|
815
|
+
```typescript
|
|
816
|
+
import 'reflect-metadata';
|
|
817
|
+
import { singleton, Container, key, provider, MetadataInjector, Registration as R, scope, register } from 'ts-ioc-container';
|
|
818
|
+
|
|
819
|
+
@register(key('ILogger'), scope((s) => s.hasTag('root')))
|
|
820
|
+
@provider(singleton()) // the same as .pipe(singleton(), scope((s) => s.hasTag('root')))
|
|
821
|
+
class Logger {}
|
|
822
|
+
describe('ScopeProvider', function () {
|
|
823
|
+
it('should return the same instance', function () {
|
|
824
|
+
const root = new Container(new MetadataInjector(), { tags: ['root'] }).add(R.fromClass(Logger));
|
|
825
|
+
const child = root.createScope();
|
|
826
|
+
expect(root.resolve('ILogger')).toBe(child.resolve('ILogger'));
|
|
827
|
+
});
|
|
828
|
+
});
|
|
829
|
+
|
|
830
|
+
```
|
|
831
|
+
|
|
832
832
|
## Module
|
|
833
833
|
Sometimes you want to encapsulate registration logic in separate module. This is what `IContainerModule` is for.
|
|
834
834
|
|
package/cjm/by.js
CHANGED
|
@@ -1,35 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.by = exports.
|
|
4
|
-
const DependencyNotFoundError_1 = require("./errors/DependencyNotFoundError");
|
|
3
|
+
exports.by = exports.all = void 0;
|
|
5
4
|
const all = () => true;
|
|
6
5
|
exports.all = all;
|
|
7
|
-
const isPresent = (value) => value !== null && value !== undefined;
|
|
8
|
-
exports.isPresent = isPresent;
|
|
9
|
-
const resolveSilently = (c, ...args) => (key) => {
|
|
10
|
-
try {
|
|
11
|
-
return c.resolve(key, { args });
|
|
12
|
-
}
|
|
13
|
-
catch (e) {
|
|
14
|
-
if (e instanceof DependencyNotFoundError_1.DependencyNotFoundError) {
|
|
15
|
-
return undefined;
|
|
16
|
-
}
|
|
17
|
-
throw e;
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
exports.resolveSilently = resolveSilently;
|
|
21
6
|
exports.by = {
|
|
22
|
-
aliases: (predicate) => (c, ...args) => c
|
|
23
|
-
.getKeysByAlias(predicate)
|
|
24
|
-
.map((0, exports.resolveSilently)(c, ...args))
|
|
25
|
-
.filter(exports.isPresent),
|
|
7
|
+
aliases: (predicate) => (c, ...args) => c.resolveManyByAlias(predicate, { args }).values(),
|
|
26
8
|
/**
|
|
27
9
|
* Get the instance that matches the given alias or fail
|
|
28
10
|
* @param predicate
|
|
29
11
|
*/
|
|
30
12
|
alias: (predicate) => (c, ...args) => {
|
|
31
|
-
|
|
32
|
-
return c.resolve(key, { args });
|
|
13
|
+
return c.resolveOneByAlias(predicate, { args });
|
|
33
14
|
},
|
|
34
15
|
/**
|
|
35
16
|
* Get all instances that match the given keys
|
|
@@ -2,13 +2,11 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AutoMockedContainer = void 0;
|
|
4
4
|
const MethodNotImplementedError_1 = require("../errors/MethodNotImplementedError");
|
|
5
|
+
const DependencyNotFoundError_1 = require("../errors/DependencyNotFoundError");
|
|
5
6
|
class AutoMockedContainer {
|
|
6
7
|
constructor() {
|
|
7
8
|
this.isDisposed = false;
|
|
8
9
|
}
|
|
9
|
-
getKeysByAlias(alias) {
|
|
10
|
-
return [];
|
|
11
|
-
}
|
|
12
10
|
hasDependency(key) {
|
|
13
11
|
return false;
|
|
14
12
|
}
|
|
@@ -35,8 +33,11 @@ class AutoMockedContainer {
|
|
|
35
33
|
add(registration) {
|
|
36
34
|
return this;
|
|
37
35
|
}
|
|
38
|
-
|
|
39
|
-
|
|
36
|
+
resolveManyByAlias(predicate, options = {}, result = new Map()) {
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
39
|
+
resolveOneByAlias(predicate, options) {
|
|
40
|
+
throw new DependencyNotFoundError_1.DependencyNotFoundError(`Cannot find by alias`);
|
|
40
41
|
}
|
|
41
42
|
}
|
|
42
43
|
exports.AutoMockedContainer = AutoMockedContainer;
|
|
@@ -9,7 +9,6 @@ class Container {
|
|
|
9
9
|
this.injector = injector;
|
|
10
10
|
this.isDisposed = false;
|
|
11
11
|
this.providers = new Map();
|
|
12
|
-
this.aliases = new Map();
|
|
13
12
|
this.scopes = new Set();
|
|
14
13
|
this.instances = new Set();
|
|
15
14
|
this.registrations = [];
|
|
@@ -21,12 +20,9 @@ class Container {
|
|
|
21
20
|
registration.applyTo(this);
|
|
22
21
|
return this;
|
|
23
22
|
}
|
|
24
|
-
register(key, provider
|
|
23
|
+
register(key, provider) {
|
|
25
24
|
this.validateContainer();
|
|
26
25
|
this.providers.set(key, provider);
|
|
27
|
-
if (aliases && aliases.length > 0) {
|
|
28
|
-
this.aliases.set(key, new Set(aliases));
|
|
29
|
-
}
|
|
30
26
|
return this;
|
|
31
27
|
}
|
|
32
28
|
resolve(token, { args = [], child = this } = {}) {
|
|
@@ -77,22 +73,21 @@ class Container {
|
|
|
77
73
|
hasDependency(key) {
|
|
78
74
|
return this.providers.has(key) ?? this.parent.hasDependency(key);
|
|
79
75
|
}
|
|
80
|
-
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
result.add(key);
|
|
76
|
+
resolveManyByAlias(predicate, { args = [], child = this } = {}, result = new Map()) {
|
|
77
|
+
for (const [key, provider] of this.providers.entries()) {
|
|
78
|
+
if (!result.has(key) && provider.matchAliases(predicate) && provider.isVisible(this, child)) {
|
|
79
|
+
result.set(key, provider.resolve(this, ...args));
|
|
85
80
|
}
|
|
86
81
|
}
|
|
87
|
-
return
|
|
82
|
+
return this.parent.resolveManyByAlias(predicate, { args, child }, result);
|
|
88
83
|
}
|
|
89
|
-
|
|
90
|
-
for (const [
|
|
91
|
-
if (predicate(
|
|
92
|
-
return
|
|
84
|
+
resolveOneByAlias(predicate, { args = [], child = this } = {}) {
|
|
85
|
+
for (const [, provider] of this.providers.entries()) {
|
|
86
|
+
if (provider.matchAliases(predicate) && provider.isVisible(this, child)) {
|
|
87
|
+
return provider.resolve(this, ...args);
|
|
93
88
|
}
|
|
94
89
|
}
|
|
95
|
-
return this.parent.
|
|
90
|
+
return this.parent.resolveOneByAlias(predicate, { args, child });
|
|
96
91
|
}
|
|
97
92
|
/**
|
|
98
93
|
* @private
|
|
@@ -7,9 +7,6 @@ class EmptyContainer {
|
|
|
7
7
|
constructor() {
|
|
8
8
|
this.isDisposed = false;
|
|
9
9
|
}
|
|
10
|
-
getKeysByAlias(alias) {
|
|
11
|
-
return [];
|
|
12
|
-
}
|
|
13
10
|
hasDependency(key) {
|
|
14
11
|
return false;
|
|
15
12
|
}
|
|
@@ -41,7 +38,10 @@ class EmptyContainer {
|
|
|
41
38
|
add(registration) {
|
|
42
39
|
return this;
|
|
43
40
|
}
|
|
44
|
-
|
|
41
|
+
resolveManyByAlias(predicate, options = {}, result = new Map()) {
|
|
42
|
+
return result;
|
|
43
|
+
}
|
|
44
|
+
resolveOneByAlias(predicate, options) {
|
|
45
45
|
throw new DependencyNotFoundError_1.DependencyNotFoundError(`Cannot find by alias`);
|
|
46
46
|
}
|
|
47
47
|
}
|
package/cjm/index.js
CHANGED
|
@@ -1,21 +1,22 @@
|
|
|
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.by = exports.hasHooks = exports.hook = exports.getHooks = exports.ProxyInjector = exports.SimpleInjector = exports.inject = exports.MetadataInjector = exports.
|
|
3
|
+
exports.getParameterMetadata = exports.getMethodMetadata = exports.setMethodMetadata = exports.setParameterMetadata = exports.getMetadata = exports.setMetadata = exports.by = exports.hasHooks = exports.hook = exports.getHooks = exports.ProxyInjector = exports.SimpleInjector = exports.inject = exports.MetadataInjector = exports.Registration = exports.register = exports.scope = exports.key = exports.AutoMockedContainer = exports.MultiCache = exports.SingletonProvider = exports.singleton = exports.ArgsProvider = exports.args = exports.argsFn = exports.ProviderDecorator = exports.Provider = exports.alias = exports.visible = exports.provider = exports.ContainerDisposedError = exports.MethodNotImplementedError = exports.DependencyNotFoundError = exports.EmptyContainer = exports.Container = exports.isDependencyKey = void 0;
|
|
4
4
|
var IContainer_1 = require("./container/IContainer");
|
|
5
5
|
Object.defineProperty(exports, "isDependencyKey", { enumerable: true, get: function () { return IContainer_1.isDependencyKey; } });
|
|
6
6
|
var Container_1 = require("./container/Container");
|
|
7
7
|
Object.defineProperty(exports, "Container", { enumerable: true, get: function () { return Container_1.Container; } });
|
|
8
8
|
var EmptyContainer_1 = require("./container/EmptyContainer");
|
|
9
9
|
Object.defineProperty(exports, "EmptyContainer", { enumerable: true, get: function () { return EmptyContainer_1.EmptyContainer; } });
|
|
10
|
-
var IProvider_1 = require("./provider/IProvider");
|
|
11
|
-
Object.defineProperty(exports, "provider", { enumerable: true, get: function () { return IProvider_1.provider; } });
|
|
12
|
-
Object.defineProperty(exports, "visible", { enumerable: true, get: function () { return IProvider_1.visible; } });
|
|
13
10
|
var DependencyNotFoundError_1 = require("./errors/DependencyNotFoundError");
|
|
14
11
|
Object.defineProperty(exports, "DependencyNotFoundError", { enumerable: true, get: function () { return DependencyNotFoundError_1.DependencyNotFoundError; } });
|
|
15
12
|
var MethodNotImplementedError_1 = require("./errors/MethodNotImplementedError");
|
|
16
13
|
Object.defineProperty(exports, "MethodNotImplementedError", { enumerable: true, get: function () { return MethodNotImplementedError_1.MethodNotImplementedError; } });
|
|
17
14
|
var ContainerDisposedError_1 = require("./errors/ContainerDisposedError");
|
|
18
15
|
Object.defineProperty(exports, "ContainerDisposedError", { enumerable: true, get: function () { return ContainerDisposedError_1.ContainerDisposedError; } });
|
|
16
|
+
var IProvider_1 = require("./provider/IProvider");
|
|
17
|
+
Object.defineProperty(exports, "provider", { enumerable: true, get: function () { return IProvider_1.provider; } });
|
|
18
|
+
Object.defineProperty(exports, "visible", { enumerable: true, get: function () { return IProvider_1.visible; } });
|
|
19
|
+
Object.defineProperty(exports, "alias", { enumerable: true, get: function () { return IProvider_1.alias; } });
|
|
19
20
|
var Provider_1 = require("./provider/Provider");
|
|
20
21
|
Object.defineProperty(exports, "Provider", { enumerable: true, get: function () { return Provider_1.Provider; } });
|
|
21
22
|
var ProviderDecorator_1 = require("./provider/ProviderDecorator");
|
|
@@ -31,13 +32,12 @@ var MultiCache_1 = require("./provider/singleton/MultiCache");
|
|
|
31
32
|
Object.defineProperty(exports, "MultiCache", { enumerable: true, get: function () { return MultiCache_1.MultiCache; } });
|
|
32
33
|
var AutoMockedContainer_1 = require("./container/AutoMockedContainer");
|
|
33
34
|
Object.defineProperty(exports, "AutoMockedContainer", { enumerable: true, get: function () { return AutoMockedContainer_1.AutoMockedContainer; } });
|
|
34
|
-
var Registration_1 = require("./registration/Registration");
|
|
35
|
-
Object.defineProperty(exports, "Registration", { enumerable: true, get: function () { return Registration_1.Registration; } });
|
|
36
35
|
var IRegistration_1 = require("./registration/IRegistration");
|
|
37
36
|
Object.defineProperty(exports, "key", { enumerable: true, get: function () { return IRegistration_1.key; } });
|
|
38
|
-
Object.defineProperty(exports, "alias", { enumerable: true, get: function () { return IRegistration_1.alias; } });
|
|
39
37
|
Object.defineProperty(exports, "scope", { enumerable: true, get: function () { return IRegistration_1.scope; } });
|
|
40
38
|
Object.defineProperty(exports, "register", { enumerable: true, get: function () { return IRegistration_1.register; } });
|
|
39
|
+
var Registration_1 = require("./registration/Registration");
|
|
40
|
+
Object.defineProperty(exports, "Registration", { enumerable: true, get: function () { return Registration_1.Registration; } });
|
|
41
41
|
var MetadataInjector_1 = require("./injector/MetadataInjector");
|
|
42
42
|
Object.defineProperty(exports, "MetadataInjector", { enumerable: true, get: function () { return MetadataInjector_1.MetadataInjector; } });
|
|
43
43
|
Object.defineProperty(exports, "inject", { enumerable: true, get: function () { return MetadataInjector_1.inject; } });
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.visible = exports.getTransformers = exports.provider = void 0;
|
|
3
|
+
exports.visible = exports.alias = exports.getTransformers = exports.provider = void 0;
|
|
4
4
|
const metadata_1 = require("../metadata");
|
|
5
5
|
const METADATA_KEY = 'provider';
|
|
6
6
|
const provider = (...mappers) => (0, metadata_1.setMetadata)(METADATA_KEY, mappers);
|
|
7
7
|
exports.provider = provider;
|
|
8
8
|
const getTransformers = (Target) => (0, metadata_1.getMetadata)(Target, METADATA_KEY) ?? [];
|
|
9
9
|
exports.getTransformers = getTransformers;
|
|
10
|
+
const alias = (...aliases) => (r) => r.addAliases(...aliases);
|
|
11
|
+
exports.alias = alias;
|
|
10
12
|
const visible = (isVisibleWhen) => (p) => p.setVisibility(isVisibleWhen);
|
|
11
13
|
exports.visible = visible;
|
package/cjm/provider/Provider.js
CHANGED
|
@@ -15,6 +15,7 @@ class Provider {
|
|
|
15
15
|
constructor(resolveDependency, isVisibleWhen = () => true) {
|
|
16
16
|
this.resolveDependency = resolveDependency;
|
|
17
17
|
this.isVisibleWhen = isVisibleWhen;
|
|
18
|
+
this.aliases = new Set();
|
|
18
19
|
}
|
|
19
20
|
pipe(...mappers) {
|
|
20
21
|
return (0, utils_1.pipe)(...mappers)(this);
|
|
@@ -29,5 +30,14 @@ class Provider {
|
|
|
29
30
|
isVisible(parent, child) {
|
|
30
31
|
return this.isVisibleWhen({ child, isParent: child === parent });
|
|
31
32
|
}
|
|
33
|
+
matchAliases(predicate) {
|
|
34
|
+
return this.aliases.size > 0 && predicate(this.aliases);
|
|
35
|
+
}
|
|
36
|
+
addAliases(...aliases) {
|
|
37
|
+
for (const alias of aliases) {
|
|
38
|
+
this.aliases.add(alias);
|
|
39
|
+
}
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
32
42
|
}
|
|
33
43
|
exports.Provider = Provider;
|
|
@@ -19,5 +19,12 @@ class ProviderDecorator {
|
|
|
19
19
|
pipe(...mappers) {
|
|
20
20
|
return (0, utils_1.pipe)(...mappers)(this);
|
|
21
21
|
}
|
|
22
|
+
matchAliases(predicate) {
|
|
23
|
+
return this.decorated.matchAliases(predicate);
|
|
24
|
+
}
|
|
25
|
+
addAliases(...aliases) {
|
|
26
|
+
this.decorated.addAliases(...aliases);
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
22
29
|
}
|
|
23
30
|
exports.ProviderDecorator = ProviderDecorator;
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.register = exports.getTransformers = exports.scope = exports.
|
|
3
|
+
exports.register = exports.getTransformers = exports.scope = exports.key = void 0;
|
|
4
4
|
const metadata_1 = require("../metadata");
|
|
5
5
|
const key = (key) => (r) => r.to(key);
|
|
6
6
|
exports.key = key;
|
|
7
|
-
const alias = (...aliases) => (r) => r.addAliases(...aliases);
|
|
8
|
-
exports.alias = alias;
|
|
9
7
|
const scope = (predicate) => (r) => r.when(predicate);
|
|
10
8
|
exports.scope = scope;
|
|
11
9
|
const METADATA_KEY = 'registration';
|
|
@@ -24,19 +24,12 @@ class Registration {
|
|
|
24
24
|
this.createProvider = createProvider;
|
|
25
25
|
this.key = key;
|
|
26
26
|
this.matchScope = matchScope;
|
|
27
|
-
this.aliases = [];
|
|
28
27
|
this.mappers = [];
|
|
29
28
|
}
|
|
30
29
|
to(key) {
|
|
31
30
|
this.key = key;
|
|
32
31
|
return this;
|
|
33
32
|
}
|
|
34
|
-
addAliases(...aliases) {
|
|
35
|
-
for (const alias of aliases) {
|
|
36
|
-
this.aliases.push(alias);
|
|
37
|
-
}
|
|
38
|
-
return this;
|
|
39
|
-
}
|
|
40
33
|
pipe(...mappers) {
|
|
41
34
|
this.mappers.push(...mappers);
|
|
42
35
|
return this;
|
|
@@ -48,7 +41,7 @@ class Registration {
|
|
|
48
41
|
if (!this.key) {
|
|
49
42
|
throw new DependencyMissingKeyError_1.DependencyMissingKeyError('No key provided for registration');
|
|
50
43
|
}
|
|
51
|
-
container.register(this.key, this.createProvider().pipe(...this.mappers)
|
|
44
|
+
container.register(this.key, this.createProvider().pipe(...this.mappers));
|
|
52
45
|
}
|
|
53
46
|
when(isValidWhen) {
|
|
54
47
|
this.matchScope = isValidWhen;
|
package/esm/by.js
CHANGED
|
@@ -1,29 +1,12 @@
|
|
|
1
|
-
import { DependencyNotFoundError } from './errors/DependencyNotFoundError';
|
|
2
1
|
export const all = () => true;
|
|
3
|
-
export const isPresent = (value) => value !== null && value !== undefined;
|
|
4
|
-
export const resolveSilently = (c, ...args) => (key) => {
|
|
5
|
-
try {
|
|
6
|
-
return c.resolve(key, { args });
|
|
7
|
-
}
|
|
8
|
-
catch (e) {
|
|
9
|
-
if (e instanceof DependencyNotFoundError) {
|
|
10
|
-
return undefined;
|
|
11
|
-
}
|
|
12
|
-
throw e;
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
2
|
export const by = {
|
|
16
|
-
aliases: (predicate) => (c, ...args) => c
|
|
17
|
-
.getKeysByAlias(predicate)
|
|
18
|
-
.map(resolveSilently(c, ...args))
|
|
19
|
-
.filter(isPresent),
|
|
3
|
+
aliases: (predicate) => (c, ...args) => c.resolveManyByAlias(predicate, { args }).values(),
|
|
20
4
|
/**
|
|
21
5
|
* Get the instance that matches the given alias or fail
|
|
22
6
|
* @param predicate
|
|
23
7
|
*/
|
|
24
8
|
alias: (predicate) => (c, ...args) => {
|
|
25
|
-
|
|
26
|
-
return c.resolve(key, { args });
|
|
9
|
+
return c.resolveOneByAlias(predicate, { args });
|
|
27
10
|
},
|
|
28
11
|
/**
|
|
29
12
|
* Get all instances that match the given keys
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import { MethodNotImplementedError } from '../errors/MethodNotImplementedError';
|
|
2
|
+
import { DependencyNotFoundError } from '../errors/DependencyNotFoundError';
|
|
2
3
|
export class AutoMockedContainer {
|
|
3
4
|
constructor() {
|
|
4
5
|
this.isDisposed = false;
|
|
5
6
|
}
|
|
6
|
-
getKeysByAlias(alias) {
|
|
7
|
-
return [];
|
|
8
|
-
}
|
|
9
7
|
hasDependency(key) {
|
|
10
8
|
return false;
|
|
11
9
|
}
|
|
@@ -32,7 +30,10 @@ export class AutoMockedContainer {
|
|
|
32
30
|
add(registration) {
|
|
33
31
|
return this;
|
|
34
32
|
}
|
|
35
|
-
|
|
36
|
-
|
|
33
|
+
resolveManyByAlias(predicate, options = {}, result = new Map()) {
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
resolveOneByAlias(predicate, options) {
|
|
37
|
+
throw new DependencyNotFoundError(`Cannot find by alias`);
|
|
37
38
|
}
|
|
38
39
|
}
|
|
@@ -6,7 +6,6 @@ export class Container {
|
|
|
6
6
|
this.injector = injector;
|
|
7
7
|
this.isDisposed = false;
|
|
8
8
|
this.providers = new Map();
|
|
9
|
-
this.aliases = new Map();
|
|
10
9
|
this.scopes = new Set();
|
|
11
10
|
this.instances = new Set();
|
|
12
11
|
this.registrations = [];
|
|
@@ -18,12 +17,9 @@ export class Container {
|
|
|
18
17
|
registration.applyTo(this);
|
|
19
18
|
return this;
|
|
20
19
|
}
|
|
21
|
-
register(key, provider
|
|
20
|
+
register(key, provider) {
|
|
22
21
|
this.validateContainer();
|
|
23
22
|
this.providers.set(key, provider);
|
|
24
|
-
if (aliases && aliases.length > 0) {
|
|
25
|
-
this.aliases.set(key, new Set(aliases));
|
|
26
|
-
}
|
|
27
23
|
return this;
|
|
28
24
|
}
|
|
29
25
|
resolve(token, { args = [], child = this } = {}) {
|
|
@@ -74,22 +70,21 @@ export class Container {
|
|
|
74
70
|
hasDependency(key) {
|
|
75
71
|
return this.providers.has(key) ?? this.parent.hasDependency(key);
|
|
76
72
|
}
|
|
77
|
-
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
result.add(key);
|
|
73
|
+
resolveManyByAlias(predicate, { args = [], child = this } = {}, result = new Map()) {
|
|
74
|
+
for (const [key, provider] of this.providers.entries()) {
|
|
75
|
+
if (!result.has(key) && provider.matchAliases(predicate) && provider.isVisible(this, child)) {
|
|
76
|
+
result.set(key, provider.resolve(this, ...args));
|
|
82
77
|
}
|
|
83
78
|
}
|
|
84
|
-
return
|
|
79
|
+
return this.parent.resolveManyByAlias(predicate, { args, child }, result);
|
|
85
80
|
}
|
|
86
|
-
|
|
87
|
-
for (const [
|
|
88
|
-
if (predicate(
|
|
89
|
-
return
|
|
81
|
+
resolveOneByAlias(predicate, { args = [], child = this } = {}) {
|
|
82
|
+
for (const [, provider] of this.providers.entries()) {
|
|
83
|
+
if (provider.matchAliases(predicate) && provider.isVisible(this, child)) {
|
|
84
|
+
return provider.resolve(this, ...args);
|
|
90
85
|
}
|
|
91
86
|
}
|
|
92
|
-
return this.parent.
|
|
87
|
+
return this.parent.resolveOneByAlias(predicate, { args, child });
|
|
93
88
|
}
|
|
94
89
|
/**
|
|
95
90
|
* @private
|
|
@@ -4,9 +4,6 @@ export class EmptyContainer {
|
|
|
4
4
|
constructor() {
|
|
5
5
|
this.isDisposed = false;
|
|
6
6
|
}
|
|
7
|
-
getKeysByAlias(alias) {
|
|
8
|
-
return [];
|
|
9
|
-
}
|
|
10
7
|
hasDependency(key) {
|
|
11
8
|
return false;
|
|
12
9
|
}
|
|
@@ -38,7 +35,10 @@ export class EmptyContainer {
|
|
|
38
35
|
add(registration) {
|
|
39
36
|
return this;
|
|
40
37
|
}
|
|
41
|
-
|
|
38
|
+
resolveManyByAlias(predicate, options = {}, result = new Map()) {
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
resolveOneByAlias(predicate, options) {
|
|
42
42
|
throw new DependencyNotFoundError(`Cannot find by alias`);
|
|
43
43
|
}
|
|
44
44
|
}
|
package/esm/index.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
export { isDependencyKey, } from './container/IContainer';
|
|
2
2
|
export { Container } from './container/Container';
|
|
3
3
|
export { EmptyContainer } from './container/EmptyContainer';
|
|
4
|
-
export { provider, visible } from './provider/IProvider';
|
|
5
4
|
export { DependencyNotFoundError } from './errors/DependencyNotFoundError';
|
|
6
5
|
export { MethodNotImplementedError } from './errors/MethodNotImplementedError';
|
|
7
6
|
export { ContainerDisposedError } from './errors/ContainerDisposedError';
|
|
7
|
+
export { provider, visible, alias } from './provider/IProvider';
|
|
8
8
|
export { Provider } from './provider/Provider';
|
|
9
9
|
export { ProviderDecorator } from './provider/ProviderDecorator';
|
|
10
10
|
export { argsFn, args, ArgsProvider } from './provider/ArgsProvider';
|
|
11
11
|
export { singleton, SingletonProvider } from './provider/singleton/SingletonProvider';
|
|
12
12
|
export { MultiCache } from './provider/singleton/MultiCache';
|
|
13
13
|
export { AutoMockedContainer } from './container/AutoMockedContainer';
|
|
14
|
+
export { key, scope, register } from './registration/IRegistration';
|
|
14
15
|
export { Registration } from './registration/Registration';
|
|
15
|
-
export { key, alias, scope, register } from './registration/IRegistration';
|
|
16
16
|
export { MetadataInjector, inject } from './injector/MetadataInjector';
|
|
17
17
|
export { SimpleInjector } from './injector/SimpleInjector';
|
|
18
18
|
export { ProxyInjector } from './injector/ProxyInjector';
|
|
@@ -2,4 +2,5 @@ import { getMetadata, setMetadata } from '../metadata';
|
|
|
2
2
|
const METADATA_KEY = 'provider';
|
|
3
3
|
export const provider = (...mappers) => setMetadata(METADATA_KEY, mappers);
|
|
4
4
|
export const getTransformers = (Target) => getMetadata(Target, METADATA_KEY) ?? [];
|
|
5
|
+
export const alias = (...aliases) => (r) => r.addAliases(...aliases);
|
|
5
6
|
export const visible = (isVisibleWhen) => (p) => p.setVisibility(isVisibleWhen);
|
package/esm/provider/Provider.js
CHANGED
|
@@ -12,6 +12,7 @@ export class Provider {
|
|
|
12
12
|
constructor(resolveDependency, isVisibleWhen = () => true) {
|
|
13
13
|
this.resolveDependency = resolveDependency;
|
|
14
14
|
this.isVisibleWhen = isVisibleWhen;
|
|
15
|
+
this.aliases = new Set();
|
|
15
16
|
}
|
|
16
17
|
pipe(...mappers) {
|
|
17
18
|
return pipe(...mappers)(this);
|
|
@@ -26,4 +27,13 @@ export class Provider {
|
|
|
26
27
|
isVisible(parent, child) {
|
|
27
28
|
return this.isVisibleWhen({ child, isParent: child === parent });
|
|
28
29
|
}
|
|
30
|
+
matchAliases(predicate) {
|
|
31
|
+
return this.aliases.size > 0 && predicate(this.aliases);
|
|
32
|
+
}
|
|
33
|
+
addAliases(...aliases) {
|
|
34
|
+
for (const alias of aliases) {
|
|
35
|
+
this.aliases.add(alias);
|
|
36
|
+
}
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
29
39
|
}
|
|
@@ -16,4 +16,11 @@ export class ProviderDecorator {
|
|
|
16
16
|
pipe(...mappers) {
|
|
17
17
|
return pipe(...mappers)(this);
|
|
18
18
|
}
|
|
19
|
+
matchAliases(predicate) {
|
|
20
|
+
return this.decorated.matchAliases(predicate);
|
|
21
|
+
}
|
|
22
|
+
addAliases(...aliases) {
|
|
23
|
+
this.decorated.addAliases(...aliases);
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
19
26
|
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { getMetadata, setMetadata } from '../metadata';
|
|
2
2
|
export const key = (key) => (r) => r.to(key);
|
|
3
|
-
export const alias = (...aliases) => (r) => r.addAliases(...aliases);
|
|
4
3
|
export const scope = (predicate) => (r) => r.when(predicate);
|
|
5
4
|
const METADATA_KEY = 'registration';
|
|
6
5
|
export const getTransformers = (Target) => getMetadata(Target, METADATA_KEY) ?? [];
|
|
@@ -21,19 +21,12 @@ export class Registration {
|
|
|
21
21
|
this.createProvider = createProvider;
|
|
22
22
|
this.key = key;
|
|
23
23
|
this.matchScope = matchScope;
|
|
24
|
-
this.aliases = [];
|
|
25
24
|
this.mappers = [];
|
|
26
25
|
}
|
|
27
26
|
to(key) {
|
|
28
27
|
this.key = key;
|
|
29
28
|
return this;
|
|
30
29
|
}
|
|
31
|
-
addAliases(...aliases) {
|
|
32
|
-
for (const alias of aliases) {
|
|
33
|
-
this.aliases.push(alias);
|
|
34
|
-
}
|
|
35
|
-
return this;
|
|
36
|
-
}
|
|
37
30
|
pipe(...mappers) {
|
|
38
31
|
this.mappers.push(...mappers);
|
|
39
32
|
return this;
|
|
@@ -45,7 +38,7 @@ export class Registration {
|
|
|
45
38
|
if (!this.key) {
|
|
46
39
|
throw new DependencyMissingKeyError('No key provided for registration');
|
|
47
40
|
}
|
|
48
|
-
container.register(this.key, this.createProvider().pipe(...this.mappers)
|
|
41
|
+
container.register(this.key, this.createProvider().pipe(...this.mappers));
|
|
49
42
|
}
|
|
50
43
|
when(isValidWhen) {
|
|
51
44
|
this.matchScope = isValidWhen;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-ioc-container",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.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": "
|
|
62
|
+
"gitHead": "85beba059c9dada4836d784ab8f7ffec08299308"
|
|
63
63
|
}
|
package/typings/by.d.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import { AliasPredicate,
|
|
1
|
+
import { AliasPredicate, IContainer, InjectionToken } from './container/IContainer';
|
|
2
2
|
export type InstancePredicate = (dep: unknown) => boolean;
|
|
3
3
|
export declare const all: InstancePredicate;
|
|
4
|
-
export declare const isPresent: <T>(value: T | null | undefined) => value is T;
|
|
5
|
-
export declare const resolveSilently: (c: IContainer, ...args: unknown[]) => (key: DependencyKey) => unknown;
|
|
6
4
|
export declare const by: {
|
|
7
|
-
aliases: (predicate: AliasPredicate) => (c: IContainer, ...args: unknown[]) => unknown
|
|
5
|
+
aliases: (predicate: AliasPredicate) => (c: IContainer, ...args: unknown[]) => IterableIterator<unknown>;
|
|
8
6
|
/**
|
|
9
7
|
* Get the instance that matches the given alias or fail
|
|
10
8
|
* @param predicate
|
|
@@ -2,7 +2,6 @@ import { AliasPredicate, DependencyKey, IContainer, InjectionToken, ResolveOptio
|
|
|
2
2
|
import { IRegistration } from '../registration/IRegistration';
|
|
3
3
|
export declare abstract class AutoMockedContainer implements IContainer {
|
|
4
4
|
isDisposed: boolean;
|
|
5
|
-
getKeysByAlias(alias: AliasPredicate): DependencyKey[];
|
|
6
5
|
hasDependency(key: string): boolean;
|
|
7
6
|
createScope(): IContainer;
|
|
8
7
|
abstract resolve<T>(key: InjectionToken<T>, options?: ResolveOptions): T;
|
|
@@ -14,5 +13,6 @@ export declare abstract class AutoMockedContainer implements IContainer {
|
|
|
14
13
|
getRegistrations(): never[];
|
|
15
14
|
hasTag(): boolean;
|
|
16
15
|
add(registration: IRegistration): this;
|
|
17
|
-
|
|
16
|
+
resolveManyByAlias(predicate: AliasPredicate, options?: ResolveOptions, result?: Map<DependencyKey, unknown>): Map<DependencyKey, unknown>;
|
|
17
|
+
resolveOneByAlias<T>(predicate: AliasPredicate, options?: ResolveOptions): T;
|
|
18
18
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AliasPredicate, DependencyKey, IContainer, IContainerModule, InjectionToken, ResolveOptions, Tag } from './IContainer';
|
|
2
2
|
import { IInjector } from '../injector/IInjector';
|
|
3
3
|
import { IProvider } from '../provider/IProvider';
|
|
4
4
|
import { IRegistration } from '../registration/IRegistration';
|
|
@@ -6,7 +6,6 @@ export declare class Container implements IContainer {
|
|
|
6
6
|
private readonly injector;
|
|
7
7
|
isDisposed: boolean;
|
|
8
8
|
private readonly providers;
|
|
9
|
-
private readonly aliases;
|
|
10
9
|
private tags;
|
|
11
10
|
private parent;
|
|
12
11
|
private scopes;
|
|
@@ -17,7 +16,7 @@ export declare class Container implements IContainer {
|
|
|
17
16
|
tags?: Tag[];
|
|
18
17
|
});
|
|
19
18
|
add(registration: IRegistration): this;
|
|
20
|
-
register(key: DependencyKey, provider: IProvider
|
|
19
|
+
register(key: DependencyKey, provider: IProvider): this;
|
|
21
20
|
resolve<T>(token: InjectionToken<T>, { args, child }?: ResolveOptions): T;
|
|
22
21
|
createScope(...tags: Tag[]): Container;
|
|
23
22
|
dispose(): void;
|
|
@@ -25,8 +24,8 @@ export declare class Container implements IContainer {
|
|
|
25
24
|
hasTag(tag: Tag): boolean;
|
|
26
25
|
use(module: IContainerModule): this;
|
|
27
26
|
hasDependency(key: DependencyKey): boolean;
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
resolveManyByAlias(predicate: AliasPredicate, { args, child }?: ResolveOptions, result?: Map<DependencyKey, unknown>): Map<DependencyKey, unknown>;
|
|
28
|
+
resolveOneByAlias<T>(predicate: AliasPredicate, { args, child }?: ResolveOptions): T;
|
|
30
29
|
/**
|
|
31
30
|
* @private
|
|
32
31
|
*/
|
|
@@ -3,7 +3,6 @@ import { IProvider } from '../provider/IProvider';
|
|
|
3
3
|
import { IRegistration } from '../registration/IRegistration';
|
|
4
4
|
export declare class EmptyContainer implements IContainer {
|
|
5
5
|
isDisposed: boolean;
|
|
6
|
-
getKeysByAlias(alias: AliasPredicate): DependencyKey[];
|
|
7
6
|
hasDependency(key: string): boolean;
|
|
8
7
|
hasTag(): boolean;
|
|
9
8
|
createScope(): IContainer;
|
|
@@ -15,5 +14,6 @@ export declare class EmptyContainer implements IContainer {
|
|
|
15
14
|
removeScope(): void;
|
|
16
15
|
use(module: IContainerModule): this;
|
|
17
16
|
add(registration: IRegistration): this;
|
|
18
|
-
|
|
17
|
+
resolveManyByAlias(predicate: AliasPredicate, options?: ResolveOptions, result?: Map<DependencyKey, unknown>): Map<DependencyKey, unknown>;
|
|
18
|
+
resolveOneByAlias<T>(predicate: AliasPredicate, options?: ResolveOptions): T;
|
|
19
19
|
}
|
|
@@ -24,7 +24,7 @@ export type AliasPredicate = (aliases: Set<Alias>) => boolean;
|
|
|
24
24
|
export interface IContainer extends Resolvable, Tagged {
|
|
25
25
|
readonly isDisposed: boolean;
|
|
26
26
|
createScope(...tags: Tag[]): IContainer;
|
|
27
|
-
register(key: DependencyKey, value: IProvider
|
|
27
|
+
register(key: DependencyKey, value: IProvider): this;
|
|
28
28
|
add(registration: IRegistration): this;
|
|
29
29
|
removeScope(child: IContainer): void;
|
|
30
30
|
getInstances(): unknown[];
|
|
@@ -32,6 +32,6 @@ export interface IContainer extends Resolvable, Tagged {
|
|
|
32
32
|
use(module: IContainerModule): this;
|
|
33
33
|
getRegistrations(): IRegistration[];
|
|
34
34
|
hasDependency(key: DependencyKey): boolean;
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
resolveManyByAlias(predicate: AliasPredicate, options?: ResolveOptions, result?: Map<DependencyKey, unknown>): Map<DependencyKey, unknown>;
|
|
36
|
+
resolveOneByAlias<T>(predicate: AliasPredicate, options?: ResolveOptions): T;
|
|
37
37
|
}
|
package/typings/index.d.ts
CHANGED
|
@@ -2,11 +2,11 @@ export { IContainer, Resolvable, IContainerModule, isDependencyKey, DependencyKe
|
|
|
2
2
|
export { constructor } from './utils';
|
|
3
3
|
export { Container } from './container/Container';
|
|
4
4
|
export { EmptyContainer } from './container/EmptyContainer';
|
|
5
|
-
export { ResolveDependency, IProvider, provider, visible } from './provider/IProvider';
|
|
6
5
|
export { IInjector } from './injector/IInjector';
|
|
7
6
|
export { DependencyNotFoundError } from './errors/DependencyNotFoundError';
|
|
8
7
|
export { MethodNotImplementedError } from './errors/MethodNotImplementedError';
|
|
9
8
|
export { ContainerDisposedError } from './errors/ContainerDisposedError';
|
|
9
|
+
export { ResolveDependency, IProvider, provider, visible, alias } from './provider/IProvider';
|
|
10
10
|
export { Provider } from './provider/Provider';
|
|
11
11
|
export { ProviderDecorator } from './provider/ProviderDecorator';
|
|
12
12
|
export { ArgsFn, argsFn, args, ArgsProvider } from './provider/ArgsProvider';
|
|
@@ -14,8 +14,8 @@ export { singleton, SingletonProvider } from './provider/singleton/SingletonProv
|
|
|
14
14
|
export { MultiCache } from './provider/singleton/MultiCache';
|
|
15
15
|
export { Cache } from './provider/singleton/Cache';
|
|
16
16
|
export { AutoMockedContainer } from './container/AutoMockedContainer';
|
|
17
|
+
export { key, IRegistration, scope, register } from './registration/IRegistration';
|
|
17
18
|
export { Registration } from './registration/Registration';
|
|
18
|
-
export { key, alias, IRegistration, scope, register } from './registration/IRegistration';
|
|
19
19
|
export { MetadataInjector, inject } from './injector/MetadataInjector';
|
|
20
20
|
export { SimpleInjector } from './injector/SimpleInjector';
|
|
21
21
|
export { ProxyInjector } from './injector/ProxyInjector';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IContainer, Tagged } from '../container/IContainer';
|
|
1
|
+
import { Alias, AliasPredicate, IContainer, Tagged } from '../container/IContainer';
|
|
2
2
|
import { constructor, MapFn } from '../utils';
|
|
3
3
|
export type ResolveDependency<T = unknown> = (container: IContainer, ...args: unknown[]) => T;
|
|
4
4
|
export type ChildrenVisibilityPredicate = (options: {
|
|
@@ -10,7 +10,10 @@ export interface IProvider<T = unknown> {
|
|
|
10
10
|
isVisible(parent: Tagged, child: Tagged): boolean;
|
|
11
11
|
pipe(...mappers: MapFn<IProvider<T>>[]): IProvider<T>;
|
|
12
12
|
setVisibility(isVisibleWhen: ChildrenVisibilityPredicate): this;
|
|
13
|
+
matchAliases(predicate: AliasPredicate): boolean;
|
|
14
|
+
addAliases(...aliases: Alias[]): this;
|
|
13
15
|
}
|
|
14
16
|
export declare const provider: (...mappers: MapFn<IProvider>[]) => ClassDecorator;
|
|
15
17
|
export declare const getTransformers: <T>(Target: constructor<T>) => MapFn<IProvider<T>>[];
|
|
18
|
+
export declare const alias: (...aliases: Alias[]) => MapFn<IProvider>;
|
|
16
19
|
export declare const visible: (isVisibleWhen: ChildrenVisibilityPredicate) => MapFn<IProvider>;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { ChildrenVisibilityPredicate, IProvider, ResolveDependency } from './IProvider';
|
|
2
|
-
import { IContainer, Tagged } from '../container/IContainer';
|
|
2
|
+
import { Alias, AliasPredicate, IContainer, Tagged } from '../container/IContainer';
|
|
3
3
|
import { constructor, MapFn } from '../utils';
|
|
4
4
|
export declare class Provider<T> implements IProvider<T> {
|
|
5
5
|
private readonly resolveDependency;
|
|
6
6
|
private isVisibleWhen;
|
|
7
|
+
private aliases;
|
|
7
8
|
static fromClass<T>(Target: constructor<T>): IProvider<T>;
|
|
8
9
|
static fromValue<T>(value: T): IProvider<T>;
|
|
9
10
|
constructor(resolveDependency: ResolveDependency<T>, isVisibleWhen?: ChildrenVisibilityPredicate);
|
|
@@ -11,4 +12,6 @@ export declare class Provider<T> implements IProvider<T> {
|
|
|
11
12
|
resolve(container: IContainer, ...args: unknown[]): T;
|
|
12
13
|
setVisibility(predicate: ChildrenVisibilityPredicate): this;
|
|
13
14
|
isVisible(parent: Tagged, child: Tagged): boolean;
|
|
15
|
+
matchAliases(predicate: AliasPredicate): boolean;
|
|
16
|
+
addAliases(...aliases: Alias[]): this;
|
|
14
17
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IContainer, Tagged } from '../container/IContainer';
|
|
1
|
+
import { Alias, AliasPredicate, IContainer, Tagged } from '../container/IContainer';
|
|
2
2
|
import { ChildrenVisibilityPredicate, IProvider } from './IProvider';
|
|
3
3
|
import { MapFn } from '../utils';
|
|
4
4
|
export declare abstract class ProviderDecorator<T> implements IProvider<T> {
|
|
@@ -8,4 +8,6 @@ export declare abstract class ProviderDecorator<T> implements IProvider<T> {
|
|
|
8
8
|
isVisible(parent: IContainer, child: Tagged): boolean;
|
|
9
9
|
resolve(container: IContainer, ...args: unknown[]): T;
|
|
10
10
|
pipe(...mappers: MapFn<IProvider<T>>[]): IProvider<T>;
|
|
11
|
+
matchAliases(predicate: AliasPredicate): boolean;
|
|
12
|
+
addAliases(...aliases: Alias[]): this;
|
|
11
13
|
}
|
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DependencyKey, IContainerModule, Tagged } from '../container/IContainer';
|
|
2
2
|
import { constructor, MapFn } from '../utils';
|
|
3
3
|
import { IProvider } from '../provider/IProvider';
|
|
4
4
|
export type ScopePredicate = (c: Tagged) => boolean;
|
|
5
5
|
export interface IRegistration<T = unknown> extends IContainerModule {
|
|
6
|
-
addAliases(...aliases: Alias[]): this;
|
|
7
6
|
when(isValidWhen: ScopePredicate): this;
|
|
8
7
|
to(key: DependencyKey): this;
|
|
9
8
|
pipe(...mappers: MapFn<IProvider<T>>[]): this;
|
|
10
9
|
}
|
|
11
10
|
export declare const key: (key: DependencyKey) => MapFn<IRegistration>;
|
|
12
|
-
export declare const alias: (...aliases: Alias[]) => MapFn<IRegistration>;
|
|
13
11
|
export declare const scope: (predicate: ScopePredicate) => MapFn<IRegistration>;
|
|
14
12
|
export declare const getTransformers: (Target: constructor<unknown>) => MapFn<IRegistration<unknown>>[];
|
|
15
13
|
export declare const register: (...mappers: MapFn<IRegistration>[]) => ClassDecorator;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DependencyKey, IContainer } from '../container/IContainer';
|
|
2
2
|
import { constructor, MapFn } from '../utils';
|
|
3
3
|
import { IProvider, ResolveDependency } from '../provider/IProvider';
|
|
4
4
|
import { IRegistration, ScopePredicate } from './IRegistration';
|
|
@@ -9,11 +9,9 @@ export declare class Registration<T = unknown> implements IRegistration<T> {
|
|
|
9
9
|
static fromClass<T>(Target: constructor<T>): IRegistration<unknown>;
|
|
10
10
|
static fromValue<T>(value: T): IRegistration<unknown>;
|
|
11
11
|
static fromFn<T>(fn: ResolveDependency<T>): Registration<T>;
|
|
12
|
-
private aliases;
|
|
13
12
|
private mappers;
|
|
14
13
|
constructor(createProvider: () => IProvider<T>, key?: DependencyKey | undefined, matchScope?: ScopePredicate);
|
|
15
14
|
to(key: DependencyKey): this;
|
|
16
|
-
addAliases(...aliases: Alias[]): this;
|
|
17
15
|
pipe(...mappers: MapFn<IProvider<T>>[]): this;
|
|
18
16
|
applyTo(container: IContainer): void;
|
|
19
17
|
when(isValidWhen: ScopePredicate): this;
|