ts-ioc-container 32.8.0 → 32.9.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 +28 -0
- package/cjm/hooks/hook.js +15 -3
- package/cjm/index.js +2 -1
- package/esm/hooks/hook.js +12 -1
- package/esm/index.js +1 -1
- package/package.json +2 -2
- package/typings/hooks/hook.d.ts +5 -1
- package/typings/index.d.ts +1 -1
package/README.md
CHANGED
|
@@ -46,6 +46,7 @@
|
|
|
46
46
|
- [OnConstruct](#onconstruct) `@onConstruct`
|
|
47
47
|
- [OnDispose](#ondispose) `@onDispose`
|
|
48
48
|
- [Inject Property](#inject-property)
|
|
49
|
+
- [Inject Method](#inject-method)
|
|
49
50
|
- [Mock](#mock)
|
|
50
51
|
- [Error](#error)
|
|
51
52
|
|
|
@@ -1282,6 +1283,33 @@ describe('inject property', () => {
|
|
|
1282
1283
|
|
|
1283
1284
|
```
|
|
1284
1285
|
|
|
1286
|
+
### Inject method
|
|
1287
|
+
|
|
1288
|
+
```typescript
|
|
1289
|
+
import { by, Container, executeHooks, hook, inject, invokeExecution, MetadataInjector, Registration } from 'ts-ioc-container';
|
|
1290
|
+
|
|
1291
|
+
describe('inject method', () => {
|
|
1292
|
+
it('should inject method', () => {
|
|
1293
|
+
class App {
|
|
1294
|
+
greeting!: string;
|
|
1295
|
+
|
|
1296
|
+
@hook('onInit', invokeExecution({ handleError: jest.fn(), handleResult: jest.fn() }))
|
|
1297
|
+
setGreeting(@inject(by.key('greeting')) greeting: string) {
|
|
1298
|
+
this.greeting = greeting;
|
|
1299
|
+
}
|
|
1300
|
+
}
|
|
1301
|
+
const expected = 'Hello world!';
|
|
1302
|
+
|
|
1303
|
+
const container = new Container(new MetadataInjector()).add(Registration.fromValue(expected).to('greeting'));
|
|
1304
|
+
const app = container.resolve(App);
|
|
1305
|
+
executeHooks(app, 'onInit', { scope: container });
|
|
1306
|
+
|
|
1307
|
+
expect(app.greeting).toBe(expected);
|
|
1308
|
+
});
|
|
1309
|
+
});
|
|
1310
|
+
|
|
1311
|
+
```
|
|
1312
|
+
|
|
1285
1313
|
## Mock
|
|
1286
1314
|
Sometimes you need to automatically mock all dependencies in container. This is what `AutoMockedContainer` is for.
|
|
1287
1315
|
|
package/cjm/hooks/hook.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.invokeExecution = exports.injectProp = exports.executeHooks = exports.hasHooks = exports.getHooks = exports.hook = void 0;
|
|
4
4
|
const ExecutionContext_1 = require("./ExecutionContext");
|
|
5
|
-
const
|
|
6
|
-
exports.injectProp = injectProp;
|
|
5
|
+
const MetadataInjector_1 = require("../injector/MetadataInjector");
|
|
7
6
|
const hook = (key, ...fns) => (target, propertyKey) => {
|
|
8
7
|
const hooks = Reflect.hasMetadata(key, target.constructor)
|
|
9
8
|
? Reflect.getMetadata(key, target.constructor)
|
|
@@ -28,3 +27,16 @@ const executeHooks = (target, key, { scope, createContext = (c) => c, }) => {
|
|
|
28
27
|
}
|
|
29
28
|
};
|
|
30
29
|
exports.executeHooks = executeHooks;
|
|
30
|
+
const injectProp = (fn) => (context) => context.injectProperty(fn);
|
|
31
|
+
exports.injectProp = injectProp;
|
|
32
|
+
const invokeExecution = ({ handleError, handleResult, }) => (context) => {
|
|
33
|
+
const args = (0, MetadataInjector_1.resolveArgs)(context.instance.constructor, context.methodName)(context.scope);
|
|
34
|
+
try {
|
|
35
|
+
const result = context.invokeMethod({ args });
|
|
36
|
+
handleResult(result, context);
|
|
37
|
+
}
|
|
38
|
+
catch (e) {
|
|
39
|
+
handleError(e, context.scope);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
exports.invokeExecution = invokeExecution;
|
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.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;
|
|
3
|
+
exports.getParameterMetadata = exports.getMethodMetadata = exports.setMethodMetadata = exports.setParameterMetadata = exports.getMetadata = exports.setMetadata = exports.byAliases = exports.byAlias = exports.IMemoKey = exports.by = exports.ExecutionContext = exports.invokeExecution = 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; } });
|
|
@@ -58,6 +58,7 @@ Object.defineProperty(exports, "hook", { enumerable: true, get: function () { re
|
|
|
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
60
|
Object.defineProperty(exports, "injectProp", { enumerable: true, get: function () { return hook_1.injectProp; } });
|
|
61
|
+
Object.defineProperty(exports, "invokeExecution", { enumerable: true, get: function () { return hook_1.invokeExecution; } });
|
|
61
62
|
var ExecutionContext_1 = require("./hooks/ExecutionContext");
|
|
62
63
|
Object.defineProperty(exports, "ExecutionContext", { enumerable: true, get: function () { return ExecutionContext_1.ExecutionContext; } });
|
|
63
64
|
// Others
|
package/esm/hooks/hook.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ExecutionContext } from './ExecutionContext';
|
|
2
|
-
|
|
2
|
+
import { resolveArgs } from '../injector/MetadataInjector';
|
|
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)
|
|
@@ -20,3 +20,14 @@ export const executeHooks = (target, key, { scope, createContext = (c) => c, })
|
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
};
|
|
23
|
+
export const injectProp = (fn) => (context) => context.injectProperty(fn);
|
|
24
|
+
export const invokeExecution = ({ handleError, handleResult, }) => (context) => {
|
|
25
|
+
const args = resolveArgs(context.instance.constructor, context.methodName)(context.scope);
|
|
26
|
+
try {
|
|
27
|
+
const result = context.invokeMethod({ args });
|
|
28
|
+
handleResult(result, context);
|
|
29
|
+
}
|
|
30
|
+
catch (e) {
|
|
31
|
+
handleError(e, context.scope);
|
|
32
|
+
}
|
|
33
|
+
};
|
package/esm/index.js
CHANGED
|
@@ -20,7 +20,7 @@ export { DependencyNotFoundError } from './errors/DependencyNotFoundError';
|
|
|
20
20
|
export { MethodNotImplementedError } from './errors/MethodNotImplementedError';
|
|
21
21
|
export { ContainerDisposedError } from './errors/ContainerDisposedError';
|
|
22
22
|
// Hooks
|
|
23
|
-
export { getHooks, hook, hasHooks, executeHooks, injectProp } from './hooks/hook';
|
|
23
|
+
export { getHooks, hook, hasHooks, executeHooks, injectProp, invokeExecution } from './hooks/hook';
|
|
24
24
|
export { ExecutionContext } from './hooks/ExecutionContext';
|
|
25
25
|
// Others
|
|
26
26
|
export { by, IMemoKey, byAlias, byAliases } from './by';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-ioc-container",
|
|
3
|
-
"version": "32.
|
|
3
|
+
"version": "32.9.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": "17060847c051dbbdfd167675ec27af2ae78efbfe"
|
|
63
63
|
}
|
package/typings/hooks/hook.d.ts
CHANGED
|
@@ -2,7 +2,6 @@ import { IContainer } from '../container/IContainer';
|
|
|
2
2
|
import { ExecutionContext } from './ExecutionContext';
|
|
3
3
|
import { InjectFn } from '../injector/MetadataInjector';
|
|
4
4
|
export type Execution<T extends ExecutionContext = ExecutionContext> = (context: T) => void;
|
|
5
|
-
export declare const injectProp: (fn: InjectFn) => Execution;
|
|
6
5
|
export declare const hook: (key: string | symbol, ...fns: Execution[]) => (target: object, propertyKey: string | symbol) => void;
|
|
7
6
|
export declare function getHooks(target: object, key: string | symbol): Map<string, Execution[]>;
|
|
8
7
|
export declare function hasHooks(target: object, key: string | symbol): boolean;
|
|
@@ -10,3 +9,8 @@ export declare const executeHooks: <Context extends ExecutionContext>(target: ob
|
|
|
10
9
|
scope: IContainer;
|
|
11
10
|
createContext?: ((c: ExecutionContext) => Context) | undefined;
|
|
12
11
|
}) => void;
|
|
12
|
+
export declare const injectProp: (fn: InjectFn) => Execution;
|
|
13
|
+
export declare const invokeExecution: ({ handleError, handleResult, }: {
|
|
14
|
+
handleError: (e: Error, s: IContainer) => void;
|
|
15
|
+
handleResult: (result: unknown, context: ExecutionContext) => void;
|
|
16
|
+
}) => Execution;
|
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, injectProp } from './hooks/hook';
|
|
20
|
+
export { getHooks, hook, hasHooks, Execution, executeHooks, injectProp, invokeExecution } 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';
|