@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,82 @@
1
+ import type { Injectable } from './Injectable.js';
2
+ /** Resolves to TValue when required, or TValue | undefined when optional. */
3
+ export type EnvVariableValue<TRequired extends boolean = false, TValue = string> = TRequired extends true ? TValue : TValue | undefined;
4
+ /**
5
+ * Options for the env variable.
6
+ */
7
+ export interface EnvVariableOptionsRequired<TRequired extends boolean> {
8
+ /**
9
+ * Whether the environment variable is required.
10
+ */
11
+ required?: TRequired;
12
+ }
13
+ /**
14
+ * Options for the env variable.
15
+ */
16
+ export interface EnvVariableOptionsParse<TRequired extends boolean, TValue = string> extends EnvVariableOptionsRequired<TRequired> {
17
+ /**
18
+ * Function to parse the environment variable value.
19
+ */
20
+ parse?: (value: EnvVariableValue<TRequired>) => TValue;
21
+ }
22
+ /**
23
+ * Options for the env variable.
24
+ */
25
+ export interface EnvVariableOptionsDefault<TValue = string> extends EnvVariableOptionsParse<false, TValue> {
26
+ /**
27
+ * Default value of the environment variable.
28
+ */
29
+ default: (() => TValue) | TValue;
30
+ }
31
+ /** An injectable environment variable with typed access and optional parsing. */
32
+ export interface EnvVariable<TName extends string = string, TRequired extends boolean = boolean, TValue = unknown> extends EnvVariableOptionsParse<TRequired, TValue>, Injectable<TValue> {
33
+ /**
34
+ * Whether the environment variable is required.
35
+ */
36
+ required: TRequired;
37
+ /**
38
+ * Name of the environment variable.
39
+ */
40
+ name: TName;
41
+ /**
42
+ * Default value of the environment variable.
43
+ */
44
+ default?: () => TValue;
45
+ /**
46
+ * Get the value of the environment variable.
47
+ */
48
+ get(): TValue | undefined;
49
+ /**
50
+ * Assert that the environment variable is set.
51
+ */
52
+ assert(): TValue;
53
+ }
54
+ /**
55
+ * Define an environment variable injectable.
56
+ * @param name - Name of the environment variable.
57
+ * @param options - Options for the environment variable.
58
+ * @returns Environment variable injectable.
59
+ * @__NO_SIDE_EFFECTS__
60
+ */
61
+ export declare function defineEnvVariable<TName extends string = string, TRequired extends boolean = true>(name: TName, options?: EnvVariableOptionsRequired<TRequired> & {
62
+ default?: never;
63
+ parse?: never;
64
+ }): EnvVariable<TName, TRequired, string>;
65
+ /**
66
+ * Define an environment variable injectable.
67
+ * @param name - Name of the environment variable.
68
+ * @param options - Options for the environment variable.
69
+ * @returns Environment variable injectable.
70
+ * @__NO_SIDE_EFFECTS__
71
+ */
72
+ export declare function defineEnvVariable<TName extends string = string, TRequired extends boolean = true, TValue = string>(name: TName, options?: EnvVariableOptionsParse<TRequired, TValue> & {
73
+ default?: never;
74
+ }): EnvVariable<TName, TRequired, TValue>;
75
+ /**
76
+ * Define an environment variable injectable.
77
+ * @param name - Name of the environment variable.
78
+ * @param options - Options for the environment variable.
79
+ * @returns Environment variable injectable.
80
+ * @__NO_SIDE_EFFECTS__
81
+ */
82
+ export declare function defineEnvVariable<TName extends string = string, TValue = string>(name: TName, options?: EnvVariableOptionsDefault<TValue>): EnvVariable<TName, false, TValue>;
@@ -0,0 +1,54 @@
1
+ import { EnvVariables } from './helpers/EnvVariables.js';
2
+ import { defineInjectable } from './Injectable.js';
3
+ const DEPS = {
4
+ env: EnvVariables,
5
+ };
6
+ /** Implementation overload for defineEnvVariable. */
7
+ export function defineEnvVariable(name, options) {
8
+ const defaultValue = options?.default;
9
+ return defineInjectable({
10
+ name,
11
+ deps: DEPS,
12
+ required: options?.required ?? !defaultValue,
13
+ parse: options?.parse,
14
+ resolve,
15
+ default: defaultValue ? (typeof defaultValue === 'function' ? defaultValue : () => defaultValue) : undefined,
16
+ get: getValue,
17
+ assert: assertValue,
18
+ });
19
+ }
20
+ function getValue() {
21
+ const value = process.env[this.name];
22
+ if (value == null) {
23
+ return this.default?.();
24
+ }
25
+ if (this.parse) {
26
+ return this.parse(value);
27
+ }
28
+ return value;
29
+ }
30
+ function assertValue() {
31
+ const value = getValue.call(this);
32
+ if (value == null) {
33
+ throw new Error(`Environment variable ${this.name} is not set.`);
34
+ }
35
+ return value;
36
+ }
37
+ function resolve(container, caller) {
38
+ const name = this.name;
39
+ const env = container.resolve(EnvVariables);
40
+ const value = env[name];
41
+ if (!value) {
42
+ if (this.default) {
43
+ return this.default();
44
+ }
45
+ if (this.required) {
46
+ throw new Error(`Environment variable ${name} is not set. Caller: ${caller?.name}`);
47
+ }
48
+ }
49
+ if (this.parse) {
50
+ return this.parse(value);
51
+ }
52
+ return value;
53
+ }
54
+ //# sourceMappingURL=defineEnvVariable.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defineEnvVariable.js","sourceRoot":"","sources":["../src/defineEnvVariable.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAGnD,MAAM,IAAI,GAAG;IACT,GAAG,EAAE,YAAY;CACpB,CAAC;AAsGF,qDAAqD;AACrD,MAAM,UAAU,iBAAiB,CAC7B,IAAY,EACZ,OAAgH;IAEhH,MAAM,YAAY,GAAG,OAAO,EAAE,OAA+C,CAAC;I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,KAAK,EAAE,CAAC;QACT,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YACf,OAAO,IAAI,CAAC,OAAO,EAAE,CAAC;QAC1B,CAAC;QAED,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YAChB,MAAM,IAAI,KAAK,CAAC,wBAAwB,IAAI,wBAAwB,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;QACxF,CAAC;IACL,CAAC;IAED,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;QACb,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IAC7B,CAAC;IAED,OAAO,KAAK,CAAC;AACjB,CAAC"}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Represents the environment variables.
3
+ */
4
+ export type EnvVariables = {
5
+ [key: string]: string | undefined;
6
+ };
7
+ /**
8
+ * The environment variables interface.
9
+ */
10
+ export declare const EnvVariables: import("../Interface.js").Interface<EnvVariables>;
@@ -0,0 +1,9 @@
1
+ import { defineInterface } from '../Interface.js';
2
+ /**
3
+ * The environment variables interface.
4
+ */
5
+ export const EnvVariables = defineInterface({
6
+ name: 'EnvVariables',
7
+ default: () => process.env,
8
+ });
9
+ //# sourceMappingURL=EnvVariables.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EnvVariables.js","sourceRoot":"","sources":["../../src/helpers/EnvVariables.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AASlD;;GAEG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,eAAe,CAAe;IACtD,IAAI,EAAE,cAAc;IACpB,OAAO,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,GAAG;CAC7B,CAAC,CAAC"}
@@ -4,4 +4,4 @@ import type { Injectable } from '../Injectable.js';
4
4
  * @param value - Value to define.
