@nzyme/ioc 0.14.0 → 0.15.1
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/Factory.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { defineService } from './Service.js';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a new factory service with the specified options.
|
|
4
|
+
* The factory resolves its dependencies once (singleton strategy),
|
|
5
|
+
* then returns a function that can be called with additional parameters.
|
|
6
|
+
*
|
|
7
|
+
* @template T - The type that the factory creates
|
|
8
|
+
* @template TExtend - Extended type (for inheritance)
|
|
9
|
+
* @template P - The parameter types that the factory accepts
|
|
10
|
+
* @template TDeps - The type of the factory's dependencies
|
|
11
|
+
* @param options - Configuration options for the factory
|
|
12
|
+
* @returns A new factory service
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const UserFactory = defineFactory({
|
|
17
|
+
* name: 'UserFactory',
|
|
18
|
+
* deps: { db: DatabaseService },
|
|
19
|
+
* setup(deps, id: string, name: string) {
|
|
20
|
+
* return { id, name, db: deps.db };
|
|
21
|
+
* },
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* // Usage:
|
|
25
|
+
* const factory = container.resolve(UserFactory);
|
|
26
|
+
* const user1 = factory('1', 'Alice');
|
|
27
|
+
* const user2 = factory('2', 'Bob');
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @__NO_SIDE_EFFECTS__
|
|
31
|
+
*/
|
|
32
|
+
export function defineFactory(options) {
|
|
33
|
+
return defineService({
|
|
34
|
+
name: options.name,
|
|
35
|
+
deps: options.deps,
|
|
36
|
+
scope: options.scope,
|
|
37
|
+
setup: deps => {
|
|
38
|
+
return (...args) => options.setup(deps, ...args);
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=Factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Factory.js","sourceRoot":"","sources":["../src/Factory.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAoE7C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,MAAM,UAAU,aAAa,CAK3B,OAA6C;IAC3C,OAAO,aAAa,CAAoD;QACpE,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,KAAK,EAAE,OAAO,CAAC,KAAK;QACpB,KAAK,EAAE,IAAI,CAAC,EAAE;YACV,OAAO,CAAC,GAAG,IAAO,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,CAAC;QACxD,CAAC;KACJ,CAAC,CAAC;AACP,CAAC"}
|
package/dist/Fetch.d.ts
ADDED
package/dist/Fetch.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Fetch.js","sourceRoot":"","sources":["../src/Fetch.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAEnD,yFAAyF;AACzF,MAAM,CAAC,MAAM,KAAK,GAAG,gBAAgB,CAAa,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC"}
|
package/dist/Injectable.d.ts
CHANGED
|
@@ -1,49 +1,84 @@
|
|
|
1
1
|
import type { Container } from './Container.js';
|
|
2
|
-
|
|
2
|
+
import type { Dependencies } from './Service.js';
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Symbol for identifying injectables.
|
|
5
|
+
* @internal
|
|
5
6
|
*/
|
|
6
|
-
export
|
|
7
|
+
export declare const INJECTABLE_SYMBOL: unique symbol;
|
|
7
8
|
/**
|
|
8
|
-
*
|
|
9
|
+
* Represents a type that can be injected into other types through the dependency injection container.
|
|
10
|
+
* @template T - The type that this injectable will resolve to
|
|
9
11
|
*/
|
|
10
|
-
export
|
|
11
|
-
/**
|
|
12
|
-
* Name of the injectable.
|
|
13
|
-
*/
|
|
14
|
-
readonly name?: string;
|
|
12
|
+
export interface Injectable<T = unknown> extends InjectableOptions<T> {
|
|
15
13
|
/**
|
|
16
|
-
*
|
|
14
|
+
* Symbol that indicates that the type is an injectable.
|
|
15
|
+
* @internal
|
|
17
16
|
*/
|
|
18
|
-
readonly
|
|
19
|
-
}
|
|
17
|
+
readonly [INJECTABLE_SYMBOL]: symbol | true;
|
|
18
|
+
}
|
|
20
19
|
/**
|
|
21
|
-
*
|
|
20
|
+
* Options for configuring an injectable.
|
|
21
|
+
* @template T - The type that the injectable will resolve to
|
|
22
22
|
*/
|
|
23
|
-
export
|
|
23
|
+
export interface InjectableOptions<T> {
|
|
24
24
|
/**
|
|
25
|
-
*
|
|
26
|
-
* @internal
|
|
25
|
+
* Optional name of the injectable for debugging and identification purposes.
|
|
27
26
|
*/
|
|
28
|
-
readonly
|
|
27
|
+
readonly name?: string;
|
|
29
28
|
/**
|
|
30
|
-
*
|
|
29
|
+
* Injectable dependencies for static dependency resolution.
|
|
31
30
|
*/
|
|
32
|
-
readonly
|
|
31
|
+
readonly deps?: Dependencies;
|
|
33
32
|
/**
|
|
34
|
-
*
|
|
33
|
+
* Function that resolves the injectable to its concrete instance.
|
|
34
|
+
* @param container - The container used for dependency resolution
|
|
35
|
+
* @param caller - Optional injectable that requested this resolution
|
|
36
|
+
* @returns The resolved instance of type T
|
|
35
37
|
*/
|
|
36
|
-
readonly resolve:
|
|
37
|
-
}
|
|
38
|
+
readonly resolve: InjectableResolve<T>;
|
|
39
|
+
}
|
|
38
40
|
/**
|
|
39
|
-
*
|
|
40
|
-
|
|
41
|
-
|
|
41
|
+
* Represents a function that resolves an injectable to its concrete instance.
|
|
42
|
+
*/
|
|
43
|
+
export interface InjectableResolve<T = unknown> {
|
|
44
|
+
(container: Container, caller?: Injectable): T;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Infers the type of the injectable by extracting the return type of its resolve function.
|
|
48
|
+
* @template T - The type of the injectable
|
|
49
|
+
*/
|
|
50
|
+
export type Resolved<T> = T extends Injectable ? ReturnType<T['resolve']> : never;
|
|
51
|
+
/**
|
|
52
|
+
* Creates a new injectable with the specified options.
|
|
53
|
+
* @template T - The type that the injectable will resolve to
|
|
54
|
+
* @param options - Configuration options for the injectable
|
|
55
|
+
* @returns A new injectable instance
|
|
42
56
|
*/
|
|
43
57
|
export declare function defineInjectable<T>(options: InjectableOptions<T>): Injectable<T>;
|
|
44
58
|
/**
|
|
45
|
-
*
|
|
46
|
-
* @
|
|
47
|
-
* @
|
|
59
|
+
* Creates a new injectable with the specified options.
|
|
60
|
+
* @template T - The type that the injectable will resolve to
|
|
61
|
+
* @param options - Configuration options for the injectable
|
|
62
|
+
* @returns A new injectable instance
|
|
63
|
+
*/
|
|
64
|
+
export declare function defineInjectable<T, TOpts extends InjectableOptions<T>>(options: TOpts): Injectable<T> & TOpts;
|
|
65
|
+
/**
|
|
66
|
+
* Creates a new injectable with the specified resolve function.
|
|
67
|
+
* @template T - The type that the injectable will resolve to
|
|
68
|
+
* @param resolve - Function that resolves the injectable to its concrete instance
|
|
69
|
+
* @returns A new injectable instance
|
|
70
|
+
*/
|
|
71
|
+
export declare function defineInjectable<T>(resolve: InjectableResolve<T>): Injectable<T>;
|
|
72
|
+
/**
|
|
73
|
+
* Creates a new injectable with the specified options.
|
|
74
|
+
* @template T - The type that the injectable will resolve to
|
|
75
|
+
* @param options - Configuration options for the injectable
|
|
76
|
+
* @returns A new injectable instance
|
|
77
|
+
*/
|
|
78
|
+
export declare function defineInjectable<T extends Injectable>(options: InjectableOptions<unknown> & Omit<T, typeof INJECTABLE_SYMBOL>): T;
|
|
79
|
+
/**
|
|
80
|
+
* Type guard to check if a value is an injectable.
|
|
81
|
+
* @param value - The value to check
|
|
82
|
+
* @returns True if the value is an injectable, false otherwise
|
|
48
83
|
*/
|
|
49
84
|
export declare function isInjectable(value: unknown): value is Injectable;
|
package/dist/Injectable.js
CHANGED
|
@@ -1,19 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Symbol for identifying injectables.
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
1
5
|
export const INJECTABLE_SYMBOL = Symbol('injectable');
|
|
2
6
|
/**
|
|
3
|
-
*
|
|
4
|
-
* @param options - Options for the injectable.
|
|
5
|
-
* @returns Injectable.
|
|
7
|
+
* @__NO_SIDE_EFFECTS__
|
|
6
8
|
*/
|
|
7
|
-
export function defineInjectable(
|
|
9
|
+
export function defineInjectable(optionsOrResolve) {
|
|
10
|
+
if (typeof optionsOrResolve === 'function') {
|
|
11
|
+
return {
|
|
12
|
+
resolve: optionsOrResolve,
|
|
13
|
+
[INJECTABLE_SYMBOL]: true,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
8
16
|
return {
|
|
9
|
-
...
|
|
17
|
+
...optionsOrResolve,
|
|
10
18
|
[INJECTABLE_SYMBOL]: true,
|
|
11
19
|
};
|
|
12
20
|
}
|
|
13
21
|
/**
|
|
14
|
-
*
|
|
15
|
-
* @param value -
|
|
16
|
-
* @returns
|
|
22
|
+
* Type guard to check if a value is an injectable.
|
|
23
|
+
* @param value - The value to check
|
|
24
|
+
* @returns True if the value is an injectable, false otherwise
|
|
17
25
|
*/
|
|
18
26
|
export function isInjectable(value) {
|
|
19
27
|
if (value == null) {
|
package/dist/Injectable.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Injectable.js","sourceRoot":"","sources":["../src/Injectable.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Injectable.js","sourceRoot":"","sources":["../src/Injectable.ts"],"names":[],"mappings":"AAGA;;;GAGG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC;AA+EtD;;GAEG;AACH,MAAM,UAAU,gBAAgB,CAAC,gBAAgE;IAC7F,IAAI,OAAO,gBAAgB,KAAK,UAAU,EAAE,CAAC;QACzC,OAAO;YACH,OAAO,EAAE,gBAAgB;YACzB,CAAC,iBAAiB,CAAC,EAAE,IAAI;SAC5B,CAAC;IACN,CAAC;IAED,OAAO;QACH,GAAG,gBAAgB;QACnB,CAAC,iBAAiB,CAAC,EAAE,IAAI;KAC5B,CAAC;AACN,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAAC,KAAc;IACvC,IAAI,KAAK,IAAI,IAAI,EAAE,CAAC;QAChB,OAAO,KAAK,CAAC;IACjB,CAAC;IAED,OAAQ,KAAoB,CAAC,iBAAiB,CAAC,KAAK,SAAS,CAAC;AAClE,CAAC"}
|
package/dist/Interface.d.ts
CHANGED
|
@@ -1,54 +1,71 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { Injectable, InjectableResolve } from './Injectable.js';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Represents an interface that can be injected into services.
|
|
4
|
+
* Interfaces provide a way to:
|
|
5
|
+
* - Define abstract dependencies
|
|
6
|
+
* - Support optional dependencies
|
|
7
|
+
* - Provide default implementations
|
|
8
|
+
* - Enable hierarchical resolution through container parents
|
|
9
|
+
*
|
|
10
|
+
* @template T - The type that the interface will resolve to
|
|
4
11
|
*/
|
|
5
|
-
export
|
|
12
|
+
export type Interface<T = unknown> = Injectable<T> & {
|
|
6
13
|
/**
|
|
7
|
-
*
|
|
14
|
+
* Sets a default value for the interface.
|
|
15
|
+
* The default value will be used when the interface is not found in the container.
|
|
16
|
+
* @param value - The default value to use
|
|
17
|
+
* @returns A new injectable that resolves to T
|
|
8
18
|
*/
|
|
9
|
-
|
|
19
|
+
default(value: T): Injectable<T>;
|
|
10
20
|
/**
|
|
11
|
-
*
|
|
21
|
+
* Sets a default value of a different type for the interface.
|
|
22
|
+
* The default value will be used when the interface is not found in the container.
|
|
23
|
+
* @template D - The type of the default value
|
|
24
|
+
* @param value - The default value to use
|
|
25
|
+
* @returns A new injectable that resolves to T | D
|
|
12
26
|
*/
|
|
13
|
-
default
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Interface injectable.
|
|
17
|
-
*/
|
|
18
|
-
export type Interface<T = unknown> = Injectable<T> & {
|
|
27
|
+
default<D>(value: D): Injectable<D | T>;
|
|
19
28
|
/**
|
|
20
|
-
*
|
|
21
|
-
*
|
|
29
|
+
* Sets a default injectable for the interface.
|
|
30
|
+
* The default injectable will be resolved when the interface is not found in the container.
|
|
31
|
+
* @template D - The type that the default injectable resolves to
|
|
32
|
+
* @param injectable - The default injectable to use
|
|
33
|
+
* @returns A new injectable that resolves to T | D
|
|
22
34
|
*/
|
|
23
|
-
|
|
35
|
+
default<D>(injectable: Injectable<D>): Injectable<D | T>;
|
|
24
36
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* @param value - Default value.
|
|
37
|
+
* Makes the interface optional, allowing it to resolve to undefined if not found.
|
|
38
|
+
* @returns A new injectable that resolves to T | undefined
|
|
28
39
|
*/
|
|
29
|
-
|
|
40
|
+
optional(): Injectable<T | undefined>;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Configuration options for defining an interface.
|
|
44
|
+
* @template T - The type that the interface will resolve to
|
|
45
|
+
*/
|
|
46
|
+
export interface InterfaceOptions<T = unknown> {
|
|
30
47
|
/**
|
|
31
|
-
*
|
|
32
|
-
* Will resolve to the default value if the interface is not registered in the container.
|
|
33
|
-
* @param value - Default value.
|
|
48
|
+
* Optional name of the interface for debugging and identification.
|
|
34
49
|
*/
|
|
35
|
-
|
|
50
|
+
name?: string;
|
|
36
51
|
/**
|
|
37
|
-
*
|
|
38
|
-
* Will resolve to the default value if the interface is not registered in the container.
|
|
39
|
-
* @param value - Default value.
|
|
52
|
+
* Optional default injectable to use when the interface is not found in the container.
|
|
40
53
|
*/
|
|
41
|
-
default
|
|
42
|
-
}
|
|
54
|
+
default?: Injectable<T> | InjectableResolve<T>;
|
|
55
|
+
}
|
|
43
56
|
/**
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
57
|
+
* Creates a new interface with the specified options.
|
|
58
|
+
* Interfaces support hierarchical resolution through container parents and optional defaults.
|
|
59
|
+
*
|
|
60
|
+
* @template T - The type that the interface will resolve to
|
|
61
|
+
* @param options - Configuration options for the interface
|
|
62
|
+
* @returns A new interface instance
|
|
63
|
+
* @__NO_SIDE_EFFECTS__
|
|
47
64
|
*/
|
|
48
65
|
export declare function defineInterface<T>(options?: InterfaceOptions<T>): Interface<T>;
|
|
49
66
|
/**
|
|
50
|
-
*
|
|
51
|
-
* @param value -
|
|
52
|
-
* @returns True if the value is an
|
|
67
|
+
* Type guard to check if a value is an Interface instance.
|
|
68
|
+
* @param value - The value to check
|
|
69
|
+
* @returns True if the value is an Interface, false otherwise
|
|
53
70
|
*/
|
|
54
71
|
export declare function isInterface(value: unknown): value is Interface;
|
package/dist/Interface.js
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
import { INJECTABLE_SYMBOL, isInjectable } from './Injectable.js';
|
|
2
|
+
/**
|
|
3
|
+
* Symbol used to identify interface instances.
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
2
6
|
const INTERFACE_SYMBOL = Symbol('interface');
|
|
3
7
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
8
|
+
* Creates a new interface with the specified options.
|
|
9
|
+
* Interfaces support hierarchical resolution through container parents and optional defaults.
|
|
10
|
+
*
|
|
11
|
+
* @template T - The type that the interface will resolve to
|
|
12
|
+
* @param options - Configuration options for the interface
|
|
13
|
+
* @returns A new interface instance
|
|
14
|
+
* @__NO_SIDE_EFFECTS__
|
|
7
15
|
*/
|
|
8
16
|
export function defineInterface(options = {}) {
|
|
9
17
|
return {
|
|
@@ -25,7 +33,10 @@ export function defineInterface(options = {}) {
|
|
|
25
33
|
return value;
|
|
26
34
|
}
|
|
27
35
|
if (options.default) {
|
|
28
|
-
|
|
36
|
+
if (isInjectable(options.default)) {
|
|
37
|
+
return options.default.resolve(container, caller);
|
|
38
|
+
}
|
|
39
|
+
return options.default(container, caller);
|
|
29
40
|
}
|
|
30
41
|
throw new Error(`Interface ${this.name ?? ''} was not found`);
|
|
31
42
|
},
|
|
@@ -34,9 +45,9 @@ export function defineInterface(options = {}) {
|
|
|
34
45
|
};
|
|
35
46
|
}
|
|
36
47
|
/**
|
|
37
|
-
*
|
|
38
|
-
* @param value -
|
|
39
|
-
* @returns True if the value is an
|
|
48
|
+
* Type guard to check if a value is an Interface instance.
|
|
49
|
+
* @param value - The value to check
|
|
50
|
+
* @returns True if the value is an Interface, false otherwise
|
|
40
51
|
*/
|
|
41
52
|
export function isInterface(value) {
|
|
42
53
|
if (value == null) {
|
|
@@ -44,20 +55,26 @@ export function isInterface(value) {
|
|
|
44
55
|
}
|
|
45
56
|
return value[INJECTABLE_SYMBOL] === INTERFACE_SYMBOL;
|
|
46
57
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
58
|
+
/**
|
|
59
|
+
* Sets a default value for an interface.
|
|
60
|
+
* The default value will be used when the interface is not found in the container.
|
|
61
|
+
*
|
|
62
|
+
* @template T - The type that the interface resolves to
|
|
63
|
+
* @template D - The type of the default value
|
|
64
|
+
* @param defaultValue - The default value or injectable to use
|
|
65
|
+
* @returns A new injectable that resolves to T | D
|
|
66
|
+
*/
|
|
67
|
+
function defaultValue(fallbackValue) {
|
|
51
68
|
return {
|
|
52
69
|
[INJECTABLE_SYMBOL]: true,
|
|
53
70
|
name: this.name,
|
|
54
71
|
resolve: (container, caller) => {
|
|
55
72
|
const value = container.get(this);
|
|
56
73
|
if (value === undefined) {
|
|
57
|
-
if (isInjectable(
|
|
58
|
-
return
|
|
74
|
+
if (isInjectable(fallbackValue)) {
|
|
75
|
+
return fallbackValue.resolve(container, caller);
|
|
59
76
|
}
|
|
60
|
-
return
|
|
77
|
+
return fallbackValue;
|
|
61
78
|
}
|
|
62
79
|
if (isInjectable(value)) {
|
|
63
80
|
return value.resolve(container, caller);
|
|
@@ -66,4 +83,12 @@ function defaultValue(defaultValue) {
|
|
|
66
83
|
},
|
|
67
84
|
};
|
|
68
85
|
}
|
|
86
|
+
/**
|
|
87
|
+
* Makes an interface optional, allowing it to resolve to undefined if not found.
|
|
88
|
+
* @template T - The type that the interface resolves to
|
|
89
|
+
* @returns A new injectable that resolves to T | undefined
|
|
90
|
+
*/
|
|
91
|
+
function optional() {
|
|
92
|
+
return this.default(undefined);
|
|
93
|
+
}
|
|
69
94
|
//# sourceMappingURL=Interface.js.map
|
package/dist/Interface.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Interface.js","sourceRoot":"","sources":["../src/Interface.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Interface.js","sourceRoot":"","sources":["../src/Interface.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAElE;;;GAGG;AACH,MAAM,gBAAgB,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC;AA0D7C;;;;;;;;GAQG;AACH,MAAM,UAAU,eAAe,CAAI,OAAO,GAAwB,EAAE;IAChE,OAAO;QACH,CAAC,iBAAiB,CAAC,EAAE,gBAAgB;QACrC,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,OAAO,CAAC,SAAS,EAAE,MAAM;YACrB,OAAO,SAAS,EAAE,CAAC;gBACf,MAAM,KAAK,GAAG,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;gBAElC,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;oBACtB,IAAI,SAAS,CAAC,MAAM,EAAE,CAAC;wBACnB,SAAS,GAAG,SAAS,CAAC,MAAM,CAAC;wBAC7B,SAAS;oBACb,CAAC;oBAED,MAAM;gBACV,CAAC;gBAED,IAAI,YAAY,CAAC,KAAK,CAAC,EAAE,CAAC;oBACtB,OAAO,KAAK,CAAC,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;gBAC5C,CAAC;gBAED,OAAO,KAAK,CAAC;YACjB,CAAC;YAED,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;gBAClB,IAAI,YAAY,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;oBAChC,OAAO,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;gBACtD,CAAC;gBAED,OAAO,OAAO,CAAC,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;YAC9C,CAAC;YAED,MAAM,IAAI,KAAK,CAAC,aAAa,IAAI,CAAC,IAAI,IAAI,EAAE,gBAAgB,CAAC,CAAC;QAClE,CAAC;QACD,QAAQ;QACR,OAAO,EAAE,YAAY;KACxB,CAAC;AACN,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,WAAW,CAAC,KAAc;IACtC,IAAI,KAAK,IAAI,IAAI,EAAE,CAAC;QAChB,OAAO,KAAK,CAAC;IACjB,CAAC;IAED,OAAQ,KAAmB,CAAC,iBAAiB,CAAC,KAAK,gBAAgB,CAAC;AACxE,CAAC;AAED;;;;;;;;GAQG;AACH,SAAS,YAAY,CAA2B,aAAgC;IAC5E,OAAO;QACH,CAAC,iBAAiB,CAAC,EAAE,IAAI;QACzB,IAAI,EAAE,IAAI,CAAC,IAAI;QACf,OAAO,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,EAAE;YAC3B,MAAM,KAAK,GAAG,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YAClC,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;gBACtB,IAAI,YAAY,CAAC,aAAa,CAAC,EAAE,CAAC;oBAC9B,OAAO,aAAa,CAAC,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;gBACpD,CAAC;gBAED,OAAO,aAAa,CAAC;YACzB,CAAC;YAED,IAAI,YAAY,CAAC,KAAK,CAAC,EAAE,CAAC;gBACtB,OAAO,KAAK,CAAC,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;YAC5C,CAAC;YAED,OAAO,KAAK,CAAC;QACjB,CAAC;KACJ,CAAC;AACN,CAAC;AAED;;;;GAIG;AACH,SAAS,QAAQ;IACb,OAAO,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;AACnC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { expect, test } from 'bun:test';
|
|
2
|
+
import { createContainer } from './Container.js';
|
|
3
|
+
import { defineScope } from './ContainerScope.js';
|
|
4
|
+
import { defineInterface } from './Interface.js';
|
|
5
|
+
test('register and get interface', () => {
|
|
6
|
+
const container = createContainer();
|
|
7
|
+
const injectable = defineInterface();
|
|
8
|
+
container.set(injectable, 'test');
|
|
9
|
+
expect(container.get(injectable)).toBe('test');
|
|
10
|
+
});
|
|
11
|
+
test('register and get named interface', () => {
|
|
12
|
+
const container = createContainer();
|
|
13
|
+
const injectable = defineInterface({
|
|
14
|
+
name: 'test',
|
|
15
|
+
});
|
|
16
|
+
container.set(injectable, 'test');
|
|
17
|
+
expect(container.get(injectable)).toBe('test');
|
|
18
|
+
});
|
|
19
|
+
test('get non registered interface', () => {
|
|
20
|
+
const container = createContainer();
|
|
21
|
+
const injectable = defineInterface({
|
|
22
|
+
name: 'test',
|
|
23
|
+
});
|
|
24
|
+
expect(container.get(injectable)).toBe(undefined);
|
|
25
|
+
});
|
|
26
|
+
test('resolve interface from parent container', () => {
|
|
27
|
+
const parent = createContainer();
|
|
28
|
+
const childScope = defineScope('child');
|
|
29
|
+
const child = parent.createChild(childScope);
|
|
30
|
+
const injectable = defineInterface({
|
|
31
|
+
name: 'test',
|
|
32
|
+
});
|
|
33
|
+
parent.set(injectable, 'test');
|
|
34
|
+
expect(child.get(injectable)).toBe(undefined);
|
|
35
|
+
expect(child.resolve(injectable)).toBe('test');
|
|
36
|
+
});
|
|
37
|
+
//# sourceMappingURL=Interface.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Interface.test.js","sourceRoot":"","sources":["../src/Interface.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAExC,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACjD,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAEjD,IAAI,CAAC,4BAA4B,EAAE,GAAG,EAAE;IACpC,MAAM,SAAS,GAAG,eAAe,EAAE,CAAC;IACpC,MAAM,UAAU,GAAG,eAAe,EAAU,CAAC;IAE7C,SAAS,CAAC,GAAG,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;IAClC,MAAM,CAAC,SAAS,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AACnD,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,kCAAkC,EAAE,GAAG,EAAE;IAC1C,MAAM,SAAS,GAAG,eAAe,EAAE,CAAC;IACpC,MAAM,UAAU,GAAG,eAAe,CAAS;QACvC,IAAI,EAAE,MAAM;KACf,CAAC,CAAC;IAEH,SAAS,CAAC,GAAG,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;IAClC,MAAM,CAAC,SAAS,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AACnD,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,8BAA8B,EAAE,GAAG,EAAE;IACtC,MAAM,SAAS,GAAG,eAAe,EAAE,CAAC;IACpC,MAAM,UAAU,GAAG,eAAe,CAAS;QACvC,IAAI,EAAE,MAAM;KACf,CAAC,CAAC;IAEH,MAAM,CAAC,SAAS,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;AACtD,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,yCAAyC,EAAE,GAAG,EAAE;IACjD,MAAM,MAAM,GAAG,eAAe,EAAE,CAAC;IACjC,MAAM,UAAU,GAAG,WAAW,CAAC,OAAO,CAAC,CAAC;IACxC,MAAM,KAAK,GAAG,MAAM,CAAC,WAAW,CAAC,UAAU,CAAC,CAAC;IAE7C,MAAM,UAAU,GAAG,eAAe,CAAS;QACvC,IAAI,EAAE,MAAM;KACf,CAAC,CAAC;IAEH,MAAM,CAAC,GAAG,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;IAE/B,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAC9C,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AACnD,CAAC,CAAC,CAAC"}
|
package/dist/Module.d.ts
CHANGED
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
import type { Container } from './Container.js';
|
|
2
|
+
/**
|
|
3
|
+
* Represents a module that can be registered with a container.
|
|
4
|
+
* A module is a function that:
|
|
5
|
+
* - Receives a container instance for dependency injection
|
|
6
|
+
* - Can accept optional parameters during initialization
|
|
7
|
+
* - Can return a result that will be available after initialization
|
|
8
|
+
*
|
|
9
|
+
* @template TParams - Type of the parameters that the module accepts
|
|
10
|
+
* @template TResult - Type of the result that the module returns
|
|
11
|
+
*/
|
|
2
12
|
export type Module<TParams extends unknown[] = [], TResult = void> = {
|
|
13
|
+
/**
|
|
14
|
+
* Initializes the module with the container and optional parameters.
|
|
15
|
+
* @param container - The container instance for dependency injection
|
|
16
|
+
* @param params - Optional parameters for module initialization
|
|
17
|
+
* @returns The module's initialization result
|
|
18
|
+
*/
|
|
3
19
|
(container: Container, ...params: TParams): TResult;
|
|
4
20
|
};
|
|
21
|
+
/**
|
|
22
|
+
* Creates a new module from a function.
|
|
23
|
+
* This is a type-safe wrapper that ensures the function matches the Module interface.
|
|
24
|
+
*
|
|
25
|
+
* @template TParams - Type of the parameters that the module accepts
|
|
26
|
+
* @template TResult - Type of the result that the module returns
|
|
27
|
+
* @param module - The function to convert into a module
|
|
28
|
+
* @returns The same function, properly typed as a Module
|
|
29
|
+
* @__NO_SIDE_EFFECTS__
|
|
30
|
+
*/
|
|
5
31
|
export declare function defineModule<TParams extends unknown[], TResult>(module: Module<TParams, TResult>): Module<TParams, TResult>;
|
package/dist/Module.js
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Creates a new module from a function.
|
|
3
|
+
* This is a type-safe wrapper that ensures the function matches the Module interface.
|
|
4
|
+
*
|
|
5
|
+
* @template TParams - Type of the parameters that the module accepts
|
|
6
|
+
* @template TResult - Type of the result that the module returns
|
|
7
|
+
* @param module - The function to convert into a module
|
|
8
|
+
* @returns The same function, properly typed as a Module
|
|
9
|
+
* @__NO_SIDE_EFFECTS__
|
|
10
|
+
*/
|
|
2
11
|
export function defineModule(module) {
|
|
3
12
|
return module;
|
|
4
13
|
}
|
package/dist/Module.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Module.js","sourceRoot":"","sources":["../src/Module.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Module.js","sourceRoot":"","sources":["../src/Module.ts"],"names":[],"mappings":"AAsBA;;;;;;;;;GASG;AACH,MAAM,UAAU,YAAY,CAAqC,MAAgC;IAC7F,OAAO,MAAM,CAAC;AAClB,CAAC"}
|