ts-ioc-container 41.2.0 → 41.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjm/container/Container.js +7 -5
- package/cjm/injector/IInjector.js +7 -0
- package/cjm/injector/MetadataInjector.js +3 -2
- package/cjm/injector/ProxyInjector.js +5 -4
- package/cjm/injector/SimpleInjector.js +4 -3
- package/cjm/provider/Provider.js +4 -3
- package/cjm/provider/SingletonProvider.js +2 -1
- package/cjm/utils.js +7 -0
- package/esm/container/Container.js +8 -6
- package/esm/injector/IInjector.js +5 -1
- package/esm/injector/MetadataInjector.js +3 -2
- package/esm/injector/ProxyInjector.js +5 -4
- package/esm/injector/SimpleInjector.js +4 -3
- package/esm/provider/Provider.js +5 -4
- package/esm/provider/SingletonProvider.js +2 -1
- package/esm/utils.js +6 -0
- package/package.json +1 -1
- package/typings/container/AutoMockedContainer.d.ts +2 -3
- package/typings/container/Container.d.ts +2 -4
- package/typings/container/EmptyContainer.d.ts +2 -3
- package/typings/container/IContainer.d.ts +12 -12
- package/typings/index.d.ts +2 -2
- package/typings/injector/IInjector.d.ts +8 -2
- package/typings/injector/MetadataInjector.d.ts +3 -3
- package/typings/injector/ProxyInjector.d.ts +3 -3
- package/typings/injector/SimpleInjector.d.ts +4 -4
- package/typings/provider/DecoratorProvider.d.ts +3 -2
- package/typings/provider/IProvider.d.ts +8 -7
- package/typings/provider/Provider.d.ts +3 -3
- package/typings/provider/SingletonProvider.d.ts +3 -2
- package/typings/resolve.d.ts +2 -5
- package/typings/utils.d.ts +1 -0
|
@@ -44,12 +44,14 @@ class Container {
|
|
|
44
44
|
getRegistrations() {
|
|
45
45
|
return [...this.parent.getRegistrations(), ...this.registrations];
|
|
46
46
|
}
|
|
47
|
-
resolveClass(token,
|
|
47
|
+
resolveClass(token, options) {
|
|
48
48
|
this.validateContainer();
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
49
|
+
return (0, utils_1.toLazyIf)(() => {
|
|
50
|
+
const instance = this.injector.resolve(this, token, options);
|
|
51
|
+
this.instances.add(instance);
|
|
52
|
+
this.onConstruct(instance, this);
|
|
53
|
+
return instance;
|
|
54
|
+
}, options?.lazy);
|
|
53
55
|
}
|
|
54
56
|
resolveOne(keyOrAlias, options) {
|
|
55
57
|
return this.resolveOneStrategy(this, keyOrAlias, options);
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MetadataInjector = void 0;
|
|
4
|
+
const IInjector_1 = require("./IInjector");
|
|
4
5
|
const inject_1 = require("./inject");
|
|
5
|
-
class MetadataInjector {
|
|
6
|
-
|
|
6
|
+
class MetadataInjector extends IInjector_1.Injector {
|
|
7
|
+
createInstance(scope, Target, { args: deps = [] } = {}) {
|
|
7
8
|
const args = (0, inject_1.resolveArgs)(Target)(scope, ...deps);
|
|
8
9
|
return new Target(...args);
|
|
9
10
|
}
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ProxyInjector = void 0;
|
|
4
|
+
const IInjector_1 = require("./IInjector");
|
|
4
5
|
function getProp(target, key) {
|
|
5
6
|
// @ts-ignore
|
|
6
7
|
return target[key];
|
|
7
8
|
}
|
|
8
|
-
class ProxyInjector {
|
|
9
|
-
|
|
9
|
+
class ProxyInjector extends IInjector_1.Injector {
|
|
10
|
+
createInstance(scope, Target, { args: deps = [] } = {}) {
|
|
10
11
|
const args = deps.reduce((acc, it) => ({ ...acc, ...it }), {});
|
|
11
12
|
const proxy = new Proxy({}, {
|
|
12
13
|
get(target, prop) {
|
|
@@ -14,8 +15,8 @@ class ProxyInjector {
|
|
|
14
15
|
return args.hasOwnProperty(prop)
|
|
15
16
|
? getProp(args, prop)
|
|
16
17
|
: prop.toString().search(/array/gi) >= 0
|
|
17
|
-
?
|
|
18
|
-
:
|
|
18
|
+
? scope.resolveMany(prop)
|
|
19
|
+
: scope.resolveOne(prop);
|
|
19
20
|
},
|
|
20
21
|
});
|
|
21
22
|
return new Target(proxy);
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SimpleInjector = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
const IInjector_1 = require("./IInjector");
|
|
5
|
+
class SimpleInjector extends IInjector_1.Injector {
|
|
6
|
+
createInstance(container, Target, { args = [] } = {}) {
|
|
7
|
+
return new Target(container, ...args);
|
|
7
8
|
}
|
|
8
9
|
}
|
|
9
10
|
exports.SimpleInjector = SimpleInjector;
|
package/cjm/provider/Provider.js
CHANGED
|
@@ -24,9 +24,10 @@ class Provider {
|
|
|
24
24
|
const fns = mappers.map((m) => ((0, ProviderPipe_1.isProviderPipe)(m) ? m.mapProvider.bind(m) : m));
|
|
25
25
|
return (0, utils_1.pipe)(...fns)(this);
|
|
26
26
|
}
|
|
27
|
-
resolve(container, { args, lazy }) {
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
resolve(container, { args = [], lazy } = {}) {
|
|
28
|
+
return (0, utils_1.toLazyIf)(() => this.resolveDependency(container, {
|
|
29
|
+
args: [...this.argsFn(container, ...args), ...args],
|
|
30
|
+
}), lazy ?? this.isLazy);
|
|
30
31
|
}
|
|
31
32
|
setAccessPredicate(predicate) {
|
|
32
33
|
this.checkAccess = predicate;
|
|
@@ -13,7 +13,8 @@ class SingletonProvider extends IProvider_1.ProviderDecorator {
|
|
|
13
13
|
this.cache = cache;
|
|
14
14
|
}
|
|
15
15
|
resolve(container, options) {
|
|
16
|
-
const
|
|
16
|
+
const { args = [] } = options;
|
|
17
|
+
const key = this.cache.getKey(...args);
|
|
17
18
|
if (!this.cache.hasValue(key)) {
|
|
18
19
|
this.cache.setValue(key, this.provider.resolve(container, options));
|
|
19
20
|
}
|
package/cjm/utils.js
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.Is = exports.Filter = exports.List = exports.promisify = exports.constant = exports.pipe = void 0;
|
|
4
4
|
exports.fillEmptyIndexes = fillEmptyIndexes;
|
|
5
5
|
exports.lazyProxy = lazyProxy;
|
|
6
|
+
exports.toLazyIf = toLazyIf;
|
|
6
7
|
const pipe = (...mappers) => (value) => mappers.reduce((acc, current) => current(acc), value);
|
|
7
8
|
exports.pipe = pipe;
|
|
8
9
|
function fillEmptyIndexes(baseArr, insertArr) {
|
|
@@ -27,6 +28,12 @@ function lazyProxy(resolveInstance) {
|
|
|
27
28
|
},
|
|
28
29
|
});
|
|
29
30
|
}
|
|
31
|
+
function toLazyIf(resolveInstance, isLazy = false) {
|
|
32
|
+
if (isLazy) {
|
|
33
|
+
return lazyProxy(resolveInstance);
|
|
34
|
+
}
|
|
35
|
+
return resolveInstance();
|
|
36
|
+
}
|
|
30
37
|
const promisify = (arg) => (arg instanceof Promise ? arg : Promise.resolve(arg));
|
|
31
38
|
exports.promisify = promisify;
|
|
32
39
|
exports.List = {
|
|
@@ -2,7 +2,7 @@ import { DEFAULT_CONTAINER_RESOLVER, } from './IContainer';
|
|
|
2
2
|
import { EmptyContainer } from './EmptyContainer';
|
|
3
3
|
import { ContainerDisposedError } from '../errors/ContainerDisposedError';
|
|
4
4
|
import { MetadataInjector } from '../injector/MetadataInjector';
|
|
5
|
-
import { Filter as F } from '../utils';
|
|
5
|
+
import { Filter as F, toLazyIf } from '../utils';
|
|
6
6
|
import { AliasMap } from './AliasMap';
|
|
7
7
|
import { DependencyNotFoundError } from '../errors/DependencyNotFoundError';
|
|
8
8
|
export class Container {
|
|
@@ -41,12 +41,14 @@ export class Container {
|
|
|
41
41
|
getRegistrations() {
|
|
42
42
|
return [...this.parent.getRegistrations(), ...this.registrations];
|
|
43
43
|
}
|
|
44
|
-
resolveClass(token,
|
|
44
|
+
resolveClass(token, options) {
|
|
45
45
|
this.validateContainer();
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
46
|
+
return toLazyIf(() => {
|
|
47
|
+
const instance = this.injector.resolve(this, token, options);
|
|
48
|
+
this.instances.add(instance);
|
|
49
|
+
this.onConstruct(instance, this);
|
|
50
|
+
return instance;
|
|
51
|
+
}, options?.lazy);
|
|
50
52
|
}
|
|
51
53
|
resolveOne(keyOrAlias, options) {
|
|
52
54
|
return this.resolveOneStrategy(this, keyOrAlias, options);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { Injector } from './IInjector';
|
|
1
2
|
import { resolveArgs } from './inject';
|
|
2
|
-
export class MetadataInjector {
|
|
3
|
-
|
|
3
|
+
export class MetadataInjector extends Injector {
|
|
4
|
+
createInstance(scope, Target, { args: deps = [] } = {}) {
|
|
4
5
|
const args = resolveArgs(Target)(scope, ...deps);
|
|
5
6
|
return new Target(...args);
|
|
6
7
|
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import { Injector } from './IInjector';
|
|
1
2
|
function getProp(target, key) {
|
|
2
3
|
// @ts-ignore
|
|
3
4
|
return target[key];
|
|
4
5
|
}
|
|
5
|
-
export class ProxyInjector {
|
|
6
|
-
|
|
6
|
+
export class ProxyInjector extends Injector {
|
|
7
|
+
createInstance(scope, Target, { args: deps = [] } = {}) {
|
|
7
8
|
const args = deps.reduce((acc, it) => ({ ...acc, ...it }), {});
|
|
8
9
|
const proxy = new Proxy({}, {
|
|
9
10
|
get(target, prop) {
|
|
@@ -11,8 +12,8 @@ export class ProxyInjector {
|
|
|
11
12
|
return args.hasOwnProperty(prop)
|
|
12
13
|
? getProp(args, prop)
|
|
13
14
|
: prop.toString().search(/array/gi) >= 0
|
|
14
|
-
?
|
|
15
|
-
:
|
|
15
|
+
? scope.resolveMany(prop)
|
|
16
|
+
: scope.resolveOne(prop);
|
|
16
17
|
},
|
|
17
18
|
});
|
|
18
19
|
return new Target(proxy);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { Injector } from './IInjector';
|
|
2
|
+
export class SimpleInjector extends Injector {
|
|
3
|
+
createInstance(container, Target, { args = [] } = {}) {
|
|
4
|
+
return new Target(container, ...args);
|
|
4
5
|
}
|
|
5
6
|
}
|
package/esm/provider/Provider.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { pipe, toLazyIf } from '../utils';
|
|
2
2
|
import { isProviderPipe } from './ProviderPipe';
|
|
3
3
|
export class Provider {
|
|
4
4
|
resolveDependency;
|
|
@@ -21,9 +21,10 @@ export class Provider {
|
|
|
21
21
|
const fns = mappers.map((m) => (isProviderPipe(m) ? m.mapProvider.bind(m) : m));
|
|
22
22
|
return pipe(...fns)(this);
|
|
23
23
|
}
|
|
24
|
-
resolve(container, { args, lazy }) {
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
resolve(container, { args = [], lazy } = {}) {
|
|
25
|
+
return toLazyIf(() => this.resolveDependency(container, {
|
|
26
|
+
args: [...this.argsFn(container, ...args), ...args],
|
|
27
|
+
}), lazy ?? this.isLazy);
|
|
27
28
|
}
|
|
28
29
|
setAccessPredicate(predicate) {
|
|
29
30
|
this.checkAccess = predicate;
|
|
@@ -10,7 +10,8 @@ export class SingletonProvider extends ProviderDecorator {
|
|
|
10
10
|
this.cache = cache;
|
|
11
11
|
}
|
|
12
12
|
resolve(container, options) {
|
|
13
|
-
const
|
|
13
|
+
const { args = [] } = options;
|
|
14
|
+
const key = this.cache.getKey(...args);
|
|
14
15
|
if (!this.cache.hasValue(key)) {
|
|
15
16
|
this.cache.setValue(key, this.provider.resolve(container, options));
|
|
16
17
|
}
|
package/esm/utils.js
CHANGED
|
@@ -20,6 +20,12 @@ export function lazyProxy(resolveInstance) {
|
|
|
20
20
|
},
|
|
21
21
|
});
|
|
22
22
|
}
|
|
23
|
+
export function toLazyIf(resolveInstance, isLazy = false) {
|
|
24
|
+
if (isLazy) {
|
|
25
|
+
return lazyProxy(resolveInstance);
|
|
26
|
+
}
|
|
27
|
+
return resolveInstance();
|
|
28
|
+
}
|
|
23
29
|
export const promisify = (arg) => (arg instanceof Promise ? arg : Promise.resolve(arg));
|
|
24
30
|
export const List = {
|
|
25
31
|
lastOf: (arr) => arr[arr.length - 1],
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type DependencyKey, type IContainer, type Instance, type ResolveManyOptions, type ResolveOneOptions, type Tag } from './IContainer';
|
|
2
2
|
import { type IRegistration } from '../registration/IRegistration';
|
|
3
3
|
import { type constructor } from '../utils';
|
|
4
|
+
import { InjectOptions } from '../injector/IInjector';
|
|
4
5
|
export declare abstract class AutoMockedContainer implements IContainer {
|
|
5
6
|
isDisposed: boolean;
|
|
6
7
|
createScope(): IContainer;
|
|
@@ -15,9 +16,7 @@ export declare abstract class AutoMockedContainer implements IContainer {
|
|
|
15
16
|
getRegistrations(): never[];
|
|
16
17
|
addRegistration(registration: IRegistration): this;
|
|
17
18
|
abstract resolveMany<T>(alias: DependencyKey, options?: ResolveManyOptions): T[];
|
|
18
|
-
abstract resolveClass<T>(target: constructor<T>, options?:
|
|
19
|
-
args?: unknown[];
|
|
20
|
-
}): T;
|
|
19
|
+
abstract resolveClass<T>(target: constructor<T>, options?: InjectOptions): T;
|
|
21
20
|
abstract resolveOneByKey<T>(keyOrAlias: DependencyKey, options?: ResolveOneOptions): T;
|
|
22
21
|
abstract resolveOneByAlias<T>(keyOrAlias: DependencyKey, options?: ResolveOneOptions): T;
|
|
23
22
|
abstract resolveOne<T>(alias: constructor<T> | DependencyKey, options?: ResolveManyOptions): T;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type CreateScopeOptions, type DependencyKey, type IContainer, type IContainerModule, type Instance, type RegisterOptions, ResolveManyOptions, type ResolveOneOptions, type Tag } from './IContainer';
|
|
2
2
|
import { type IInjector } from '../injector/IInjector';
|
|
3
|
-
import { type IProvider } from '../provider/IProvider';
|
|
3
|
+
import { type IProvider, ProviderOptions } from '../provider/IProvider';
|
|
4
4
|
import { type IRegistration } from '../registration/IRegistration';
|
|
5
5
|
import { type constructor } from '../utils';
|
|
6
6
|
type ResolveOneStrategy = <T>(scope: IContainer, keyOrAlias: constructor<T> | DependencyKey, options?: ResolveOneOptions) => T;
|
|
@@ -28,9 +28,7 @@ export declare class Container implements IContainer {
|
|
|
28
28
|
register(key: DependencyKey, provider: IProvider, { aliases }?: RegisterOptions): this;
|
|
29
29
|
addRegistration(registration: IRegistration): this;
|
|
30
30
|
getRegistrations(): IRegistration[];
|
|
31
|
-
resolveClass<T>(token: constructor<T>,
|
|
32
|
-
args?: unknown[];
|
|
33
|
-
}): T;
|
|
31
|
+
resolveClass<T>(token: constructor<T>, options?: ProviderOptions): T;
|
|
34
32
|
resolveOne<T>(keyOrAlias: constructor<T> | DependencyKey, options?: ResolveOneOptions): T;
|
|
35
33
|
resolveOneByKey<T>(keyOrAlias: DependencyKey, { args, child, lazy }?: ResolveOneOptions): T;
|
|
36
34
|
resolveOneByAlias<T>(keyOrAlias: DependencyKey, { args, child, lazy }?: ResolveOneOptions): T;
|
|
@@ -2,12 +2,11 @@ import { type DependencyKey, type IContainer, type IContainerModule, type Resolv
|
|
|
2
2
|
import { type IProvider } from '../provider/IProvider';
|
|
3
3
|
import { type IRegistration } from '../registration/IRegistration';
|
|
4
4
|
import { type constructor } from '../utils';
|
|
5
|
+
import { InjectOptions } from '../injector/IInjector';
|
|
5
6
|
export declare class EmptyContainer implements IContainer {
|
|
6
7
|
get isDisposed(): boolean;
|
|
7
8
|
getParent(): undefined;
|
|
8
|
-
resolveClass<T>(token: constructor<T>, options?:
|
|
9
|
-
args?: [];
|
|
10
|
-
}): T;
|
|
9
|
+
resolveClass<T>(token: constructor<T>, options?: InjectOptions): T;
|
|
11
10
|
getScopes(): never[];
|
|
12
11
|
getInstances(): never[];
|
|
13
12
|
createScope(): IContainer;
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import { type IProvider } from '../provider/IProvider';
|
|
1
|
+
import { type IProvider, ProviderOptions } from '../provider/IProvider';
|
|
2
2
|
import { type constructor } from '../utils';
|
|
3
3
|
import { type IRegistration } from '../registration/IRegistration';
|
|
4
4
|
export type Tag = string;
|
|
5
5
|
export type DependencyKey = string | symbol;
|
|
6
6
|
export type InjectionToken<T = unknown> = constructor<T> | DependencyKey;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
child?: Tagged;
|
|
10
|
-
lazy?: boolean;
|
|
7
|
+
type WithChild = {
|
|
8
|
+
child: Tagged;
|
|
11
9
|
};
|
|
12
|
-
export type
|
|
13
|
-
|
|
10
|
+
export type ResolveOneOptions = ProviderOptions & Partial<WithChild>;
|
|
11
|
+
type WithExcludedKeys = {
|
|
12
|
+
excludedKeys: Set<DependencyKey>;
|
|
14
13
|
};
|
|
14
|
+
export type ResolveManyOptions = ResolveOneOptions & Partial<WithExcludedKeys>;
|
|
15
15
|
export interface Resolvable {
|
|
16
16
|
resolve<T>(key: InjectionToken<T>, options?: ResolveOneOptions): T;
|
|
17
17
|
}
|
|
@@ -21,9 +21,10 @@ export interface IContainerModule {
|
|
|
21
21
|
export interface Tagged {
|
|
22
22
|
hasTag(tag: Tag): boolean;
|
|
23
23
|
}
|
|
24
|
-
|
|
25
|
-
tags
|
|
24
|
+
type WithTags = {
|
|
25
|
+
tags: Tag[];
|
|
26
26
|
};
|
|
27
|
+
export type CreateScopeOptions = Partial<WithTags>;
|
|
27
28
|
export interface Instance<T = unknown> {
|
|
28
29
|
new (...args: unknown[]): T;
|
|
29
30
|
}
|
|
@@ -35,9 +36,7 @@ export interface IContainer extends Tagged {
|
|
|
35
36
|
register(key: DependencyKey, value: IProvider, options?: RegisterOptions): this;
|
|
36
37
|
addRegistration(registration: IRegistration): this;
|
|
37
38
|
getRegistrations(): IRegistration[];
|
|
38
|
-
resolveClass<T>(target: constructor<T>, options?:
|
|
39
|
-
args?: unknown[];
|
|
40
|
-
}): T;
|
|
39
|
+
resolveClass<T>(target: constructor<T>, options?: ProviderOptions): T;
|
|
41
40
|
resolveOne<T>(alias: constructor<T> | DependencyKey, options?: ResolveOneOptions): T;
|
|
42
41
|
resolveOneByKey<T>(key: DependencyKey, options?: ResolveOneOptions): T;
|
|
43
42
|
resolveOneByAlias<T>(key: DependencyKey, options?: ResolveOneOptions): T;
|
|
@@ -51,3 +50,4 @@ export interface IContainer extends Tagged {
|
|
|
51
50
|
dispose(): void;
|
|
52
51
|
}
|
|
53
52
|
export declare const DEFAULT_CONTAINER_RESOLVER: <T>(scope: IContainer, keyOrAlias: constructor<T> | DependencyKey, options?: ResolveOneOptions) => T;
|
|
53
|
+
export {};
|
package/typings/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { type IContainer, type Resolvable, type IContainerModule, type DependencyKey, type InjectionToken, type Tag, type Tagged, type Instance, } from './container/IContainer';
|
|
1
|
+
export { type IContainer, type Resolvable, type IContainerModule, type DependencyKey, type InjectionToken, type Tag, type Tagged, type Instance, type ResolveOneOptions, type ResolveManyOptions, } from './container/IContainer';
|
|
2
2
|
export { Container } from './container/Container';
|
|
3
3
|
export { EmptyContainer } from './container/EmptyContainer';
|
|
4
4
|
export { AutoMockedContainer } from './container/AutoMockedContainer';
|
|
@@ -7,7 +7,7 @@ export { type IInjector, type InjectOptions } from './injector/IInjector';
|
|
|
7
7
|
export { MetadataInjector } from './injector/MetadataInjector';
|
|
8
8
|
export { SimpleInjector } from './injector/SimpleInjector';
|
|
9
9
|
export { ProxyInjector } from './injector/ProxyInjector';
|
|
10
|
-
export { type ResolveDependency, type IProvider, scopeAccess, lazy, argsFn, args, type ArgsFn, ProviderDecorator, type
|
|
10
|
+
export { type ResolveDependency, type IProvider, scopeAccess, lazy, argsFn, args, type ArgsFn, ProviderDecorator, type IMapper, type ProviderOptions, } from './provider/IProvider';
|
|
11
11
|
export { Provider } from './provider/Provider';
|
|
12
12
|
export { singleton, SingletonProvider } from './provider/SingletonProvider';
|
|
13
13
|
export { type Cache, multiCache, MultiCache } from './provider/Cache';
|
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
import { type constructor } from '../utils';
|
|
2
2
|
import { type IContainer, ResolveOneOptions } from '../container/IContainer';
|
|
3
|
-
|
|
3
|
+
type WithArgs = {
|
|
4
4
|
args: unknown[];
|
|
5
5
|
};
|
|
6
|
+
export type InjectOptions = Partial<WithArgs>;
|
|
6
7
|
export interface IInjector {
|
|
7
|
-
resolve<T>(container: IContainer, value: constructor<T>, options
|
|
8
|
+
resolve<T>(container: IContainer, value: constructor<T>, options?: InjectOptions): T;
|
|
8
9
|
}
|
|
9
10
|
export interface IInjectFnResolver<T> {
|
|
10
11
|
resolve(s: IContainer, options?: ResolveOneOptions): T;
|
|
11
12
|
}
|
|
13
|
+
export declare abstract class Injector implements IInjector {
|
|
14
|
+
resolve<T>(scope: IContainer, Target: constructor<T>, options?: InjectOptions): T;
|
|
15
|
+
protected abstract createInstance<T>(scope: IContainer, Target: constructor<T>, options?: InjectOptions): T;
|
|
16
|
+
}
|
|
17
|
+
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { InjectOptions, Injector } from './IInjector';
|
|
2
2
|
import type { IContainer } from '../container/IContainer';
|
|
3
3
|
import type { constructor } from '../utils';
|
|
4
|
-
export declare class MetadataInjector
|
|
5
|
-
|
|
4
|
+
export declare class MetadataInjector extends Injector {
|
|
5
|
+
protected createInstance<T>(scope: IContainer, Target: constructor<T>, { args: deps }?: InjectOptions): T;
|
|
6
6
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { InjectOptions, Injector } from './IInjector';
|
|
2
2
|
import type { IContainer } from '../container/IContainer';
|
|
3
3
|
import type { constructor } from '../utils';
|
|
4
|
-
export declare class ProxyInjector
|
|
5
|
-
|
|
4
|
+
export declare class ProxyInjector extends Injector {
|
|
5
|
+
protected createInstance<T>(scope: IContainer, Target: constructor<T>, { args: deps }?: InjectOptions): T;
|
|
6
6
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { InjectOptions, Injector } from './IInjector';
|
|
2
2
|
import type { IContainer } from '../container/IContainer';
|
|
3
|
-
import
|
|
4
|
-
export declare class SimpleInjector
|
|
5
|
-
|
|
3
|
+
import { constructor } from '../utils';
|
|
4
|
+
export declare class SimpleInjector extends Injector {
|
|
5
|
+
protected createInstance<T>(container: IContainer, Target: constructor<T>, { args }?: InjectOptions): T;
|
|
6
6
|
}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import type { IContainer } from '../container/IContainer';
|
|
2
|
-
import type { IProvider
|
|
2
|
+
import type { IProvider } from './IProvider';
|
|
3
3
|
import { ProviderDecorator } from './IProvider';
|
|
4
|
+
import { InjectOptions } from '../injector/IInjector';
|
|
4
5
|
export type DecorateFn<Instance = any> = (dep: Instance, scope: IContainer) => Instance;
|
|
5
6
|
export declare class DecoratorProvider<Instance> extends ProviderDecorator<Instance> {
|
|
6
7
|
private provider;
|
|
7
8
|
private decorateFn;
|
|
8
9
|
constructor(provider: IProvider<Instance>, decorateFn: DecorateFn<Instance>);
|
|
9
|
-
resolve(scope: IContainer, options:
|
|
10
|
+
resolve(scope: IContainer, options: InjectOptions): Instance;
|
|
10
11
|
}
|
|
11
12
|
export declare const decorate: (decorateFn: DecorateFn) => import("./ProviderPipe").ProviderPipe<unknown>;
|
|
@@ -1,22 +1,23 @@
|
|
|
1
1
|
import { IContainer, Tagged } from '../container/IContainer';
|
|
2
2
|
import { MapFn } from '../utils';
|
|
3
3
|
import { ProviderPipe } from './ProviderPipe';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
lazy
|
|
4
|
+
import { InjectOptions } from '../injector/IInjector';
|
|
5
|
+
export type WithLazy = {
|
|
6
|
+
lazy: boolean;
|
|
7
7
|
};
|
|
8
|
-
export type
|
|
8
|
+
export type ProviderOptions = InjectOptions & Partial<WithLazy>;
|
|
9
|
+
export type ResolveDependency<T = unknown> = (container: IContainer, options: InjectOptions) => T;
|
|
9
10
|
export type ScopeAccessOptions = {
|
|
10
11
|
invocationScope: Tagged;
|
|
11
12
|
providerScope: Tagged;
|
|
12
13
|
};
|
|
13
14
|
export type ScopeAccessFn = (options: ScopeAccessOptions) => boolean;
|
|
14
15
|
export type ArgsFn = (l: IContainer, ...args: unknown[]) => unknown[];
|
|
15
|
-
export interface IMapper
|
|
16
|
+
export interface IMapper {
|
|
16
17
|
mapItem<T>(target: IProvider<T>): IProvider<T>;
|
|
17
18
|
}
|
|
18
19
|
export interface IProvider<T = any> {
|
|
19
|
-
resolve(container: IContainer, options:
|
|
20
|
+
resolve(container: IContainer, options: ProviderOptions): T;
|
|
20
21
|
hasAccess(options: ScopeAccessOptions): boolean;
|
|
21
22
|
pipe(...mappers: (MapFn<IProvider<T>> | ProviderPipe<T>)[]): IProvider<T>;
|
|
22
23
|
setAccessPredicate(hasAccessWhen: ScopeAccessFn): this;
|
|
@@ -33,7 +34,7 @@ export declare abstract class ProviderDecorator<T> implements IProvider<T> {
|
|
|
33
34
|
protected constructor(decorated: IProvider<T>);
|
|
34
35
|
setAccessPredicate(predicate: ScopeAccessFn): this;
|
|
35
36
|
hasAccess(options: ScopeAccessOptions): boolean;
|
|
36
|
-
resolve(container: IContainer, options:
|
|
37
|
+
resolve(container: IContainer, options: ProviderOptions): T;
|
|
37
38
|
pipe(...mappers: (MapFn<IProvider<T>> | ProviderPipe<T>)[]): IProvider<T>;
|
|
38
39
|
setArgs(argsFn: ArgsFn): this;
|
|
39
40
|
lazy(): this;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ArgsFn, IProvider, ProviderOptions, ResolveDependency, ScopeAccessFn, ScopeAccessOptions } from './IProvider';
|
|
2
2
|
import type { DependencyKey, IContainer } from '../container/IContainer';
|
|
3
|
-
import
|
|
3
|
+
import { constructor, MapFn } from '../utils';
|
|
4
4
|
import type { ProviderPipe } from './ProviderPipe';
|
|
5
5
|
export declare class Provider<T = any> implements IProvider<T> {
|
|
6
6
|
private readonly resolveDependency;
|
|
@@ -12,7 +12,7 @@ export declare class Provider<T = any> implements IProvider<T> {
|
|
|
12
12
|
private isLazy;
|
|
13
13
|
constructor(resolveDependency: ResolveDependency<T>);
|
|
14
14
|
pipe(...mappers: (MapFn<IProvider<T>> | ProviderPipe<T>)[]): IProvider<T>;
|
|
15
|
-
resolve(container: IContainer, { args, lazy }
|
|
15
|
+
resolve(container: IContainer, { args, lazy }?: ProviderOptions): T;
|
|
16
16
|
setAccessPredicate(predicate: ScopeAccessFn): this;
|
|
17
17
|
lazy(): this;
|
|
18
18
|
setArgs(argsFn: ArgsFn): this;
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import type { IContainer } from '../container/IContainer';
|
|
2
|
-
import type { IProvider
|
|
2
|
+
import type { IProvider } from './IProvider';
|
|
3
3
|
import { ProviderDecorator } from './IProvider';
|
|
4
4
|
import type { Cache } from './Cache';
|
|
5
|
+
import { InjectOptions } from '../injector/IInjector';
|
|
5
6
|
export declare class SingletonProvider<T> extends ProviderDecorator<T> {
|
|
6
7
|
private readonly provider;
|
|
7
8
|
private readonly cache;
|
|
8
9
|
constructor(provider: IProvider<T>, cache: Cache<unknown, T>);
|
|
9
|
-
resolve(container: IContainer, options:
|
|
10
|
+
resolve(container: IContainer, options: InjectOptions): T;
|
|
10
11
|
}
|
|
11
12
|
export declare const singleton: <T = unknown>(cacheProvider?: () => Cache<unknown, T>) => import("./ProviderPipe").ProviderPipe<T>;
|
package/typings/resolve.d.ts
CHANGED
|
@@ -2,17 +2,14 @@ import { type CreateScopeOptions, type DependencyKey, type IContainer, type Inst
|
|
|
2
2
|
import { type constructor } from './utils';
|
|
3
3
|
import { type DepKey } from './DepKey';
|
|
4
4
|
import type { IInjectFnResolver } from './injector/IInjector';
|
|
5
|
+
import { ProviderOptions } from './provider/IProvider';
|
|
5
6
|
export type InstancePredicate = (dep: unknown) => boolean;
|
|
6
|
-
export type InjectOptions = {
|
|
7
|
-
lazy: boolean;
|
|
8
|
-
args: unknown[];
|
|
9
|
-
};
|
|
10
7
|
export type ArgsFn = (l: IContainer) => unknown[];
|
|
11
8
|
export declare class InjectionResolver<T> {
|
|
12
9
|
private resolveByOptions;
|
|
13
10
|
private isLazy;
|
|
14
11
|
private getArgs;
|
|
15
|
-
constructor(resolveByOptions: (s: IContainer, options:
|
|
12
|
+
constructor(resolveByOptions: (s: IContainer, options: ProviderOptions) => T);
|
|
16
13
|
args(...deps: unknown[]): this;
|
|
17
14
|
argsFn(fn: ArgsFn): this;
|
|
18
15
|
lazy(): this;
|
package/typings/utils.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export declare const pipe: <T>(...mappers: MapFn<T>[]) => MapFn<T>;
|
|
|
10
10
|
export declare function fillEmptyIndexes<T>(baseArr: (T | undefined)[], insertArr: T[]): T[];
|
|
11
11
|
export declare const constant: <T>(value: T) => () => T;
|
|
12
12
|
export declare function lazyProxy<T>(resolveInstance: () => T): T;
|
|
13
|
+
export declare function toLazyIf<T>(resolveInstance: () => T, isLazy?: boolean): T;
|
|
13
14
|
export declare const promisify: <T>(arg: T | Promise<T>) => Promise<T>;
|
|
14
15
|
export declare const List: {
|
|
15
16
|
lastOf: <T>(arr: T[]) => T;
|