5
5
  * @returns Fallback injectable.
6
6
  */
7
- export declare function fallback<T>(...injectables: Injectable<T | undefined | null>[]): Injectable<NonNullable<T> | undefined>;
7
+ export declare function fallback<T>(...injectables: Injectable<T | null | undefined>[]): Injectable<NonNullable<T> | undefined>;
@@ -0,0 +1,7 @@
1
+ import type { Injectable } from '../Injectable.js';
2
+ /**
3
+ * Define a lazy injectable.
4
+ * @param injectable - Injectable to define.
5
+ * @returns Lazy injectable.
6
+ */
7
+ export declare function injectLazy<T>(injectable: Injectable<T>): Injectable<() => T>;
@@ -0,0 +1,20 @@
1
+ import { defineInjectable } from '../Injectable.js';
2
+ /**
3
+ * Define a lazy injectable.
4
+ * @param injectable - Injectable to define.
5
+ * @returns Lazy injectable.
6
+ */
7
+ export function injectLazy(injectable) {
8
+ let value;
9
+ return defineInjectable({
10
+ resolve: container => {
11
+ return () => {
12
+ if (!value) {
13
+ value = container.resolve(injectable);
14
+ }
15
+ return value;
16
+ };
17
+ },
18
+ });
19
+ }
20
+ //# sourceMappingURL=injectLazy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"injectLazy.js","sourceRoot":"","sources":["../../src/helpers/injectLazy.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAEpD;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAI,UAAyB;IACnD,IAAI,KAAoB,CAAC;IAEzB,OAAO,gBAAgB,CAAC;QACpB,OAAO,EAAE,SAAS,CAAC,EAAE;YACjB,OAAO,GAAG,EAAE;gBACR,IAAI,CAAC,KAAK,EAAE,CAAC;oBACT,KAAK,GAAG,SAAS,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;gBAC1C,CAAC;gBAED,OAAO,KAAK,CAAC;YACjB,CAAC,CAAC;QACN,CAAC;KACJ,CAAC,CAAC;AACP,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,13 +1,21 @@
1
+ export * from './Caller.js';
1
2
  export * from './Command.js';
