awilixify 3.2.2 → 3.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -0
- package/dist/lib/decorators/decorator-state.types.d.ts +1 -1
- package/dist/lib/di/common.types.d.ts +0 -1
- package/dist/lib/di/contexts/container-context-base.d.ts +4 -0
- package/dist/lib/di/contexts/container-context-base.js +40 -19
- package/dist/lib/di/contexts/di-context-async.js +4 -23
- package/dist/lib/di/contexts/di-context-base.js +5 -5
- package/dist/lib/di/contexts/di-context.d.ts +5 -0
- package/dist/lib/di/contexts/di-context.js +54 -0
- package/dist/lib/di/contexts/module-graph-ensurer.d.ts +9 -0
- package/dist/lib/di/contexts/module-graph-ensurer.js +27 -0
- package/dist/lib/di/errors.d.ts +21 -0
- package/dist/lib/di/errors.js +43 -0
- package/dist/lib/di/lifecycle-hooks.d.ts +10 -0
- package/dist/lib/di/lifecycle-hooks.js +8 -0
- package/dist/lib/di/modules/configure-module.d.ts +21 -0
- package/dist/lib/di/modules/configure-module.js +11 -0
- package/dist/lib/di/modules/module-def.types.d.ts +1 -1
- package/dist/lib/di/processors/controller-processor.d.ts +4 -1
- package/dist/lib/di/processors/controller-processor.js +57 -52
- package/dist/lib/di/processors/handler-processor.d.ts +3 -1
- package/dist/lib/di/processors/handler-processor.js +5 -3
- package/dist/lib/di/processors/initializer-processor.d.ts +3 -1
- package/dist/lib/di/processors/initializer-processor.js +25 -21
- package/dist/lib/di/processors/interceptor-processor.d.ts +4 -1
- package/dist/lib/di/processors/interceptor-processor.js +22 -16
- package/dist/lib/di/processors/keyed-feature-registrar.d.ts +4 -1
- package/dist/lib/di/processors/keyed-feature-registrar.js +58 -61
- package/dist/lib/di/processors/lifecycle-processor.d.ts +9 -9
- package/dist/lib/di/processors/lifecycle-processor.js +67 -29
- package/dist/lib/di/providers/inquirer-resolver-factory.d.ts +10 -0
- package/dist/lib/di/providers/inquirer-resolver-factory.js +61 -0
- package/dist/lib/di/providers/provider-resolver.d.ts +5 -2
- package/dist/lib/di/providers/provider-resolver.js +43 -30
- package/dist/lib/di/providers/types/controller.types.d.ts +2 -1
- package/dist/lib/di/providers/types/handler.types.d.ts +0 -5
- package/dist/lib/di/providers/types/initializer.types.d.ts +2 -1
- package/dist/lib/di/providers/types/interceptor.types.d.ts +3 -2
- package/dist/lib/di/providers/types/middleware.types.d.ts +1 -4
- package/dist/lib/di/providers/types/provider.types.d.ts +16 -4
- package/dist/lib/di/providers/types/provider.types.js +1 -1
- package/dist/lib/di/type-guards.d.ts +7 -4
- package/dist/lib/di/type-guards.js +13 -6
- package/dist/lib/extension-api/index.d.ts +1 -0
- package/dist/lib/extension-api/index.js +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/package.json +19 -10
- package/dist/tsconfig.tsbuildinfo +0 -1
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
Transport-agnostic, type-safe, modular DI and CQRS framework on top of [Awilix](https://github.com/jeffijoe/awilix).
|
|
6
6
|
|
|
7
|
-
[](https://github.com/awilixify/awilixify/actions)
|
|
7
|
+
[](https://github.com/awilixify/awilixify/actions/workflows/ci.yml)
|
|
8
8
|
[](https://codecov.io/gh/awilixify/awilixify)
|
|
9
9
|
|
|
10
10
|
> [!TIP]
|
package/dist/index.d.ts
CHANGED
|
@@ -3,12 +3,13 @@ export type { DecoratorState, DecoratorToken, } from "./lib/decorators/decorator
|
|
|
3
3
|
export type { ModuleScope } from "./lib/di/contexts/container-context-base.js";
|
|
4
4
|
export * from "./lib/di/contexts/di-context.js";
|
|
5
5
|
export * from "./lib/di/contexts/di-context-async.js";
|
|
6
|
+
export { type ConfiguredModule, configureModule, } from "./lib/di/modules/configure-module.js";
|
|
6
7
|
export * from "./lib/di/modules/module.types.js";
|
|
7
8
|
export * from "./lib/di/modules/module-def.types.js";
|
|
8
9
|
export * from "./lib/di/modules/module-factories.js";
|
|
9
10
|
export * from "./lib/di/modules/module-overrides.js";
|
|
10
11
|
export * from "./lib/di/modules/module-ref.types.js";
|
|
11
|
-
export type { ConstructorController, Controller, InitializerContext, InterceptContext, Interceptor,
|
|
12
|
+
export type { AfterInit, BeforeShutdown, ConstructorController, Controller, InitializerContext, InquirerContext, InterceptContext, Interceptor, OnInit, OnShutdown, } from "./lib/di/providers/provider.types.js";
|
|
12
13
|
export { Initializer } from "./lib/di/providers/provider.types.js";
|
|
13
14
|
export { isResultLike } from "./lib/di/type-guards.js";
|
|
14
15
|
export * from "./lib/mediator/contract.types.js";
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export * from "./lib/decorators/decorator-state.js";
|
|
2
2
|
export * from "./lib/di/contexts/di-context.js";
|
|
3
3
|
export * from "./lib/di/contexts/di-context-async.js";
|
|
4
|
+
export { configureModule, } from "./lib/di/modules/configure-module.js";
|
|
4
5
|
export * from "./lib/di/modules/module.types.js";
|
|
5
6
|
export * from "./lib/di/modules/module-def.types.js";
|
|
6
7
|
export * from "./lib/di/modules/module-factories.js";
|
|
@@ -5,7 +5,7 @@ export type DecoratorState<TMethod, TRoot = null> = {
|
|
|
5
5
|
decoratorNames: Map<MethodName, string>;
|
|
6
6
|
};
|
|
7
7
|
export type DecoratorMethodState<TState> = TState extends DecoratorState<infer TMethod, any> ? TMethod : never;
|
|
8
|
-
|
|
8
|
+
type DecoratorRootState<TState> = TState extends DecoratorState<any, infer TRoot> ? TRoot : never;
|
|
9
9
|
export type StateInitializers<TState extends DecoratorState<any, any>> = {
|
|
10
10
|
method: () => DecoratorMethodState<TState>;
|
|
11
11
|
root: () => DecoratorRootState<TState>;
|
|
@@ -3,6 +3,7 @@ import { type DevtoolsProcessorRef } from "../../devtools/devtools.types.js";
|
|
|
3
3
|
import type { InternalModuleLike as M } from "../modules/runtime-module.types.js";
|
|
4
4
|
import { type LifecycleMethods, LifecycleProcessor } from "../processors/lifecycle-processor.js";
|
|
5
5
|
import { type OverrideOptions, OverridesProcessor } from "../processors/overrides-processor.js";
|
|
6
|
+
import { InquirerResolverFactory } from "../providers/inquirer-resolver-factory.js";
|
|
6
7
|
import { ProviderDependencySorter } from "../providers/provider-dependency-sorter.js";
|
|
7
8
|
import type { ProviderResolver } from "../providers/provider-resolver.js";
|
|
8
9
|
import { ModuleGraphEnsurer } from "./module-graph-ensurer.js";
|
|
@@ -31,6 +32,7 @@ export declare abstract class ContainerContextBase<TOptions extends ContainerCon
|
|
|
31
32
|
protected readonly devtoolsProcessorRef: DevtoolsProcessorRef;
|
|
32
33
|
protected readonly moduleTreeMap: WeakMap<M, ModuleScope<Awilix.AwilixContainer<{}>>>;
|
|
33
34
|
protected readonly options: TOptions;
|
|
35
|
+
protected readonly inquirerResolverFactory: InquirerResolverFactory;
|
|
34
36
|
protected readonly sorter: ProviderDependencySorter;
|
|
35
37
|
protected readonly moduleGraphEnsurer: ModuleGraphEnsurer;
|
|
36
38
|
protected readonly lifecycleProcessor: LifecycleProcessor;
|
|
@@ -41,6 +43,8 @@ export declare abstract class ContainerContextBase<TOptions extends ContainerCon
|
|
|
41
43
|
protected bootstrapModule(module: M): ModuleScope;
|
|
42
44
|
protected createContainer(module?: M): Awilix.AwilixContainer;
|
|
43
45
|
protected registerModuleWithScope(m: M, scope: Awilix.AwilixContainer, moduleChain: M[]): ModuleScope;
|
|
46
|
+
protected prepareModuleRegistration(moduleWithOverrides: M, scope: Awilix.AwilixContainer, imports: M[]): string | undefined;
|
|
47
|
+
protected finalizeModuleRegistration(m: M, moduleWithOverrides: M, scope: Awilix.AwilixContainer, importedModulesWithScope: RegisteredModuleScope[]): ModuleScope;
|
|
44
48
|
protected initializeGlobalModules(rootModule: M): void;
|
|
45
49
|
protected registerImportedFeatures(scope: Awilix.AwilixContainer, importedModulesWithScope: RegisteredModuleScope[]): void;
|
|
46
50
|
protected registerExportedProviders(scope: Awilix.AwilixContainer, importedModulesWithScope: RegisteredModuleScope[]): void;
|
|
@@ -3,6 +3,7 @@ import { AWILIXIFY_DEVTOOLS_PROCESSOR, } from "../../devtools/devtools.types.js"
|
|
|
3
3
|
import * as ERRORS from "../errors.js";
|
|
4
4
|
import { LifecycleProcessor, } from "../processors/lifecycle-processor.js";
|
|
5
5
|
import { OverridesProcessor, } from "../processors/overrides-processor.js";
|
|
6
|
+
import { InquirerResolverFactory } from "../providers/inquirer-resolver-factory.js";
|
|
6
7
|
import { ProviderDependencySorter } from "../providers/provider-dependency-sorter.js";
|
|
7
8
|
import * as GUARGS from "../type-guards.js";
|
|
8
9
|
import { ModuleGraphEnsurer } from "./module-graph-ensurer.js";
|
|
@@ -13,6 +14,7 @@ export class ContainerContextBase {
|
|
|
13
14
|
devtoolsProcessorRef = {};
|
|
14
15
|
moduleTreeMap = new WeakMap();
|
|
15
16
|
options;
|
|
17
|
+
inquirerResolverFactory = new InquirerResolverFactory();
|
|
16
18
|
sorter = new ProviderDependencySorter();
|
|
17
19
|
moduleGraphEnsurer = new ModuleGraphEnsurer();
|
|
18
20
|
lifecycleProcessor;
|
|
@@ -59,24 +61,7 @@ export class ContainerContextBase {
|
|
|
59
61
|
}
|
|
60
62
|
const imports = this.resolveImports(m);
|
|
61
63
|
const moduleWithOverrides = this.overridesProcessor.applyModuleOverrides(m);
|
|
62
|
-
const moduleId = this.
|
|
63
|
-
module: moduleWithOverrides,
|
|
64
|
-
scope,
|
|
65
|
-
importedModules: imports,
|
|
66
|
-
});
|
|
67
|
-
this.moduleGraphEnsurer.ensureImportedModulesUniqueness({
|
|
68
|
-
module: moduleWithOverrides,
|
|
69
|
-
resolvedImports: imports,
|
|
70
|
-
globalModules: this.globalModulesWithScope.map((el) => el.module),
|
|
71
|
-
});
|
|
72
|
-
this.moduleGraphEnsurer.ensureNoProviderNameConflicts({
|
|
73
|
-
module: moduleWithOverrides,
|
|
74
|
-
resolvedImports: imports,
|
|
75
|
-
globalModules: this.globalModulesWithScope.map((el) => el.module),
|
|
76
|
-
getExportedProviderKeys: (module) => this.getExportedProviderKeys(module),
|
|
77
|
-
});
|
|
78
|
-
this.ensureAdditionalNameConflicts(moduleWithOverrides, imports);
|
|
79
|
-
this.markModuleIfImportsUseForwardRef(moduleWithOverrides);
|
|
64
|
+
const moduleId = this.prepareModuleRegistration(moduleWithOverrides, scope, imports);
|
|
80
65
|
const isCircular = moduleChain.includes(m);
|
|
81
66
|
if (isCircular) {
|
|
82
67
|
this.moduleGraphEnsurer.ensureCircularDependencyHasForwardRef({
|
|
@@ -114,6 +99,41 @@ export class ContainerContextBase {
|
|
|
114
99
|
}),
|
|
115
100
|
});
|
|
116
101
|
}
|
|
102
|
+
return this.finalizeModuleRegistration(m, moduleWithOverrides, scope, importedModulesWithScope);
|
|
103
|
+
}
|
|
104
|
+
prepareModuleRegistration(moduleWithOverrides, scope, imports) {
|
|
105
|
+
if (Object.values(moduleWithOverrides.providers ?? {}).some((provider) => GUARGS.hasUseClass(provider) && provider.injectInquirer === true)) {
|
|
106
|
+
this.inquirerResolverFactory.enableTracking();
|
|
107
|
+
}
|
|
108
|
+
const moduleId = this.registerModuleInGraph({
|
|
109
|
+
module: moduleWithOverrides,
|
|
110
|
+
scope,
|
|
111
|
+
importedModules: imports,
|
|
112
|
+
});
|
|
113
|
+
this.moduleGraphEnsurer.ensureImportedModulesUniqueness({
|
|
114
|
+
module: moduleWithOverrides,
|
|
115
|
+
resolvedImports: imports,
|
|
116
|
+
globalModules: this.globalModulesWithScope.map((el) => el.module),
|
|
117
|
+
});
|
|
118
|
+
this.moduleGraphEnsurer.ensureNoProviderNameConflicts({
|
|
119
|
+
module: moduleWithOverrides,
|
|
120
|
+
resolvedImports: imports,
|
|
121
|
+
globalModules: this.globalModulesWithScope.map((el) => el.module),
|
|
122
|
+
getExportedProviderKeys: (module) => this.getExportedProviderKeys(module),
|
|
123
|
+
});
|
|
124
|
+
this.ensureAdditionalNameConflicts(moduleWithOverrides, imports);
|
|
125
|
+
this.markModuleIfImportsUseForwardRef(moduleWithOverrides);
|
|
126
|
+
return moduleId;
|
|
127
|
+
}
|
|
128
|
+
finalizeModuleRegistration(m, moduleWithOverrides, scope, importedModulesWithScope) {
|
|
129
|
+
this.moduleGraphEnsurer.ensureEagerProvidersUseSingletonLifetime({
|
|
130
|
+
module: moduleWithOverrides,
|
|
131
|
+
scope,
|
|
132
|
+
});
|
|
133
|
+
this.moduleGraphEnsurer.ensureShutdownHooksUseSingletonLifetime({
|
|
134
|
+
module: moduleWithOverrides,
|
|
135
|
+
scope,
|
|
136
|
+
});
|
|
117
137
|
this.lifecycleProcessor.collectEagerProviders(moduleWithOverrides, scope);
|
|
118
138
|
this.afterRegisterProviders(moduleWithOverrides, scope, importedModulesWithScope);
|
|
119
139
|
const moduleTree = this.createModuleScope(m.name, scope, importedModulesWithScope);
|
|
@@ -182,7 +202,8 @@ export class ContainerContextBase {
|
|
|
182
202
|
scope,
|
|
183
203
|
importedScopes,
|
|
184
204
|
init: (options) => this.lifecycleProcessor.init(options),
|
|
185
|
-
|
|
205
|
+
close: () => this.lifecycleProcessor.close(),
|
|
206
|
+
enableShutdownHooks: () => this.lifecycleProcessor.enableShutdownHooks(),
|
|
186
207
|
};
|
|
187
208
|
}
|
|
188
209
|
resolveImports(m) {
|
|
@@ -41,23 +41,7 @@ export class AsyncDIContext extends DIContextBase {
|
|
|
41
41
|
}
|
|
42
42
|
const imports = await this.resolveImportsAsync(m);
|
|
43
43
|
const moduleWithOverrides = this.overridesProcessor.applyModuleOverrides(m);
|
|
44
|
-
const moduleId = this.
|
|
45
|
-
module: moduleWithOverrides,
|
|
46
|
-
scope,
|
|
47
|
-
importedModules: imports,
|
|
48
|
-
});
|
|
49
|
-
this.moduleGraphEnsurer.ensureImportedModulesUniqueness({
|
|
50
|
-
module: moduleWithOverrides,
|
|
51
|
-
resolvedImports: imports,
|
|
52
|
-
globalModules: this.globalModulesWithScope.map((el) => el.module),
|
|
53
|
-
});
|
|
54
|
-
this.moduleGraphEnsurer.ensureNoProviderNameConflicts({
|
|
55
|
-
module: moduleWithOverrides,
|
|
56
|
-
resolvedImports: imports,
|
|
57
|
-
globalModules: this.globalModulesWithScope.map((el) => el.module),
|
|
58
|
-
getExportedProviderKeys: (module) => this.getExportedProviderKeys(module),
|
|
59
|
-
});
|
|
60
|
-
this.markModuleIfImportsUseForwardRef(moduleWithOverrides);
|
|
44
|
+
const moduleId = this.prepareModuleRegistration(moduleWithOverrides, scope, imports);
|
|
61
45
|
const isCircular = moduleChain.includes(m);
|
|
62
46
|
if (isCircular) {
|
|
63
47
|
this.moduleGraphEnsurer.ensureCircularDependencyHasForwardRef({
|
|
@@ -87,12 +71,12 @@ export class AsyncDIContext extends DIContextBase {
|
|
|
87
71
|
};
|
|
88
72
|
}))),
|
|
89
73
|
];
|
|
90
|
-
this.
|
|
74
|
+
this.registerImportedFeatures(scope, importedModulesWithScope);
|
|
91
75
|
const moduleForSorting = {
|
|
92
76
|
...moduleWithOverrides,
|
|
93
77
|
imports: importedModulesWithScope.map((el) => el.module),
|
|
94
78
|
};
|
|
95
|
-
this.
|
|
79
|
+
this.beforeRegisterProviders(moduleWithOverrides, scope, importedModulesWithScope);
|
|
96
80
|
const providers = this.sorter.sortByDependencies(moduleForSorting);
|
|
97
81
|
for (const [key, provider] of Object.entries(providers)) {
|
|
98
82
|
scope.register({
|
|
@@ -105,10 +89,7 @@ export class AsyncDIContext extends DIContextBase {
|
|
|
105
89
|
}),
|
|
106
90
|
});
|
|
107
91
|
}
|
|
108
|
-
this.
|
|
109
|
-
this.afterRegisterProviders(moduleWithOverrides, scope, importedModulesWithScope);
|
|
110
|
-
const moduleTree = this.createModuleScope(m.name, scope, importedModulesWithScope);
|
|
111
|
-
this.moduleTreeMap.set(m, moduleTree);
|
|
92
|
+
const moduleTree = this.finalizeModuleRegistration(m, moduleWithOverrides, scope, importedModulesWithScope);
|
|
112
93
|
this.moduleTreePromiseMap.delete(m);
|
|
113
94
|
return moduleTree;
|
|
114
95
|
}
|
|
@@ -15,11 +15,11 @@ export class DIContextBase extends ContainerContextBase {
|
|
|
15
15
|
hasScopeContext: hasRequestScopeContext,
|
|
16
16
|
resolveFromScope: resolveFromRequestScope,
|
|
17
17
|
});
|
|
18
|
-
this.handlerProcessor = new HandlerProcessor(this.options.providerOptions || {}, this.devtoolsProcessorRef);
|
|
19
|
-
this.interceptorProcessor = new InterceptorProcessor(this.options.providerOptions || {}, this.devtoolsProcessorRef);
|
|
20
|
-
this.initializerProcessor = new InitializerProcessor(this.devtoolsProcessorRef);
|
|
21
|
-
this.controllerProcessor = new ControllerProcessor(this.interceptorProcessor, this.options.providerOptions || {}, this.options.skipRegisterRoutes === true, this.devtoolsProcessorRef);
|
|
22
|
-
this.providerResolver = new ProviderResolver(this.interceptorProcessor, this.options.providerOptions || {}, this.
|
|
18
|
+
this.handlerProcessor = new HandlerProcessor(this.options.providerOptions || {}, this.inquirerResolverFactory, this.devtoolsProcessorRef);
|
|
19
|
+
this.interceptorProcessor = new InterceptorProcessor(this.options.providerOptions || {}, this.inquirerResolverFactory, this.devtoolsProcessorRef);
|
|
20
|
+
this.initializerProcessor = new InitializerProcessor(this.inquirerResolverFactory, this.devtoolsProcessorRef);
|
|
21
|
+
this.controllerProcessor = new ControllerProcessor(this.interceptorProcessor, this.options.providerOptions || {}, this.options.skipRegisterRoutes === true, this.inquirerResolverFactory, this.devtoolsProcessorRef);
|
|
22
|
+
this.providerResolver = new ProviderResolver(this.interceptorProcessor, this.options.providerOptions || {}, getOrCreateRequestScope, this.inquirerResolverFactory, this.devtoolsProcessorRef);
|
|
23
23
|
}
|
|
24
24
|
beforeRegisterProviders(m, scope, importedModulesWithScope) {
|
|
25
25
|
this.interceptorProcessor.processInterceptors(m, scope, importedModulesWithScope);
|
|
@@ -2,6 +2,11 @@ import type { InternalModuleLike as M } from "../modules/runtime-module.types.js
|
|
|
2
2
|
import type { DiContextOptions } from "./di-context-base.js";
|
|
3
3
|
import { DIContextBase } from "./di-context-base.js";
|
|
4
4
|
export declare class DIContext extends DIContextBase {
|
|
5
|
+
private readonly configuredModules;
|
|
6
|
+
private readonly configuringModules;
|
|
5
7
|
private constructor();
|
|
6
8
|
static create(module: M, options?: DiContextOptions): import("./container-context-base.js").ModuleScope<import("awilix").AwilixContainer<{}>>;
|
|
9
|
+
private resolveConfiguredModule;
|
|
10
|
+
protected resolveImports(module: M): M[];
|
|
11
|
+
protected get globalModules(): M[];
|
|
7
12
|
}
|
|
@@ -1,9 +1,63 @@
|
|
|
1
|
+
import * as ERRORS from "../errors.js";
|
|
2
|
+
import { configuredModuleSymbolKey, isConfiguredModule, } from "../modules/configure-module.js";
|
|
3
|
+
import { isPromiseLike } from "../type-guards.js";
|
|
1
4
|
import { DIContextBase } from "./di-context-base.js";
|
|
2
5
|
export class DIContext extends DIContextBase {
|
|
6
|
+
configuredModules = new WeakMap();
|
|
7
|
+
configuringModules = new WeakSet();
|
|
3
8
|
constructor(options) {
|
|
4
9
|
super(options);
|
|
5
10
|
}
|
|
6
11
|
static create(module, options) {
|
|
12
|
+
if (isConfiguredModule(module)) {
|
|
13
|
+
throw new ERRORS.ConfiguredModuleAsRootError(module.name);
|
|
14
|
+
}
|
|
7
15
|
return new DIContext(options ?? {}).bootstrapModule(module);
|
|
8
16
|
}
|
|
17
|
+
resolveConfiguredModule(module) {
|
|
18
|
+
if (!isConfiguredModule(module))
|
|
19
|
+
return module;
|
|
20
|
+
const cached = this.configuredModules.get(module);
|
|
21
|
+
if (cached)
|
|
22
|
+
return cached;
|
|
23
|
+
if (this.configuringModules.has(module)) {
|
|
24
|
+
throw new ERRORS.CircularModuleConfigurationDependencyError(module.name);
|
|
25
|
+
}
|
|
26
|
+
this.configuringModules.add(module);
|
|
27
|
+
try {
|
|
28
|
+
const configuration = module[configuredModuleSymbolKey];
|
|
29
|
+
const configurationModule = {
|
|
30
|
+
name: module.name,
|
|
31
|
+
imports: configuration.imports,
|
|
32
|
+
};
|
|
33
|
+
const { scope } = this.registerModuleWithScope(configurationModule, this.createContainer(configurationModule), []);
|
|
34
|
+
const args = configuration.inject.map((key) => {
|
|
35
|
+
if (!scope.hasRegistration(key)) {
|
|
36
|
+
throw new ERRORS.ModuleConfigurationDependencyNotFoundError(module.name, key);
|
|
37
|
+
}
|
|
38
|
+
return scope.resolve(key);
|
|
39
|
+
});
|
|
40
|
+
const result = configuration.useFactory(...args);
|
|
41
|
+
if (isPromiseLike(result)) {
|
|
42
|
+
throw new ERRORS.AsyncModuleConfigurationFactoryError(module.name);
|
|
43
|
+
}
|
|
44
|
+
if (!result || typeof result.name !== "string") {
|
|
45
|
+
throw new ERRORS.InvalidModuleConfigurationFactoryResultError(module.name);
|
|
46
|
+
}
|
|
47
|
+
const resolved = this.resolveConfiguredModule(result);
|
|
48
|
+
this.configuredModules.set(module, resolved);
|
|
49
|
+
return resolved;
|
|
50
|
+
}
|
|
51
|
+
finally {
|
|
52
|
+
this.configuringModules.delete(module);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
resolveImports(module) {
|
|
56
|
+
return super
|
|
57
|
+
.resolveImports(module)
|
|
58
|
+
.map((imported) => this.resolveConfiguredModule(imported));
|
|
59
|
+
}
|
|
60
|
+
get globalModules() {
|
|
61
|
+
return super.globalModules.map((module) => this.resolveConfiguredModule(module));
|
|
62
|
+
}
|
|
9
63
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type AwilixContainer } from "awilix";
|
|
1
2
|
import type { InternalModuleLike as M } from "../modules/runtime-module.types.js";
|
|
2
3
|
type EnsureImportedModulesUniquenessParams = {
|
|
3
4
|
module: M;
|
|
@@ -23,6 +24,14 @@ type EnsureCircularDependencyHasForwardRefParams = {
|
|
|
23
24
|
forwardRefModules: WeakSet<M>;
|
|
24
25
|
};
|
|
25
26
|
export declare class ModuleGraphEnsurer {
|
|
27
|
+
ensureEagerProvidersUseSingletonLifetime({ module, scope, }: {
|
|
28
|
+
module: M;
|
|
29
|
+
scope: AwilixContainer;
|
|
30
|
+
}): void;
|
|
31
|
+
ensureShutdownHooksUseSingletonLifetime({ module, scope, }: {
|
|
32
|
+
module: M;
|
|
33
|
+
scope: AwilixContainer;
|
|
34
|
+
}): void;
|
|
26
35
|
ensureImportedModulesUniqueness({ module, resolvedImports, globalModules, }: EnsureImportedModulesUniquenessParams): void;
|
|
27
36
|
ensureGlobalModulesDoNotImportGlobalModules({ globalModules, globalModuleImports, }: EnsureGlobalModulesDoNotImportGlobalModulesParams): void;
|
|
28
37
|
ensureNoProviderNameConflicts({ module, resolvedImports, globalModules, getExportedProviderKeys, }: EnsureNoProviderNameConflictsParams): void;
|
|
@@ -1,5 +1,32 @@
|
|
|
1
|
+
import { Lifetime } from "awilix";
|
|
1
2
|
import * as ERRORS from "../errors.js";
|
|
3
|
+
import { ShutdownHook } from "../lifecycle-hooks.js";
|
|
4
|
+
import * as GUARDS from "../type-guards.js";
|
|
2
5
|
export class ModuleGraphEnsurer {
|
|
6
|
+
ensureEagerProvidersUseSingletonLifetime({ module, scope, }) {
|
|
7
|
+
for (const [key, provider] of Object.entries(module.providers || {})) {
|
|
8
|
+
if (!GUARDS.isEagerProvider(provider))
|
|
9
|
+
continue;
|
|
10
|
+
if (scope.registrations[key]?.lifetime === Lifetime.SINGLETON)
|
|
11
|
+
continue;
|
|
12
|
+
throw new ERRORS.EagerProviderRequiresSingletonLifetimeError(module.name, key);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
ensureShutdownHooksUseSingletonLifetime({ module, scope, }) {
|
|
16
|
+
for (const [key, provider] of Object.entries(module.providers || {})) {
|
|
17
|
+
if (scope.registrations[key]?.lifetime === Lifetime.SINGLETON)
|
|
18
|
+
continue;
|
|
19
|
+
const useClass = GUARDS.hasUseClass(provider)
|
|
20
|
+
? provider.useClass
|
|
21
|
+
: GUARDS.isCostructorProvider(provider)
|
|
22
|
+
? provider
|
|
23
|
+
: undefined;
|
|
24
|
+
if (useClass &&
|
|
25
|
+
Object.values(ShutdownHook).some((hook) => GUARDS.hasProviderShutdownHook(useClass.prototype, hook))) {
|
|
26
|
+
throw new ERRORS.ShutdownHookRequiresSingletonLifetimeError(module.name, key);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
3
30
|
ensureImportedModulesUniqueness({ module, resolvedImports, globalModules, }) {
|
|
4
31
|
const importedNames = new Set();
|
|
5
32
|
const imports = [...globalModules, ...resolvedImports];
|
package/dist/lib/di/errors.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
export declare class InquirerRequiresTransientLifetimeError extends Error {
|
|
2
|
+
constructor(className: string);
|
|
3
|
+
}
|
|
1
4
|
export declare class DuplicateControllersInModuleError extends Error {
|
|
2
5
|
constructor(moduleName: string);
|
|
3
6
|
}
|
|
@@ -52,12 +55,30 @@ export declare class AsyncFactoryRequiresSingletonLifetimeError extends Error {
|
|
|
52
55
|
export declare class EagerProviderRequiresSingletonLifetimeError extends Error {
|
|
53
56
|
constructor(moduleName: string, providerKey: string);
|
|
54
57
|
}
|
|
58
|
+
export declare class ShutdownHookRequiresSingletonLifetimeError extends Error {
|
|
59
|
+
constructor(moduleName: string, providerKey: string);
|
|
60
|
+
}
|
|
55
61
|
export declare class EagerProviderInitDependencyNotFoundError extends Error {
|
|
56
62
|
constructor(moduleName: string, providerKey: string, dependencyKey: string);
|
|
57
63
|
}
|
|
58
64
|
export declare class CircularProviderInitDependencyError extends Error {
|
|
59
65
|
constructor(providerKeys: string[]);
|
|
60
66
|
}
|
|
67
|
+
export declare class ConfiguredModuleAsRootError extends Error {
|
|
68
|
+
constructor(moduleName: string);
|
|
69
|
+
}
|
|
70
|
+
export declare class CircularModuleConfigurationDependencyError extends Error {
|
|
71
|
+
constructor(moduleName: string);
|
|
72
|
+
}
|
|
73
|
+
export declare class ModuleConfigurationDependencyNotFoundError extends Error {
|
|
74
|
+
constructor(moduleName: string, dependencyKey: string);
|
|
75
|
+
}
|
|
76
|
+
export declare class AsyncModuleConfigurationFactoryError extends Error {
|
|
77
|
+
constructor(moduleName: string);
|
|
78
|
+
}
|
|
79
|
+
export declare class InvalidModuleConfigurationFactoryResultError extends Error {
|
|
80
|
+
constructor(moduleName: string);
|
|
81
|
+
}
|
|
61
82
|
export declare class AsyncModuleRequiresAsyncCreateError extends Error {
|
|
62
83
|
constructor(moduleName: string);
|
|
63
84
|
}
|
package/dist/lib/di/errors.js
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
export class InquirerRequiresTransientLifetimeError extends Error {
|
|
2
|
+
constructor(className) {
|
|
3
|
+
super(`Provider "${className}" with injectInquirer requires TRANSIENT lifetime.`);
|
|
4
|
+
this.name = "InquirerRequiresTransientLifetimeError";
|
|
5
|
+
}
|
|
6
|
+
}
|
|
1
7
|
export class DuplicateControllersInModuleError extends Error {
|
|
2
8
|
constructor(moduleName) {
|
|
3
9
|
super(`Module "${moduleName}" has duplicate controllers in its controllers array.`);
|
|
@@ -117,6 +123,13 @@ export class EagerProviderRequiresSingletonLifetimeError extends Error {
|
|
|
117
123
|
this.name = "EagerProviderRequiresSingletonLifetimeError";
|
|
118
124
|
}
|
|
119
125
|
}
|
|
126
|
+
export class ShutdownHookRequiresSingletonLifetimeError extends Error {
|
|
127
|
+
constructor(moduleName, providerKey) {
|
|
128
|
+
super(`Provider "${providerKey}" in module "${moduleName}" declares an application shutdown hook with a non-singleton lifetime. ` +
|
|
129
|
+
`Providers with application shutdown hooks must use SINGLETON lifetime.`);
|
|
130
|
+
this.name = "ShutdownHookRequiresSingletonLifetimeError";
|
|
131
|
+
}
|
|
132
|
+
}
|
|
120
133
|
export class EagerProviderInitDependencyNotFoundError extends Error {
|
|
121
134
|
constructor(moduleName, providerKey, dependencyKey) {
|
|
122
135
|
super(`Provider "${providerKey}" in module "${moduleName}" declares initAfter dependency "${dependencyKey}", ` +
|
|
@@ -130,6 +143,36 @@ export class CircularProviderInitDependencyError extends Error {
|
|
|
130
143
|
this.name = "CircularProviderInitDependencyError";
|
|
131
144
|
}
|
|
132
145
|
}
|
|
146
|
+
export class ConfiguredModuleAsRootError extends Error {
|
|
147
|
+
constructor(moduleName) {
|
|
148
|
+
super(`Configured module "${moduleName}" must be an import or global, not the root module.`);
|
|
149
|
+
this.name = "ConfiguredModuleAsRootError";
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
export class CircularModuleConfigurationDependencyError extends Error {
|
|
153
|
+
constructor(moduleName) {
|
|
154
|
+
super(`Circular module configuration dependency detected for "${moduleName}".`);
|
|
155
|
+
this.name = "CircularModuleConfigurationDependencyError";
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
export class ModuleConfigurationDependencyNotFoundError extends Error {
|
|
159
|
+
constructor(moduleName, dependencyKey) {
|
|
160
|
+
super(`Configuration dependency "${dependencyKey}" in module "${moduleName}" is not exported by its imports.`);
|
|
161
|
+
this.name = "ModuleConfigurationDependencyNotFoundError";
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
export class AsyncModuleConfigurationFactoryError extends Error {
|
|
165
|
+
constructor(moduleName) {
|
|
166
|
+
super(`Module "${moduleName}" configuration factory must be synchronous.`);
|
|
167
|
+
this.name = "AsyncModuleConfigurationFactoryError";
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
export class InvalidModuleConfigurationFactoryResultError extends Error {
|
|
171
|
+
constructor(moduleName) {
|
|
172
|
+
super(`Module "${moduleName}" configuration factory must return a module.`);
|
|
173
|
+
this.name = "InvalidModuleConfigurationFactoryResultError";
|
|
174
|
+
}
|
|
175
|
+
}
|
|
133
176
|
export class AsyncModuleRequiresAsyncCreateError extends Error {
|
|
134
177
|
constructor(moduleName) {
|
|
135
178
|
super(`Module "${moduleName}" imports an async module. ` +
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare const InitHook: {
|
|
2
|
+
readonly OnInit: "onInit";
|
|
3
|
+
readonly AfterInit: "afterInit";
|
|
4
|
+
};
|
|
5
|
+
export type InitHook = (typeof InitHook)[keyof typeof InitHook];
|
|
6
|
+
export declare const ShutdownHook: {
|
|
7
|
+
readonly BeforeShutdown: "beforeShutdown";
|
|
8
|
+
readonly OnShutdown: "onShutdown";
|
|
9
|
+
};
|
|
10
|
+
export type ShutdownHook = (typeof ShutdownHook)[keyof typeof ShutdownHook];
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { AnyModule, ImportModule, ModuleDefinition, ModuleImport } from "./module.types.js";
|
|
2
|
+
import type { ExtractExportsFromImports } from "./module-def.types.js";
|
|
3
|
+
import type { InternalModuleLike } from "./runtime-module.types.js";
|
|
4
|
+
export declare const configuredModuleSymbolKey: unique symbol;
|
|
5
|
+
type Definition<M> = M extends ImportModule<infer Def extends ModuleDefinition> ? Def : ModuleDefinition;
|
|
6
|
+
export type ConfiguredModule<M extends AnyModule> = InternalModuleLike & ImportModule<Definition<M>> & {
|
|
7
|
+
readonly [configuredModuleSymbolKey]: {
|
|
8
|
+
imports: readonly ModuleImport[];
|
|
9
|
+
inject: readonly string[];
|
|
10
|
+
useFactory: (...args: any[]) => M;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
export declare function configureModule<const Imports extends readonly ModuleImport[], const Keys extends readonly Extract<keyof ExtractExportsFromImports<Imports>, string>[], M extends AnyModule>(options: {
|
|
14
|
+
imports: Imports;
|
|
15
|
+
inject: Keys;
|
|
16
|
+
useFactory: (...args: {
|
|
17
|
+
[K in keyof Keys]: ExtractExportsFromImports<Imports>[Keys[K]];
|
|
18
|
+
}) => M;
|
|
19
|
+
}): ConfiguredModule<M>;
|
|
20
|
+
export declare function isConfiguredModule(module: InternalModuleLike): module is ConfiguredModule<AnyModule>;
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export const configuredModuleSymbolKey = Symbol("awilixify.configureModule");
|
|
2
|
+
let nextId = 0;
|
|
3
|
+
export function configureModule(options) {
|
|
4
|
+
return {
|
|
5
|
+
name: `ConfiguredModule_${++nextId}`,
|
|
6
|
+
[configuredModuleSymbolKey]: options,
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
export function isConfiguredModule(module) {
|
|
10
|
+
return configuredModuleSymbolKey in module;
|
|
11
|
+
}
|
|
@@ -70,7 +70,7 @@ type ExtractModuleDefFromModule<T> = Awaited<T> extends ImportModule<infer TDef
|
|
|
70
70
|
} ? {
|
|
71
71
|
exports: E;
|
|
72
72
|
} : never;
|
|
73
|
-
type ExtractExportsFromImports<T extends readonly ModuleImport[]> = T extends readonly [
|
|
73
|
+
export type ExtractExportsFromImports<T extends readonly ModuleImport[]> = T extends readonly [
|
|
74
74
|
infer First,
|
|
75
75
|
...infer Rest extends readonly ModuleImport[]
|
|
76
76
|
] ? ExtractModuleDefFromModule<First> extends {
|
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
import * as Awilix from "awilix";
|
|
2
2
|
import type { DevtoolsProcessorRef } from "../../devtools/devtools.types.js";
|
|
3
3
|
import type { InternalModuleLike as M } from "../modules/runtime-module.types.js";
|
|
4
|
+
import type { InquirerResolverFactory } from "../providers/inquirer-resolver-factory.js";
|
|
4
5
|
import type { ControllerRuntimeEntry } from "./initializer-processor.js";
|
|
5
6
|
import type { InterceptorProcessor } from "./interceptor-processor.js";
|
|
6
7
|
export declare class ControllerProcessor {
|
|
7
8
|
private readonly interceptorProcessor;
|
|
8
9
|
private readonly providerOptions;
|
|
9
10
|
private readonly skipRegisterRoutes;
|
|
11
|
+
private readonly inquirerResolverFactory;
|
|
10
12
|
private readonly devtoolsProcessorRef;
|
|
11
13
|
private readonly registeredControllers;
|
|
12
|
-
constructor(interceptorProcessor: InterceptorProcessor, providerOptions: Partial<Awilix.BuildResolverOptions<any>>, skipRegisterRoutes: boolean, devtoolsProcessorRef: DevtoolsProcessorRef);
|
|
14
|
+
constructor(interceptorProcessor: InterceptorProcessor, providerOptions: Partial<Awilix.BuildResolverOptions<any>>, skipRegisterRoutes: boolean, inquirerResolverFactory: InquirerResolverFactory, devtoolsProcessorRef: DevtoolsProcessorRef);
|
|
13
15
|
processControllers(m: M, diScope: Awilix.AwilixContainer): ControllerRuntimeEntry[];
|
|
16
|
+
private registerController;
|
|
14
17
|
private resolveBySymbol;
|
|
15
18
|
private get devtoolsTracer();
|
|
16
19
|
private createTracedControllerResolver;
|