ts-ioc-container 30.2.0 → 30.3.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 +5 -5
- package/cjm/by.js +3 -3
- package/cjm/container/AutoMockedContainer.js +0 -4
- package/cjm/container/Container.js +2 -12
- package/cjm/container/EmptyContainer.js +1 -4
- package/cjm/provider/Provider.js +1 -1
- package/esm/by.js +3 -3
- package/esm/container/AutoMockedContainer.js +0 -4
- package/esm/container/Container.js +2 -12
- package/esm/container/EmptyContainer.js +1 -4
- package/esm/provider/Provider.js +1 -1
- package/package.json +2 -2
- package/typings/container/AutoMockedContainer.d.ts +2 -3
- package/typings/container/Container.d.ts +2 -3
- package/typings/container/EmptyContainer.d.ts +2 -3
- package/typings/container/IContainer.d.ts +5 -2
package/README.md
CHANGED
|
@@ -318,7 +318,7 @@ describe('SimpleInjector', function () {
|
|
|
318
318
|
}
|
|
319
319
|
|
|
320
320
|
const container = new Container(new SimpleInjector()).use(R.fromClass(App).to('App'));
|
|
321
|
-
const app = container.resolve<App>('App', 'Hello world');
|
|
321
|
+
const app = container.resolve<App>('App', { args: ['Hello world'] });
|
|
322
322
|
|
|
323
323
|
expect(app.greeting).toBe('Hello world');
|
|
324
324
|
});
|
|
@@ -378,7 +378,7 @@ describe('ProxyInjector', function () {
|
|
|
378
378
|
.use(R.fromClass(App).to('App').pipe(args({ greetingTemplate })))
|
|
379
379
|
.use(R.fromClass(Logger).to('logger'));
|
|
380
380
|
|
|
381
|
-
const app = container.resolve<App>('App', { name: `world` });
|
|
381
|
+
const app = container.resolve<App>('App', { args: [{ name: `world` }] });
|
|
382
382
|
expect(app.greeting).toBe('Hello world');
|
|
383
383
|
});
|
|
384
384
|
});
|
|
@@ -592,7 +592,7 @@ describe('ArgsProvider', function () {
|
|
|
592
592
|
it('should set provider arguments with highest priority in compare to resolve arguments', function () {
|
|
593
593
|
const root = createContainer().use(R.fromClass(Logger).pipe(args('name')));
|
|
594
594
|
|
|
595
|
-
const logger = root.resolve<Logger>('logger', 'file');
|
|
595
|
+
const logger = root.resolve<Logger>('logger', { args: ['file'] });
|
|
596
596
|
|
|
597
597
|
expect(logger.name).toBe('name');
|
|
598
598
|
expect(logger.type).toBe('file');
|
|
@@ -849,13 +849,13 @@ describe('onDispose', function () {
|
|
|
849
849
|
Sometimes you need to automatically mock all dependencies in container. This is what `AutoMockedContainer` is for.
|
|
850
850
|
|
|
851
851
|
```typescript
|
|
852
|
-
import { AutoMockedContainer, Container, DependencyKey, MetadataInjector
|
|
852
|
+
import { AutoMockedContainer, Container, DependencyKey, MetadataInjector } from 'ts-ioc-container';
|
|
853
853
|
import { IMock, Mock } from 'moq.ts';
|
|
854
854
|
|
|
855
855
|
export class MoqContainer extends AutoMockedContainer {
|
|
856
856
|
private mocks = new Map<DependencyKey, IMock<any>>();
|
|
857
857
|
|
|
858
|
-
|
|
858
|
+
resolve<T>(key: DependencyKey): T {
|
|
859
859
|
return this.resolveMock<T>(key).object();
|
|
860
860
|
}
|
|
861
861
|
|
package/cjm/by.js
CHANGED
|
@@ -8,7 +8,7 @@ const isPresent = (value) => value !== null && value !== undefined;
|
|
|
8
8
|
exports.isPresent = isPresent;
|
|
9
9
|
const resolveSilently = (c, ...args) => (key) => {
|
|
10
10
|
try {
|
|
11
|
-
return c.resolve(key,
|
|
11
|
+
return c.resolve(key, { args });
|
|
12
12
|
}
|
|
13
13
|
catch (e) {
|
|
14
14
|
if (e instanceof DependencyNotFoundError_1.DependencyNotFoundError) {
|
|
@@ -27,13 +27,13 @@ exports.by = {
|
|
|
27
27
|
* Get all instances that match the given keys
|
|
28
28
|
* @param keys
|
|
29
29
|
*/
|
|
30
|
-
keys: (...keys) => (с, ...args) => keys.map((t) => с.resolve(t,
|
|
30
|
+
keys: (...keys) => (с, ...args) => keys.map((t) => с.resolve(t, { args })),
|
|
31
31
|
/**
|
|
32
32
|
* Get the instance that matches the given key
|
|
33
33
|
* @param key
|
|
34
34
|
* @param deps
|
|
35
35
|
*/
|
|
36
|
-
key: (key, ...deps) => (c, ...args) => c.resolve(key, ...deps, ...args),
|
|
36
|
+
key: (key, ...deps) => (c, ...args) => c.resolve(key, { args: [...deps, ...args] }),
|
|
37
37
|
/**
|
|
38
38
|
* Get all instances that match the given predicate
|
|
39
39
|
* @param predicate
|
|
@@ -2,7 +2,6 @@
|
|
|
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");
|
|
6
5
|
class AutoMockedContainer {
|
|
7
6
|
getKeysByAlias(alias) {
|
|
8
7
|
return [];
|
|
@@ -13,9 +12,6 @@ class AutoMockedContainer {
|
|
|
13
12
|
createScope() {
|
|
14
13
|
throw new MethodNotImplementedError_1.MethodNotImplementedError();
|
|
15
14
|
}
|
|
16
|
-
resolve(key, ...args) {
|
|
17
|
-
throw new DependencyNotFoundError_1.DependencyNotFoundError(`Cannot find ${key.toString()}`);
|
|
18
|
-
}
|
|
19
15
|
dispose() { }
|
|
20
16
|
register() {
|
|
21
17
|
return this;
|
|
@@ -23,17 +23,7 @@ class Container {
|
|
|
23
23
|
}
|
|
24
24
|
return this;
|
|
25
25
|
}
|
|
26
|
-
resolve(token,
|
|
27
|
-
this.validateContainer();
|
|
28
|
-
if ((0, IContainer_1.isConstructor)(token)) {
|
|
29
|
-
return this.resolveByConstructor(token, ...args);
|
|
30
|
-
}
|
|
31
|
-
const provider = this.providers.get(token);
|
|
32
|
-
return provider?.isVisible(this, this)
|
|
33
|
-
? provider.resolve(this, ...args)
|
|
34
|
-
: this.parent.resolveFromChild(this, token, ...args);
|
|
35
|
-
}
|
|
36
|
-
resolveFromChild(child, token, ...args) {
|
|
26
|
+
resolve(token, { args = [], child = this } = {}) {
|
|
37
27
|
this.validateContainer();
|
|
38
28
|
if ((0, IContainer_1.isConstructor)(token)) {
|
|
39
29
|
return this.resolveByConstructor(token, ...args);
|
|
@@ -41,7 +31,7 @@ class Container {
|
|
|
41
31
|
const provider = this.providers.get(token);
|
|
42
32
|
return provider?.isVisible(this, child)
|
|
43
33
|
? provider.resolve(this, ...args)
|
|
44
|
-
: this.parent.
|
|
34
|
+
: this.parent.resolve(token, { args, child });
|
|
45
35
|
}
|
|
46
36
|
resolveByConstructor(token, ...args) {
|
|
47
37
|
const instance = this.injector.resolve(this, token, ...args);
|
|
@@ -22,10 +22,7 @@ class EmptyContainer {
|
|
|
22
22
|
register(key, value) {
|
|
23
23
|
throw new MethodNotImplementedError_1.MethodNotImplementedError();
|
|
24
24
|
}
|
|
25
|
-
resolve(key) {
|
|
26
|
-
throw new DependencyNotFoundError_1.DependencyNotFoundError(`Cannot find ${key.toString()}`);
|
|
27
|
-
}
|
|
28
|
-
resolveFromChild(child, key, ...args) {
|
|
25
|
+
resolve(key, options) {
|
|
29
26
|
throw new DependencyNotFoundError_1.DependencyNotFoundError(`Cannot find ${key.toString()}`);
|
|
30
27
|
}
|
|
31
28
|
getAllProviders() {
|
package/cjm/provider/Provider.js
CHANGED
|
@@ -11,7 +11,7 @@ exports.setVisibility = setVisibility;
|
|
|
11
11
|
class Provider {
|
|
12
12
|
static fromClass(Target) {
|
|
13
13
|
const mappers = (0, metadata_1.getMetadata)(Target, PROVIDER_KEY) ?? [];
|
|
14
|
-
return new Provider((container, ...args) => container.resolve(Target,
|
|
14
|
+
return new Provider((container, ...args) => container.resolve(Target, { args })).pipe(...mappers);
|
|
15
15
|
}
|
|
16
16
|
static fromValue(value) {
|
|
17
17
|
return new Provider(() => value);
|
package/esm/by.js
CHANGED
|
@@ -3,7 +3,7 @@ export const all = () => true;
|
|
|
3
3
|
export const isPresent = (value) => value !== null && value !== undefined;
|
|
4
4
|
export const resolveSilently = (c, ...args) => (key) => {
|
|
5
5
|
try {
|
|
6
|
-
return c.resolve(key,
|
|
6
|
+
return c.resolve(key, { args });
|
|
7
7
|
}
|
|
8
8
|
catch (e) {
|
|
9
9
|
if (e instanceof DependencyNotFoundError) {
|
|
@@ -21,13 +21,13 @@ export const by = {
|
|
|
21
21
|
* Get all instances that match the given keys
|
|
22
22
|
* @param keys
|
|
23
23
|
*/
|
|
24
|
-
keys: (...keys) => (с, ...args) => keys.map((t) => с.resolve(t,
|
|
24
|
+
keys: (...keys) => (с, ...args) => keys.map((t) => с.resolve(t, { args })),
|
|
25
25
|
/**
|
|
26
26
|
* Get the instance that matches the given key
|
|
27
27
|
* @param key
|
|
28
28
|
* @param deps
|
|
29
29
|
*/
|
|
30
|
-
key: (key, ...deps) => (c, ...args) => c.resolve(key, ...deps, ...args),
|
|
30
|
+
key: (key, ...deps) => (c, ...args) => c.resolve(key, { args: [...deps, ...args] }),
|
|
31
31
|
/**
|
|
32
32
|
* Get all instances that match the given predicate
|
|
33
33
|
* @param predicate
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { MethodNotImplementedError } from '../errors/MethodNotImplementedError';
|
|
2
|
-
import { DependencyNotFoundError } from '../errors/DependencyNotFoundError';
|
|
3
2
|
export class AutoMockedContainer {
|
|
4
3
|
getKeysByAlias(alias) {
|
|
5
4
|
return [];
|
|
@@ -10,9 +9,6 @@ export class AutoMockedContainer {
|
|
|
10
9
|
createScope() {
|
|
11
10
|
throw new MethodNotImplementedError();
|
|
12
11
|
}
|
|
13
|
-
resolve(key, ...args) {
|
|
14
|
-
throw new DependencyNotFoundError(`Cannot find ${key.toString()}`);
|
|
15
|
-
}
|
|
16
12
|
dispose() { }
|
|
17
13
|
register() {
|
|
18
14
|
return this;
|
|
@@ -20,17 +20,7 @@ export class Container {
|
|
|
20
20
|
}
|
|
21
21
|
return this;
|
|
22
22
|
}
|
|
23
|
-
resolve(token,
|
|
24
|
-
this.validateContainer();
|
|
25
|
-
if (isConstructor(token)) {
|
|
26
|
-
return this.resolveByConstructor(token, ...args);
|
|
27
|
-
}
|
|
28
|
-
const provider = this.providers.get(token);
|
|
29
|
-
return provider?.isVisible(this, this)
|
|
30
|
-
? provider.resolve(this, ...args)
|
|
31
|
-
: this.parent.resolveFromChild(this, token, ...args);
|
|
32
|
-
}
|
|
33
|
-
resolveFromChild(child, token, ...args) {
|
|
23
|
+
resolve(token, { args = [], child = this } = {}) {
|
|
34
24
|
this.validateContainer();
|
|
35
25
|
if (isConstructor(token)) {
|
|
36
26
|
return this.resolveByConstructor(token, ...args);
|
|
@@ -38,7 +28,7 @@ export class Container {
|
|
|
38
28
|
const provider = this.providers.get(token);
|
|
39
29
|
return provider?.isVisible(this, child)
|
|
40
30
|
? provider.resolve(this, ...args)
|
|
41
|
-
: this.parent.
|
|
31
|
+
: this.parent.resolve(token, { args, child });
|
|
42
32
|
}
|
|
43
33
|
resolveByConstructor(token, ...args) {
|
|
44
34
|
const instance = this.injector.resolve(this, token, ...args);
|
|
@@ -19,10 +19,7 @@ export class EmptyContainer {
|
|
|
19
19
|
register(key, value) {
|
|
20
20
|
throw new MethodNotImplementedError();
|
|
21
21
|
}
|
|
22
|
-
resolve(key) {
|
|
23
|
-
throw new DependencyNotFoundError(`Cannot find ${key.toString()}`);
|
|
24
|
-
}
|
|
25
|
-
resolveFromChild(child, key, ...args) {
|
|
22
|
+
resolve(key, options) {
|
|
26
23
|
throw new DependencyNotFoundError(`Cannot find ${key.toString()}`);
|
|
27
24
|
}
|
|
28
25
|
getAllProviders() {
|
package/esm/provider/Provider.js
CHANGED
|
@@ -6,7 +6,7 @@ export const setVisibility = (isVisibleWhen) => (p) => p.setVisibility(isVisible
|
|
|
6
6
|
export class Provider {
|
|
7
7
|
static fromClass(Target) {
|
|
8
8
|
const mappers = getMetadata(Target, PROVIDER_KEY) ?? [];
|
|
9
|
-
return new Provider((container, ...args) => container.resolve(Target,
|
|
9
|
+
return new Provider((container, ...args) => container.resolve(Target, { args })).pipe(...mappers);
|
|
10
10
|
}
|
|
11
11
|
static fromValue(value) {
|
|
12
12
|
return new Provider(() => value);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-ioc-container",
|
|
3
|
-
"version": "30.
|
|
3
|
+
"version": "30.3.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": "d5b0081792a674f41d74a8c716482270de229e93"
|
|
63
63
|
}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { AliasPredicate, DependencyKey, IContainer, InjectionToken,
|
|
1
|
+
import { AliasPredicate, DependencyKey, IContainer, InjectionToken, ResolveOptions } from './IContainer';
|
|
2
2
|
import { IProvider } from '../provider/IProvider';
|
|
3
3
|
export declare abstract class AutoMockedContainer implements IContainer {
|
|
4
4
|
getKeysByAlias(alias: AliasPredicate): DependencyKey[];
|
|
5
5
|
hasDependency(key: string): boolean;
|
|
6
6
|
createScope(): IContainer;
|
|
7
|
-
resolve<T>(key: InjectionToken<T>,
|
|
8
|
-
abstract resolveFromChild<T>(child: Tagged, key: InjectionToken<T>, ...args: unknown[]): T;
|
|
7
|
+
abstract resolve<T>(key: InjectionToken<T>, options?: ResolveOptions): T;
|
|
9
8
|
dispose(): void;
|
|
10
9
|
register(): this;
|
|
11
10
|
getInstances(): unknown[];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Alias, AliasPredicate, DependencyKey, IContainer, IContainerModule, InjectionToken,
|
|
1
|
+
import { Alias, 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
|
export declare class Container implements IContainer {
|
|
@@ -15,8 +15,7 @@ export declare class Container implements IContainer {
|
|
|
15
15
|
tags?: Tag[];
|
|
16
16
|
});
|
|
17
17
|
register(key: DependencyKey, provider: IProvider, aliases?: Alias[]): this;
|
|
18
|
-
resolve<T>(token: InjectionToken<T>,
|
|
19
|
-
resolveFromChild<T>(child: Tagged, token: InjectionToken<T>, ...args: unknown[]): T;
|
|
18
|
+
resolve<T>(token: InjectionToken<T>, { args, child }?: ResolveOptions): T;
|
|
20
19
|
private resolveByConstructor;
|
|
21
20
|
createScope(...tags: Tag[]): Container;
|
|
22
21
|
dispose(): void;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AliasPredicate, DependencyKey, IContainer, IContainerModule, InjectionToken,
|
|
1
|
+
import { AliasPredicate, DependencyKey, IContainer, IContainerModule, InjectionToken, ResolveOptions } from './IContainer';
|
|
2
2
|
import { IProvider } from '../provider/IProvider';
|
|
3
3
|
export declare class EmptyContainer implements IContainer {
|
|
4
4
|
getKeysByAlias(alias: AliasPredicate): DependencyKey[];
|
|
@@ -7,8 +7,7 @@ export declare class EmptyContainer implements IContainer {
|
|
|
7
7
|
createScope(): IContainer;
|
|
8
8
|
dispose(): void;
|
|
9
9
|
register(key: DependencyKey, value: IProvider): this;
|
|
10
|
-
resolve<T>(key: InjectionToken<T
|
|
11
|
-
resolveFromChild<T>(child: Tagged, key: InjectionToken<T>, ...args: unknown[]): T;
|
|
10
|
+
resolve<T>(key: InjectionToken<T>, options: ResolveOptions): T;
|
|
12
11
|
getAllProviders(): Map<DependencyKey, IProvider>;
|
|
13
12
|
getInstances(): unknown[];
|
|
14
13
|
removeScope(): void;
|
|
@@ -5,9 +5,12 @@ export type DependencyKey = string | symbol;
|
|
|
5
5
|
export declare function isDependencyKey<T>(token: InjectionToken<T>): token is DependencyKey;
|
|
6
6
|
export declare function isConstructor<T>(token: InjectionToken<T>): token is constructor<T>;
|
|
7
7
|
export type InjectionToken<T = unknown> = constructor<T> | DependencyKey;
|
|
8
|
+
export type ResolveOptions = {
|
|
9
|
+
args?: unknown[];
|
|
10
|
+
child?: Tagged;
|
|
11
|
+
};
|
|
8
12
|
export interface Resolvable {
|
|
9
|
-
resolve<T>(key: InjectionToken<T>,
|
|
10
|
-
resolveFromChild<T>(child: Tagged, key: InjectionToken<T>, ...args: unknown[]): T;
|
|
13
|
+
resolve<T>(key: InjectionToken<T>, options?: ResolveOptions): T;
|
|
11
14
|
}
|
|
12
15
|
export interface IContainerModule {
|
|
13
16
|
applyTo(container: IContainer): void;
|