2
3
  export * from './Container.js';
3
4
  export * from './ContainerScope.js';
5
+ export * from './defineEnvVariable.js';
6
+ export * from './Factory.js';
7
+ export * from './Fetch.js';
8
+ export * from './helpers/callerName.js';
9
+ export * from './helpers/constValue.js';
10
+ export * from './helpers/EnvVariables.js';
11
+ export * from './helpers/fallback.js';
12
+ export * from './helpers/injectLazy.js';
4
13
  export * from './Injectable.js';
5
14
  export * from './Interface.js';
6
15
  export * from './Module.js';
7
16
  export * from './Service.js';
8
- export * from './Caller.js';
9
- export * from './helpers/envVariable.js';
10
- export * from './helpers/constValue.js';
11
- export * from './helpers/fallback.js';
12
- export * from './helpers/callerName.js';
17
+ export * from './services/ContextProvider.js';
18
+ export * from './utils/getAllDeps.js';
19
+ export * from './utils/isDependentOn.js';
13
20
  export * from './utils/resolveDeps.js';
21
+ export * from './utils/sortByDependency.js';
package/dist/index.js CHANGED
@@ -1,14 +1,22 @@
1
+ export * from './Caller.js';
1
2
  export * from './Command.js';
2
3
  export * from './Container.js';
3
4
  export * from './ContainerScope.js';
5
+ export * from './defineEnvVariable.js';
6
+ export * from './Factory.js';
7
+ export * from './Fetch.js';
8
+ export * from './helpers/callerName.js';
9
+ export * from './helpers/constValue.js';
10
+ export * from './helpers/EnvVariables.js';
11
+ export * from './helpers/fallback.js';
12
+ export * from './helpers/injectLazy.js';
4
13
  export * from './Injectable.js';
5
14
  export * from './Interface.js';
6
15
  export * from './Module.js';
7
16
  export * from './Service.js';
8
- export * from './Caller.js';
9
- export * from './helpers/envVariable.js';
10
- export * from './helpers/constValue.js';
11
- export * from './helpers/fallback.js';
12
- export * from './helpers/callerName.js';
17
+ export * from './services/ContextProvider.js';
18
+ export * from './utils/getAllDeps.js';
19
+ export * from './utils/isDependentOn.js';
13
20
  export * from './utils/resolveDeps.js';
21
+ export * from './utils/sortByDependency.js';
14
22
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,qBAAqB,CAAC;AACpC,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAE5B,cAAc,0BAA0B,CAAC;AACzC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC;AAExC,cAAc,wBAAwB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,qBAAqB,CAAC;AACpC,cAAc,wBAAwB,CAAC;AACvC,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,yBAAyB,CAAC;AACxC,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC;AACxC,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,+BAA+B,CAAC;AAC9C,cAAc,uBAAuB,CAAC;AACtC,cAAc,0BAA0B,CAAC;AACzC,cAAc,wBAAwB,CAAC;AACvC,cAAc,6BAA6B,CAAC"}
@@ -1,15 +1,62 @@
1
1
  import type { Container } from './Container.js';
