ts-ioc-container 31.1.2 → 31.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -5
- package/cjm/container/Container.js +5 -8
- package/cjm/hook.js +12 -6
- package/cjm/index.js +5 -1
- package/cjm/metadata.js +7 -1
- package/cjm/registration/IRegistration.js +1 -1
- package/cjm/registration/Registration.js +1 -1
- package/esm/container/Container.js +5 -8
- package/esm/hook.js +10 -5
- package/esm/index.js +2 -2
- package/esm/metadata.js +4 -0
- package/esm/registration/IRegistration.js +1 -1
- package/esm/registration/Registration.js +1 -1
- package/package.json +2 -2
- package/typings/container/Container.d.ts +1 -1
- package/typings/container/IContainer.d.ts +2 -2
- package/typings/hook.d.ts +4 -2
- package/typings/index.d.ts +2 -2
- package/typings/metadata.d.ts +2 -0
- package/typings/registration/IRegistration.d.ts +1 -1
- package/typings/registration/Registration.d.ts +1 -1
package/README.md
CHANGED
|
@@ -649,7 +649,7 @@ describe('alias', () => {
|
|
|
649
649
|
it('should resolve by some alias', () => {
|
|
650
650
|
class App implements IApplication {
|
|
651
651
|
private appliedMiddleware: Set<string> = new Set();
|
|
652
|
-
constructor(@inject(by.aliases((it) => it.
|
|
652
|
+
constructor(@inject(by.aliases((it) => it.has(IMiddlewareKey))) public middleware: IMiddleware[]) {}
|
|
653
653
|
|
|
654
654
|
markMiddlewareAsApplied(name: string): void {
|
|
655
655
|
this.appliedMiddleware.add(name);
|
|
@@ -687,8 +687,8 @@ describe('alias', () => {
|
|
|
687
687
|
|
|
688
688
|
const container = new Container(new MetadataInjector()).addRegistration(R.fromClass(FileLogger));
|
|
689
689
|
|
|
690
|
-
expect(by.alias((aliases) => aliases.
|
|
691
|
-
expect(() => by.alias((aliases) => aliases.
|
|
690
|
+
expect(by.alias((aliases) => aliases.has('ILogger'))(container)).toBeInstanceOf(FileLogger);
|
|
691
|
+
expect(() => by.alias((aliases) => aliases.has('logger'))(container)).toThrowError(DependencyNotFoundError);
|
|
692
692
|
});
|
|
693
693
|
});
|
|
694
694
|
|
|
@@ -764,7 +764,7 @@ class MyInjector implements IInjector {
|
|
|
764
764
|
resolve<T>(container: IContainer, value: constructor<T>, ...deps: unknown[]): T {
|
|
765
765
|
const instance = this.injector.resolve(container, value, ...deps);
|
|
766
766
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
767
|
-
for (const h of getHooks(instance as object, 'onConstruct')) {
|
|
767
|
+
for (const [h] of getHooks(instance as object, 'onConstruct')) {
|
|
768
768
|
// @ts-ignore
|
|
769
769
|
instance[h]();
|
|
770
770
|
}
|
|
@@ -814,6 +814,7 @@ import {
|
|
|
814
814
|
MetadataInjector,
|
|
815
815
|
register,
|
|
816
816
|
} from 'ts-ioc-container';
|
|
817
|
+
import * as console from 'node:console';
|
|
817
818
|
|
|
818
819
|
@register(key('logsRepo'))
|
|
819
820
|
@provider(singleton())
|
|
@@ -853,7 +854,7 @@ describe('onDispose', function () {
|
|
|
853
854
|
|
|
854
855
|
for (const instance of container.getInstances()) {
|
|
855
856
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
856
|
-
for (const h of getHooks(instance as object, 'onDispose')) {
|
|
857
|
+
for (const [h] of getHooks(instance as object, 'onDispose')) {
|
|
857
858
|
// @ts-ignore
|
|
858
859
|
await instance[h]();
|
|
859
860
|
}
|
|
@@ -25,7 +25,7 @@ class Container {
|
|
|
25
25
|
this.validateContainer();
|
|
26
26
|
this.providers.set(key, provider);
|
|
27
27
|
if (aliases && aliases.length > 0) {
|
|
28
|
-
this.aliases.set(key, aliases);
|
|
28
|
+
this.aliases.set(key, new Set(aliases));
|
|
29
29
|
}
|
|
30
30
|
return this;
|
|
31
31
|
}
|
|
@@ -70,10 +70,8 @@ class Container {
|
|
|
70
70
|
hasTag(tag) {
|
|
71
71
|
return this.tags.has(tag);
|
|
72
72
|
}
|
|
73
|
-
use(
|
|
74
|
-
|
|
75
|
-
module.applyTo(this);
|
|
76
|
-
}
|
|
73
|
+
use(module) {
|
|
74
|
+
module.applyTo(this);
|
|
77
75
|
return this;
|
|
78
76
|
}
|
|
79
77
|
hasDependency(key) {
|
|
@@ -108,9 +106,8 @@ class Container {
|
|
|
108
106
|
* @private
|
|
109
107
|
*/
|
|
110
108
|
getRegistrations() {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
: this.parent.getRegistrations();
|
|
109
|
+
const registrations = this.parent.getRegistrations();
|
|
110
|
+
return this.registrations.length > 0 ? registrations.concat(this.registrations) : registrations;
|
|
114
111
|
}
|
|
115
112
|
/**
|
|
116
113
|
* @private
|
package/cjm/hook.js
CHANGED
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getHooks = exports.hook = void 0;
|
|
4
|
-
const hook = (key) => (target, propertyKey) => {
|
|
5
|
-
const hooks = Reflect.hasMetadata(key, target.constructor)
|
|
6
|
-
|
|
3
|
+
exports.hasHooks = exports.getHooks = exports.hook = void 0;
|
|
4
|
+
const hook = (key, fn = () => []) => (target, propertyKey) => {
|
|
5
|
+
const hooks = Reflect.hasMetadata(key, target.constructor)
|
|
6
|
+
? Reflect.getMetadata(key, target.constructor)
|
|
7
|
+
: new Map();
|
|
8
|
+
hooks.set(propertyKey, fn);
|
|
9
|
+
Reflect.defineMetadata(key, hooks, target.constructor); // eslint-disable-line @typescript-eslint/ban-types
|
|
7
10
|
};
|
|
8
11
|
exports.hook = hook;
|
|
9
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
10
12
|
function getHooks(target, key) {
|
|
11
|
-
return Reflect.hasMetadata(key, target.constructor) ? Reflect.getMetadata(key, target.constructor) :
|
|
13
|
+
return Reflect.hasMetadata(key, target.constructor) ? Reflect.getMetadata(key, target.constructor) : new Map();
|
|
12
14
|
}
|
|
13
15
|
exports.getHooks = getHooks;
|
|
16
|
+
function hasHooks(target, key) {
|
|
17
|
+
return Reflect.hasMetadata(key, target.constructor);
|
|
18
|
+
}
|
|
19
|
+
exports.hasHooks = hasHooks;
|
package/cjm/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.setParameterMetadata = exports.getMetadata = exports.setMetadata = exports.by = exports.hook = exports.getHooks = exports.ProxyInjector = exports.SimpleInjector = exports.inject = exports.MetadataInjector = exports.register = exports.scope = exports.alias = exports.key = exports.Registration = exports.AutoMockedContainer = exports.SingletonProvider = exports.singleton = exports.ArgsProvider = exports.args = exports.argsFn = exports.ProviderDecorator = exports.Provider = exports.ContainerDisposedError = exports.MethodNotImplementedError = exports.DependencyNotFoundError = exports.visible = exports.provider = exports.EmptyContainer = exports.Container = exports.isDependencyKey = void 0;
|
|
3
|
+
exports.getParameterMetadata = exports.getMethodMetadata = exports.setMethodMetadata = exports.setParameterMetadata = exports.getMetadata = exports.setMetadata = exports.by = exports.hasHooks = exports.hook = exports.getHooks = exports.ProxyInjector = exports.SimpleInjector = exports.inject = exports.MetadataInjector = exports.register = exports.scope = exports.alias = exports.key = exports.Registration = exports.AutoMockedContainer = exports.SingletonProvider = exports.singleton = exports.ArgsProvider = exports.args = exports.argsFn = exports.ProviderDecorator = exports.Provider = exports.ContainerDisposedError = exports.MethodNotImplementedError = exports.DependencyNotFoundError = exports.visible = exports.provider = exports.EmptyContainer = exports.Container = exports.isDependencyKey = void 0;
|
|
4
4
|
var IContainer_1 = require("./container/IContainer");
|
|
5
5
|
Object.defineProperty(exports, "isDependencyKey", { enumerable: true, get: function () { return IContainer_1.isDependencyKey; } });
|
|
6
6
|
var Container_1 = require("./container/Container");
|
|
@@ -46,9 +46,13 @@ Object.defineProperty(exports, "ProxyInjector", { enumerable: true, get: functio
|
|
|
46
46
|
var hook_1 = require("./hook");
|
|
47
47
|
Object.defineProperty(exports, "getHooks", { enumerable: true, get: function () { return hook_1.getHooks; } });
|
|
48
48
|
Object.defineProperty(exports, "hook", { enumerable: true, get: function () { return hook_1.hook; } });
|
|
49
|
+
Object.defineProperty(exports, "hasHooks", { enumerable: true, get: function () { return hook_1.hasHooks; } });
|
|
49
50
|
var by_1 = require("./by");
|
|
50
51
|
Object.defineProperty(exports, "by", { enumerable: true, get: function () { return by_1.by; } });
|
|
51
52
|
var metadata_1 = require("./metadata");
|
|
52
53
|
Object.defineProperty(exports, "setMetadata", { enumerable: true, get: function () { return metadata_1.setMetadata; } });
|
|
53
54
|
Object.defineProperty(exports, "getMetadata", { enumerable: true, get: function () { return metadata_1.getMetadata; } });
|
|
54
55
|
Object.defineProperty(exports, "setParameterMetadata", { enumerable: true, get: function () { return metadata_1.setParameterMetadata; } });
|
|
56
|
+
Object.defineProperty(exports, "setMethodMetadata", { enumerable: true, get: function () { return metadata_1.setMethodMetadata; } });
|
|
57
|
+
Object.defineProperty(exports, "getMethodMetadata", { enumerable: true, get: function () { return metadata_1.getMethodMetadata; } });
|
|
58
|
+
Object.defineProperty(exports, "getParameterMetadata", { enumerable: true, get: function () { return metadata_1.getParameterMetadata; } });
|
package/cjm/metadata.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getParameterMetadata = exports.setParameterMetadata = exports.getMetadata = exports.setMetadata = void 0;
|
|
3
|
+
exports.getMethodMetadata = exports.setMethodMetadata = exports.getParameterMetadata = exports.setParameterMetadata = exports.getMetadata = exports.setMetadata = void 0;
|
|
4
4
|
const setMetadata = (key, value) => (target) => {
|
|
5
5
|
Reflect.defineMetadata(key, value, target);
|
|
6
6
|
};
|
|
@@ -19,3 +19,9 @@ const getParameterMetadata = (key, target) => {
|
|
|
19
19
|
return Reflect.getOwnMetadata(key, target) ?? [];
|
|
20
20
|
};
|
|
21
21
|
exports.getParameterMetadata = getParameterMetadata;
|
|
22
|
+
const setMethodMetadata = (key, value) => (target, propertyKey) => {
|
|
23
|
+
Reflect.defineMetadata(key, value, target.constructor, propertyKey);
|
|
24
|
+
};
|
|
25
|
+
exports.setMethodMetadata = setMethodMetadata;
|
|
26
|
+
const getMethodMetadata = (key, target, propertyKey) => Reflect.getMetadata(key, target.constructor, propertyKey);
|
|
27
|
+
exports.getMethodMetadata = getMethodMetadata;
|
|
@@ -6,7 +6,7 @@ const key = (key) => (r) => r.to(key);
|
|
|
6
6
|
exports.key = key;
|
|
7
7
|
const alias = (...aliases) => (r) => r.addAliases(...aliases);
|
|
8
8
|
exports.alias = alias;
|
|
9
|
-
const scope = (predicate) => (r) => r.
|
|
9
|
+
const scope = (predicate) => (r) => r.when(predicate);
|
|
10
10
|
exports.scope = scope;
|
|
11
11
|
const METADATA_KEY = 'registration';
|
|
12
12
|
const getTransformers = (Target) => (0, metadata_1.getMetadata)(Target, METADATA_KEY) ?? [];
|
|
@@ -22,7 +22,7 @@ export class Container {
|
|
|
22
22
|
this.validateContainer();
|
|
23
23
|
this.providers.set(key, provider);
|
|
24
24
|
if (aliases && aliases.length > 0) {
|
|
25
|
-
this.aliases.set(key, aliases);
|
|
25
|
+
this.aliases.set(key, new Set(aliases));
|
|
26
26
|
}
|
|
27
27
|
return this;
|
|
28
28
|
}
|
|
@@ -67,10 +67,8 @@ export class Container {
|
|
|
67
67
|
hasTag(tag) {
|
|
68
68
|
return this.tags.has(tag);
|
|
69
69
|
}
|
|
70
|
-
use(
|
|
71
|
-
|
|
72
|
-
module.applyTo(this);
|
|
73
|
-
}
|
|
70
|
+
use(module) {
|
|
71
|
+
module.applyTo(this);
|
|
74
72
|
return this;
|
|
75
73
|
}
|
|
76
74
|
hasDependency(key) {
|
|
@@ -105,9 +103,8 @@ export class Container {
|
|
|
105
103
|
* @private
|
|
106
104
|
*/
|
|
107
105
|
getRegistrations() {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
: this.parent.getRegistrations();
|
|
106
|
+
const registrations = this.parent.getRegistrations();
|
|
107
|
+
return this.registrations.length > 0 ? registrations.concat(this.registrations) : registrations;
|
|
111
108
|
}
|
|
112
109
|
/**
|
|
113
110
|
* @private
|
package/esm/hook.js
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
|
-
export const hook = (key) => (target, propertyKey) => {
|
|
2
|
-
const hooks = Reflect.hasMetadata(key, target.constructor)
|
|
3
|
-
|
|
1
|
+
export const hook = (key, fn = () => []) => (target, propertyKey) => {
|
|
2
|
+
const hooks = Reflect.hasMetadata(key, target.constructor)
|
|
3
|
+
? Reflect.getMetadata(key, target.constructor)
|
|
4
|
+
: new Map();
|
|
5
|
+
hooks.set(propertyKey, fn);
|
|
6
|
+
Reflect.defineMetadata(key, hooks, target.constructor); // eslint-disable-line @typescript-eslint/ban-types
|
|
4
7
|
};
|
|
5
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
6
8
|
export function getHooks(target, key) {
|
|
7
|
-
return Reflect.hasMetadata(key, target.constructor) ? Reflect.getMetadata(key, target.constructor) :
|
|
9
|
+
return Reflect.hasMetadata(key, target.constructor) ? Reflect.getMetadata(key, target.constructor) : new Map();
|
|
10
|
+
}
|
|
11
|
+
export function hasHooks(target, key) {
|
|
12
|
+
return Reflect.hasMetadata(key, target.constructor);
|
|
8
13
|
}
|
package/esm/index.js
CHANGED
|
@@ -15,6 +15,6 @@ export { key, alias, scope, register } from './registration/IRegistration';
|
|
|
15
15
|
export { MetadataInjector, inject } from './injector/MetadataInjector';
|
|
16
16
|
export { SimpleInjector } from './injector/SimpleInjector';
|
|
17
17
|
export { ProxyInjector } from './injector/ProxyInjector';
|
|
18
|
-
export { getHooks, hook } from './hook';
|
|
18
|
+
export { getHooks, hook, hasHooks } from './hook';
|
|
19
19
|
export { by } from './by';
|
|
20
|
-
export { setMetadata, getMetadata, setParameterMetadata } from './metadata';
|
|
20
|
+
export { setMetadata, getMetadata, setParameterMetadata, setMethodMetadata, getMethodMetadata, getParameterMetadata, } from './metadata';
|
package/esm/metadata.js
CHANGED
|
@@ -12,3 +12,7 @@ export const setParameterMetadata = (key, value) => (target, propertyKey, parame
|
|
|
12
12
|
export const getParameterMetadata = (key, target) => {
|
|
13
13
|
return Reflect.getOwnMetadata(key, target) ?? [];
|
|
14
14
|
};
|
|
15
|
+
export const setMethodMetadata = (key, value) => (target, propertyKey) => {
|
|
16
|
+
Reflect.defineMetadata(key, value, target.constructor, propertyKey);
|
|
17
|
+
};
|
|
18
|
+
export const getMethodMetadata = (key, target, propertyKey) => Reflect.getMetadata(key, target.constructor, propertyKey);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { getMetadata, setMetadata } from '../metadata';
|
|
2
2
|
export const key = (key) => (r) => r.to(key);
|
|
3
3
|
export const alias = (...aliases) => (r) => r.addAliases(...aliases);
|
|
4
|
-
export const scope = (predicate) => (r) => r.
|
|
4
|
+
export const scope = (predicate) => (r) => r.when(predicate);
|
|
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);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-ioc-container",
|
|
3
|
-
"version": "31.1.
|
|
3
|
+
"version": "31.1.3",
|
|
4
4
|
"description": "Typescript IoC container",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public",
|
|
@@ -59,5 +59,5 @@
|
|
|
59
59
|
"ts-node": "^10.9.1",
|
|
60
60
|
"typescript": "5.4.3"
|
|
61
61
|
},
|
|
62
|
-
"gitHead": "
|
|
62
|
+
"gitHead": "691a320cd299d32f09a22b7c36a7887a6e2b8370"
|
|
63
63
|
}
|
|
@@ -23,7 +23,7 @@ export declare class Container implements IContainer {
|
|
|
23
23
|
dispose(): void;
|
|
24
24
|
getInstances(): unknown[];
|
|
25
25
|
hasTag(tag: Tag): boolean;
|
|
26
|
-
use(
|
|
26
|
+
use(module: IContainerModule): this;
|
|
27
27
|
hasDependency(key: DependencyKey): boolean;
|
|
28
28
|
getKeysByAlias(predicate: AliasPredicate): DependencyKey[];
|
|
29
29
|
getKeyByAlias(predicate: AliasPredicate): DependencyKey;
|
|
@@ -20,7 +20,7 @@ export interface Tagged {
|
|
|
20
20
|
hasTag(tag: Tag): boolean;
|
|
21
21
|
}
|
|
22
22
|
export type Alias = string;
|
|
23
|
-
export type AliasPredicate = (aliases: Alias
|
|
23
|
+
export type AliasPredicate = (aliases: Set<Alias>) => boolean;
|
|
24
24
|
export interface IContainer extends Resolvable, Tagged {
|
|
25
25
|
readonly isDisposed: boolean;
|
|
26
26
|
createScope(...tags: Tag[]): IContainer;
|
|
@@ -29,7 +29,7 @@ export interface IContainer extends Resolvable, Tagged {
|
|
|
29
29
|
removeScope(child: IContainer): void;
|
|
30
30
|
getInstances(): unknown[];
|
|
31
31
|
dispose(): void;
|
|
32
|
-
use(
|
|
32
|
+
use(module: IContainerModule): this;
|
|
33
33
|
getRegistrations(): IRegistration[];
|
|
34
34
|
hasDependency(key: DependencyKey): boolean;
|
|
35
35
|
getKeysByAlias(alias: AliasPredicate): DependencyKey[];
|
package/typings/hook.d.ts
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
export declare
|
|
1
|
+
import { ArgsFn } from './provider/ArgsProvider';
|
|
2
|
+
export declare const hook: (key: string | symbol, fn?: ArgsFn) => MethodDecorator;
|
|
3
|
+
export declare function getHooks(target: object, key: string | symbol): Map<string, ArgsFn>;
|
|
4
|
+
export declare function hasHooks(target: object, key: string | symbol): boolean;
|
package/typings/index.d.ts
CHANGED
|
@@ -17,6 +17,6 @@ export { key, alias, IRegistration, scope, register } from './registration/IRegi
|
|
|
17
17
|
export { MetadataInjector, inject } from './injector/MetadataInjector';
|
|
18
18
|
export { SimpleInjector } from './injector/SimpleInjector';
|
|
19
19
|
export { ProxyInjector } from './injector/ProxyInjector';
|
|
20
|
-
export { getHooks, hook } from './hook';
|
|
20
|
+
export { getHooks, hook, hasHooks } from './hook';
|
|
21
21
|
export { by, InstancePredicate } from './by';
|
|
22
|
-
export { setMetadata, getMetadata, setParameterMetadata } from './metadata';
|
|
22
|
+
export { setMetadata, getMetadata, setParameterMetadata, setMethodMetadata, getMethodMetadata, getParameterMetadata, } from './metadata';
|
package/typings/metadata.d.ts
CHANGED
|
@@ -3,3 +3,5 @@ export declare const setMetadata: <T>(key: string | symbol, value: T) => ClassDe
|
|
|
3
3
|
export declare function getMetadata<T>(target: object, key: string | symbol): T | undefined;
|
|
4
4
|
export declare const setParameterMetadata: (key: string | symbol, value: unknown) => ParameterDecorator;
|
|
5
5
|
export declare const getParameterMetadata: (key: string | symbol, target: constructor<unknown>) => unknown[];
|
|
6
|
+
export declare const setMethodMetadata: (key: string, value: unknown) => MethodDecorator;
|
|
7
|
+
export declare const getMethodMetadata: (key: string, target: object, propertyKey: string) => unknown;
|
|
@@ -3,7 +3,7 @@ import { constructor, MapFn } from '../utils';
|
|
|
3
3
|
export type ScopePredicate = (c: Tagged) => boolean;
|
|
4
4
|
export interface IRegistration extends IContainerModule {
|
|
5
5
|
addAliases(...aliases: Alias[]): this;
|
|
6
|
-
|
|
6
|
+
when(isValidWhen: ScopePredicate): this;
|
|
7
7
|
to(key: DependencyKey): this;
|
|
8
8
|
}
|
|
9
9
|
export declare const key: (key: DependencyKey) => MapFn<IRegistration>;
|
|
@@ -15,5 +15,5 @@ export declare class Registration<T = unknown> implements IRegistration {
|
|
|
15
15
|
addAliases(...aliases: Alias[]): this;
|
|
16
16
|
pipe(...mappers: MapFn<IProvider<T>>[]): this;
|
|
17
17
|
applyTo(container: IContainer): void;
|
|
18
|
-
|
|
18
|
+
when(isValidWhen: ScopePredicate): this;
|
|
19
19
|
}
|