@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.
Files changed (106) hide show
  1. package/dist/Caller.d.ts +8 -0
  2. package/dist/Caller.js +8 -0
  3. package/dist/Caller.js.map +1 -1
  4. package/dist/Command.d.ts +44 -11
  5. package/dist/Command.js +29 -9
  6. package/dist/Command.js.map +1 -1
  7. package/dist/Command.test.d.ts +1 -0
  8. package/dist/Command.test.js +136 -0
  9. package/dist/Command.test.js.map +1 -0
  10. package/dist/Container.d.ts +119 -14
  11. package/dist/Container.js +28 -4
  12. package/dist/Container.js.map +1 -1
  13. package/dist/ContainerScope.d.ts +18 -0
  14. package/dist/ContainerScope.js +7 -1
  15. package/dist/ContainerScope.js.map +1 -1
  16. package/dist/Factory.d.ts +84 -0
  17. package/dist/Factory.js +42 -0
  18. package/dist/Factory.js.map +1 -0
  19. package/dist/Fetch.d.ts +3 -0
  20. package/dist/Fetch.js +4 -0
  21. package/dist/Fetch.js.map +1 -0
  22. package/dist/Injectable.d.ts +63 -28
  23. package/dist/Injectable.js +16 -8
  24. package/dist/Injectable.js.map +1 -1
  25. package/dist/Interface.d.ts +51 -34
  26. package/dist/Interface.js +39 -14
  27. package/dist/Interface.js.map +1 -1
  28. package/dist/Interface.test.d.ts +1 -0
  29. package/dist/Interface.test.js +37 -0
  30. package/dist/Interface.test.js.map +1 -0
  31. package/dist/Module.d.ts +26 -0
  32. package/dist/Module.js +10 -1
  33. package/dist/Module.js.map +1 -1
  34. package/dist/Service.d.ts +102 -30
  35. package/dist/Service.js +24 -19
  36. package/dist/Service.js.map +1 -1
  37. package/dist/Service.test.d.ts +1 -0
  38. package/dist/Service.test.js +378 -0
  39. package/dist/Service.test.js.map +1 -0
  40. package/dist/defineEnvVariable.d.ts +82 -0
  41. package/dist/defineEnvVariable.js +54 -0
  42. package/dist/defineEnvVariable.js.map +1 -0
  43. package/dist/helpers/EnvVariables.d.ts +10 -0
  44. package/dist/helpers/EnvVariables.js +9 -0
  45. package/dist/helpers/EnvVariables.js.map +1 -0
  46. package/dist/helpers/fallback.d.ts +1 -1
  47. package/dist/helpers/injectLazy.d.ts +7 -0
  48. package/dist/helpers/injectLazy.js +20 -0
  49. package/dist/helpers/injectLazy.js.map +1 -0
  50. package/dist/index.d.ts +13 -5
  51. package/dist/index.js +13 -5
  52. package/dist/index.js.map +1 -1
  53. package/dist/serviceResolve.d.ts +60 -13
  54. package/dist/serviceResolve.js +64 -22
  55. package/dist/serviceResolve.js.map +1 -1
  56. package/dist/services/ContextProvider.d.ts +8 -0
  57. package/dist/services/ContextProvider.js +37 -0
  58. package/dist/services/ContextProvider.js.map +1 -0
  59. package/dist/utils/getAllDeps.d.ts +5 -0
  60. package/dist/utils/getAllDeps.js +25 -0
  61. package/dist/utils/getAllDeps.js.map +1 -0
  62. package/dist/utils/isDependentOn.d.ts +6 -0
  63. package/dist/utils/isDependentOn.js +9 -0
  64. package/dist/utils/isDependentOn.js.map +1 -0
  65. package/dist/utils/resolveDeps.d.ts +2 -2
  66. package/dist/utils/resolveDeps.js.map +1 -1
  67. package/dist/utils/sortByDependency.d.ts +7 -0
  68. package/dist/utils/sortByDependency.js +46 -0
  69. package/dist/utils/sortByDependency.js.map +1 -0
  70. package/dist/utils/sortByDependency.test.d.ts +1 -0
  71. package/dist/utils/sortByDependency.test.js +100 -0
  72. package/dist/utils/sortByDependency.test.js.map +1 -0
  73. package/package.json +31 -38
  74. package/dist/Caller.cjs +0 -9
  75. package/dist/Caller.cjs.map +0 -1
  76. package/dist/Command.cjs +0 -20
  77. package/dist/Command.cjs.map +0 -1
  78. package/dist/Container.cjs +0 -69
  79. package/dist/Container.cjs.map +0 -1
  80. package/dist/ContainerScope.cjs +0 -10
  81. package/dist/ContainerScope.cjs.map +0 -1
  82. package/dist/Injectable.cjs +0 -29
  83. package/dist/Injectable.cjs.map +0 -1
  84. package/dist/Interface.cjs +0 -73
  85. package/dist/Interface.cjs.map +0 -1
  86. package/dist/Module.cjs +0 -8
  87. package/dist/Module.cjs.map +0 -1
  88. package/dist/Service.cjs +0 -39
  89. package/dist/Service.cjs.map +0 -1
  90. package/dist/helpers/callerName.cjs +0 -15
  91. package/dist/helpers/callerName.cjs.map +0 -1
  92. package/dist/helpers/constValue.cjs +0 -15
  93. package/dist/helpers/constValue.cjs.map +0 -1
  94. package/dist/helpers/envVariable.cjs +0 -23
  95. package/dist/helpers/envVariable.cjs.map +0 -1
  96. package/dist/helpers/envVariable.d.ts +0 -16
  97. package/dist/helpers/envVariable.js +0 -20
  98. package/dist/helpers/envVariable.js.map +0 -1
  99. package/dist/helpers/fallback.cjs +0 -23
  100. package/dist/helpers/fallback.cjs.map +0 -1
  101. package/dist/index.cjs +0 -17
  102. package/dist/index.cjs.map +0 -1
  103. package/dist/serviceResolve.cjs +0 -83
  104. package/dist/serviceResolve.cjs.map +0 -1
  105. package/dist/utils/resolveDeps.cjs +0 -18
  106. package/dist/utils/resolveDeps.cjs.map +0 -1