2
2
  import type { Injectable } from './Injectable.js';
3
- import type { Service, ServiceOptions } from './Service.js';
4
- export type ServiceResolutionParams = {
5
- service: Service;
6
- options: ServiceOptions;
7
- container: Container;
8
- caller?: Injectable;
9
- };
10
- export type ServiceResolutionStrategy = (params: ServiceResolutionParams) => unknown;
11
- export type ServiceResolutionType = 'transient' | 'singleton' | 'lazy';
12
- export declare function getResolutionStrategy(strategy: ServiceResolutionType | ServiceResolutionStrategy): ServiceResolutionStrategy;
13
- export declare function singletonStrategy({ service, options, container, caller, }: ServiceResolutionParams): unknown;
14
- export declare function transientStrategy({ service, options, container, caller, }: ServiceResolutionParams): unknown;
15
- export declare function lazyStrategy(params: ServiceResolutionParams): {};
3
+ import type { Service } from './Service.js';
4
+ /**
5
+ * Function that determines how a service instance is created and cached.
6
+ *
7
+ * @param service - The service to resolve
8
+ * @param container - The container used for dependency resolution
9
+ * @param caller - Optional injectable that requested this resolution
10
+ * @returns The resolved service instance
11
+ */
12
+ export type ServiceResolutionStrategy = (service: Service, container: Container, caller?: Injectable) => unknown;
13
+ /**
14
+ * Built-in service resolution strategies:
15
+ * - 'transient': Creates a new instance on each resolution
16
+ * - 'singleton': Creates and caches a single instance
17
+ * - 'lazy': Creates a proxy that resolves to a singleton on first use
18
+ */
19
+ export type ServiceResolutionType = 'lazy' | 'singleton' | 'transient';
20
+ /**
21
+ * Creates a new service resolution strategy.
22
+ * This is a type-safe wrapper that ensures the function matches the ServiceResolutionStrategy interface.
23
+ *
24
+ * @param strategy - The function that implements the resolution strategy
25
+ * @returns The same function, properly typed as a ServiceResolutionStrategy
26
+ * @__NO_SIDE_EFFECTS__
27
+ */
28
+ export declare function defineResolutionStrategy(strategy: ServiceResolutionStrategy): ServiceResolutionStrategy;
29
+ /**
30
+ * Gets the resolution strategy function for a given strategy type or function.
31
+ *
32
+ * @param strategy - The strategy type or function
33
+ * @returns The resolution strategy function
34
+ * @throws Error if the strategy type is invalid
35
+ */
36
+ export declare function getResolutionStrategy(strategy: ServiceResolutionStrategy | ServiceResolutionType): ServiceResolutionStrategy;
37
+ /**
38
+ * Resolution strategy that creates and caches a single instance of the service.
39
+ * The instance is:
40
+ * - Created on first resolution
41
+ * - Cached in the container
42
+ * - Reused for subsequent resolutions
43
+ * - Scoped to the container's scope
44
+ */
45
+ export declare const singletonStrategy: ServiceResolutionStrategy;
46
+ /**
47
+ * Resolution strategy that creates a new instance of the service on each resolution.
48
+ * The instance is:
49
+ * - Created on each resolution
50
+ * - Not cached in the container
51
+ * - Scoped to the container's scope
52
+ */
53
+ export declare const transientStrategy: ServiceResolutionStrategy;
54
+ /**
55
+ * Resolution strategy that creates a proxy that resolves to a singleton on first use.
56
+ * The proxy:
57
+ * - Is created immediately
58
+ * - Defers instance creation until first use
59
+ * - Caches the instance after creation
60
+ * - Forwards all operations to the cached instance
61
+ */
62
+ export declare const lazyStrategy: ServiceResolutionStrategy;
@@ -1,21 +1,48 @@
1
- import { noop, createMemo } from '@nzyme/utils';
1
+ import { createMemo } from '@nzyme/utils/createMemo.js';
2
+ import { noop } from '@nzyme/utils/functions/noop.js';
2
3
  import { resolveDeps } from './utils/resolveDeps.js';
