ts-ioc-container 41.1.2 → 41.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/cjm/DepKey.js +1 -5
- package/cjm/container/Container.js +7 -5
- package/cjm/container/IContainer.js +1 -5
- package/cjm/hooks/hook.js +1 -1
- package/cjm/index.js +3 -4
- 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/injector/inject.js +3 -4
- package/cjm/provider/Provider.js +4 -3
- package/cjm/provider/SingletonProvider.js +2 -1
- package/cjm/registration/IRegistration.js +2 -2
- package/cjm/registration/Registration.js +1 -1
- package/cjm/resolve.js +4 -5
- package/cjm/utils.js +11 -13
- package/esm/DepKey.js +0 -3
- package/esm/container/Container.js +8 -6
- package/esm/container/IContainer.js +2 -5
- package/esm/hooks/hook.js +2 -2
- package/esm/index.js +1 -2
- 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/injector/inject.js +4 -5
- package/esm/provider/Provider.js +5 -4
- package/esm/provider/SingletonProvider.js +2 -1
- package/esm/registration/IRegistration.js +2 -2
- package/esm/registration/Registration.js +2 -2
- package/esm/resolve.js +4 -5
- package/esm/utils.js +9 -10
- package/package.json +1 -1
- package/typings/DepKey.d.ts +0 -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 -13
- package/typings/index.d.ts +3 -3
- 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/registration/IRegistration.d.ts +1 -1
- package/typings/registration/Registration.d.ts +1 -1
- package/typings/resolve.d.ts +2 -5
- package/typings/utils.d.ts +3 -8
package/cjm/DepKey.js
CHANGED
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.depKey =
|
|
3
|
+
exports.depKey = void 0;
|
|
4
4
|
const Registration_1 = require("./registration/Registration");
|
|
5
5
|
const Provider_1 = require("./provider/Provider");
|
|
6
|
-
const isDepKey = (key) => {
|
|
7
|
-
return typeof key === 'object' && key !== null && 'key' in key;
|
|
8
|
-
};
|
|
9
|
-
exports.isDepKey = isDepKey;
|
|
10
6
|
const depKey = (key) => {
|
|
11
7
|
const scopePredicates = [];
|
|
12
8
|
const mappers = [];
|
|
@@ -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,14 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DEFAULT_CONTAINER_RESOLVER = void 0;
|
|
4
|
-
exports.isDependencyKey = isDependencyKey;
|
|
5
4
|
const utils_1 = require("../utils");
|
|
6
5
|
const DependencyNotFoundError_1 = require("../errors/DependencyNotFoundError");
|
|
7
|
-
function isDependencyKey(token) {
|
|
8
|
-
return ['string', 'symbol'].includes(typeof token);
|
|
9
|
-
}
|
|
10
6
|
const DEFAULT_CONTAINER_RESOLVER = (scope, keyOrAlias, options) => {
|
|
11
|
-
if (
|
|
7
|
+
if (utils_1.Is.constructor(keyOrAlias)) {
|
|
12
8
|
return scope.resolveClass(keyOrAlias, options);
|
|
13
9
|
}
|
|
14
10
|
try {
|
package/cjm/hooks/hook.js
CHANGED
|
@@ -8,7 +8,7 @@ const utils_1 = require("../utils");
|
|
|
8
8
|
const UnexpectedHookResultError_1 = require("../errors/UnexpectedHookResultError");
|
|
9
9
|
const inject_1 = require("../injector/inject");
|
|
10
10
|
const isHookClassConstructor = (execute) => {
|
|
11
|
-
return
|
|
11
|
+
return utils_1.Is.constructor(execute) && execute.prototype.execute;
|
|
12
12
|
};
|
|
13
13
|
const toHookFn = (execute) => isHookClassConstructor(execute) ? (context) => context.scope.resolveOne(execute).execute(context) : execute;
|
|
14
14
|
exports.toHookFn = toHookFn;
|
package/cjm/index.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.depKey = exports.by = exports.getParameterMetadata = exports.getMethodMetadata = exports.setMethodMetadata = exports.setParameterMetadata = exports.getMetadata = exports.setMetadata = exports.HookContext = exports.runOnDisposeHooks = exports.runOnConstructHooks = exports.onConstruct = exports.onDispose = exports.injectProp = exports.runHooksAsync = exports.runHooks = exports.hasHooks = exports.hook = exports.getHooks = exports.UnexpectedHookResultError = exports.ContainerDisposedError = exports.MethodNotImplementedError = exports.DependencyNotFoundError = exports.Registration = exports.register = exports.scope = exports.asAlias = exports.asKey = exports.decorate = exports.MultiCache = exports.multiCache = exports.SingletonProvider = exports.singleton = exports.Provider = exports.ProviderDecorator = exports.args = exports.argsFn = exports.lazy = exports.scopeAccess = exports.ProxyInjector = exports.SimpleInjector = exports.MetadataInjector = exports.resolveArgs = exports.inject = exports.AutoMockedContainer = exports.EmptyContainer = exports.Container =
|
|
4
|
-
// Containers
|
|
5
|
-
var IContainer_1 = require("./container/IContainer");
|
|
6
|
-
Object.defineProperty(exports, "isDependencyKey", { enumerable: true, get: function () { return IContainer_1.isDependencyKey; } });
|
|
3
|
+
exports.depKey = exports.Is = exports.by = exports.getParameterMetadata = exports.getMethodMetadata = exports.setMethodMetadata = exports.setParameterMetadata = exports.getMetadata = exports.setMetadata = exports.HookContext = exports.runOnDisposeHooks = exports.runOnConstructHooks = exports.onConstruct = exports.onDispose = exports.injectProp = exports.runHooksAsync = exports.runHooks = exports.hasHooks = exports.hook = exports.getHooks = exports.UnexpectedHookResultError = exports.ContainerDisposedError = exports.MethodNotImplementedError = exports.DependencyNotFoundError = exports.Registration = exports.register = exports.scope = exports.asAlias = exports.asKey = exports.decorate = exports.MultiCache = exports.multiCache = exports.SingletonProvider = exports.singleton = exports.Provider = exports.ProviderDecorator = exports.args = exports.argsFn = exports.lazy = exports.scopeAccess = exports.ProxyInjector = exports.SimpleInjector = exports.MetadataInjector = exports.resolveArgs = exports.inject = exports.AutoMockedContainer = exports.EmptyContainer = exports.Container = void 0;
|
|
7
4
|
var Container_1 = require("./container/Container");
|
|
8
5
|
Object.defineProperty(exports, "Container", { enumerable: true, get: function () { return Container_1.Container; } });
|
|
9
6
|
var EmptyContainer_1 = require("./container/EmptyContainer");
|
|
@@ -79,5 +76,7 @@ Object.defineProperty(exports, "getParameterMetadata", { enumerable: true, get:
|
|
|
79
76
|
// Others
|
|
80
77
|
var resolve_1 = require("./resolve");
|
|
81
78
|
Object.defineProperty(exports, "by", { enumerable: true, get: function () { return resolve_1.by; } });
|
|
79
|
+
var utils_1 = require("./utils");
|
|
80
|
+
Object.defineProperty(exports, "Is", { enumerable: true, get: function () { return utils_1.Is; } });
|
|
82
81
|
var DepKey_1 = require("./DepKey");
|
|
83
82
|
Object.defineProperty(exports, "depKey", { enumerable: true, get: function () { return DepKey_1.depKey; } });
|
|
@@ -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/injector/inject.js
CHANGED
|
@@ -4,10 +4,9 @@ exports.resolveArgs = exports.toInjectFn = exports.inject = void 0;
|
|
|
4
4
|
exports.isInjectBuilder = isInjectBuilder;
|
|
5
5
|
const metadata_1 = require("../metadata");
|
|
6
6
|
const utils_1 = require("../utils");
|
|
7
|
-
const IContainer_1 = require("../container/IContainer");
|
|
8
7
|
const HookContext_1 = require("../hooks/HookContext");
|
|
9
8
|
const inject = (fn) => (target, propertyKey, parameterIndex) => {
|
|
10
|
-
(0, metadata_1.setParameterMetadata)((0, HookContext_1.hookMetaKey)(propertyKey), (0, exports.toInjectFn)(fn))(
|
|
9
|
+
(0, metadata_1.setParameterMetadata)((0, HookContext_1.hookMetaKey)(propertyKey), (0, exports.toInjectFn)(fn))(utils_1.Is.instance(target) ? target.constructor : target, propertyKey, parameterIndex);
|
|
11
10
|
};
|
|
12
11
|
exports.inject = inject;
|
|
13
12
|
function isInjectBuilder(fn) {
|
|
@@ -17,10 +16,10 @@ const toInjectFn = (target) => {
|
|
|
17
16
|
if (typeof target === 'object' && isInjectBuilder(target)) {
|
|
18
17
|
return (s) => target.resolve(s);
|
|
19
18
|
}
|
|
20
|
-
if (
|
|
19
|
+
if (utils_1.Is.constructor(target)) {
|
|
21
20
|
return (scope) => scope.resolveClass(target);
|
|
22
21
|
}
|
|
23
|
-
if (
|
|
22
|
+
if (utils_1.Is.dependencyKey(target)) {
|
|
24
23
|
return (scope) => scope.resolveOne(target);
|
|
25
24
|
}
|
|
26
25
|
return target;
|
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
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.asKey = exports.asAlias = exports.register = exports.getTransformers = exports.scope = void 0;
|
|
4
|
-
const
|
|
4
|
+
const utils_1 = require("../utils");
|
|
5
5
|
const metadata_1 = require("../metadata");
|
|
6
6
|
const ProviderPipe_1 = require("../provider/ProviderPipe");
|
|
7
7
|
const scope = (...predicates) => (r) => r.when(...predicates);
|
|
@@ -11,7 +11,7 @@ const getTransformers = (Target) => (0, metadata_1.getMetadata)(Target, METADATA
|
|
|
11
11
|
exports.getTransformers = getTransformers;
|
|
12
12
|
const register = (...mappers) => (0, metadata_1.setMetadata)(METADATA_KEY, mappers.map((m) => ((0, ProviderPipe_1.isProviderPipe)(m) ? (r) => m.mapRegistration(r) : m)));
|
|
13
13
|
exports.register = register;
|
|
14
|
-
const asAlias = (target) => (r) => r.bindToAlias(
|
|
14
|
+
const asAlias = (target) => (r) => r.bindToAlias(utils_1.Is.dependencyKey(target) ? target : target.key);
|
|
15
15
|
exports.asAlias = asAlias;
|
|
16
16
|
const asKey = (key) => (r) => r.bindToKey(key);
|
|
17
17
|
exports.asKey = asKey;
|
|
@@ -15,7 +15,7 @@ class Registration {
|
|
|
15
15
|
return transform(new Registration(() => Provider_1.Provider.fromClass(Target), Target.name));
|
|
16
16
|
}
|
|
17
17
|
static fromValue(value) {
|
|
18
|
-
if (
|
|
18
|
+
if (utils_1.Is.constructor(value)) {
|
|
19
19
|
const transform = (0, utils_1.pipe)(...(0, IRegistration_1.getTransformers)(value));
|
|
20
20
|
return transform(new Registration(() => Provider_1.Provider.fromValue(value), value.name));
|
|
21
21
|
}
|
package/cjm/resolve.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.by = exports.InstancesResolver = exports.InjectionResolver = void 0;
|
|
4
|
-
const
|
|
5
|
-
const DepKey_1 = require("./DepKey");
|
|
4
|
+
const utils_1 = require("./utils");
|
|
6
5
|
class InjectionResolver {
|
|
7
6
|
resolveByOptions;
|
|
8
7
|
isLazy = false;
|
|
@@ -55,18 +54,18 @@ class InstancesResolver {
|
|
|
55
54
|
exports.InstancesResolver = InstancesResolver;
|
|
56
55
|
exports.by = {
|
|
57
56
|
many: (target) => {
|
|
58
|
-
const alias =
|
|
57
|
+
const alias = utils_1.Is.dependencyKey(target) ? target : target.key;
|
|
59
58
|
return new InjectionResolver((s, options) => s.resolveMany(alias, options));
|
|
60
59
|
},
|
|
61
60
|
one: (target) => {
|
|
62
|
-
const key =
|
|
61
|
+
const key = utils_1.Is.DepKey(target) ? target.key : target;
|
|
63
62
|
return new InjectionResolver((s, options) => s.resolveOne(key, options));
|
|
64
63
|
},
|
|
65
64
|
/**
|
|
66
65
|
* Use it only for optimization. Otherwise, recommended to use `by.one`
|
|
67
66
|
*/
|
|
68
67
|
aliasOne: (target) => {
|
|
69
|
-
const alias =
|
|
68
|
+
const alias = utils_1.Is.DepKey(target) ? target.key : target;
|
|
70
69
|
return new InjectionResolver((s, options) => s.resolveOneByAlias(alias, options));
|
|
71
70
|
},
|
|
72
71
|
instances: (predicate = () => true) => new InstancesResolver(predicate),
|
package/cjm/utils.js
CHANGED
|
@@ -1,20 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Is = exports.Filter = exports.List = exports.promisify = exports.constant = exports.pipe =
|
|
4
|
-
exports.isInstance = isInstance;
|
|
3
|
+
exports.Is = exports.Filter = exports.List = exports.promisify = exports.constant = exports.pipe = void 0;
|
|
5
4
|
exports.fillEmptyIndexes = fillEmptyIndexes;
|
|
6
5
|
exports.lazyProxy = lazyProxy;
|
|
7
|
-
|
|
8
|
-
* @deprecated Use Is.constructor instead
|
|
9
|
-
*/
|
|
10
|
-
const isConstructor = (T) => typeof T === 'function' && !!T.prototype;
|
|
11
|
-
exports.isConstructor = isConstructor;
|
|
12
|
-
/**
|
|
13
|
-
* @deprecated Use Is.instance instead
|
|
14
|
-
*/
|
|
15
|
-
function isInstance(target) {
|
|
16
|
-
return Object.prototype.hasOwnProperty.call(target, 'constructor');
|
|
17
|
-
}
|
|
6
|
+
exports.toLazyIf = toLazyIf;
|
|
18
7
|
const pipe = (...mappers) => (value) => mappers.reduce((acc, current) => current(acc), value);
|
|
19
8
|
exports.pipe = pipe;
|
|
20
9
|
function fillEmptyIndexes(baseArr, insertArr) {
|
|
@@ -39,6 +28,12 @@ function lazyProxy(resolveInstance) {
|
|
|
39
28
|
},
|
|
40
29
|
});
|
|
41
30
|
}
|
|
31
|
+
function toLazyIf(resolveInstance, isLazy = false) {
|
|
32
|
+
if (isLazy) {
|
|
33
|
+
return lazyProxy(resolveInstance);
|
|
34
|
+
}
|
|
35
|
+
return resolveInstance();
|
|
36
|
+
}
|
|
42
37
|
const promisify = (arg) => (arg instanceof Promise ? arg : Promise.resolve(arg));
|
|
43
38
|
exports.promisify = promisify;
|
|
44
39
|
exports.List = {
|
|
@@ -56,4 +51,7 @@ exports.Is = {
|
|
|
56
51
|
constructor: (target) => typeof target === 'function' && !!target.prototype,
|
|
57
52
|
dependencyKey: (target) => ['string', 'symbol'].includes(typeof target),
|
|
58
53
|
injectBuilder: (target) => exports.Is.object(target) && 'resolve' in target && typeof target['resolve'] === 'function',
|
|
54
|
+
DepKey: (key) => {
|
|
55
|
+
return typeof key === 'object' && key !== null && 'key' in key;
|
|
56
|
+
},
|
|
59
57
|
};
|
package/esm/DepKey.js
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
import { Registration } from './registration/Registration';
|
|
2
2
|
import { Provider } from './provider/Provider';
|
|
3
|
-
export const isDepKey = (key) => {
|
|
4
|
-
return typeof key === 'object' && key !== null && 'key' in key;
|
|
5
|
-
};
|
|
6
3
|
export const depKey = (key) => {
|
|
7
4
|
const scopePredicates = [];
|
|
8
5
|
const mappers = [];
|
|
@@ -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,10 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Is } from '../utils';
|
|
2
2
|
import { DependencyNotFoundError } from '../errors/DependencyNotFoundError';
|
|
3
|
-
export function isDependencyKey(token) {
|
|
4
|
-
return ['string', 'symbol'].includes(typeof token);
|
|
5
|
-
}
|
|
6
3
|
export const DEFAULT_CONTAINER_RESOLVER = (scope, keyOrAlias, options) => {
|
|
7
|
-
if (
|
|
4
|
+
if (Is.constructor(keyOrAlias)) {
|
|
8
5
|
return scope.resolveClass(keyOrAlias, options);
|
|
9
6
|
}
|
|
10
7
|
try {
|
package/esm/hooks/hook.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { createHookContext } from './HookContext';
|
|
2
|
-
import {
|
|
2
|
+
import { Is, promisify } from '../utils';
|
|
3
3
|
import { UnexpectedHookResultError } from '../errors/UnexpectedHookResultError';
|
|
4
4
|
import { toInjectFn } from '../injector/inject';
|
|
5
5
|
const isHookClassConstructor = (execute) => {
|
|
6
|
-
return
|
|
6
|
+
return Is.constructor(execute) && execute.prototype.execute;
|
|
7
7
|
};
|
|
8
8
|
export const toHookFn = (execute) => isHookClassConstructor(execute) ? (context) => context.scope.resolveOne(execute).execute(context) : execute;
|
|
9
9
|
export const hook = (key, ...fns) => (target, propertyKey) => {
|
package/esm/index.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
// Containers
|
|
2
|
-
export { isDependencyKey, } from './container/IContainer';
|
|
3
1
|
export { Container } from './container/Container';
|
|
4
2
|
export { EmptyContainer } from './container/EmptyContainer';
|
|
5
3
|
export { AutoMockedContainer } from './container/AutoMockedContainer';
|
|
@@ -29,4 +27,5 @@ export { HookContext } from './hooks/HookContext';
|
|
|
29
27
|
export { setMetadata, getMetadata, setParameterMetadata, setMethodMetadata, getMethodMetadata, getParameterMetadata, } from './metadata';
|
|
30
28
|
// Others
|
|
31
29
|
export { by } from './resolve';
|
|
30
|
+
export { Is } from './utils';
|
|
32
31
|
export { depKey } from './DepKey';
|
|
@@ -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/injector/inject.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { getParameterMetadata, setParameterMetadata } from '../metadata';
|
|
2
|
-
import { constant, fillEmptyIndexes,
|
|
3
|
-
import { isDependencyKey } from '../container/IContainer';
|
|
2
|
+
import { constant, fillEmptyIndexes, Is } from '../utils';
|
|
4
3
|
import { hookMetaKey } from '../hooks/HookContext';
|
|
5
4
|
export const inject = (fn) => (target, propertyKey, parameterIndex) => {
|
|
6
|
-
setParameterMetadata(hookMetaKey(propertyKey), toInjectFn(fn))(
|
|
5
|
+
setParameterMetadata(hookMetaKey(propertyKey), toInjectFn(fn))(Is.instance(target) ? target.constructor : target, propertyKey, parameterIndex);
|
|
7
6
|
};
|
|
8
7
|
export function isInjectBuilder(fn) {
|
|
9
8
|
return 'resolve' in fn && typeof fn['resolve'] === 'function';
|
|
@@ -12,10 +11,10 @@ export const toInjectFn = (target) => {
|
|
|
12
11
|
if (typeof target === 'object' && isInjectBuilder(target)) {
|
|
13
12
|
return (s) => target.resolve(s);
|
|
14
13
|
}
|
|
15
|
-
if (
|
|
14
|
+
if (Is.constructor(target)) {
|
|
16
15
|
return (scope) => scope.resolveClass(target);
|
|
17
16
|
}
|
|
18
|
-
if (
|
|
17
|
+
if (Is.dependencyKey(target)) {
|
|
19
18
|
return (scope) => scope.resolveOne(target);
|
|
20
19
|
}
|
|
21
20
|
return target;
|
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
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Is } from '../utils';
|
|
2
2
|
import { getMetadata, setMetadata } from '../metadata';
|
|
3
3
|
import { isProviderPipe } from '../provider/ProviderPipe';
|
|
4
4
|
export const scope = (...predicates) => (r) => r.when(...predicates);
|
|
5
5
|
const METADATA_KEY = 'registration';
|
|
6
6
|
export const getTransformers = (Target) => getMetadata(Target, METADATA_KEY) ?? [];
|
|
7
7
|
export const register = (...mappers) => setMetadata(METADATA_KEY, mappers.map((m) => (isProviderPipe(m) ? (r) => m.mapRegistration(r) : m)));
|
|
8
|
-
export const asAlias = (target) => (r) => r.bindToAlias(
|
|
8
|
+
export const asAlias = (target) => (r) => r.bindToAlias(Is.dependencyKey(target) ? target : target.key);
|
|
9
9
|
export const asKey = (key) => (r) => r.bindToKey(key);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Is, pipe } from '../utils';
|
|
2
2
|
import { Provider } from '../provider/Provider';
|
|
3
3
|
import { DependencyMissingKeyError } from '../errors/DependencyMissingKeyError';
|
|
4
4
|
import { getTransformers } from './IRegistration';
|
|
@@ -12,7 +12,7 @@ export class Registration {
|
|
|
12
12
|
return transform(new Registration(() => Provider.fromClass(Target), Target.name));
|
|
13
13
|
}
|
|
14
14
|
static fromValue(value) {
|
|
15
|
-
if (
|
|
15
|
+
if (Is.constructor(value)) {
|
|
16
16
|
const transform = pipe(...getTransformers(value));
|
|
17
17
|
return transform(new Registration(() => Provider.fromValue(value), value.name));
|
|
18
18
|
}
|
package/esm/resolve.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { isDepKey } from './DepKey';
|
|
1
|
+
import { Is } from './utils';
|
|
3
2
|
export class InjectionResolver {
|
|
4
3
|
resolveByOptions;
|
|
5
4
|
isLazy = false;
|
|
@@ -50,18 +49,18 @@ export class InstancesResolver {
|
|
|
50
49
|
}
|
|
51
50
|
export const by = {
|
|
52
51
|
many: (target) => {
|
|
53
|
-
const alias =
|
|
52
|
+
const alias = Is.dependencyKey(target) ? target : target.key;
|
|
54
53
|
return new InjectionResolver((s, options) => s.resolveMany(alias, options));
|
|
55
54
|
},
|
|
56
55
|
one: (target) => {
|
|
57
|
-
const key =
|
|
56
|
+
const key = Is.DepKey(target) ? target.key : target;
|
|
58
57
|
return new InjectionResolver((s, options) => s.resolveOne(key, options));
|
|
59
58
|
},
|
|
60
59
|
/**
|
|
61
60
|
* Use it only for optimization. Otherwise, recommended to use `by.one`
|
|
62
61
|
*/
|
|
63
62
|
aliasOne: (target) => {
|
|
64
|
-
const alias =
|
|
63
|
+
const alias = Is.DepKey(target) ? target.key : target;
|
|
65
64
|
return new InjectionResolver((s, options) => s.resolveOneByAlias(alias, options));
|
|
66
65
|
},
|
|
67
66
|
instances: (predicate = () => true) => new InstancesResolver(predicate),
|
package/esm/utils.js
CHANGED
|
@@ -1,13 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @deprecated Use Is.constructor instead
|
|
3
|
-
*/
|
|
4
|
-
export const isConstructor = (T) => typeof T === 'function' && !!T.prototype;
|
|
5
|
-
/**
|
|
6
|
-
* @deprecated Use Is.instance instead
|
|
7
|
-
*/
|
|
8
|
-
export function isInstance(target) {
|
|
9
|
-
return Object.prototype.hasOwnProperty.call(target, 'constructor');
|
|
10
|
-
}
|
|
11
1
|
export const pipe = (...mappers) => (value) => mappers.reduce((acc, current) => current(acc), value);
|
|
12
2
|
export function fillEmptyIndexes(baseArr, insertArr) {
|
|
13
3
|
const a = [...baseArr];
|
|
@@ -30,6 +20,12 @@ export function lazyProxy(resolveInstance) {
|
|
|
30
20
|
},
|
|
31
21
|
});
|
|
32
22
|
}
|
|
23
|
+
export function toLazyIf(resolveInstance, isLazy = false) {
|
|
24
|
+
if (isLazy) {
|
|
25
|
+
return lazyProxy(resolveInstance);
|
|
26
|
+
}
|
|
27
|
+
return resolveInstance();
|
|
28
|
+
}
|
|
33
29
|
export const promisify = (arg) => (arg instanceof Promise ? arg : Promise.resolve(arg));
|
|
34
30
|
export const List = {
|
|
35
31
|
lastOf: (arr) => arr[arr.length - 1],
|
|
@@ -46,4 +42,7 @@ export const Is = {
|
|
|
46
42
|
constructor: (target) => typeof target === 'function' && !!target.prototype,
|
|
47
43
|
dependencyKey: (target) => ['string', 'symbol'].includes(typeof target),
|
|
48
44
|
injectBuilder: (target) => Is.object(target) && 'resolve' in target && typeof target['resolve'] === 'function',
|
|
45
|
+
DepKey: (key) => {
|
|
46
|
+
return typeof key === 'object' && key !== null && 'key' in key;
|
|
47
|
+
},
|
|
49
48
|
};
|
package/package.json
CHANGED
package/typings/DepKey.d.ts
CHANGED
|
@@ -12,5 +12,4 @@ export type DepKey<T> = IInjectFnResolver<T> & {
|
|
|
12
12
|
when(value: ScopePredicate): DepKey<T>;
|
|
13
13
|
asAlias: (registration: IRegistration<T>) => IRegistration<T>;
|
|
14
14
|
};
|
|
15
|
-
export declare const isDepKey: <T>(key: unknown) => key is DepKey<T>;
|
|
16
15
|
export declare const depKey: <T>(key: DependencyKey) => DepKey<T>;
|
|
@@ -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,18 +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
|
-
export declare function isDependencyKey(token: unknown): token is DependencyKey;
|
|
7
6
|
export type InjectionToken<T = unknown> = constructor<T> | DependencyKey;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
child?: Tagged;
|
|
11
|
-
lazy?: boolean;
|
|
7
|
+
type WithChild = {
|
|
8
|
+
child: Tagged;
|
|
12
9
|
};
|
|
13
|
-
export type
|
|
14
|
-
|
|
10
|
+
export type ResolveOneOptions = ProviderOptions & Partial<WithChild>;
|
|
11
|
+
type WithExcludedKeys = {
|
|
12
|
+
excludedKeys: Set<DependencyKey>;
|
|
15
13
|
};
|
|
14
|
+
export type ResolveManyOptions = ResolveOneOptions & Partial<WithExcludedKeys>;
|
|
16
15
|
export interface Resolvable {
|
|
17
16
|
resolve<T>(key: InjectionToken<T>, options?: ResolveOneOptions): T;
|
|
18
17
|
}
|
|
@@ -22,9 +21,10 @@ export interface IContainerModule {
|
|
|
22
21
|
export interface Tagged {
|
|
23
22
|
hasTag(tag: Tag): boolean;
|
|
24
23
|
}
|
|
25
|
-
|
|
26
|
-
tags
|
|
24
|
+
type WithTags = {
|
|
25
|
+
tags: Tag[];
|
|
27
26
|
};
|
|
27
|
+
export type CreateScopeOptions = Partial<WithTags>;
|
|
28
28
|
export interface Instance<T = unknown> {
|
|
29
29
|
new (...args: unknown[]): T;
|
|
30
30
|
}
|
|
@@ -36,9 +36,7 @@ export interface IContainer extends Tagged {
|
|
|
36
36
|
register(key: DependencyKey, value: IProvider, options?: RegisterOptions): this;
|
|
37
37
|
addRegistration(registration: IRegistration): this;
|
|
38
38
|
getRegistrations(): IRegistration[];
|
|
39
|
-
resolveClass<T>(target: constructor<T>, options?:
|
|
40
|
-
args?: unknown[];
|
|
41
|
-
}): T;
|
|
39
|
+
resolveClass<T>(target: constructor<T>, options?: ProviderOptions): T;
|
|
42
40
|
resolveOne<T>(alias: constructor<T> | DependencyKey, options?: ResolveOneOptions): T;
|
|
43
41
|
resolveOneByKey<T>(key: DependencyKey, options?: ResolveOneOptions): T;
|
|
44
42
|
resolveOneByAlias<T>(key: DependencyKey, options?: ResolveOneOptions): T;
|
|
@@ -52,3 +50,4 @@ export interface IContainer extends Tagged {
|
|
|
52
50
|
dispose(): void;
|
|
53
51
|
}
|
|
54
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,
|
|
1
|
+
export { type IContainer, type Resolvable, type IContainerModule, type DependencyKey, type InjectionToken, type Tag, type Tagged, type Instance, } 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, } 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';
|
|
@@ -23,6 +23,6 @@ export { getHooks, hook, hasHooks, type HookFn, type HookClass, runHooks, runHoo
|
|
|
23
23
|
export { HookContext, type InjectFn, type IHookContext } from './hooks/HookContext';
|
|
24
24
|
export { setMetadata, getMetadata, setParameterMetadata, setMethodMetadata, getMethodMetadata, getParameterMetadata, } from './metadata';
|
|
25
25
|
export { by } from './resolve';
|
|
26
|
-
export { type constructor } from './utils';
|
|
26
|
+
export { type constructor, Is } from './utils';
|
|
27
27
|
export { depKey, type DepKey } from './DepKey';
|
|
28
28
|
export { type InstancePredicate } from './resolve';
|
|
@@ -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>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { DependencyKey, IContainer, IContainerModule } from '../container/IContainer';
|
|
2
|
-
import
|
|
2
|
+
import { constructor, MapFn } from '../utils';
|
|
3
3
|
import type { IProvider } from '../provider/IProvider';
|
|
4
4
|
import type { DepKey } from '../DepKey';
|
|
5
5
|
import { ProviderPipe } from '../provider/ProviderPipe';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { DependencyKey, IContainer } from '../container/IContainer';
|
|
2
|
-
import
|
|
2
|
+
import { constructor, MapFn } from '../utils';
|
|
3
3
|
import type { IProvider, ResolveDependency } from '../provider/IProvider';
|
|
4
4
|
import type { IRegistration, ScopePredicate } from './IRegistration';
|
|
5
5
|
import type { ProviderPipe } from '../provider/ProviderPipe';
|
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
|
@@ -1,22 +1,16 @@
|
|
|
1
1
|
import { DependencyKey } from './container/IContainer';
|
|
2
2
|
import { IInjectFnResolver } from './injector/IInjector';
|
|
3
|
+
import { DepKey } from './DepKey';
|
|
3
4
|
export type constructor<T> = new (...args: any[]) => T;
|
|
4
|
-
/**
|
|
5
|
-
* @deprecated Use Is.constructor instead
|
|
6
|
-
*/
|
|
7
|
-
export declare const isConstructor: (T: unknown) => T is constructor<unknown>;
|
|
8
5
|
export interface InstanceOfClass<T = unknown> {
|
|
9
6
|
new (...args: unknown[]): T;
|
|
10
7
|
}
|
|
11
|
-
/**
|
|
12
|
-
* @deprecated Use Is.instance instead
|
|
13
|
-
*/
|
|
14
|
-
export declare function isInstance(target: object): target is InstanceOfClass;
|
|
15
8
|
export type MapFn<T> = (value: T) => T;
|
|
16
9
|
export declare const pipe: <T>(...mappers: MapFn<T>[]) => MapFn<T>;
|
|
17
10
|
export declare function fillEmptyIndexes<T>(baseArr: (T | undefined)[], insertArr: T[]): T[];
|
|
18
11
|
export declare const constant: <T>(value: T) => () => T;
|
|
19
12
|
export declare function lazyProxy<T>(resolveInstance: () => T): T;
|
|
13
|
+
export declare function toLazyIf<T>(resolveInstance: () => T, isLazy?: boolean): T;
|
|
20
14
|
export declare const promisify: <T>(arg: T | Promise<T>) => Promise<T>;
|
|
21
15
|
export declare const List: {
|
|
22
16
|
lastOf: <T>(arr: T[]) => T;
|
|
@@ -30,4 +24,5 @@ export declare const Is: {
|
|
|
30
24
|
constructor: (target: unknown) => target is constructor<unknown>;
|
|
31
25
|
dependencyKey: (target: unknown) => target is DependencyKey;
|
|
32
26
|
injectBuilder: (target: unknown) => target is IInjectFnResolver<unknown>;
|
|
27
|
+
DepKey: <T>(key: unknown) => key is DepKey<T>;
|
|
33
28
|
};
|