@@ -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"}
@@ -0,0 +1,3 @@
1
+ import type { Fetch as FetchShape } from '@nzyme/types/Fetch.js';
2
+ /** Default injectable request transport backed by the current runtime's global fetch. */
3
+ export declare const Fetch: import("./Injectable.js").Injectable<FetchShape>;
package/dist/Fetch.js ADDED
@@ -0,0 +1,4 @@
1
+ import { defineInjectable } from './Injectable.js';
2
+ /** Default injectable request transport backed by the current runtime's global fetch. */
3
+ export const Fetch = defineInjectable(() => globalThis.fetch);
4
+ //# sourceMappingURL=Fetch.js.map
@@ -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"}
@@ -1,49 +1,84 @@
1
1
  import type { Container } from './Container.js';
2
- export declare const INJECTABLE_SYMBOL: unique symbol;
2
+ import type { Dependencies } from './Service.js';
3
3
  /**
4
- * Infers the type of the injectable.
4
+ * Symbol for identifying injectables.
5
+ * @internal
5
6
  */
6
- export type Injected<T> = T extends Injectable ? ReturnType<T['resolve']> : never;
7
+ export declare const INJECTABLE_SYMBOL: unique symbol;
7
8
  /**
8
- * Options for the injectable.
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 type InjectableOptions<T> = {
11
- /**
12
- * Name of the injectable.
13
- */
14
- readonly name?: string;
12
+ export interface Injectable<T = unknown> extends InjectableOptions<T> {
15
13
  /**
16
- * Resolve function.
14
+ * Symbol that indicates that the type is an injectable.
15
+ * @internal
17
16
  */
18
- readonly resolve: (container: Container, caller?: Injectable) => T;
19
- };
17
+ readonly [INJECTABLE_SYMBOL]: symbol | true;
18
+ }
20
19
  /**
21
- * Injectable is a type that can be injected into other types.
20
+ * Options for configuring an injectable.
21
+ * @template T - The type that the injectable will resolve to
22
22
  */