4
+ /**
5
+ * Creates a new service resolution strategy.
6
+ * This is a type-safe wrapper that ensures the function matches the ServiceResolutionStrategy interface.
7
+ *
8
+ * @param strategy - The function that implements the resolution strategy
9
+ * @returns The same function, properly typed as a ServiceResolutionStrategy
10
+ * @__NO_SIDE_EFFECTS__
11
+ */
12
+ export function defineResolutionStrategy(strategy) {
13
+ return strategy;
14
+ }
15
+ /**
16
+ * Gets the resolution strategy function for a given strategy type or function.
17
+ *
18
+ * @param strategy - The strategy type or function
19
+ * @returns The resolution strategy function
20
+ * @throws Error if the strategy type is invalid
21
+ */
3
22
  export function getResolutionStrategy(strategy) {
4
23
  if (typeof strategy === 'function') {
5
24
  return strategy;
6
25
  }
7
26
  switch (strategy) {
8
- case 'transient':
9
- return transientStrategy;
10
- case 'singleton':
11
- return singletonStrategy;
12
27
  case 'lazy':
13
28
  return lazyStrategy;
29
+ case 'singleton':
30
+ return singletonStrategy;
31
+ case 'transient':
32
+ return transientStrategy;
14
33
  default:
15
34
  throw new Error(`Invalid service resolution strategy: ${strategy}`);
16
35
  }
17
36
  }
