@nzyme/ioc 0.13.0 → 0.15.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/dist/Caller.d.ts +8 -0
- package/dist/Caller.js +8 -0
- package/dist/Caller.js.map +1 -1
- package/dist/Command.d.ts +44 -11
- package/dist/Command.js +29 -9
- package/dist/Command.js.map +1 -1
- package/dist/Command.test.d.ts +1 -0
- package/dist/Command.test.js +136 -0
- package/dist/Command.test.js.map +1 -0
- package/dist/Container.d.ts +119 -14
- package/dist/Container.js +28 -4
- package/dist/Container.js.map +1 -1
- package/dist/ContainerScope.d.ts +18 -0
- package/dist/ContainerScope.js +7 -1
- package/dist/ContainerScope.js.map +1 -1
- package/dist/Factory.d.ts +84 -0
- package/dist/Factory.js +42 -0
- package/dist/Factory.js.map +1 -0
- package/dist/Fetch.d.ts +3 -0
- package/dist/Fetch.js +4 -0
- package/dist/Fetch.js.map +1 -0
- package/dist/Injectable.d.ts +63 -28
- package/dist/Injectable.js +16 -8
- package/dist/Injectable.js.map +1 -1
- package/dist/Interface.d.ts +51 -34
- package/dist/Interface.js +39 -14
- package/dist/Interface.js.map +1 -1
- package/dist/Interface.test.d.ts +1 -0
- package/dist/Interface.test.js +37 -0
- package/dist/Interface.test.js.map +1 -0
- package/dist/Module.d.ts +26 -0
- package/dist/Module.js +10 -1
- package/dist/Module.js.map +1 -1
- package/dist/Service.d.ts +102 -30
- package/dist/Service.js +24 -19
- package/dist/Service.js.map +1 -1
- package/dist/Service.test.d.ts +1 -0
- package/dist/Service.test.js +378 -0
- package/dist/Service.test.js.map +1 -0
- package/dist/defineEnvVariable.d.ts +82 -0
- package/dist/defineEnvVariable.js +54 -0
- package/dist/defineEnvVariable.js.map +1 -0
- package/dist/helpers/EnvVariables.d.ts +10 -0
- package/dist/helpers/EnvVariables.js +9 -0
- package/dist/helpers/EnvVariables.js.map +1 -0
- package/dist/helpers/fallback.d.ts +1 -1
- package/dist/helpers/injectLazy.d.ts +7 -0
- package/dist/helpers/injectLazy.js +20 -0
- package/dist/helpers/injectLazy.js.map +1 -0
- package/dist/index.d.ts +13 -5
- package/dist/index.js +13 -5
- package/dist/index.js.map +1 -1
- package/dist/serviceResolve.d.ts +60 -13
- package/dist/serviceResolve.js +64 -22
- package/dist/serviceResolve.js.map +1 -1
- package/dist/services/ContextProvider.d.ts +8 -0
- package/dist/services/ContextProvider.js +37 -0
- package/dist/services/ContextProvider.js.map +1 -0
- package/dist/utils/getAllDeps.d.ts +5 -0
- package/dist/utils/getAllDeps.js +25 -0
- package/dist/utils/getAllDeps.js.map +1 -0
- package/dist/utils/isDependentOn.d.ts +6 -0
- package/dist/utils/isDependentOn.js +9 -0
- package/dist/utils/isDependentOn.js.map +1 -0
- package/dist/utils/resolveDeps.d.ts +2 -2
- package/dist/utils/resolveDeps.js.map +1 -1
- package/dist/utils/sortByDependency.d.ts +7 -0
- package/dist/utils/sortByDependency.js +46 -0
- package/dist/utils/sortByDependency.js.map +1 -0
- package/dist/utils/sortByDependency.test.d.ts +1 -0
- package/dist/utils/sortByDependency.test.js +100 -0
- package/dist/utils/sortByDependency.test.js.map +1 -0
- package/package.json +31 -38
- package/dist/Caller.cjs +0 -9
- package/dist/Caller.cjs.map +0 -1
- package/dist/Command.cjs +0 -20
- package/dist/Command.cjs.map +0 -1
- package/dist/Container.cjs +0 -69
- package/dist/Container.cjs.map +0 -1
- package/dist/ContainerScope.cjs +0 -10
- package/dist/ContainerScope.cjs.map +0 -1
- package/dist/Injectable.cjs +0 -29
- package/dist/Injectable.cjs.map +0 -1
- package/dist/Interface.cjs +0 -73
- package/dist/Interface.cjs.map +0 -1
- package/dist/Module.cjs +0 -8
- package/dist/Module.cjs.map +0 -1
- package/dist/Service.cjs +0 -39
- package/dist/Service.cjs.map +0 -1
- package/dist/helpers/callerName.cjs +0 -15
- package/dist/helpers/callerName.cjs.map +0 -1
- package/dist/helpers/constValue.cjs +0 -15
- package/dist/helpers/constValue.cjs.map +0 -1
- package/dist/helpers/envVariable.cjs +0 -23
- package/dist/helpers/envVariable.cjs.map +0 -1
- package/dist/helpers/envVariable.d.ts +0 -16
- package/dist/helpers/envVariable.js +0 -20
- package/dist/helpers/envVariable.js.map +0 -1
- package/dist/helpers/fallback.cjs +0 -23
- package/dist/helpers/fallback.cjs.map +0 -1
- package/dist/index.cjs +0 -17
- package/dist/index.cjs.map +0 -1
- package/dist/serviceResolve.cjs +0 -83
- package/dist/serviceResolve.cjs.map +0 -1
- package/dist/utils/resolveDeps.cjs +0 -18
- package/dist/utils/resolveDeps.cjs.map +0 -1
package/dist/Caller.d.ts
CHANGED
|
@@ -1 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Special injectable that resolves to the service that requested the injection.
|
|
3
|
+
* This is useful for:
|
|
4
|
+
* - Getting information about the dependency chain
|
|
5
|
+
* - Implementing circular dependency patterns
|
|
6
|
+
* - Accessing the calling service's context
|
|
7
|
+
* - Debugging dependency resolution
|
|
8
|
+
*/
|
|
1
9
|
export declare const Caller: import("./Injectable.js").Injectable<import("./Injectable.js").Injectable<unknown> | undefined>;
|
package/dist/Caller.js
CHANGED
|
@@ -1,4 +1,12 @@
|
|
|
1
1
|
import { defineInjectable } from './Injectable.js';
|
|
2
|
+
/**
|
|
3
|
+
* Special injectable that resolves to the service that requested the injection.
|
|
4
|
+
* This is useful for:
|
|
5
|
+
* - Getting information about the dependency chain
|
|
6
|
+
* - Implementing circular dependency patterns
|
|
7
|
+
* - Accessing the calling service's context
|
|
8
|
+
* - Debugging dependency resolution
|
|
9
|
+
*/
|
|
2
10
|
export const Caller = defineInjectable({
|
|
3
11
|
name: 'Caller',
|
|
4
12
|
resolve: (_container, service) => service,
|
package/dist/Caller.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Caller.js","sourceRoot":"","sources":["../src/Caller.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAEnD,MAAM,CAAC,MAAM,MAAM,GAAG,gBAAgB,CAAC;IACnC,IAAI,EAAE,QAAQ;IACd,OAAO,EAAE,CAAC,UAAU,EAAE,OAAO,EAAE,EAAE,CAAC,OAAO;CAC5C,CAAC,CAAC"}
|
|
1
|
+
{"version":3,"file":"Caller.js","sourceRoot":"","sources":["../src/Caller.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAEnD;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,gBAAgB,CAAC;IACnC,IAAI,EAAE,QAAQ;IACd,OAAO,EAAE,CAAC,UAAU,EAAE,OAAO,EAAE,EAAE,CAAC,OAAO;CAC5C,CAAC,CAAC"}
|
package/dist/Command.d.ts
CHANGED
|
@@ -1,13 +1,46 @@
|
|
|
1
|
-
import type { EmptyObject } from '@nzyme/types';
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
1
|
+
import type { EmptyObject } from '@nzyme/types/EmptyObject.js';
|
|
2
|
+
import type { OmitProps } from '@nzyme/types/Object.js';
|
|
3
|
+
import type { Dependencies, Service, ServiceOptions } from './Service.js';
|
|
4
|
+
/**
|
|
5
|
+
* Represents a command as a specialized service with singleton resolution.
|
|
6
|
+
* Commands are services that encapsulate business logic and are resolved as singletons.
|
|
7
|
+
*
|
|
8
|
+
* @template T - Type of the command function
|
|
9
|
+
*/
|
|
11
10
|
export type Command<T extends CommandFunction = CommandFunction> = Service<T>;
|
|
11
|
+
/**
|
|
12
|
+
* Represents a command function that can be executed with parameters.
|
|
13
|
+
* Commands are specialized services that encapsulate business logic or operations.
|
|
14
|
+
*
|
|
15
|
+
* @template P - Array of parameter types that the command accepts
|
|
16
|
+
* @template R - Return type of the command execution
|
|
17
|
+
*/
|
|
18
|
+
export type CommandFunction<P extends any[] = any[], R = unknown> = (...params: P) => R;
|
|
19
|
+
/**
|
|
20
|
+
* Configuration options for defining a command.
|
|
21
|
+
* Extends ServiceOptions but excludes resolution strategy as commands use a fixed strategy.
|
|
22
|
+
*
|
|
23
|
+
* @template T - Type of the command function
|
|
24
|
+
* @template TExtend - Extended type of the command function (for inheritance)
|
|
25
|
+
* @template TDeps - Dependencies required by the command
|
|
26
|
+
*/
|
|
27
|
+
export type CommandOptions<T extends CommandFunction, TExtend extends T = T, TDeps extends Dependencies = EmptyObject> = OmitProps<ServiceOptions<T, TExtend, TDeps>, 'resolution'>;
|
|
28
|
+
/**
|
|
29
|
+
* Extracts the result type of a command, including handling of Promise results.
|
|
30
|
+
*
|
|
31
|
+
* @template T - Type of the command
|
|
32
|
+
* @returns The resolved return type of the command, handling both sync and async results
|
|
33
|
+
*/
|
|
12
34
|
export type CommandResult<T extends Command<any>> = T extends Command<infer R> ? Awaited<ReturnType<R>> : never;
|
|
13
|
-
|
|
35
|
+
/**
|
|
36
|
+
* Creates a new command with the specified options.
|
|
37
|
+
* Commands are resolved as singletons and their results are memoized.
|
|
38
|
+
*
|
|
39
|
+
* @template T - Type of the command function
|
|
40
|
+
* @template TExtend - Extended type of the command function (for inheritance)
|
|
41
|
+
* @template TDeps - Dependencies required by the command
|
|
42
|
+
* @param options - Configuration options for the command
|
|
43
|
+
* @returns A new command instance
|
|
44
|
+
* @__NO_SIDE_EFFECTS__
|
|
45
|
+
*/
|
|
46
|
+
export declare function defineCommand<T extends CommandFunction, TExtend extends T = T, TDeps extends Dependencies = EmptyObject>(options: ServiceOptions<T, TExtend, TDeps>): Service<TExtend, TDeps>;
|
package/dist/Command.js
CHANGED
|
@@ -1,17 +1,37 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { defineService } from './Service.js';
|
|
2
|
+
import { defineResolutionStrategy, singletonStrategy } from './serviceResolve.js';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a new command with the specified options.
|
|
5
|
+
* Commands are resolved as singletons and their results are memoized.
|
|
6
|
+
*
|
|
7
|
+
* @template T - Type of the command function
|
|
8
|
+
* @template TExtend - Extended type of the command function (for inheritance)
|
|
9
|
+
* @template TDeps - Dependencies required by the command
|
|
10
|
+
* @param options - Configuration options for the command
|
|
11
|
+
* @returns A new command instance
|
|
12
|
+
* @__NO_SIDE_EFFECTS__
|
|
13
|
+
*/
|
|
5
14
|
export function defineCommand(options) {
|
|
6
15
|
return defineService({
|
|
7
16
|
...options,
|
|
8
17
|
resolution: commandStrategy,
|
|
9
18
|
});
|
|
10
19
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
20
|
+
/**
|
|
21
|
+
* Custom resolution strategy for commands.
|
|
22
|
+
* Ensures commands are:
|
|
23
|
+
* - Resolved as singletons
|
|
24
|
+
* - Properly memoized for performance
|
|
25
|
+
* - Maintain consistent execution context
|
|
26
|
+
*/
|
|
27
|
+
const commandStrategy = defineResolutionStrategy((service, container, caller) => {
|
|
28
|
+
let instance;
|
|
29
|
+
const command = (...args) => {
|
|
30
|
+
if (instance === undefined) {
|
|
31
|
+
instance = singletonStrategy(service, container, caller);
|
|
32
|
+
}
|
|
33
|
+
return instance(...args);
|
|
34
|
+
};
|
|
15
35
|
return command;
|
|
16
|
-
}
|
|
36
|
+
});
|
|
17
37
|
//# sourceMappingURL=Command.js.map
|
package/dist/Command.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Command.js","sourceRoot":"","sources":["../src/Command.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Command.js","sourceRoot":"","sources":["../src/Command.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE7C,OAAO,EAAE,wBAAwB,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AA2ClF;;;;;;;;;;GAUG;AACH,MAAM,UAAU,aAAa,CAI3B,OAA0C;IACxC,OAAO,aAAa,CAAoB;QACpC,GAAG,OAAO;QACV,UAAU,EAAE,eAAe;KAC9B,CAAC,CAAC;AACP,CAAC;AAED;;;;;;GAMG;AACH,MAAM,eAAe,GAAG,wBAAwB,CAAC,CAAC,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,EAAE;IAC5E,IAAI,QAAqC,CAAC;IAE1C,MAAM,OAAO,GAAoB,CAAC,GAAG,IAAe,EAAE,EAAE;QACpD,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;YACzB,QAAQ,GAAG,iBAAiB,CAAC,OAAO,EAAE,SAAS,EAAE,MAAM,CAAoB,CAAC;QAChF,CAAC;QAED,OAAO,QAAQ,CAAC,GAAG,IAAI,CAAC,CAAC;IAC7B,CAAC,CAAC;IAEF,OAAO,OAAO,CAAC;AACnB,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { expect, test } from 'bun:test';
|
|
2
|
+
import { defineCommand } from './Command.js';
|
|
3
|
+
import { createContainer } from './Container.js';
|
|
4
|
+
import { defineInterface } from './Interface.js';
|
|
5
|
+
import { defineService } from './Service.js';
|
|
6
|
+
test('resolve command with no deps', () => {
|
|
7
|
+
const container = createContainer();
|
|
8
|
+
let count = 0;
|
|
9
|
+
const command = defineCommand({
|
|
10
|
+
setup() {
|
|
11
|
+
count++;
|
|
12
|
+
return (value) => {
|
|
13
|
+
return value;
|
|
14
|
+
};
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
expect(count).toBe(0);
|
|
18
|
+
const resolved = container.resolve(command);
|
|
19
|
+
expect(count).toBe(0); // Commands are lazily resolved
|
|
20
|
+
expect(resolved(4)).toBe(4);
|
|
21
|
+
expect(count).toBe(1);
|
|
22
|
+
// Resolved commands are cached
|
|
23
|
+
const other = container.resolve(command);
|
|
24
|
+
expect(other).not.toBe(resolved); // Cached function is a different instance
|
|
25
|
+
expect(count).toBe(1);
|
|
26
|
+
expect(other(4)).toBe(4);
|
|
27
|
+
expect(count).toBe(1);
|
|
28
|
+
});
|
|
29
|
+
test('resolve command registered as injectable', () => {
|
|
30
|
+
const container = createContainer();
|
|
31
|
+
let count = 0;
|
|
32
|
+
const injectable = defineInterface();
|
|
33
|
+
const command = defineCommand({
|
|
34
|
+
implements: injectable,
|
|
35
|
+
setup() {
|
|
36
|
+
count++;
|
|
37
|
+
return (value) => {
|
|
38
|
+
return value;
|
|
39
|
+
};
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
container.set(injectable, command);
|
|
43
|
+
expect(count).toBe(0);
|
|
44
|
+
const resolved = container.resolve(injectable);
|
|
45
|
+
expect(count).toBe(0); // Commands are lazily resolved
|
|
46
|
+
expect(resolved(4)).toBe(4);
|
|
47
|
+
expect(count).toBe(1);
|
|
48
|
+
// Resolved commands are cached
|
|
49
|
+
const other = container.resolve(injectable);
|
|
50
|
+
expect(other).not.toBe(resolved); // Cached function is a different instance
|
|
51
|
+
expect(count).toBe(1);
|
|
52
|
+
expect(other(5)).toBe(5);
|
|
53
|
+
expect(count).toBe(1);
|
|
54
|
+
// Resolve command directly
|
|
55
|
+
const direct = container.resolve(command);
|
|
56
|
+
expect(direct).not.toBe(resolved); // Cached function is a different instance
|
|
57
|
+
expect(direct(6)).toBe(6);
|
|
58
|
+
expect(count).toBe(1);
|
|
59
|
+
});
|
|
60
|
+
test('resolve command with service dependency', () => {
|
|
61
|
+
const container = createContainer();
|
|
62
|
+
let serviceCount = 0;
|
|
63
|
+
const service = defineService({
|
|
64
|
+
setup() {
|
|
65
|
+
serviceCount++;
|
|
66
|
+
return 'foo';
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
let commandCount = 0;
|
|
70
|
+
const command = defineCommand({
|
|
71
|
+
deps: {
|
|
72
|
+
service,
|
|
73
|
+
},
|
|
74
|
+
setup({ service: _service }) {
|
|
75
|
+
commandCount++;
|
|
76
|
+
return (value) => {
|
|
77
|
+
return value;
|
|
78
|
+
};
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
expect(serviceCount).toBe(0);
|
|
82
|
+
expect(commandCount).toBe(0);
|
|
83
|
+
const resolvedCommand = container.resolve(command);
|
|
84
|
+
expect(serviceCount).toBe(0); // Commands are lazily resolved
|
|
85
|
+
expect(commandCount).toBe(0); // Commands are lazily resolved
|
|
86
|
+
expect(resolvedCommand(4)).toBe(4);
|
|
87
|
+
expect(serviceCount).toBe(1);
|
|
88
|
+
expect(commandCount).toBe(1);
|
|
89
|
+
// Resolved commands are cached
|
|
90
|
+
const resolvedCommand2 = container.resolve(command);
|
|
91
|
+
expect(resolvedCommand2).not.toBe(resolvedCommand); // Cached function is a different instance
|
|
92
|
+
expect(serviceCount).toBe(1);
|
|
93
|
+
expect(commandCount).toBe(1);
|
|
94
|
+
expect(resolvedCommand2(4)).toBe(4);
|
|
95
|
+
expect(serviceCount).toBe(1);
|
|
96
|
+
expect(commandCount).toBe(1);
|
|
97
|
+
});
|
|
98
|
+
test('resolve service with command dependency', () => {
|
|
99
|
+
const container = createContainer();
|
|
100
|
+
let commandCount = 0;
|
|
101
|
+
const command = defineCommand({
|
|
102
|
+
setup() {
|
|
103
|
+
commandCount++;
|
|
104
|
+
return (value) => {
|
|
105
|
+
return value;
|
|
106
|
+
};
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
let serviceCount = 0;
|
|
110
|
+
const service = defineService({
|
|
111
|
+
deps: {
|
|
112
|
+
command,
|
|
113
|
+
},
|
|
114
|
+
setup({ command: resolvedCommand }) {
|
|
115
|
+
serviceCount++;
|
|
116
|
+
return resolvedCommand;
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
expect(serviceCount).toBe(0);
|
|
120
|
+
expect(commandCount).toBe(0);
|
|
121
|
+
const resolvedService = container.resolve(service);
|
|
122
|
+
expect(serviceCount).toBe(1);
|
|
123
|
+
expect(commandCount).toBe(0); // Commands are lazily resolved
|
|
124
|
+
expect(resolvedService(4)).toBe(4);
|
|
125
|
+
expect(serviceCount).toBe(1);
|
|
126
|
+
expect(commandCount).toBe(1);
|
|
127
|
+
// Resolved commands are cached
|
|
128
|
+
const resolvedCommand = container.resolve(command);
|
|
129
|
+
expect(resolvedCommand).not.toBe(resolvedService); // Cached function is a different instance
|
|
130
|
+
expect(serviceCount).toBe(1);
|
|
131
|
+
expect(commandCount).toBe(1);
|
|
132
|
+
expect(resolvedCommand(4)).toBe(4);
|
|
133
|
+
expect(serviceCount).toBe(1);
|
|
134
|
+
expect(commandCount).toBe(1);
|
|
135
|
+
});
|
|
136
|
+
//# sourceMappingURL=Command.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Command.test.js","sourceRoot":"","sources":["../src/Command.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAExC,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7C,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACjD,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE7C,IAAI,CAAC,8BAA8B,EAAE,GAAG,EAAE;IACtC,MAAM,SAAS,GAAG,eAAe,EAAE,CAAC;IAEpC,IAAI,KAAK,GAAG,CAAC,CAAC;IAEd,MAAM,OAAO,GAAG,aAAa,CAAC;QAC1B,KAAK;YACD,KAAK,EAAE,CAAC;YACR,OAAO,CAAC,KAAa,EAAE,EAAE;gBACrB,OAAO,KAAK,CAAC;YACjB,CAAC,CAAC;QACN,CAAC;KACJ,CAAC,CAAC;IAEH,MAAM,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAEtB,MAAM,QAAQ,GAAG,SAAS,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAC5C,MAAM,CAAC,KAAK,C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AC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAE7B,+BAA+B;IAC/B,MAAM,eAAe,GAAG,SAAS,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IACnD,MAAM,CAAC,eAAe,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC,CAAC,0CAA0C;IAC7F,MAAM,CAAC,YAAY,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAC7B,MAAM,CAAC,YAAY,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAE7B,MAAM,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACnC,MAAM,CAAC,YAAY,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAC7B,MAAM,CAAC,YAAY,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACjC,CAAC,CAAC,CAAC"}
|
package/dist/Container.d.ts
CHANGED
|
@@ -1,27 +1,132 @@
|
|
|
1
1
|
import type { ContainerScope } from './ContainerScope.js';
|
|
2
|
-
import {
|
|
2
|
+
import type { Injectable } from './Injectable.js';
|
|
3
3
|
import type { Interface } from './Interface.js';
|
|
4
4
|
import type { Module } from './Module.js';
|
|
5
|
-
import {
|
|
5
|
+
import type { Service } from './Service.js';
|
|
6
|
+
/**
|
|
7
|
+
* Symbol used to identify container instances.
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
6
10
|
export declare const CONTAINER_SYMBOL: unique symbol;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
createChild?: (scope: ContainerScope) => Container;
|
|
12
|
-
};
|
|
11
|
+
/**
|
|
12
|
+
* A dependency injection container that manages the lifecycle and resolution of services.
|
|
13
|
+
* Provides methods for registering, resolving, and managing dependencies.
|
|
14
|
+
*/
|
|
13
15
|
export type Container = {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
/**
|
|
17
|
+
* Registers a module with the container and executes its initialization logic.
|
|
18
|
+
* @template TParams - Type of the module's parameters
|
|
19
|
+
* @template TResult - Type of the module's result
|
|
20
|
+
* @param module - The module to register
|
|
21
|
+
* @param params - Parameters to pass to the module
|
|
22
|
+
*/
|
|
17
23
|
addModule<TParams extends unknown[], TResult>(this: void, module: Module<TParams, TResult>, ...params: TParams): void;
|
|
24
|
+
/**
|
|
25
|
+
* Symbol used to identify container instances.
|
|
26
|
+
* @internal
|
|
27
|
+
*/
|
|
28
|
+
readonly [CONTAINER_SYMBOL]: true;
|
|
29
|
+
/**
|
|
30
|
+
* Creates a new child container with the specified scope.
|
|
31
|
+
* @param scope - The scope for the new child container
|
|
32
|
+
* @returns A new child container instance
|
|
33
|
+
*/
|
|
18
34
|
createChild(this: void, scope: ContainerScope): Container;
|
|
19
|
-
|
|
35
|
+
/**
|
|
36
|
+
* Retrieves an instance or injectable from the container.
|
|
37
|
+
* @template T - The type of the injectable
|
|
38
|
+
* @param injectable - The injectable to retrieve
|
|
39
|
+
* @returns The instance or injectable if found, undefined otherwise
|
|
40
|
+
*/
|
|
41
|
+
get<T>(this: void, injectable: Injectable<T>): Injectable<T> | T | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* Optional parent container in the dependency injection hierarchy.
|
|
44
|
+
*/
|
|
45
|
+
readonly parent?: Container;
|
|
46
|
+
/**
|
|
47
|
+
* Registers a service in the container.
|
|
48
|
+
* @template T - The type of the service
|
|
49
|
+
* @param service - The service to register
|
|
50
|
+
*/
|
|
51
|
+
register<T>(this: void, service: Injectable<T>): void;
|
|
52
|
+
/**
|
|
53
|
+
* Resolves an injectable to its concrete instance.
|
|
54
|
+
* @template T - The type of the injectable
|
|
55
|
+
* @param injectable - The injectable to resolve
|
|
56
|
+
* @param caller - Optional source interface for resolution
|
|
57
|
+
* @returns The resolved instance
|
|
58
|
+
* @throws Error if the injectable cannot be resolved
|
|
59
|
+
*/
|
|
60
|
+
resolve<T>(this: void, injectable: Injectable<T>, caller?: Interface): T;
|
|
61
|
+
/**
|
|
62
|
+
* Optional scope that defines the lifecycle and visibility of services.
|
|
63
|
+
*/
|
|
64
|
+
readonly scope?: ContainerScope;
|
|
65
|
+
/**
|
|
66
|
+
* Registers an instance in the container.
|
|
67
|
+
* @template T - The type of the instance
|
|
68
|
+
* @param injectable - The injectable to associate with the instance
|
|
69
|
+
* @param instance - The instance to register
|
|
70
|
+
*/
|
|
20
71
|
set<T>(this: void, injectable: Injectable<T>, instance: T): void;
|
|
72
|
+
/**
|
|
73
|
+
* Registers a service in the container.
|
|
74
|
+
* @template T - The type of the service
|
|
75
|
+
* @param injectable - The injectable to associate with the service
|
|
76
|
+
* @param service - The service to register
|
|
77
|
+
*/
|
|
21
78
|
set<T>(this: void, injectable: Injectable<T>, service: Injectable<T>): void;
|
|
22
|
-
|
|
23
|
-
|
|
79
|
+
/**
|
|
80
|
+
* Attempts to resolve an injectable, returning undefined if resolution fails.
|
|
81
|
+
* @template T - The type of the injectable
|
|
82
|
+
* @param injectable - The injectable to resolve
|
|
83
|
+
* @param caller - Optional source interface for resolution
|
|
84
|
+
* @returns The resolved instance if successful, undefined otherwise
|
|
85
|
+
*/
|
|
86
|
+
tryResolve<T>(this: void, injectable: Injectable<T>, caller?: Interface): T | undefined;
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Configuration options for creating a new container instance.
|
|
90
|
+
*/
|
|
91
|
+
export type ContainerOptions = {
|
|
92
|
+
/**
|
|
93
|
+
* Optional custom function for creating child containers.
|
|
94
|
+
* @param scope - The scope for the new child container
|
|
95
|
+
* @returns A new child container instance
|
|
96
|
+
*/
|
|
97
|
+
createChild?: (scope: ContainerScope) => Container;
|
|
98
|
+
/**
|
|
99
|
+
* Optional parent container for hierarchical dependency injection.
|
|
100
|
+
* When specified, the container will delegate unresolved dependencies to its parent.
|
|
101
|
+
*/
|
|
102
|
+
parent?: Container;
|
|
103
|
+
/**
|
|
104
|
+
* Optional custom resolution strategy for services.
|
|
105
|
+
* @param service - The service to resolve
|
|
106
|
+
* @param scope - Optional source interface for resolution
|
|
107
|
+
* @returns The resolved service instance
|
|
108
|
+
*/
|
|
109
|
+
resolve?: (service: Service, scope?: Interface) => unknown;
|
|
110
|
+
/**
|
|
111
|
+
* Optional scope that defines the lifecycle and visibility of services within the container.
|
|
112
|
+
*/
|
|
113
|
+
scope?: ContainerScope;
|
|
24
114
|
};
|
|
115
|
+
/**
|
|
116
|
+
* Injectable that provides access to the container itself.
|
|
117
|
+
* Useful for services that need to perform custom dependency resolution.
|
|
118
|
+
*/
|
|
25
119
|
export declare const Container: Injectable<Container>;
|
|
120
|
+
/**
|
|
121
|
+
* Creates a new dependency injection container with the specified options.
|
|
122
|
+
* @param options - Optional configuration for the container
|
|
123
|
+
* @returns A new container instance
|
|
124
|
+
* @__NO_SIDE_EFFECTS__
|
|
125
|
+
*/
|
|
26
126
|
export declare function createContainer(options?: ContainerOptions): Container;
|
|
127
|
+
/**
|
|
128
|
+
* Type guard to check if a value is a Container instance.
|
|
129
|
+
* @param value - The value to check
|
|
130
|
+
* @returns True if the value is a Container, false otherwise
|
|
131
|
+
*/
|
|
27
132
|
export declare function isContainer(value: unknown): value is Container;
|
package/dist/Container.js
CHANGED
|
@@ -1,17 +1,31 @@
|
|
|
1
1
|
import { defineInjectable, isInjectable } from './Injectable.js';
|
|
2
2
|
import { isService } from './Service.js';
|
|
3
|
+
/**
|
|
4
|
+
* Symbol used to identify container instances.
|
|
5
|
+
* @internal
|
|
6
|
+
*/
|
|
3
7
|
export const CONTAINER_SYMBOL = Symbol('container');
|
|
8
|
+
/**
|
|
9
|
+
* Injectable that provides access to the container itself.
|
|
10
|
+
* Useful for services that need to perform custom dependency resolution.
|
|
11
|
+
*/
|
|
4
12
|
export const Container = defineInjectable({
|
|
5
13
|
name: 'Container',
|
|
6
14
|
resolve: container => container,
|
|
7
15
|
});
|
|
16
|
+
/**
|
|
17
|
+
* Creates a new dependency injection container with the specified options.
|
|
18
|
+
* @param options - Optional configuration for the container
|
|
19
|
+
* @returns A new container instance
|
|
20
|
+
* @__NO_SIDE_EFFECTS__
|
|
21
|
+
*/
|
|
8
22
|
export function createContainer(options) {
|
|
9
23
|
const instances = new Map();
|
|
10
24
|
const injectables = new Map();
|
|
11
25
|
const parent = options?.parent;
|
|
12
26
|
const scope = options?.scope;
|
|
13
27
|
const resolve = options?.resolve ?? ((service, source) => service.resolve(container, source));
|
|
14
|
-
const createChild = options?.createChild ?? (
|
|
28
|
+
const createChild = options?.createChild ?? (childScope => createContainer({ parent: container, scope: childScope }));
|
|
15
29
|
const container = {
|
|
16
30
|
[CONTAINER_SYMBOL]: true,
|
|
17
31
|
scope,
|
|
@@ -20,13 +34,13 @@ export function createContainer(options) {
|
|
|
20
34
|
createChild,
|
|
21
35
|
get,
|
|
22
36
|
set,
|
|
23
|
-
|
|
37
|
+
register,
|
|
38
|
+
resolve: resolve,
|
|
24
39
|
tryResolve,
|
|
25
40
|
};
|
|
26
41
|
return container;
|
|
27
42
|
function get(injectable) {
|
|
28
|
-
return (instances.get(injectable) ??
|
|
29
|
-
injectables.get(injectable));
|
|
43
|
+
return (instances.get(injectable) ?? injectables.get(injectable));
|
|
30
44
|
}
|
|
31
45
|
function set(injectable, instanceOrService) {
|
|
32
46
|
if (isInjectable(instanceOrService)) {
|
|
@@ -36,6 +50,11 @@ export function createContainer(options) {
|
|
|
36
50
|
instances.set(injectable, instanceOrService);
|
|
37
51
|
}
|
|
38
52
|
}
|
|
53
|
+
function register(service) {
|
|
54
|
+
if (service.implements) {
|
|
55
|
+
injectables.set(service.implements, service);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
39
58
|
function tryResolve(injectable, source) {
|
|
40
59
|
const instance = instances.get(injectable);
|
|
41
60
|
if (instance) {
|
|
@@ -58,6 +77,11 @@ export function createContainer(options) {
|
|
|
58
77
|
return resolve(service, source);
|
|
59
78
|
}
|
|
60
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* Type guard to check if a value is a Container instance.
|
|
82
|
+
* @param value - The value to check
|
|
83
|
+
* @returns True if the value is a Container, false otherwise
|
|
84
|
+
*/
|
|
61
85
|
export function isContainer(value) {
|
|
62
86
|
return typeof value === 'object' && value != null && CONTAINER_SYMBOL in value;
|
|
63
87
|
}
|
package/dist/Container.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Container.js","sourceRoot":"","sources":["../src/Container.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,YAAY,
|
|
1
|
+
{"version":3,"file":"Container.js","sourceRoot":"","sources":["../src/Container.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAIjE,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAGzC;;;GAGG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC;AAgHpD;;;GAGG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,gBAAgB,CAAC;IACtC,IAAI,EAAE,WAAW;IACjB,OAAO,EAAE,SAAS,CAAC,EAAE,CAAC,SAAS;CAClC,CAAC,CAAC;AAEH;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CAAC,OAA0B;IACtD,MAAM,SAAS,GAAG,IAAI,GAAG,EAAmB,CAAC;IAC7C,MAAM,WAAW,GAAG,IAAI,GAAG,EAAsB,CAAC;IAClD,MAAM,MAAM,GAAG,OAAO,EAAE,MAAM,CAAC;IAC/B,MAAM,KAAK,GAAG,OAAO,EAAE,KAAK,CAAC;IAC7B,MAAM,OAAO,GAAG,OAAO,EAAE,OAAO,IAAI,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC,CAAC;IAC9F,MAAM,WAAW,GACb,OAAO,EAAE,WAAW,IAAI,CAAC,UAAU,CAAC,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,CAAC,UAAU,CAA2B,CAAC;QACtE,IAAI,OAAO,EAAE,CAAC;YACV,OAAO,cAAc,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QAC3C,CAAC;QAED,IAAI,SAAS,CAAC,UAAU,CAAC,EAAE,CAAC;YACxB,OAAO,cAAc,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;QAC9C,CAAC;QAED,OAAO,MAAM,EAAE,UAAU,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;IAClD,CAAC;IAED,SAAS,cAAc,CAAI,OAAmB,EAAE,MAAkB;QAC9D,IAAI,OAAO,CAAC,KAAK,KAAK,KAAK,EAAE,CAAC;YAC1B,OAAO,MAAM,EAAE,UAAU,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QAC/C,CAAC;QAED,OAAO,OAAO,CAAC,OAAO,EAAE,MAAM,CAAkB,CAAC;IACrD,CAAC;AACL,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,WAAW,CAAC,KAAc;IACtC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,IAAI,IAAI,IAAI,gBAAgB,IAAI,KAAK,CAAC;AACnF,CAAC"}
|
package/dist/ContainerScope.d.ts
CHANGED
|
@@ -1,6 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a scope for a container that controls service resolution and lifecycle.
|
|
3
|
+
* Scopes are used to:
|
|
4
|
+
* - Control service visibility in child containers
|
|
5
|
+
* - Manage service lifecycle
|
|
6
|
+
* - Implement custom resolution strategies
|
|
7
|
+
*/
|
|
1
8
|
export type ContainerScope = {
|
|
9
|
+
/**
|
|
10
|
+
* Unique identifier for the scope.
|
|
11
|
+
* Used for debugging and identification purposes.
|
|
12
|
+
*/
|
|
2
13
|
readonly name: string;
|
|
3
14
|
};
|
|
15
|
+
/**
|
|
16
|
+
* Creates a new immutable container scope with the specified name.
|
|
17
|
+
* The scope object is frozen to prevent modifications after creation.
|
|
18
|
+
* @param name - Unique identifier for the scope
|
|
19
|
+
* @returns A frozen scope object that cannot be modified
|
|
20
|
+
* @__NO_SIDE_EFFECTS__
|
|
21
|
+
*/
|
|
4
22
|
export declare function defineScope(name: string): Readonly<{
|
|
5
23
|
name: string;
|
|
6
24
|
}>;
|
package/dist/ContainerScope.js
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Creates a new immutable container scope with the specified name.
|
|
3
|
+
* The scope object is frozen to prevent modifications after creation.
|
|
4
|
+
* @param name - Unique identifier for the scope
|
|
5
|
+
* @returns A frozen scope object that cannot be modified
|
|
6
|
+
* @__NO_SIDE_EFFECTS__
|
|
7
|
+
*/
|
|
2
8
|
export function defineScope(name) {
|
|
3
9
|
return Object.freeze({
|
|
4
10
|
name,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ContainerScope.js","sourceRoot":"","sources":["../src/ContainerScope.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ContainerScope.js","sourceRoot":"","sources":["../src/ContainerScope.ts"],"names":[],"mappings":"AAeA;;;;;;GAMG;AACH,MAAM,UAAU,WAAW,CAAC,IAAY;IACpC,OAAO,MAAM,CAAC,MAAM,CAAC;QACjB,IAAI;KACP,CAAC,CAAC;AACP,CAAC"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { SomeObject } from '@nzyme/types/Object.js';
|
|
2
|
+
import type { ContainerScope } from './ContainerScope.js';
|
|
3
|
+
import type { Dependencies, ResolveDeps, Service } from './Service.js';
|
|
4
|
+
/**
|
|
5
|
+
* Represents a factory service that creates instances with additional parameters.
|
|
6
|
+
* Dependencies are resolved once (singleton), and the factory function can be called
|
|
7
|
+
* multiple times with different parameters.
|
|
8
|
+
*
|
|
9
|
+
* @template T - The type that the factory creates
|
|
10
|
+
* @template P - The parameter types that the factory accepts
|
|
11
|
+
* @template TDeps - The type of the factory's dependencies
|
|
12
|
+
*/
|
|
13
|
+
export type Factory<T = unknown, P extends unknown[] = unknown[], TDeps extends Dependencies = any> = Service<(...args: P) => T, TDeps>;
|
|
14
|
+
/**
|
|
15
|
+
* Configuration options for defining a factory.
|
|
16
|
+
*
|
|
17
|
+
* @template T - The type that the factory creates
|
|
18
|
+
* @template TExtend - Extended type (for inheritance)
|
|
19
|
+
* @template P - The parameter types that the factory accepts
|
|
20
|
+
* @template TDeps - The type of the factory's dependencies
|
|
21
|
+
*/
|
|
22
|
+
export interface FactoryOptions<T = unknown, TExtend extends T = T, P extends unknown[] = unknown[], TDeps extends Dependencies = Dependencies> {
|
|
23
|
+
/**
|
|
24
|
+
* Optional name of the factory for debugging and identification.
|
|
25
|
+
*/
|
|
26
|
+
readonly name?: string;
|
|
27
|
+
/**
|
|
28
|
+
* Optional dependencies required by this factory.
|
|
29
|
+
*/
|
|
30
|
+
readonly deps?: TDeps;
|
|
31
|
+
/**
|
|
32
|
+
* Optional scope that controls the factory's lifecycle.
|
|
33
|
+
*/
|
|
34
|
+
readonly scope?: ContainerScope;
|
|
35
|
+
/**
|
|
36
|
+
* Function that creates instances with resolved dependencies and additional parameters.
|
|
37
|
+
* @param deps - The resolved dependencies
|
|
38
|
+
* @param args - Additional parameters passed to the factory
|
|
39
|
+
* @returns The created instance
|
|
40
|
+
*/
|
|
41
|
+
readonly setup: FactorySetup<TDeps, TExtend, P>;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Setup function type for factories.
|
|
45
|
+
* Receives resolved dependencies and additional parameters.
|
|
46
|
+
*
|
|
47
|
+
* @template TDeps - The type of the factory's dependencies
|
|
48
|
+
* @template T - The type that the factory creates
|
|
49
|
+
* @template P - The parameter types that the factory accepts
|
|
50
|
+
*/
|
|
51
|
+
export interface FactorySetup<TDeps extends Dependencies, T, P extends unknown[]> {
|
|
52
|
+
(deps: ResolveDeps<TDeps>, ...args: P): T;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Creates a new factory service with the specified options.
|
|
56
|
+
* The factory resolves its dependencies once (singleton strategy),
|
|
57
|
+
* then returns a function that can be called with additional parameters.
|
|
58
|
+
*
|
|
59
|
+
* @template T - The type that the factory creates
|
|
60
|
+
* @template TExtend - Extended type (for inheritance)
|
|
61
|
+
* @template P - The parameter types that the factory accepts
|
|
62
|
+
* @template TDeps - The type of the factory's dependencies
|
|
63
|
+
* @param options - Configuration options for the factory
|
|
64
|
+
* @returns A new factory service
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* const UserFactory = defineFactory({
|
|
69
|
+
* name: 'UserFactory',
|
|
70
|
+
* deps: { db: DatabaseService },
|
|
71
|
+
* setup(deps, id: string, name: string) {
|
|
72
|
+
* return { id, name, db: deps.db };
|
|
73
|
+
* },
|
|
74
|
+
* });
|
|
75
|
+
*
|
|
76
|
+
* // Usage:
|
|
77
|
+
* const factory = container.resolve(UserFactory);
|
|
78
|
+
* const user1 = factory('1', 'Alice');
|
|
79
|
+
* const user2 = factory('2', 'Bob');
|
|
80
|
+
* ```
|
|
81
|
+
*
|
|
82
|
+
* @__NO_SIDE_EFFECTS__
|
|
83
|
+
*/
|
|
84
|
+
export declare function defineFactory<T, TExtend extends T = T, P extends unknown[] = unknown[], TDeps extends Dependencies = SomeObject>(options: FactoryOptions<T, TExtend, P, TDeps>): Factory<TExtend, P, TDeps>;
|