23
- export type Injectable<T = unknown> = {
23
+ export interface InjectableOptions<T> {
24
24
  /**
25
- * Symbol that indicates that the type is an injectable.
26
- * @internal
25
+ * Optional name of the injectable for debugging and identification purposes.
27
26
  */
28
- readonly [INJECTABLE_SYMBOL]: true | symbol;
27
+ readonly name?: string;
29
28
  /**
30
- * Name of the injectable.
29
+ * Injectable dependencies for static dependency resolution.
31
30
  */
32
- readonly name?: string;
31
+ readonly deps?: Dependencies;
33
32
  /**
34
- * Resolve function.
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: (container: Container, caller?: Injectable) => T;
37
- };
38
+ readonly resolve: InjectableResolve<T>;
39
+ }
38
40
  /**
39
- * Define an injectable.
40
- * @param options - Options for the injectable.
41
- * @returns Injectable.
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
- * Check if the value is an injectable.
46
- * @param value - Value to check.
47
- * @returns Whether the value is an injectable.
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;
@@ -1,19 +1,27 @@
1
+ /**
2
+ * Symbol for identifying injectables.
3
+ * @internal
4
+ */
1
5
  export const INJECTABLE_SYMBOL = Symbol('injectable');
2
6
  /**
3
- * Define an injectable.
4
- * @param options - Options for the injectable.
5
- * @returns Injectable.
7
+ * @__NO_SIDE_EFFECTS__
6
8
  */
7
- export function defineInjectable(options) {
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
- ...options,
17
+ ...optionsOrResolve,
10
18
  [INJECTABLE_SYMBOL]: true,
11
19
  };
12
20
  }
13
21
  /**
14
- * Check if the value is an injectable.
15
- * @param value - Value to check.
16
- * @returns Whether the value is an injectable.
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) {
@@ -1 +1 @@
1
- {"version":3,"file":"Injectable.js","sourceRoot":"","sources":["../src/Injectable.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,iBAAiB,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC;AAwCtD;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAAI,OAA6B;IAC7D,OAAO;QACH,GAAG,OAAO;QACV,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"}
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"}
@@ -1,54 +1,71 @@
1
- import { type Injectable } from './Injectable.js';
1
+ import type { Injectable, InjectableResolve } from './Injectable.js';
2
2
  /**
3
- * Options for the interface.
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 interface InterfaceOptions<T = unknown> {
12
+ export type Interface<T = unknown> = Injectable<T> & {
6
13
  /**
7
- * Name of the interface.
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
- name?: string;
19
+ default(value: T): Injectable<T>;
10
20
  /**
11
- * Default value for the interface.
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?: Injectable<T>;
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
- * Make the interface optional.
21
- * Will resolve to `undefined` if the interface is not registered in the container.
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
- optional(): Injectable<T | undefined>;
35
+ default<D>(injectable: Injectable<D>): Injectable<D | T>;
24
36
  /**
25
- * Set the default value for the interface.
26
- * Will resolve to the default value if the interface is not registered in the container.
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
- default(value: T): Injectable<T>;
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
- * Set the default value for the interface.
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
- default<D>(value: D): Injectable<T | D>;
50
+ name?: string;
36
51
  /**
37
- * Set the default value for the interface.
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<D>(injectable: Injectable<D>): Injectable<T | D>;
42
- };
54
+ default?: Injectable<T> | InjectableResolve<T>;
55
+ }
43
56
  /**
44
- * Define an interface injectable.
45
- * @param options - Options for the interface.
46
- * @returns Interface injectable.
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
- * Check if the value is an interface injectable.
51
- * @param value - Value to check.
52
- * @returns True if the value is an interface, false otherwise.
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
- * Define an interface injectable.
5
- * @param options - Options for the interface.
6
- * @returns Interface injectable.
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
- return options.default.resolve(container, caller);
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
- * Check if the value is an interface injectable.
38
- * @param value - Value to check.
39
- * @returns True if the value is an interface, false otherwise.
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
- function optional() {
48
- return this.default(undefined);
49
- }
50
- function defaultValue(defaultValue) {
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(defaultValue)) {
58
- return defaultValue.resolve(container, caller);
74
+ if (isInjectable(fallbackValue)) {
75
+ return fallbackValue.resolve(container, caller);
59
76
  }
60
- return defaultValue;
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
@@ -1 +1 @@
1
- {"version":3,"file":"Interface.js","sourceRoot":"","sources":["../src/Interface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAmB,MAAM,iBAAiB,CAAC;AAEnF,MAAM,gBAAgB,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC;AA6C7C;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAAI,UAA+B,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,E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@ -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;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AC,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
- /*#__NO_SIDE_EFFECTS__*/
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
  }
@@ -1 +1 @@
1
- {"version":3,"file":"Module.js","sourceRoot":"","sources":["../src/Module.ts"],"names":[],"mappings":"AAMA,wBAAwB;AACxB,MAAM,UAAU,YAAY,CAAqC,MAAgC;IAC7F,OAAO,MAAM,CAAC;AAClB,CAAC"}
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"}