18
- export function singletonStrategy({ service, options, container, caller, }) {
37
+ /**
38
+ * Resolution strategy that creates and caches a single instance of the service.
39
+ * The instance is:
40
+ * - Created on first resolution
41
+ * - Cached in the container
42
+ * - Reused for subsequent resolutions
43
+ * - Scoped to the container's scope
44
+ */
45
+ export const singletonStrategy = defineResolutionStrategy((service, container) => {
19
46
  let instance = container.get(service);
20
47
  if (instance) {
21
48
  return instance;
@@ -30,15 +57,22 @@ export function singletonStrategy({ service, options, container, caller, }) {
30
57
  return instance;
31
58
  }
32
59
  }
33
- const deps = resolveDeps(service.deps, container, caller);
34
- instance = options.setup(deps);
60
+ const deps = resolveDeps(service.deps, container, service);
61
+ instance = service.create(deps);
35
62
  container.set(service, instance);
36
- if (options.implements && !container.get(options.implements)) {
37
- container.set(options.implements, instance);
63
+ if (service.implements && !container.get(service.implements)) {
64
+ container.set(service.implements, instance);
38
65
  }
39
66
  return instance;
40
- }
41
- export function transientStrategy({ service, options, container, caller, }) {
67
+ });
68
+ /**
69
+ * Resolution strategy that creates a new instance of the service on each resolution.
70
+ * The instance is:
71
+ * - Created on each resolution
72
+ * - Not cached in the container
73
+ * - Scoped to the container's scope
74
+ */
75
+ export const transientStrategy = defineResolutionStrategy((service, container, caller) => {
42
76
  if (service.scope) {
43
77
  while (container.scope !== service.scope) {
44
78
  if (!container.parent) {
@@ -48,30 +82,38 @@ export function transientStrategy({ service, options, container, caller, }) {
48
82
  }
49
83
  }
50
84
  const deps = resolveDeps(service.deps, container, caller);
51
- return options.setup(deps);
52
- }
53
- export function lazyStrategy(params) {
85
+ return service.create(deps);
86
+ });
87
+ /**
88
+ * Resolution strategy that creates a proxy that resolves to a singleton on first use.
89
+ * The proxy:
90
+ * - Is created immediately
91
+ * - Defers instance creation until first use
92
+ * - Caches the instance after creation
93
+ * - Forwards all operations to the cached instance
94
+ */
95
+ export const lazyStrategy = defineResolutionStrategy((service, container, caller) => {
54
96
  // Short-circuit if already resolved
55
- const existing = params.container.get(params.service);
97
+ const existing = container.get(service);
56
98
  if (existing) {
57
99
  return existing;
58
100
  }
59
- const memo = createMemo(() => singletonStrategy(params));
101
+ const memo = createMemo(() => singletonStrategy(service, container, caller));
60
102
  const proxy = new Proxy(noop, {
61
- get: (target, prop) => {
103
+ get: (_target, prop) => {
62
104
  return memo()[prop];
63
105
  },
64
- set: (target, prop, value) => {
106
+ set: (_target, prop, value) => {
65
107
  memo()[prop] = value;
66
108
  return true;
67
109
  },
68
- has: (target, prop) => {
110
+ has: (_target, prop) => {
69
111
  return prop in memo();
70
112
  },
71
- apply: (target, thisArg, args) => {
113
+ apply: (_target, _thisArg, args) => {
72
114
  return memo()(...args);
73
115
  },
74
116
  });
75
117
  return proxy;
76
- }
118
+ });
77
119
  //# sourceMappingURL=serviceResolve.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"serviceResolve.js","sourceRoot":"","sources":["../src/serviceResolve.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAKhD,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAYrD,MAAM,UAAU,qBAAqB,CAAC,QAA2D;IAC7F,IAAI,OAAO,QAAQ,KAAK,UAAU,EAAE,CAAC;QACjC,OAAO,QAAQ,CAAC;IACpB,CAAC;IAED,QAAQ,QAAQ,EAAE,CAAC;QACf,KAAK,WAAW;YACZ,OAAO,iBAAiB,CAAC;QAC7B,KAAK,WAAW;YACZ,OAAO,iBAAiB,CAAC;QAC7B,KAAK,MAAM;YACP,OAAO,YAAY,CAAC;QACxB;YACI,MAAM,IAAI,KAAK,CAAC,wCAAwC,QAAkB,EAAE,CAAC,CAAC;IACtF,CAAC;AACL,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAC,EAC9B,OAAO,EACP,OAAO,EACP,SAAS,EACT,MAAM,GACgB;IACtB,IAAI,QAAQ,GAAG,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IACtC,IAAI,QAAQ,EAAE,CAAC;QACX,OAAO,QAAQ,CAAC;IACpB,CAAC;IAED,OAAO,SAAS,CAAC,KAAK,KAAK,OAAO,CAAC,KAAK,EAAE,CAAC;QACvC,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC;YACpB,MAAM,IAAI,K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,CAAC;QACX,OAAO,QAAQ,CAAC;IACpB,CAAC;IAED,MAAM,IAAI,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,iBAAiB,CAAC,OAAO,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC,CAAC;IAC7E,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC,IAAI,EAAE;QAC1B,GAAG,EAAE,CAAC,OAAO,EAAE,IAAI,EAAE,EAAE;YACnB,OAAQ,IAAI,EAAuC,CAAC,IAAI,CAAC,CAAC;QAC9D,CAAC;QACD,GAAG,EAAE,CAAC,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE;YACzB,IAAI,EAAuC,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC;YAC3D,OAAO,IAAI,CAAC;QAChB,CAAC;QACD,GAAG,EAAE,CAAC,OAAO,EAAE,IAAI,EAAE,EAAE;YACnB,OAAO,IAAI,IAAK,IAAI,EAAuC,CAAC;QAChE,CAAC;QACD,KAAK,EAAE,CAAC,OAAO,EAAE,QAAQ,EAAE,IAAe,EAAE,EAAE;YAC1C,OAAQ,IAAI,EAAsC,CAAC,GAAG,IAAI,CAAC,CAAC;QAChE,CAAC;KACJ,CAAC,CAAC;IAEH,OAAO,KAAK,CAAC;AACjB,CAAC,CAAC,CAAC"}
@@ -0,0 +1,8 @@
1
+ /** Service that provides a request-scoped key-value store for sharing data within a context. */
2
+ export declare const ContextProvider: import("../Service.js").Service<{
3
+ get: <T>(key: symbol) => T | undefined;
4
+ getOrCreate: <T>(key: symbol, factory: () => T) => T;
5
+ newContext: () => void;
6
+ remove: (key: symbol) => void;
7
+ set: <T>(key: symbol, value: T) => T;
8
+ }, import("@nzyme/types/Object.js").SomeObject>;
@@ -0,0 +1,37 @@
1
+ import { defineService } from '../Service.js';
2
+ /** Service that provides a request-scoped key-value store for sharing data within a context. */
3
+ export const ContextProvider = defineService({
4
+ name: 'ContextProvider',
5
+ setup() {
6
+ let ctx = new Map();
7
+ return {
8
+ get,
9
+ getOrCreate,
10
+ newContext,
11
+ remove,
12
+ set,
13
+ };
14
+ function get(key) {
15
+ return ctx.get(key);
16
+ }
17
+ function getOrCreate(key, factory) {
18
+ let value = ctx.get(key);
19
+ if (value === undefined) {
20
+ value = factory();
21
+ ctx.set(key, value);
22
+ }
23
+ return value;
24
+ }
25
+ function set(key, value) {
26
+ ctx.set(key, value);
27
+ return value;
28
+ }
29
+ function remove(key) {
30
+ ctx.delete(key);
31
+ }
32
+ function newContext() {
33
+ ctx = new Map();
34
+ }
35
+ },
36
+ });
37
+ //# sourceMappingURL=ContextProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ContextProvider.js","sourceRoot":"","sources":["../../src/services/ContextProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAE9C,gGAAgG;AAChG,MAAM,CAAC,MAAM,eAAe,GAAG,aAAa,CAAC;IACzC,IAAI,EAAE,iBAAiB;IACvB,KAAK;QACD,IAAI,GAAG,GAAG,IAAI,GAAG,EAAmB,CAAC;QAErC,OAAO;YACH,GAAG;YACH,WAAW;YACX,UAAU;YACV,MAAM;YACN,GAAG;SACN,CAAC;QAEF,SAAS,GAAG,CAAI,GAAW;YACvB,OAAO,GAAG,CAAC,GAAG,CAAC,GAAG,CAAkB,CAAC;QACzC,CAAC;QAED,SAAS,WAAW,CAAI,GAAW,EAAE,OAAgB;YACjD,IAAI,KAAK,GAAG,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;YACzB,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;gBACtB,KAAK,GAAG,OAAO,EAAE,CAAC;gBAClB,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;YACxB,CAAC;YAED,OAAO,KAAU,CAAC;QACtB,CAAC;QAED,SAAS,GAAG,CAAI,GAAW,EAAE,KAAQ;YACjC,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;YACpB,OAAO,KAAK,CAAC;QACjB,CAAC;QAED,SAAS,MAAM,CAAC,GAAW;YACvB,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACpB,CAAC;QAED,SAAS,UAAU;YACf,GAAG,GAAG,IAAI,GAAG,EAAE,CAAC;QACpB,CAAC;IACL,CAAC;CACJ,CAAC,CAAC"}
@@ -0,0 +1,5 @@
1
+ import type { Injectable } from '../Injectable.js';
2
+ /**
3
+ * Returns a set of all direct and nested dependencies of a service.
4
+ */
5
+ export declare function getAllDeps(injectable: Injectable): ReadonlySet<Injectable>;
@@ -0,0 +1,25 @@
1
+ const depsCacheSymbol = Symbol('ioc:deps');
2
+ /**
3
+ * Returns a set of all direct and nested dependencies of a service.
4
+ */
5
+ export function getAllDeps(injectable) {
6
+ let deps = injectable[depsCacheSymbol];
7
+ if (deps) {
8
+ return deps;
9
+ }
10
+ deps = new Set();
11
+ injectable[depsCacheSymbol] = deps;
12
+ if (!injectable.deps) {
13
+ return deps;
14
+ }
15
+ for (const dep of Object.values(injectable.deps)) {
16
+ deps.add(dep);
17
+ const nestedDeps = getAllDeps(dep);
18
+ for (const nestedDep of nestedDeps) {
19
+ deps.add(nestedDep);
20
+ }
21
+ }
22
+ deps = Object.freeze(deps);
23
+ return deps;
24
+ }
25
+ //# sourceMappingURL=getAllDeps.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getAllDeps.js","sourceRoot":"","sources":["../../src/utils/getAllDeps.ts"],"names":[],"mappings":"AAEA,MAAM,eAAe,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC;AAM3C;;GAEG;AACH,MAAM,UAAU,UAAU,CAAC,UAAsB;IAC7C,IAAI,IAAI,GAAI,UAAiC,CAAC,eAAe,CAAC,CAAC;IAE/D,IAAI,IAAI,EAAE,CAAC;QACP,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,IAAI,GAAG,IAAI,GAAG,EAAc,CAAC;IAC5B,UAAiC,CAAC,eAAe,CAAC,GAAG,IAAI,CAAC;IAE3D,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC;QACnB,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;QAC/C,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACd,MAAM,UAAU,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC;QACnC,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE,CAAC;YACjC,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QACxB,CAAC;IACL,CAAC;IAED,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC3B,OAAO,IAAI,CAAC;AAChB,CAAC"}
@@ -0,0 +1,6 @@
1
+ import type { Injectable } from '../Injectable.js';
2
+ import type { Service } from '../Service.js';
3
+ /**
4
+ * Checks if a service is dependent on an injectable.
5
+ */
6
+ export declare function isDependentOn(service: Service, injectable: Injectable): boolean;
@@ -0,0 +1,9 @@
1
+ import { getAllDeps } from './getAllDeps.js';
2
+ /**
3
+ * Checks if a service is dependent on an injectable.
4
+ */
5
+ export function isDependentOn(service, injectable) {
6
+ const deps = getAllDeps(service);
7
+ return deps.has(injectable);
8
+ }
9
+ //# sourceMappingURL=isDependentOn.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"isDependentOn.js","sourceRoot":"","sources":["../../src/utils/isDependentOn.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C;;GAEG;AACH,MAAM,UAAU,aAAa,CAAC,OAAgB,EAAE,UAAsB;IAClE,MAAM,IAAI,GAAG,UAAU,CAAC,OAAO,CAAC,CAAC;IACjC,OAAO,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;AAChC,CAAC"}
@@ -1,6 +1,6 @@
1
1
  import type { Container } from '../Container.js';
2
2
  import type { Injectable } from '../Injectable.js';
3
- import type { ServiceDependencies } from '../Service.js';
3
+ import type { Dependencies, ResolveDeps } from '../Service.js';
4
4
  /**
5
5
  * Resolves the dependencies of a service.
6
6
  * @param deps - The dependencies to resolve.
@@ -8,4 +8,4 @@ import type { ServiceDependencies } from '../Service.js';
8
8
  * @param caller - The caller of the service.
9
9
  * @returns The resolved dependencies.
10
10
  */
11
- export declare function resolveDeps(deps: ServiceDependencies, container: Container, caller?: Injectable): Record<string, unknown>;
11
+ export declare function resolveDeps<TDeps extends Dependencies>(deps: TDeps, container: Container, caller?: Injectable): ResolveDeps<TDeps>;
@@ -1 +1 @@
1
- {"version":3,"file":"resolveDeps.js","sourceRoot":"","sources":["../../src/utils/resolveDeps.ts"],"names":[],"mappings":"AAIA;;;;;;GAMG;AACH,MAAM,UAAU,WAAW,CAAC,IAAyB,EAAE,SAAoB,EAAE,MAAmB;IAC5F,MAAM,QAAQ,GAA4B,EAAE,CAAC;IAE7C,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;QAC9C,QAAQ,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;IACrD,CAAC;IAED,OAAO,QAAQ,CAAC;AACpB,CAAC"}
1
+ {"version":3,"file":"resolveDeps.js","sourceRoot":"","sources":["../../src/utils/resolveDeps.ts"],"names":[],"mappings":"AAIA;;;;;;GAMG;AACH,MAAM,UAAU,WAAW,CACvB,IAAW,EACX,SAAoB,EACpB,MAAmB;IAEnB,MAAM,QAAQ,GAA4B,EAAE,CAAC;IAE7C,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;QAC9C,QAAQ,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;IACrD,CAAC;IAED,OAAO,QAA8B,CAAC;AAC1C,CAAC"}
@@ -0,0 +1,7 @@
1
+ import type { Service } from '../Service.js';
2
+ /**
3
+ * Sorts an array of services by dependency using topological sort.
4
+ * Services with no dependencies come first, followed by their dependents.
5
+ * @__NO_SIDE_EFFECTS__
6
+ */
7
+ export declare function sortByDependency<S extends Service>(services: S[]): S[];