awilixify 3.3.0 → 3.5.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.js +6 -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 +30 -18
- package/dist/lib/di/contexts/di-context-async.js +4 -31
- 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/errors.d.ts +18 -0
- package/dist/lib/di/errors.js +36 -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 +5 -1
- package/dist/lib/di/processors/interceptor-processor.js +59 -69
- 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/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 +4 -2
- package/dist/lib/di/type-guards.d.ts +1 -0
- package/dist/lib/extension-api/index.d.ts +1 -0
- package/dist/lib/extension-api/index.js +1 -0
- package/dist/lib/http/exceptions.d.ts +2 -2
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +18 -9
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 { AfterInit, BeforeShutdown, ConstructorController, Controller, InitializerContext, InterceptContext, Interceptor, OnInit, OnShutdown, } from "./lib/di/providers/provider.types.js";
|
|
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";
|
|
@@ -40,6 +40,11 @@ function hasOwnDecoratorMethodMetadata(target) {
|
|
|
40
40
|
state.methods instanceof Map &&
|
|
41
41
|
state.methods.size > 0);
|
|
42
42
|
}
|
|
43
|
+
function validateDecoratorTarget(context) {
|
|
44
|
+
if (context.kind === "method" && (context.static || context.private)) {
|
|
45
|
+
throw new Error(`Cannot decorate ${context.static ? "static" : "#private"} method "${String(context.name)}": only non-static, non-#private instance methods are supported`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
43
48
|
function createUpdater(token, initializers) {
|
|
44
49
|
const initializeState = () => ({
|
|
45
50
|
root: initializers.root(),
|
|
@@ -47,6 +52,7 @@ function createUpdater(token, initializers) {
|
|
|
47
52
|
decoratorNames: new Map(),
|
|
48
53
|
});
|
|
49
54
|
function update(context, updater, decoratorName) {
|
|
55
|
+
validateDecoratorTarget(context);
|
|
50
56
|
if (!context.metadata)
|
|
51
57
|
return;
|
|
52
58
|
const state = context.metadata[token.stateSymbol] || initializeState();
|
|
@@ -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,33 @@ 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) {
|
|
117
129
|
this.moduleGraphEnsurer.ensureEagerProvidersUseSingletonLifetime({
|
|
118
130
|
module: moduleWithOverrides,
|
|
119
131
|
scope,
|
|
@@ -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,18 +89,7 @@ export class AsyncDIContext extends DIContextBase {
|
|
|
105
89
|
}),
|
|
106
90
|
});
|
|
107
91
|
}
|
|
108
|
-
this.
|
|
109
|
-
module: moduleWithOverrides,
|
|
110
|
-
scope,
|
|
111
|
-
});
|
|
112
|
-
this.moduleGraphEnsurer.ensureShutdownHooksUseSingletonLifetime({
|
|
113
|
-
module: moduleWithOverrides,
|
|
114
|
-
scope,
|
|
115
|
-
});
|
|
116
|
-
this.lifecycleProcessor.collectEagerProviders(moduleWithOverrides, scope);
|
|
117
|
-
this.afterRegisterProviders(moduleWithOverrides, scope, importedModulesWithScope);
|
|
118
|
-
const moduleTree = this.createModuleScope(m.name, scope, importedModulesWithScope);
|
|
119
|
-
this.moduleTreeMap.set(m, moduleTree);
|
|
92
|
+
const moduleTree = this.finalizeModuleRegistration(m, moduleWithOverrides, scope, importedModulesWithScope);
|
|
120
93
|
this.moduleTreePromiseMap.delete(m);
|
|
121
94
|
return moduleTree;
|
|
122
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
|
}
|
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
|
}
|
|
@@ -61,6 +64,21 @@ export declare class EagerProviderInitDependencyNotFoundError extends Error {
|
|
|
61
64
|
export declare class CircularProviderInitDependencyError extends Error {
|
|
62
65
|
constructor(providerKeys: string[]);
|
|
63
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
|
+
}
|
|
64
82
|
export declare class AsyncModuleRequiresAsyncCreateError extends Error {
|
|
65
83
|
constructor(moduleName: string);
|
|
66
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.`);
|
|
@@ -137,6 +143,36 @@ export class CircularProviderInitDependencyError extends Error {
|
|
|
137
143
|
this.name = "CircularProviderInitDependencyError";
|
|
138
144
|
}
|
|
139
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
|
+
}
|
|
140
176
|
export class AsyncModuleRequiresAsyncCreateError extends Error {
|
|
141
177
|
constructor(moduleName) {
|
|
142
178
|
super(`Module "${moduleName}" imports an async module. ` +
|
|
@@ -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;
|
|
@@ -6,12 +6,14 @@ export class ControllerProcessor {
|
|
|
6
6
|
interceptorProcessor;
|
|
7
7
|
providerOptions;
|
|
8
8
|
skipRegisterRoutes;
|
|
9
|
+
inquirerResolverFactory;
|
|
9
10
|
devtoolsProcessorRef;
|
|
10
11
|
registeredControllers = new WeakMap();
|
|
11
|
-
constructor(interceptorProcessor, providerOptions, skipRegisterRoutes, devtoolsProcessorRef) {
|
|
12
|
+
constructor(interceptorProcessor, providerOptions, skipRegisterRoutes, inquirerResolverFactory, devtoolsProcessorRef) {
|
|
12
13
|
this.interceptorProcessor = interceptorProcessor;
|
|
13
14
|
this.providerOptions = providerOptions;
|
|
14
15
|
this.skipRegisterRoutes = skipRegisterRoutes;
|
|
16
|
+
this.inquirerResolverFactory = inquirerResolverFactory;
|
|
15
17
|
this.devtoolsProcessorRef = devtoolsProcessorRef;
|
|
16
18
|
}
|
|
17
19
|
processControllers(m, diScope) {
|
|
@@ -29,57 +31,7 @@ export class ControllerProcessor {
|
|
|
29
31
|
: { useClass: c };
|
|
30
32
|
const existingModule = this.registeredControllers.get(useClass);
|
|
31
33
|
if (!existingModule) {
|
|
32
|
-
this.
|
|
33
|
-
const controllerSymbol = Symbol(`controller_${useClass.name}`);
|
|
34
|
-
const options = {
|
|
35
|
-
...this.providerOptions,
|
|
36
|
-
...m.providerOptions,
|
|
37
|
-
...awilixOptions,
|
|
38
|
-
};
|
|
39
|
-
const isWithNewScope = options.lifetime !== Awilix.Lifetime.SINGLETON;
|
|
40
|
-
const baseResolver = Awilix.asClass(useClass, {
|
|
41
|
-
...options,
|
|
42
|
-
...(isWithNewScope && {
|
|
43
|
-
injector: () => ({
|
|
44
|
-
resolveSelf: () => this.resolveBySymbol(controllerSymbol, diScope, isWithNewScope),
|
|
45
|
-
}),
|
|
46
|
-
}),
|
|
47
|
-
});
|
|
48
|
-
const tracedResolver = this.devtoolsTracer
|
|
49
|
-
? this.createTracedControllerResolver({
|
|
50
|
-
module: m,
|
|
51
|
-
options,
|
|
52
|
-
className: useClass.name,
|
|
53
|
-
registrationKey: useClass.name,
|
|
54
|
-
resolver: baseResolver,
|
|
55
|
-
})
|
|
56
|
-
: baseResolver;
|
|
57
|
-
const interceptedResolver = this.interceptorProcessor.createInterceptedProviderResolver({
|
|
58
|
-
module: m,
|
|
59
|
-
useClass,
|
|
60
|
-
options,
|
|
61
|
-
resolver: tracedResolver,
|
|
62
|
-
});
|
|
63
|
-
const traceStarterResolver = this.devtoolsTracer
|
|
64
|
-
? this.createTraceStarterResolver({
|
|
65
|
-
module: m,
|
|
66
|
-
options,
|
|
67
|
-
className: useClass.name,
|
|
68
|
-
registrationKey: useClass.name,
|
|
69
|
-
resolver: interceptedResolver,
|
|
70
|
-
})
|
|
71
|
-
: interceptedResolver;
|
|
72
|
-
diScope.register({
|
|
73
|
-
[controllerSymbol]: traceStarterResolver,
|
|
74
|
-
});
|
|
75
|
-
if (!this.skipRegisterRoutes &&
|
|
76
|
-
typeof useClass.prototype.registerRoutes === "function") {
|
|
77
|
-
baseResolver.resolve(diScope).registerRoutes();
|
|
78
|
-
}
|
|
79
|
-
runtimeEntries.push({
|
|
80
|
-
controllerClass: useClass,
|
|
81
|
-
resolve: () => this.resolveBySymbol(controllerSymbol, diScope, isWithNewScope),
|
|
82
|
-
});
|
|
34
|
+
runtimeEntries.push(this.registerController(m, diScope, useClass, awilixOptions));
|
|
83
35
|
continue;
|
|
84
36
|
}
|
|
85
37
|
if (existingModule === m) {
|
|
@@ -89,6 +41,59 @@ export class ControllerProcessor {
|
|
|
89
41
|
}
|
|
90
42
|
return runtimeEntries;
|
|
91
43
|
}
|
|
44
|
+
registerController(m, diScope, useClass, awilixOptions) {
|
|
45
|
+
this.registeredControllers.set(useClass, m);
|
|
46
|
+
const controllerSymbol = Symbol(`controller_${useClass.name}`);
|
|
47
|
+
const options = {
|
|
48
|
+
...this.providerOptions,
|
|
49
|
+
...m.providerOptions,
|
|
50
|
+
...awilixOptions,
|
|
51
|
+
};
|
|
52
|
+
const isWithNewScope = options.lifetime !== Awilix.Lifetime.SINGLETON;
|
|
53
|
+
const baseResolver = this.inquirerResolverFactory.create(useClass, {
|
|
54
|
+
...options,
|
|
55
|
+
...(isWithNewScope && {
|
|
56
|
+
injector: () => ({
|
|
57
|
+
resolveSelf: () => this.resolveBySymbol(controllerSymbol, diScope, isWithNewScope),
|
|
58
|
+
}),
|
|
59
|
+
}),
|
|
60
|
+
});
|
|
61
|
+
const tracedResolver = this.devtoolsTracer
|
|
62
|
+
? this.createTracedControllerResolver({
|
|
63
|
+
module: m,
|
|
64
|
+
options,
|
|
65
|
+
className: useClass.name,
|
|
66
|
+
registrationKey: useClass.name,
|
|
67
|
+
resolver: baseResolver,
|
|
68
|
+
})
|
|
69
|
+
: baseResolver;
|
|
70
|
+
const interceptedResolver = this.interceptorProcessor.createInterceptedProviderResolver({
|
|
71
|
+
module: m,
|
|
72
|
+
useClass,
|
|
73
|
+
options,
|
|
74
|
+
resolver: tracedResolver,
|
|
75
|
+
});
|
|
76
|
+
const traceStarterResolver = this.devtoolsTracer
|
|
77
|
+
? this.createTraceStarterResolver({
|
|
78
|
+
module: m,
|
|
79
|
+
options,
|
|
80
|
+
className: useClass.name,
|
|
81
|
+
registrationKey: useClass.name,
|
|
82
|
+
resolver: interceptedResolver,
|
|
83
|
+
})
|
|
84
|
+
: interceptedResolver;
|
|
85
|
+
diScope.register({
|
|
86
|
+
[controllerSymbol]: traceStarterResolver,
|
|
87
|
+
});
|
|
88
|
+
if (!this.skipRegisterRoutes &&
|
|
89
|
+
typeof useClass.prototype.registerRoutes === "function") {
|
|
90
|
+
baseResolver.resolve(diScope).registerRoutes();
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
controllerClass: useClass,
|
|
94
|
+
resolve: () => this.resolveBySymbol(controllerSymbol, diScope, isWithNewScope),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
92
97
|
resolveBySymbol(symbol, scope, withNewScope) {
|
|
93
98
|
if (withNewScope)
|
|
94
99
|
return resolveFromRequestScope(scope, symbol);
|
|
@@ -2,6 +2,7 @@ import * as Awilix from "awilix";
|
|
|
2
2
|
import type { DevtoolsProcessorRef } from "../../devtools/devtools.types.js";
|
|
3
3
|
import type { RegisteredModuleScope } from "../contexts/container-context-base.js";
|
|
4
4
|
import type { InternalModuleLike as M } from "../modules/runtime-module.types.js";
|
|
5
|
+
import type { InquirerResolverFactory } from "../providers/inquirer-resolver-factory.js";
|
|
5
6
|
export declare const HandlerType: {
|
|
6
7
|
readonly Query: "query";
|
|
7
8
|
readonly Command: "command";
|
|
@@ -9,10 +10,11 @@ export declare const HandlerType: {
|
|
|
9
10
|
export type HandlerType = (typeof HandlerType)[keyof typeof HandlerType];
|
|
10
11
|
export declare class HandlerProcessor {
|
|
11
12
|
private readonly providerOptions;
|
|
13
|
+
private readonly inquirerResolverFactory;
|
|
12
14
|
private readonly devtoolsProcessorRef;
|
|
13
15
|
private readonly keyedFeatureRegistrar;
|
|
14
16
|
private static readonly handlerConfig;
|
|
15
|
-
constructor(providerOptions: Partial<Awilix.BuildResolverOptions<any>>, devtoolsProcessorRef: DevtoolsProcessorRef);
|
|
17
|
+
constructor(providerOptions: Partial<Awilix.BuildResolverOptions<any>>, inquirerResolverFactory: InquirerResolverFactory, devtoolsProcessorRef: DevtoolsProcessorRef);
|
|
16
18
|
processHandlers(m: M, scope: Awilix.AwilixContainer, importedModulesWithScope: RegisteredModuleScope[], handlerType: HandlerType): void;
|
|
17
19
|
private createTracedHandlerResolver;
|
|
18
20
|
private get devtoolsTracer();
|