ts-ioc-container 32.7.0 → 32.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -0
- package/cjm/hooks/ExecutionContext.js +4 -0
- package/cjm/hooks/hook.js +4 -3
- package/cjm/index.js +4 -2
- package/esm/hooks/ExecutionContext.js +4 -0
- package/esm/hooks/hook.js +2 -2
- package/esm/index.js +3 -2
- package/package.json +2 -2
- package/typings/hooks/ExecutionContext.d.ts +2 -0
- package/typings/hooks/hook.d.ts +2 -0
- package/typings/index.d.ts +1 -1
package/README.md
CHANGED
|
@@ -14,6 +14,8 @@
|
|
|
14
14
|
- supports `tagged scopes`
|
|
15
15
|
- fully test covered :100:
|
|
16
16
|
- can be used with decorators `@inject`
|
|
17
|
+
- can [inject properties](#inject-property)
|
|
18
|
+
- can inject [lazy dependencies](#lazy)
|
|
17
19
|
- composable and open to extend
|
|
18
20
|
- awesome for testing (auto mocking)
|
|
19
21
|
|
|
@@ -43,6 +45,7 @@
|
|
|
43
45
|
- [Hook](#hook) `@hook`
|
|
44
46
|
- [OnConstruct](#onconstruct) `@onConstruct`
|
|
45
47
|
- [OnDispose](#ondispose) `@onDispose`
|
|
48
|
+
- [Inject Property](#inject-property)
|
|
46
49
|
- [Mock](#mock)
|
|
47
50
|
- [Error](#error)
|
|
48
51
|
|
|
@@ -357,6 +360,7 @@ describe('lazy provider', () => {
|
|
|
357
360
|
|
|
358
361
|
### Metadata
|
|
359
362
|
This type of injector uses `@inject` decorator to mark where dependencies should be injected. It's bases on `reflect-metadata` package. That's why I call it `MetadataInjector`.
|
|
363
|
+
Also you can [inject property.](#inject-property)
|
|
360
364
|
|
|
361
365
|
```typescript
|
|
362
366
|
import 'reflect-metadata';
|
|
@@ -1255,6 +1259,29 @@ describe('onDispose', function () {
|
|
|
1255
1259
|
|
|
1256
1260
|
```
|
|
1257
1261
|
|
|
1262
|
+
### Inject property
|
|
1263
|
+
|
|
1264
|
+
```typescript
|
|
1265
|
+
import { by, Container, executeHooks, hook, injectProp, MetadataInjector, Registration } from 'ts-ioc-container';
|
|
1266
|
+
|
|
1267
|
+
describe('inject property', () => {
|
|
1268
|
+
it('should inject property', () => {
|
|
1269
|
+
class App {
|
|
1270
|
+
@hook('onInit', injectProp(by.key('greeting')))
|
|
1271
|
+
greeting!: string;
|
|
1272
|
+
}
|
|
1273
|
+
const expected = 'Hello world!';
|
|
1274
|
+
|
|
1275
|
+
const container = new Container(new MetadataInjector()).add(Registration.fromValue(expected).to('greeting'));
|
|
1276
|
+
const app = container.resolve(App);
|
|
1277
|
+
executeHooks(app, 'onInit', { scope: container });
|
|
1278
|
+
|
|
1279
|
+
expect(app.greeting).toBe(expected);
|
|
1280
|
+
});
|
|
1281
|
+
});
|
|
1282
|
+
|
|
1283
|
+
```
|
|
1284
|
+
|
|
1258
1285
|
## Mock
|
|
1259
1286
|
Sometimes you need to automatically mock all dependencies in container. This is what `AutoMockedContainer` is for.
|
|
1260
1287
|
|
package/cjm/hooks/hook.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.executeHooks = exports.hasHooks = exports.getHooks = exports.hook = void 0;
|
|
3
|
+
exports.executeHooks = exports.hasHooks = exports.getHooks = exports.hook = exports.injectProp = void 0;
|
|
4
4
|
const ExecutionContext_1 = require("./ExecutionContext");
|
|
5
|
-
const
|
|
5
|
+
const injectProp = (fn) => (context) => context.injectProperty(fn);
|
|
6
|
+
exports.injectProp = injectProp;
|
|
6
7
|
const hook = (key, ...fns) => (target, propertyKey) => {
|
|
7
8
|
const hooks = Reflect.hasMetadata(key, target.constructor)
|
|
8
9
|
? Reflect.getMetadata(key, target.constructor)
|
|
9
|
-
:
|
|
10
|
+
: new Map();
|
|
10
11
|
hooks.set(propertyKey, (hooks.get(propertyKey) ?? []).concat(fns));
|
|
11
12
|
Reflect.defineMetadata(key, hooks, target.constructor); // eslint-disable-line @typescript-eslint/ban-types
|
|
12
13
|
};
|
package/cjm/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getParameterMetadata = exports.getMethodMetadata = exports.setMethodMetadata = exports.setParameterMetadata = exports.getMetadata = exports.setMetadata = exports.byAliases = exports.byAlias = exports.IMemoKey = exports.by = exports.ExecutionContext = exports.executeHooks = exports.hasHooks = exports.hook = exports.getHooks = exports.ContainerDisposedError = exports.MethodNotImplementedError = exports.DependencyNotFoundError = exports.Registration = exports.register = exports.scope = exports.key = exports.decorate = exports.multiCache = exports.MultiCache = exports.SingletonProvider = exports.singleton = exports.Provider = exports.ProviderDecorator = exports.args = exports.argsFn = exports.alias = exports.visible = exports.provider = exports.ProxyInjector = exports.SimpleInjector = exports.getInjectFns = exports.resolveArgs = exports.inject = exports.MetadataInjector = exports.AutoMockedContainer = exports.EmptyContainer = exports.Container = exports.isDependencyKey = void 0;
|
|
3
|
+
exports.getParameterMetadata = exports.getMethodMetadata = exports.setMethodMetadata = exports.setParameterMetadata = exports.getMetadata = exports.setMetadata = exports.byAliases = exports.byAlias = exports.IMemoKey = exports.by = exports.ExecutionContext = exports.injectProp = exports.executeHooks = exports.hasHooks = exports.hook = exports.getHooks = exports.ContainerDisposedError = exports.MethodNotImplementedError = exports.DependencyNotFoundError = exports.Registration = exports.register = exports.scope = exports.key = exports.decorate = exports.multiCache = exports.MultiCache = exports.SingletonProvider = exports.singleton = exports.Provider = exports.ProviderDecorator = exports.args = exports.argsFn = exports.alias = exports.visible = exports.provider = exports.ProxyInjector = exports.SimpleInjector = exports.getInjectFns = exports.resolveArgs = exports.inject = exports.MetadataInjector = exports.AutoMockedContainer = exports.EmptyContainer = exports.Container = exports.isDependencyKey = void 0;
|
|
4
4
|
// Containers
|
|
5
5
|
var IContainer_1 = require("./container/IContainer");
|
|
6
6
|
Object.defineProperty(exports, "isDependencyKey", { enumerable: true, get: function () { return IContainer_1.isDependencyKey; } });
|
|
@@ -51,14 +51,16 @@ var MethodNotImplementedError_1 = require("./errors/MethodNotImplementedError");
|
|
|
51
51
|
Object.defineProperty(exports, "MethodNotImplementedError", { enumerable: true, get: function () { return MethodNotImplementedError_1.MethodNotImplementedError; } });
|
|
52
52
|
var ContainerDisposedError_1 = require("./errors/ContainerDisposedError");
|
|
53
53
|
Object.defineProperty(exports, "ContainerDisposedError", { enumerable: true, get: function () { return ContainerDisposedError_1.ContainerDisposedError; } });
|
|
54
|
-
//
|
|
54
|
+
// Hooks
|
|
55
55
|
var hook_1 = require("./hooks/hook");
|
|
56
56
|
Object.defineProperty(exports, "getHooks", { enumerable: true, get: function () { return hook_1.getHooks; } });
|
|
57
57
|
Object.defineProperty(exports, "hook", { enumerable: true, get: function () { return hook_1.hook; } });
|
|
58
58
|
Object.defineProperty(exports, "hasHooks", { enumerable: true, get: function () { return hook_1.hasHooks; } });
|
|
59
59
|
Object.defineProperty(exports, "executeHooks", { enumerable: true, get: function () { return hook_1.executeHooks; } });
|
|
60
|
+
Object.defineProperty(exports, "injectProp", { enumerable: true, get: function () { return hook_1.injectProp; } });
|
|
60
61
|
var ExecutionContext_1 = require("./hooks/ExecutionContext");
|
|
61
62
|
Object.defineProperty(exports, "ExecutionContext", { enumerable: true, get: function () { return ExecutionContext_1.ExecutionContext; } });
|
|
63
|
+
// Others
|
|
62
64
|
var by_1 = require("./by");
|
|
63
65
|
Object.defineProperty(exports, "by", { enumerable: true, get: function () { return by_1.by; } });
|
|
64
66
|
Object.defineProperty(exports, "IMemoKey", { enumerable: true, get: function () { return by_1.IMemoKey; } });
|
package/esm/hooks/hook.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { ExecutionContext } from './ExecutionContext';
|
|
2
|
-
const
|
|
2
|
+
export const injectProp = (fn) => (context) => context.injectProperty(fn);
|
|
3
3
|
export const hook = (key, ...fns) => (target, propertyKey) => {
|
|
4
4
|
const hooks = Reflect.hasMetadata(key, target.constructor)
|
|
5
5
|
? Reflect.getMetadata(key, target.constructor)
|
|
6
|
-
:
|
|
6
|
+
: new Map();
|
|
7
7
|
hooks.set(propertyKey, (hooks.get(propertyKey) ?? []).concat(fns));
|
|
8
8
|
Reflect.defineMetadata(key, hooks, target.constructor); // eslint-disable-line @typescript-eslint/ban-types
|
|
9
9
|
};
|
package/esm/index.js
CHANGED
|
@@ -19,8 +19,9 @@ export { Registration } from './registration/Registration';
|
|
|
19
19
|
export { DependencyNotFoundError } from './errors/DependencyNotFoundError';
|
|
20
20
|
export { MethodNotImplementedError } from './errors/MethodNotImplementedError';
|
|
21
21
|
export { ContainerDisposedError } from './errors/ContainerDisposedError';
|
|
22
|
-
//
|
|
23
|
-
export { getHooks, hook, hasHooks, executeHooks } from './hooks/hook';
|
|
22
|
+
// Hooks
|
|
23
|
+
export { getHooks, hook, hasHooks, executeHooks, injectProp } from './hooks/hook';
|
|
24
24
|
export { ExecutionContext } from './hooks/ExecutionContext';
|
|
25
|
+
// Others
|
|
25
26
|
export { by, IMemoKey, byAlias, byAliases } from './by';
|
|
26
27
|
export { setMetadata, getMetadata, setParameterMetadata, setMethodMetadata, getMethodMetadata, getParameterMetadata, } from './metadata';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-ioc-container",
|
|
3
|
-
"version": "32.
|
|
3
|
+
"version": "32.8.0",
|
|
4
4
|
"description": "Typescript IoC container",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public",
|
|
@@ -59,5 +59,5 @@
|
|
|
59
59
|
"ts-node": "^10.9.1",
|
|
60
60
|
"typescript": "5.4.3"
|
|
61
61
|
},
|
|
62
|
-
"gitHead": "
|
|
62
|
+
"gitHead": "a2647045895a5eae9199e84d613cc5cb735c111c"
|
|
63
63
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { InjectFn } from '../injector/MetadataInjector';
|
|
1
2
|
import { IContainer } from '../container/IContainer';
|
|
2
3
|
export declare class ExecutionContext {
|
|
3
4
|
instance: object;
|
|
@@ -8,4 +9,5 @@ export declare class ExecutionContext {
|
|
|
8
9
|
invokeMethod({ args }: {
|
|
9
10
|
args: unknown[];
|
|
10
11
|
}): unknown;
|
|
12
|
+
injectProperty(fn: InjectFn): void;
|
|
11
13
|
}
|
package/typings/hooks/hook.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { IContainer } from '../container/IContainer';
|
|
2
2
|
import { ExecutionContext } from './ExecutionContext';
|
|
3
|
+
import { InjectFn } from '../injector/MetadataInjector';
|
|
3
4
|
export type Execution<T extends ExecutionContext = ExecutionContext> = (context: T) => void;
|
|
5
|
+
export declare const injectProp: (fn: InjectFn) => Execution;
|
|
4
6
|
export declare const hook: (key: string | symbol, ...fns: Execution[]) => (target: object, propertyKey: string | symbol) => void;
|
|
5
7
|
export declare function getHooks(target: object, key: string | symbol): Map<string, Execution[]>;
|
|
6
8
|
export declare function hasHooks(target: object, key: string | symbol): boolean;
|
package/typings/index.d.ts
CHANGED
|
@@ -17,7 +17,7 @@ export { Registration } from './registration/Registration';
|
|
|
17
17
|
export { DependencyNotFoundError } from './errors/DependencyNotFoundError';
|
|
18
18
|
export { MethodNotImplementedError } from './errors/MethodNotImplementedError';
|
|
19
19
|
export { ContainerDisposedError } from './errors/ContainerDisposedError';
|
|
20
|
-
export { getHooks, hook, hasHooks, Execution, executeHooks } from './hooks/hook';
|
|
20
|
+
export { getHooks, hook, hasHooks, Execution, executeHooks, injectProp } from './hooks/hook';
|
|
21
21
|
export { ExecutionContext } from './hooks/ExecutionContext';
|
|
22
22
|
export { by, InstancePredicate, IMemo, IMemoKey, byAlias, byAliases } from './by';
|
|
23
23
|
export { constructor } from './utils';
|