ts-ioc-container 54.0.1 → 55.0.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/container/Container.js +16 -0
- package/cjm/container/EmptyContainer.js +7 -0
- package/cjm/errors/ContainerNotFoundError.js +12 -0
- package/cjm/index.js +13 -11
- package/cjm/injector/MetadataInjector.js +1 -1
- package/cjm/metadata/class.js +7 -7
- package/cjm/metadata/method.js +7 -7
- package/cjm/metadata/parameter.js +7 -7
- package/cjm/registration/IRegistration.js +1 -1
- package/esm/container/Container.js +16 -0
- package/esm/container/EmptyContainer.js +7 -0
- package/esm/errors/ContainerNotFoundError.js +8 -0
- package/esm/index.js +4 -3
- package/esm/injector/MetadataInjector.js +2 -2
- package/esm/metadata/class.js +3 -3
- package/esm/metadata/method.js +3 -3
- package/esm/metadata/parameter.js +3 -3
- package/esm/registration/IRegistration.js +2 -2
- package/package.json +1 -1
- package/typings/container/Container.d.ts +2 -0
- package/typings/container/EmptyContainer.d.ts +2 -0
- package/typings/container/IContainer.d.ts +2 -0
- package/typings/errors/ContainerNotFoundError.d.ts +5 -0
- package/typings/index.d.ts +4 -3
- package/typings/metadata/class.d.ts +3 -3
- package/typings/metadata/method.d.ts +3 -3
- package/typings/metadata/parameter.d.ts +3 -3
|
@@ -6,6 +6,7 @@ const ContainerDisposedError_1 = require("../errors/ContainerDisposedError");
|
|
|
6
6
|
const MetadataInjector_1 = require("../injector/MetadataInjector");
|
|
7
7
|
const AliasMap_1 = require("./AliasMap");
|
|
8
8
|
const DependencyNotFoundError_1 = require("../errors/DependencyNotFoundError");
|
|
9
|
+
const ContainerNotFoundError_1 = require("../errors/ContainerNotFoundError");
|
|
9
10
|
const basic_1 = require("../utils/basic");
|
|
10
11
|
const array_1 = require("../utils/array");
|
|
11
12
|
class Container {
|
|
@@ -131,6 +132,21 @@ class Container {
|
|
|
131
132
|
getScopes() {
|
|
132
133
|
return [...this.scopes];
|
|
133
134
|
}
|
|
135
|
+
hasInstance(instance) {
|
|
136
|
+
return this.instances.includes(instance);
|
|
137
|
+
}
|
|
138
|
+
getScopeByInstanceOrFail(instance) {
|
|
139
|
+
this.validateContainer();
|
|
140
|
+
const queue = [this];
|
|
141
|
+
while (queue.length > 0) {
|
|
142
|
+
const scope = queue.shift();
|
|
143
|
+
if (scope.hasInstance(instance)) {
|
|
144
|
+
return scope;
|
|
145
|
+
}
|
|
146
|
+
queue.push(...scope.getScopes());
|
|
147
|
+
}
|
|
148
|
+
throw new ContainerNotFoundError_1.ContainerNotFoundError('Cannot find scope for the given instance');
|
|
149
|
+
}
|
|
134
150
|
removeScope(child) {
|
|
135
151
|
this.scopes = this.scopes.filter((s) => s !== child);
|
|
136
152
|
}
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.EmptyContainer = void 0;
|
|
4
4
|
const MethodNotImplementedError_1 = require("../errors/MethodNotImplementedError");
|
|
5
5
|
const DependencyNotFoundError_1 = require("../errors/DependencyNotFoundError");
|
|
6
|
+
const ContainerNotFoundError_1 = require("../errors/ContainerNotFoundError");
|
|
6
7
|
class EmptyContainer {
|
|
7
8
|
get isDisposed() {
|
|
8
9
|
throw new MethodNotImplementedError_1.MethodNotImplementedError();
|
|
@@ -14,9 +15,15 @@ class EmptyContainer {
|
|
|
14
15
|
getScopes() {
|
|
15
16
|
return [];
|
|
16
17
|
}
|
|
18
|
+
getScopeByInstanceOrFail(instance) {
|
|
19
|
+
throw new ContainerNotFoundError_1.ContainerNotFoundError('Cannot find scope for the given instance');
|
|
20
|
+
}
|
|
17
21
|
getInstances() {
|
|
18
22
|
return [];
|
|
19
23
|
}
|
|
24
|
+
hasInstance(instance) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
20
27
|
createScope() {
|
|
21
28
|
throw new MethodNotImplementedError_1.MethodNotImplementedError();
|
|
22
29
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ContainerNotFoundError = void 0;
|
|
4
|
+
const ContainerError_1 = require("./ContainerError");
|
|
5
|
+
class ContainerNotFoundError extends ContainerError_1.ContainerError {
|
|
6
|
+
name = 'ContainerNotFoundError';
|
|
7
|
+
constructor(message) {
|
|
8
|
+
super(message);
|
|
9
|
+
Object.setPrototypeOf(this, ContainerNotFoundError.prototype);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.ContainerNotFoundError = ContainerNotFoundError;
|
package/cjm/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
exports.resolveConstructor = exports.Is = exports.pipe = exports.select = exports.once = exports.shallowCache = exports.debounce = exports.throttle = exports.handleAsyncError = exports.handleError = exports.getMethodTags = exports.
|
|
3
|
+
exports.ClassToken = exports.toSingleAlias = exports.SingleAliasToken = exports.toGroupAlias = exports.GroupAliasToken = exports.InjectionToken = exports.HooksRunner = exports.AddOnDisposeHookModule = exports.onDispose = exports.onDisposeHooksRunner = exports.AddOnConstructHookModule = exports.onConstruct = exports.onConstructHooksRunner = exports.injectProp = exports.createHookContext = exports.createHookContextFactory = exports.HookContext = exports.hasHooks = exports.hook = exports.getHooks = exports.CannonSingletonApplyTwiceError = exports.UnexpectedHookResultError = exports.ProviderDisposedError = exports.ContainerDisposedError = exports.MethodNotImplementedError = exports.DependencyMissingKeyError = exports.ContainerNotFoundError = exports.DependencyNotFoundError = exports.Registration = exports.appendArgsFn = exports.appendArgs = exports.decorate = exports.singleton = exports.lazy = exports.scopeAccess = exports.scope = exports.bindTo = exports.register = exports.Provider = exports.ProxyInjector = exports.SimpleInjector = exports.resolveArgs = exports.argsFn = exports.args = exports.inject = exports.MetadataInjector = exports.Injector = exports.EmptyContainer = exports.Container = exports.isDependencyKey = void 0;
|
|
4
|
+
exports.resolveConstructor = exports.Is = exports.pipe = exports.select = exports.once = exports.shallowCache = exports.debounce = exports.throttle = exports.handleAsyncError = exports.handleError = exports.getMethodTags = exports.addMethodTag = exports.getMethodLabels = exports.addMethodLabel = exports.getMethodMeta = exports.addMethodMeta = exports.getParamTags = exports.addParamTag = exports.getParamLabels = exports.addParamLabel = exports.getParamMeta = exports.addParamMeta = exports.getClassTags = exports.addClassTag = exports.getClassLabels = exports.addClassLabel = exports.getClassMeta = exports.addClassMeta = exports.GroupInstanceToken = exports.ConstantToken = exports.FunctionToken = exports.SingleToken = void 0;
|
|
5
5
|
// Containers
|
|
6
6
|
var IContainer_1 = require("./container/IContainer");
|
|
7
7
|
Object.defineProperty(exports, "isDependencyKey", { enumerable: true, get: function () { return IContainer_1.isDependencyKey; } });
|
|
@@ -40,6 +40,8 @@ Object.defineProperty(exports, "Registration", { enumerable: true, get: function
|
|
|
40
40
|
// Errors
|
|
41
41
|
var DependencyNotFoundError_1 = require("./errors/DependencyNotFoundError");
|
|
42
42
|
Object.defineProperty(exports, "DependencyNotFoundError", { enumerable: true, get: function () { return DependencyNotFoundError_1.DependencyNotFoundError; } });
|
|
43
|
+
var ContainerNotFoundError_1 = require("./errors/ContainerNotFoundError");
|
|
44
|
+
Object.defineProperty(exports, "ContainerNotFoundError", { enumerable: true, get: function () { return ContainerNotFoundError_1.ContainerNotFoundError; } });
|
|
43
45
|
var DependencyMissingKeyError_1 = require("./errors/DependencyMissingKeyError");
|
|
44
46
|
Object.defineProperty(exports, "DependencyMissingKeyError", { enumerable: true, get: function () { return DependencyMissingKeyError_1.DependencyMissingKeyError; } });
|
|
45
47
|
var MethodNotImplementedError_1 = require("./errors/MethodNotImplementedError");
|
|
@@ -94,25 +96,25 @@ var GroupInstanceToken_1 = require("./token/GroupInstanceToken");
|
|
|
94
96
|
Object.defineProperty(exports, "GroupInstanceToken", { enumerable: true, get: function () { return GroupInstanceToken_1.GroupInstanceToken; } });
|
|
95
97
|
// Metadata
|
|
96
98
|
var class_1 = require("./metadata/class");
|
|
97
|
-
Object.defineProperty(exports, "
|
|
99
|
+
Object.defineProperty(exports, "addClassMeta", { enumerable: true, get: function () { return class_1.addClassMeta; } });
|
|
98
100
|
Object.defineProperty(exports, "getClassMeta", { enumerable: true, get: function () { return class_1.getClassMeta; } });
|
|
99
|
-
Object.defineProperty(exports, "
|
|
101
|
+
Object.defineProperty(exports, "addClassLabel", { enumerable: true, get: function () { return class_1.addClassLabel; } });
|
|
100
102
|
Object.defineProperty(exports, "getClassLabels", { enumerable: true, get: function () { return class_1.getClassLabels; } });
|
|
101
|
-
Object.defineProperty(exports, "
|
|
103
|
+
Object.defineProperty(exports, "addClassTag", { enumerable: true, get: function () { return class_1.addClassTag; } });
|
|
102
104
|
Object.defineProperty(exports, "getClassTags", { enumerable: true, get: function () { return class_1.getClassTags; } });
|
|
103
105
|
var parameter_1 = require("./metadata/parameter");
|
|
104
|
-
Object.defineProperty(exports, "
|
|
106
|
+
Object.defineProperty(exports, "addParamMeta", { enumerable: true, get: function () { return parameter_1.addParamMeta; } });
|
|
105
107
|
Object.defineProperty(exports, "getParamMeta", { enumerable: true, get: function () { return parameter_1.getParamMeta; } });
|
|
106
|
-
Object.defineProperty(exports, "
|
|
108
|
+
Object.defineProperty(exports, "addParamLabel", { enumerable: true, get: function () { return parameter_1.addParamLabel; } });
|
|
107
109
|
Object.defineProperty(exports, "getParamLabels", { enumerable: true, get: function () { return parameter_1.getParamLabels; } });
|
|
108
|
-
Object.defineProperty(exports, "
|
|
110
|
+
Object.defineProperty(exports, "addParamTag", { enumerable: true, get: function () { return parameter_1.addParamTag; } });
|
|
109
111
|
Object.defineProperty(exports, "getParamTags", { enumerable: true, get: function () { return parameter_1.getParamTags; } });
|
|
110
112
|
var method_1 = require("./metadata/method");
|
|
111
|
-
Object.defineProperty(exports, "
|
|
113
|
+
Object.defineProperty(exports, "addMethodMeta", { enumerable: true, get: function () { return method_1.addMethodMeta; } });
|
|
112
114
|
Object.defineProperty(exports, "getMethodMeta", { enumerable: true, get: function () { return method_1.getMethodMeta; } });
|
|
113
|
-
Object.defineProperty(exports, "
|
|
115
|
+
Object.defineProperty(exports, "addMethodLabel", { enumerable: true, get: function () { return method_1.addMethodLabel; } });
|
|
114
116
|
Object.defineProperty(exports, "getMethodLabels", { enumerable: true, get: function () { return method_1.getMethodLabels; } });
|
|
115
|
-
Object.defineProperty(exports, "
|
|
117
|
+
Object.defineProperty(exports, "addMethodTag", { enumerable: true, get: function () { return method_1.addMethodTag; } });
|
|
116
118
|
Object.defineProperty(exports, "getMethodTags", { enumerable: true, get: function () { return method_1.getMethodTags; } });
|
|
117
119
|
var errorHandler_1 = require("./utils/errorHandler");
|
|
118
120
|
Object.defineProperty(exports, "handleError", { enumerable: true, get: function () { return errorHandler_1.handleError; } });
|
|
@@ -14,7 +14,7 @@ class MetadataInjector extends IInjector_1.Injector {
|
|
|
14
14
|
exports.MetadataInjector = MetadataInjector;
|
|
15
15
|
const hookMetaKey = (methodName = 'constructor') => `inject:${methodName}`;
|
|
16
16
|
const inject = (fn) => (target, propertyKey, parameterIndex) => {
|
|
17
|
-
(0, parameter_1.
|
|
17
|
+
(0, parameter_1.addParamMeta)(hookMetaKey(propertyKey), () => (0, toToken_1.toToken)(fn))(basic_1.Is.instance(target) ? target.constructor : target, propertyKey, parameterIndex);
|
|
18
18
|
};
|
|
19
19
|
exports.inject = inject;
|
|
20
20
|
const args = (index) => (c, { args = [] }) => {
|
package/cjm/metadata/class.js
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getClassTags = exports.
|
|
3
|
+
exports.getClassTags = exports.addClassTag = exports.getClassLabels = exports.addClassLabel = exports.addClassMeta = void 0;
|
|
4
4
|
exports.getClassMeta = getClassMeta;
|
|
5
5
|
const basic_1 = require("../utils/basic");
|
|
6
|
-
const
|
|
6
|
+
const addClassMeta = (key, mapFn) => (target) => {
|
|
7
7
|
const value = Reflect.getOwnMetadata(key, target);
|
|
8
8
|
Reflect.defineMetadata(key, mapFn(value), target);
|
|
9
9
|
};
|
|
10
|
-
exports.
|
|
10
|
+
exports.addClassMeta = addClassMeta;
|
|
11
11
|
function getClassMeta(target, key) {
|
|
12
12
|
return Reflect.getOwnMetadata(key, (0, basic_1.resolveConstructor)(target));
|
|
13
13
|
}
|
|
14
|
-
const
|
|
15
|
-
exports.
|
|
14
|
+
const addClassLabel = (key, label) => (0, exports.addClassMeta)('label', (prev = new Map()) => prev.set(key, label));
|
|
15
|
+
exports.addClassLabel = addClassLabel;
|
|
16
16
|
const getClassLabels = (target) => getClassMeta(target, 'label') ?? new Map();
|
|
17
17
|
exports.getClassLabels = getClassLabels;
|
|
18
|
-
const
|
|
19
|
-
exports.
|
|
18
|
+
const addClassTag = (tag) => (0, exports.addClassMeta)('tag', (prev = new Set()) => prev.add(tag));
|
|
19
|
+
exports.addClassTag = addClassTag;
|
|
20
20
|
const getClassTags = (target) => getClassMeta(target, 'tag') ?? new Set();
|
|
21
21
|
exports.getClassTags = getClassTags;
|
package/cjm/metadata/method.js
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getMethodTags = exports.
|
|
3
|
+
exports.getMethodTags = exports.addMethodTag = exports.getMethodLabels = exports.addMethodLabel = exports.getMethodMeta = exports.addMethodMeta = void 0;
|
|
4
4
|
const basic_1 = require("../utils/basic");
|
|
5
|
-
const
|
|
5
|
+
const addMethodMeta = (key, mapFn) => (target, propertyKey) => {
|
|
6
6
|
const metadata = Reflect.getMetadata(key, target.constructor, propertyKey);
|
|
7
7
|
Reflect.defineMetadata(key, mapFn(metadata), target.constructor, propertyKey);
|
|
8
8
|
};
|
|
9
|
-
exports.
|
|
9
|
+
exports.addMethodMeta = addMethodMeta;
|
|
10
10
|
const getMethodMeta = (key, target, propertyKey) => Reflect.getMetadata(key, (0, basic_1.resolveConstructor)(target), propertyKey);
|
|
11
11
|
exports.getMethodMeta = getMethodMeta;
|
|
12
|
-
const
|
|
13
|
-
exports.
|
|
12
|
+
const addMethodLabel = (key, label) => (0, exports.addMethodMeta)('label', (prev = new Map()) => prev.set(key, label));
|
|
13
|
+
exports.addMethodLabel = addMethodLabel;
|
|
14
14
|
const getMethodLabels = (target, propertyKey) => (0, exports.getMethodMeta)('label', target, propertyKey) ?? new Map();
|
|
15
15
|
exports.getMethodLabels = getMethodLabels;
|
|
16
|
-
const
|
|
17
|
-
exports.
|
|
16
|
+
const addMethodTag = (tag) => (0, exports.addMethodMeta)('tag', (prev = new Set()) => prev.add(tag));
|
|
17
|
+
exports.addMethodTag = addMethodTag;
|
|
18
18
|
const getMethodTags = (target, propertyKey) => (0, exports.getMethodMeta)('tag', target, propertyKey) ?? new Set();
|
|
19
19
|
exports.getMethodTags = getMethodTags;
|
|
@@ -1,32 +1,32 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getParamTags = exports.
|
|
3
|
+
exports.getParamTags = exports.addParamTag = exports.getParamLabels = exports.addParamLabel = exports.getParamMeta = exports.addParamMeta = void 0;
|
|
4
4
|
const basic_1 = require("../utils/basic");
|
|
5
|
-
const
|
|
5
|
+
const addParamMeta = (key, mapFn) => (target, _, parameterIndex) => {
|
|
6
6
|
const metadata = Reflect.getOwnMetadata(key, target) ?? [];
|
|
7
7
|
metadata[parameterIndex] = mapFn(metadata[parameterIndex]);
|
|
8
8
|
Reflect.defineMetadata(key, metadata, target);
|
|
9
9
|
};
|
|
10
|
-
exports.
|
|
10
|
+
exports.addParamMeta = addParamMeta;
|
|
11
11
|
const getParamMeta = (key, target) => {
|
|
12
12
|
return Reflect.getOwnMetadata(key, (0, basic_1.resolveConstructor)(target)) ?? [];
|
|
13
13
|
};
|
|
14
14
|
exports.getParamMeta = getParamMeta;
|
|
15
|
-
const
|
|
15
|
+
const addParamLabel = (key, label) => (0, exports.addParamMeta)('label', (prev) => {
|
|
16
16
|
const map = prev ?? new Map();
|
|
17
17
|
return map.set(key, label);
|
|
18
18
|
});
|
|
19
|
-
exports.
|
|
19
|
+
exports.addParamLabel = addParamLabel;
|
|
20
20
|
const getParamLabels = (target, parameterIndex) => {
|
|
21
21
|
const all = (0, exports.getParamMeta)('label', target);
|
|
22
22
|
return all[parameterIndex] ?? new Map();
|
|
23
23
|
};
|
|
24
24
|
exports.getParamLabels = getParamLabels;
|
|
25
|
-
const
|
|
25
|
+
const addParamTag = (tag) => (0, exports.addParamMeta)('tag', (prev) => {
|
|
26
26
|
const set = prev ?? new Set();
|
|
27
27
|
return set.add(tag);
|
|
28
28
|
});
|
|
29
|
-
exports.
|
|
29
|
+
exports.addParamTag = addParamTag;
|
|
30
30
|
const getParamTags = (target, parameterIndex) => {
|
|
31
31
|
const all = (0, exports.getParamMeta)('tag', target);
|
|
32
32
|
return all[parameterIndex] ?? new Set();
|
|
@@ -14,7 +14,7 @@ exports.registerPipe = registerPipe;
|
|
|
14
14
|
const METADATA_KEY = 'registration';
|
|
15
15
|
const getTransformers = (Target) => (0, class_1.getClassMeta)(Target, METADATA_KEY) ?? [];
|
|
16
16
|
exports.getTransformers = getTransformers;
|
|
17
|
-
const register = (...mappers) => (0, class_1.
|
|
17
|
+
const register = (...mappers) => (0, class_1.addClassMeta)(METADATA_KEY, (acc) => {
|
|
18
18
|
const result = mappers.map((m) => (0, exports.isProviderPipe)(m) ? (r) => m.mapRegistration(r) : m);
|
|
19
19
|
return acc ? [...result, ...acc] : result;
|
|
20
20
|
});
|
|
@@ -3,6 +3,7 @@ import { ContainerDisposedError } from '../errors/ContainerDisposedError';
|
|
|
3
3
|
import { MetadataInjector } from '../injector/MetadataInjector';
|
|
4
4
|
import { AliasMap } from './AliasMap';
|
|
5
5
|
import { DependencyNotFoundError } from '../errors/DependencyNotFoundError';
|
|
6
|
+
import { ContainerNotFoundError } from '../errors/ContainerNotFoundError';
|
|
6
7
|
import { Is } from '../utils/basic';
|
|
7
8
|
import { Filter as F } from '../utils/array';
|
|
8
9
|
export class Container {
|
|
@@ -128,6 +129,21 @@ export class Container {
|
|
|
128
129
|
getScopes() {
|
|
129
130
|
return [...this.scopes];
|
|
130
131
|
}
|
|
132
|
+
hasInstance(instance) {
|
|
133
|
+
return this.instances.includes(instance);
|
|
134
|
+
}
|
|
135
|
+
getScopeByInstanceOrFail(instance) {
|
|
136
|
+
this.validateContainer();
|
|
137
|
+
const queue = [this];
|
|
138
|
+
while (queue.length > 0) {
|
|
139
|
+
const scope = queue.shift();
|
|
140
|
+
if (scope.hasInstance(instance)) {
|
|
141
|
+
return scope;
|
|
142
|
+
}
|
|
143
|
+
queue.push(...scope.getScopes());
|
|
144
|
+
}
|
|
145
|
+
throw new ContainerNotFoundError('Cannot find scope for the given instance');
|
|
146
|
+
}
|
|
131
147
|
removeScope(child) {
|
|
132
148
|
this.scopes = this.scopes.filter((s) => s !== child);
|
|
133
149
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { MethodNotImplementedError } from '../errors/MethodNotImplementedError';
|
|
2
2
|
import { DependencyNotFoundError } from '../errors/DependencyNotFoundError';
|
|
3
|
+
import { ContainerNotFoundError } from '../errors/ContainerNotFoundError';
|
|
3
4
|
export class EmptyContainer {
|
|
4
5
|
get isDisposed() {
|
|
5
6
|
throw new MethodNotImplementedError();
|
|
@@ -11,9 +12,15 @@ export class EmptyContainer {
|
|
|
11
12
|
getScopes() {
|
|
12
13
|
return [];
|
|
13
14
|
}
|
|
15
|
+
getScopeByInstanceOrFail(instance) {
|
|
16
|
+
throw new ContainerNotFoundError('Cannot find scope for the given instance');
|
|
17
|
+
}
|
|
14
18
|
getInstances() {
|
|
15
19
|
return [];
|
|
16
20
|
}
|
|
21
|
+
hasInstance(instance) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
17
24
|
createScope() {
|
|
18
25
|
throw new MethodNotImplementedError();
|
|
19
26
|
}
|
package/esm/index.js
CHANGED
|
@@ -13,6 +13,7 @@ export { register, bindTo, scope, scopeAccess, lazy, singleton, decorate, append
|
|
|
13
13
|
export { Registration } from './registration/Registration';
|
|
14
14
|
// Errors
|
|
15
15
|
export { DependencyNotFoundError } from './errors/DependencyNotFoundError';
|
|
16
|
+
export { ContainerNotFoundError } from './errors/ContainerNotFoundError';
|
|
16
17
|
export { DependencyMissingKeyError } from './errors/DependencyMissingKeyError';
|
|
17
18
|
export { MethodNotImplementedError } from './errors/MethodNotImplementedError';
|
|
18
19
|
export { ContainerDisposedError } from './errors/ContainerDisposedError';
|
|
@@ -36,9 +37,9 @@ export { FunctionToken } from './token/FunctionToken';
|
|
|
36
37
|
export { ConstantToken } from './token/ConstantToken';
|
|
37
38
|
export { GroupInstanceToken } from './token/GroupInstanceToken';
|
|
38
39
|
// Metadata
|
|
39
|
-
export {
|
|
40
|
-
export {
|
|
41
|
-
export {
|
|
40
|
+
export { addClassMeta, getClassMeta, addClassLabel, getClassLabels, addClassTag, getClassTags } from './metadata/class';
|
|
41
|
+
export { addParamMeta, getParamMeta, addParamLabel, getParamLabels, addParamTag, getParamTags, } from './metadata/parameter';
|
|
42
|
+
export { addMethodMeta, getMethodMeta, addMethodLabel, getMethodLabels, addMethodTag, getMethodTags, } from './metadata/method';
|
|
42
43
|
export { handleError, handleAsyncError } from './utils/errorHandler';
|
|
43
44
|
export { throttle } from './utils/throttle';
|
|
44
45
|
export { debounce } from './utils/debounce';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Injector } from './IInjector';
|
|
2
2
|
import { Is } from '../utils/basic';
|
|
3
|
-
import { getParamMeta,
|
|
3
|
+
import { getParamMeta, addParamMeta } from '../metadata/parameter';
|
|
4
4
|
import { argToToken, toToken } from '../token/toToken';
|
|
5
5
|
export class MetadataInjector extends Injector {
|
|
6
6
|
createInstance(scope, Target, { args: deps = [] } = {}) {
|
|
@@ -10,7 +10,7 @@ export class MetadataInjector extends Injector {
|
|
|
10
10
|
}
|
|
11
11
|
const hookMetaKey = (methodName = 'constructor') => `inject:${methodName}`;
|
|
12
12
|
export const inject = (fn) => (target, propertyKey, parameterIndex) => {
|
|
13
|
-
|
|
13
|
+
addParamMeta(hookMetaKey(propertyKey), () => toToken(fn))(Is.instance(target) ? target.constructor : target, propertyKey, parameterIndex);
|
|
14
14
|
};
|
|
15
15
|
export const args = (index) => (c, { args = [] }) => {
|
|
16
16
|
return args[index];
|
package/esm/metadata/class.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { resolveConstructor } from '../utils/basic';
|
|
2
|
-
export const
|
|
2
|
+
export const addClassMeta = (key, mapFn) => (target) => {
|
|
3
3
|
const value = Reflect.getOwnMetadata(key, target);
|
|
4
4
|
Reflect.defineMetadata(key, mapFn(value), target);
|
|
5
5
|
};
|
|
6
6
|
export function getClassMeta(target, key) {
|
|
7
7
|
return Reflect.getOwnMetadata(key, resolveConstructor(target));
|
|
8
8
|
}
|
|
9
|
-
export const
|
|
9
|
+
export const addClassLabel = (key, label) => addClassMeta('label', (prev = new Map()) => prev.set(key, label));
|
|
10
10
|
export const getClassLabels = (target) => getClassMeta(target, 'label') ?? new Map();
|
|
11
|
-
export const
|
|
11
|
+
export const addClassTag = (tag) => addClassMeta('tag', (prev = new Set()) => prev.add(tag));
|
|
12
12
|
export const getClassTags = (target) => getClassMeta(target, 'tag') ?? new Set();
|
package/esm/metadata/method.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { resolveConstructor } from '../utils/basic';
|
|
2
|
-
export const
|
|
2
|
+
export const addMethodMeta = (key, mapFn) => (target, propertyKey) => {
|
|
3
3
|
const metadata = Reflect.getMetadata(key, target.constructor, propertyKey);
|
|
4
4
|
Reflect.defineMetadata(key, mapFn(metadata), target.constructor, propertyKey);
|
|
5
5
|
};
|
|
6
6
|
export const getMethodMeta = (key, target, propertyKey) => Reflect.getMetadata(key, resolveConstructor(target), propertyKey);
|
|
7
|
-
export const
|
|
7
|
+
export const addMethodLabel = (key, label) => addMethodMeta('label', (prev = new Map()) => prev.set(key, label));
|
|
8
8
|
export const getMethodLabels = (target, propertyKey) => getMethodMeta('label', target, propertyKey) ?? new Map();
|
|
9
|
-
export const
|
|
9
|
+
export const addMethodTag = (tag) => addMethodMeta('tag', (prev = new Set()) => prev.add(tag));
|
|
10
10
|
export const getMethodTags = (target, propertyKey) => getMethodMeta('tag', target, propertyKey) ?? new Set();
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { resolveConstructor } from '../utils/basic';
|
|
2
|
-
export const
|
|
2
|
+
export const addParamMeta = (key, mapFn) => (target, _, parameterIndex) => {
|
|
3
3
|
const metadata = Reflect.getOwnMetadata(key, target) ?? [];
|
|
4
4
|
metadata[parameterIndex] = mapFn(metadata[parameterIndex]);
|
|
5
5
|
Reflect.defineMetadata(key, metadata, target);
|
|
@@ -7,7 +7,7 @@ export const paramMeta = (key, mapFn) => (target, _, parameterIndex) => {
|
|
|
7
7
|
export const getParamMeta = (key, target) => {
|
|
8
8
|
return Reflect.getOwnMetadata(key, resolveConstructor(target)) ?? [];
|
|
9
9
|
};
|
|
10
|
-
export const
|
|
10
|
+
export const addParamLabel = (key, label) => addParamMeta('label', (prev) => {
|
|
11
11
|
const map = prev ?? new Map();
|
|
12
12
|
return map.set(key, label);
|
|
13
13
|
});
|
|
@@ -15,7 +15,7 @@ export const getParamLabels = (target, parameterIndex) => {
|
|
|
15
15
|
const all = getParamMeta('label', target);
|
|
16
16
|
return all[parameterIndex] ?? new Map();
|
|
17
17
|
};
|
|
18
|
-
export const
|
|
18
|
+
export const addParamTag = (tag) => addParamMeta('tag', (prev) => {
|
|
19
19
|
const set = prev ?? new Set();
|
|
20
20
|
return set.add(tag);
|
|
21
21
|
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { SingleToken } from '../token/SingleToken';
|
|
2
2
|
import { isBindToken } from '../token/BindToken';
|
|
3
|
-
import {
|
|
3
|
+
import { addClassMeta, getClassMeta } from '../metadata/class';
|
|
4
4
|
export const isProviderPipe = (obj) => obj !== null && typeof obj === 'object' && 'mapProvider' in obj;
|
|
5
5
|
export const registerPipe = (mapProvider) => ({
|
|
6
6
|
mapProvider,
|
|
@@ -8,7 +8,7 @@ export const registerPipe = (mapProvider) => ({
|
|
|
8
8
|
});
|
|
9
9
|
const METADATA_KEY = 'registration';
|
|
10
10
|
export const getTransformers = (Target) => getClassMeta(Target, METADATA_KEY) ?? [];
|
|
11
|
-
export const register = (...mappers) =>
|
|
11
|
+
export const register = (...mappers) => addClassMeta(METADATA_KEY, (acc) => {
|
|
12
12
|
const result = mappers.map((m) => isProviderPipe(m) ? (r) => m.mapRegistration(r) : m);
|
|
13
13
|
return acc ? [...result, ...acc] : result;
|
|
14
14
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-ioc-container",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "55.0.0",
|
|
4
4
|
"description": "Fast, lightweight TypeScript dependency injection container with a clean API, scoped lifecycles, decorators, tokens, hooks, lazy injection, customizable providers, and no global container objects.",
|
|
5
5
|
"workspaces": [
|
|
6
6
|
"docs"
|
|
@@ -35,6 +35,8 @@ export declare class Container implements IContainer {
|
|
|
35
35
|
addOnDisposeHook(...hooks: OnDisposeHook[]): this;
|
|
36
36
|
addInstance(instance: Instance): void;
|
|
37
37
|
getScopes(): IContainer[];
|
|
38
|
+
hasInstance(instance: object): boolean;
|
|
39
|
+
getScopeByInstanceOrFail(instance: object): IContainer;
|
|
38
40
|
removeScope(child: IContainer): void;
|
|
39
41
|
useModule(module: IContainerModule): this;
|
|
40
42
|
getParent(): IContainer;
|
|
@@ -9,7 +9,9 @@ export declare class EmptyContainer implements IContainer {
|
|
|
9
9
|
addInstance(instance: Instance): void;
|
|
10
10
|
getParent(): undefined;
|
|
11
11
|
getScopes(): never[];
|
|
12
|
+
getScopeByInstanceOrFail(instance: object): IContainer;
|
|
12
13
|
getInstances(): never[];
|
|
14
|
+
hasInstance(instance: object): boolean;
|
|
13
15
|
createScope(): IContainer;
|
|
14
16
|
dispose(): void;
|
|
15
17
|
register(key: DependencyKey, value: IProvider): this;
|
|
@@ -44,10 +44,12 @@ export interface IContainer extends Tagged {
|
|
|
44
44
|
resolveOneByAlias<T>(alias: DependencyKey, options?: ResolveOneOptions): T;
|
|
45
45
|
createScope(options?: CreateScopeOptions): IContainer;
|
|
46
46
|
getScopes(): IContainer[];
|
|
47
|
+
getScopeByInstanceOrFail(instance: object): IContainer;
|
|
47
48
|
removeScope(child: IContainer): void;
|
|
48
49
|
useModule(module: IContainerModule): this;
|
|
49
50
|
getParent(): IContainer | undefined;
|
|
50
51
|
getInstances(cascade?: boolean): Instance[];
|
|
52
|
+
hasInstance(instance: object): boolean;
|
|
51
53
|
dispose(): void;
|
|
52
54
|
addInstance(instance: Instance): void;
|
|
53
55
|
}
|
package/typings/index.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export { Provider } from './provider/Provider';
|
|
|
10
10
|
export { type IRegistration, type ReturnTypeOfRegistration, type ScopeMatchRule, type ProviderPipe, register, bindTo, scope, scopeAccess, lazy, singleton, decorate, appendArgs, appendArgsFn, } from './registration/IRegistration';
|
|
11
11
|
export { Registration } from './registration/Registration';
|
|
12
12
|
export { DependencyNotFoundError } from './errors/DependencyNotFoundError';
|
|
13
|
+
export { ContainerNotFoundError } from './errors/ContainerNotFoundError';
|
|
13
14
|
export { DependencyMissingKeyError } from './errors/DependencyMissingKeyError';
|
|
14
15
|
export { MethodNotImplementedError } from './errors/MethodNotImplementedError';
|
|
15
16
|
export { ContainerDisposedError } from './errors/ContainerDisposedError';
|
|
@@ -30,9 +31,9 @@ export { SingleToken } from './token/SingleToken';
|
|
|
30
31
|
export { FunctionToken } from './token/FunctionToken';
|
|
31
32
|
export { ConstantToken } from './token/ConstantToken';
|
|
32
33
|
export { type InstancePredicate, GroupInstanceToken } from './token/GroupInstanceToken';
|
|
33
|
-
export {
|
|
34
|
-
export {
|
|
35
|
-
export {
|
|
34
|
+
export { addClassMeta, getClassMeta, addClassLabel, getClassLabels, addClassTag, getClassTags } from './metadata/class';
|
|
35
|
+
export { addParamMeta, getParamMeta, addParamLabel, getParamLabels, addParamTag, getParamTags, } from './metadata/parameter';
|
|
36
|
+
export { addMethodMeta, getMethodMeta, addMethodLabel, getMethodLabels, addMethodTag, getMethodTags, } from './metadata/method';
|
|
36
37
|
export { handleError, handleAsyncError, type HandleErrorParams } from './utils/errorHandler';
|
|
37
38
|
export { throttle } from './utils/throttle';
|
|
38
39
|
export { debounce } from './utils/debounce';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export declare const
|
|
1
|
+
export declare const addClassMeta: <T>(key: string | symbol, mapFn: (prev: T | undefined) => T) => ClassDecorator;
|
|
2
2
|
export declare function getClassMeta<T>(target: object, key: string | symbol): T | undefined;
|
|
3
|
-
export declare const
|
|
3
|
+
export declare const addClassLabel: (key: string, label: string) => ClassDecorator;
|
|
4
4
|
export declare const getClassLabels: (target: object) => Map<string, string>;
|
|
5
|
-
export declare const
|
|
5
|
+
export declare const addClassTag: (tag: string) => ClassDecorator;
|
|
6
6
|
export declare const getClassTags: (target: object) => Set<string>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export declare const
|
|
1
|
+
export declare const addMethodMeta: <T>(key: string, mapFn: (prev: T | undefined) => T) => MethodDecorator;
|
|
2
2
|
export declare const getMethodMeta: (key: string, target: object, propertyKey: string) => unknown;
|
|
3
|
-
export declare const
|
|
3
|
+
export declare const addMethodLabel: (key: string, label: string) => MethodDecorator;
|
|
4
4
|
export declare const getMethodLabels: (target: object, propertyKey: string) => Map<string, string>;
|
|
5
|
-
export declare const
|
|
5
|
+
export declare const addMethodTag: (tag: string) => MethodDecorator;
|
|
6
6
|
export declare const getMethodTags: (target: object, propertyKey: string) => Set<string>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export declare const
|
|
1
|
+
export declare const addParamMeta: (key: string | symbol, mapFn: (prev: unknown) => unknown) => ParameterDecorator;
|
|
2
2
|
export declare const getParamMeta: (key: string | symbol, target: object) => unknown[];
|
|
3
|
-
export declare const
|
|
3
|
+
export declare const addParamLabel: (key: string, label: string) => ParameterDecorator;
|
|
4
4
|
export declare const getParamLabels: (target: object, parameterIndex: number) => Map<string, string>;
|
|
5
|
-
export declare const
|
|
5
|
+
export declare const addParamTag: (tag: string) => ParameterDecorator;
|
|
6
6
|
export declare const getParamTags: (target: object, parameterIndex: number) => Set<string>;
|