awilixify 3.1.1 → 3.1.3
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 +3 -3
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/lib/codegen/cli.d.ts +2 -0
- package/dist/lib/codegen/cli.js +57 -0
- package/dist/lib/codegen/config.d.ts +8 -0
- package/dist/lib/codegen/config.js +3 -0
- package/dist/lib/codegen/generate-asyncapi-client.d.ts +55 -0
- package/dist/lib/codegen/generate-asyncapi-client.js +359 -0
- package/dist/lib/codegen/generate-orval-api-client.d.ts +36 -0
- package/dist/lib/codegen/generate-orval-api-client.js +211 -0
- package/dist/lib/codegen/index.d.ts +3 -0
- package/dist/lib/codegen/index.js +3 -0
- package/dist/lib/decorators/decorator-state.d.ts +1 -1
- package/dist/lib/decorators/decorator-state.js +7 -1
- package/dist/lib/decorators/decorator-state.types.d.ts +1 -0
- package/dist/lib/devtools/devtools.types.d.ts +14 -9
- package/dist/lib/devtools/index.d.ts +2 -1
- package/dist/lib/devtools/index.js +2 -1
- package/dist/lib/devtools/trace-context.d.ts +12 -0
- package/dist/lib/devtools/trace-context.js +46 -0
- package/dist/lib/di/contexts/di-context-base.js +1 -1
- package/dist/lib/di/processors/controller-processor.d.ts +6 -1
- package/dist/lib/di/processors/controller-processor.js +87 -5
- package/dist/lib/di/processors/handler-processor.js +6 -17
- package/dist/lib/di/processors/initializer-processor.js +1 -16
- package/dist/lib/di/processors/interceptor-processor.js +133 -17
- package/dist/lib/di/processors/keyed-feature-registrar.d.ts +5 -1
- package/dist/lib/di/processors/keyed-feature-registrar.js +48 -7
- package/dist/lib/di/providers/provider-resolver.js +27 -22
- package/dist/lib/http/openapi-builder.d.ts +2 -2
- package/dist/lib/http/openapi-builder.js +10 -5
- package/dist/lib/mediator/mediator.d.ts +2 -1
- package/dist/lib/mediator/mediator.js +24 -4
- package/dist/lib/operations/operation.decorator.d.ts +23 -0
- package/dist/lib/operations/operation.decorator.js +30 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +15 -3
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as Awilix from "awilix";
|
|
2
2
|
import { hasDecoratorMethodMetadata, resolveDecoratorState, } from "../../decorators/decorator-state.js";
|
|
3
|
+
import { isPromiseLike } from "../type-guards.js";
|
|
3
4
|
import { KeyedFeatureRegistrar } from "./keyed-feature-registrar.js";
|
|
4
5
|
export class InterceptorProcessor {
|
|
5
6
|
devtoolsProcessorRef;
|
|
@@ -36,12 +37,17 @@ export class InterceptorProcessor {
|
|
|
36
37
|
}
|
|
37
38
|
createInterceptedProviderInstance(instance, metadataTarget, resolveInterceptors, moduleName) {
|
|
38
39
|
const wrappers = new Map();
|
|
40
|
+
const targetBoundMethods = new Set();
|
|
39
41
|
const self = this;
|
|
40
42
|
return new Proxy(instance, {
|
|
41
43
|
get(target, propertyKey, proxyReceiver) {
|
|
42
44
|
const value = Reflect.get(target, propertyKey, proxyReceiver);
|
|
43
45
|
if (typeof value !== "function")
|
|
44
46
|
return value;
|
|
47
|
+
if (propertyKey === "constructor")
|
|
48
|
+
return value;
|
|
49
|
+
if (isClassReference(value))
|
|
50
|
+
return value;
|
|
45
51
|
const existing = wrappers.get(propertyKey);
|
|
46
52
|
if (existing)
|
|
47
53
|
return existing;
|
|
@@ -60,16 +66,37 @@ export class InterceptorProcessor {
|
|
|
60
66
|
});
|
|
61
67
|
}
|
|
62
68
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
69
|
+
const receiver = !self.devtoolsTracer || targetBoundMethods.has(propertyKey)
|
|
70
|
+
? target
|
|
71
|
+
: proxyReceiver;
|
|
72
|
+
const apply = () => {
|
|
73
|
+
if (metadataByToken.size === 0)
|
|
74
|
+
return value.apply(receiver, args);
|
|
75
|
+
return self.callWithInterceptorChain({
|
|
76
|
+
target: receiver,
|
|
77
|
+
methodName: propertyKey,
|
|
78
|
+
moduleName,
|
|
79
|
+
args,
|
|
80
|
+
metadataByToken,
|
|
81
|
+
interceptors,
|
|
82
|
+
proceed: () => value.apply(receiver, args),
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
if (receiver === target)
|
|
86
|
+
return apply();
|
|
87
|
+
return applyWithPrivateFieldFallback(apply, () => {
|
|
88
|
+
targetBoundMethods.add(propertyKey);
|
|
89
|
+
if (metadataByToken.size === 0)
|
|
90
|
+
return value.apply(target, args);
|
|
91
|
+
return self.callWithInterceptorChain({
|
|
92
|
+
target,
|
|
93
|
+
methodName: propertyKey,
|
|
94
|
+
moduleName,
|
|
95
|
+
args,
|
|
96
|
+
metadataByToken,
|
|
97
|
+
interceptors,
|
|
98
|
+
proceed: () => value.apply(target, args),
|
|
99
|
+
});
|
|
73
100
|
});
|
|
74
101
|
};
|
|
75
102
|
wrappers.set(propertyKey, wrapped);
|
|
@@ -88,7 +115,6 @@ export class InterceptorProcessor {
|
|
|
88
115
|
if (metadata === undefined) {
|
|
89
116
|
return invoke(index + 1, next);
|
|
90
117
|
}
|
|
91
|
-
const interceptorName = current.constructor.name;
|
|
92
118
|
const callInterceptor = () => current.intercept({
|
|
93
119
|
target,
|
|
94
120
|
methodName,
|
|
@@ -98,15 +124,53 @@ export class InterceptorProcessor {
|
|
|
98
124
|
decoratorState: metadata.state,
|
|
99
125
|
proceed: () => invoke(index + 1, next),
|
|
100
126
|
});
|
|
101
|
-
|
|
127
|
+
const devtoolsTracer = this.devtoolsTracer;
|
|
128
|
+
if (!devtoolsTracer)
|
|
102
129
|
return callInterceptor();
|
|
103
|
-
|
|
130
|
+
let proceedDurationMs = 0;
|
|
131
|
+
const trackedProceed = () => {
|
|
132
|
+
const proceedStart = Date.now();
|
|
133
|
+
try {
|
|
134
|
+
const result = devtoolsTracer.runInCurrentSpan(() => invoke(index + 1, next));
|
|
135
|
+
if (isPromiseLike(result)) {
|
|
136
|
+
return Promise.resolve(result).finally(() => {
|
|
137
|
+
proceedDurationMs += Date.now() - proceedStart;
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
proceedDurationMs += Date.now() - proceedStart;
|
|
141
|
+
return result;
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
proceedDurationMs += Date.now() - proceedStart;
|
|
145
|
+
throw error;
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
const trackedCallInterceptor = () => current.intercept({
|
|
149
|
+
target,
|
|
150
|
+
methodName,
|
|
151
|
+
args,
|
|
152
|
+
moduleName,
|
|
153
|
+
metadata: metadata.method,
|
|
154
|
+
decoratorState: metadata.state,
|
|
155
|
+
proceed: trackedProceed,
|
|
156
|
+
});
|
|
157
|
+
return devtoolsTracer.recordSpan({
|
|
104
158
|
kind: "interceptor",
|
|
105
159
|
moduleName,
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
160
|
+
className: current.constructor.name,
|
|
161
|
+
registrationKey: current.constructor.name,
|
|
162
|
+
methodName: "intercept",
|
|
163
|
+
args: [
|
|
164
|
+
{
|
|
165
|
+
moduleName,
|
|
166
|
+
methodName,
|
|
167
|
+
decoratorName: metadata.state.decoratorNames.get(methodName) ??
|
|
168
|
+
extractDecoratorDescription(current.token.stateSymbol.description),
|
|
169
|
+
metadata: serializeMetadata(metadata.method),
|
|
170
|
+
},
|
|
171
|
+
],
|
|
172
|
+
getProceedDurationMs: () => proceedDurationMs,
|
|
173
|
+
callback: trackedCallInterceptor,
|
|
110
174
|
});
|
|
111
175
|
};
|
|
112
176
|
return invoke(0, proceed);
|
|
@@ -115,3 +179,55 @@ export class InterceptorProcessor {
|
|
|
115
179
|
return this.devtoolsProcessorRef.current?.tracer;
|
|
116
180
|
}
|
|
117
181
|
}
|
|
182
|
+
function applyWithPrivateFieldFallback(tryFn, fallbackFn) {
|
|
183
|
+
try {
|
|
184
|
+
const result = tryFn();
|
|
185
|
+
if (isPromiseLike(result)) {
|
|
186
|
+
return result.catch((error) => {
|
|
187
|
+
if (!isPrivateMemberAccessError(error))
|
|
188
|
+
throw error;
|
|
189
|
+
return fallbackFn();
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
return result;
|
|
193
|
+
}
|
|
194
|
+
catch (error) {
|
|
195
|
+
if (!isPrivateMemberAccessError(error))
|
|
196
|
+
throw error;
|
|
197
|
+
return fallbackFn();
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
function isPrivateMemberAccessError(error) {
|
|
201
|
+
return (error instanceof TypeError &&
|
|
202
|
+
/Cannot (read|access) private (member|method)/.test(error.message));
|
|
203
|
+
}
|
|
204
|
+
function serializeMetadata(value, depth = 0) {
|
|
205
|
+
if (depth > 5)
|
|
206
|
+
return "[...]";
|
|
207
|
+
if (value === null || value === undefined)
|
|
208
|
+
return value;
|
|
209
|
+
if (typeof value === "function")
|
|
210
|
+
return value.toString();
|
|
211
|
+
if (typeof value !== "object")
|
|
212
|
+
return value;
|
|
213
|
+
if (Array.isArray(value)) {
|
|
214
|
+
return value.map((item) => serializeMetadata(item, depth + 1));
|
|
215
|
+
}
|
|
216
|
+
const result = {};
|
|
217
|
+
for (const [key, val] of Object.entries(value)) {
|
|
218
|
+
result[key] = serializeMetadata(val, depth + 1);
|
|
219
|
+
}
|
|
220
|
+
return result;
|
|
221
|
+
}
|
|
222
|
+
function extractDecoratorDescription(description) {
|
|
223
|
+
if (!description)
|
|
224
|
+
return null;
|
|
225
|
+
const prefix = "DecoratorState:";
|
|
226
|
+
if (description.startsWith(prefix)) {
|
|
227
|
+
return description.slice(prefix.length);
|
|
228
|
+
}
|
|
229
|
+
return description;
|
|
230
|
+
}
|
|
231
|
+
function isClassReference(value) {
|
|
232
|
+
return Boolean(value.prototype && value.prototype.constructor === value);
|
|
233
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as Awilix from "awilix";
|
|
2
|
+
import type { DevtoolsProcessorRef } from "../../devtools/devtools.types.js";
|
|
2
3
|
import type { RegisteredModuleScope } from "../contexts/container-context-base.js";
|
|
3
4
|
import type { InternalModuleLike as M } from "../modules/runtime-module.types.js";
|
|
4
5
|
type KeyedFeatureKind = "queryPreHandlers" | "commandPreHandlers" | "interceptors" | "initializers";
|
|
@@ -10,9 +11,12 @@ type RegisterKeyedFeatureParams = {
|
|
|
10
11
|
};
|
|
11
12
|
export declare class KeyedFeatureRegistrar {
|
|
12
13
|
private readonly providerOptions;
|
|
13
|
-
|
|
14
|
+
private readonly devtoolsProcessorRef;
|
|
15
|
+
constructor(providerOptions: Partial<Awilix.BuildResolverOptions<any>>, devtoolsProcessorRef?: DevtoolsProcessorRef);
|
|
14
16
|
private readonly featureConfig;
|
|
15
17
|
register<T>({ featureKind, module, scope, importedModulesWithScope, }: RegisterKeyedFeatureParams): Map<string, () => T>;
|
|
18
|
+
private createFeatureResolver;
|
|
19
|
+
private get devtoolsTracer();
|
|
16
20
|
private getProviderOptions;
|
|
17
21
|
private ensureInitializerTokenUniqueness;
|
|
18
22
|
}
|
|
@@ -2,10 +2,13 @@ import * as Awilix from "awilix";
|
|
|
2
2
|
import * as ERRORS from "../errors.js";
|
|
3
3
|
import { ProviderResolver } from "../providers/provider-resolver.js";
|
|
4
4
|
import { getOrCreateRequestScope, resolveFromRequestScope, } from "../request-scope-context.js";
|
|
5
|
+
import { hasUseClass } from "../type-guards.js";
|
|
5
6
|
export class KeyedFeatureRegistrar {
|
|
6
7
|
providerOptions;
|
|
7
|
-
|
|
8
|
+
devtoolsProcessorRef;
|
|
9
|
+
constructor(providerOptions, devtoolsProcessorRef = {}) {
|
|
8
10
|
this.providerOptions = providerOptions;
|
|
11
|
+
this.devtoolsProcessorRef = devtoolsProcessorRef;
|
|
9
12
|
}
|
|
10
13
|
featureConfig = {
|
|
11
14
|
queryPreHandlers: {
|
|
@@ -42,10 +45,13 @@ export class KeyedFeatureRegistrar {
|
|
|
42
45
|
}
|
|
43
46
|
const symbol = Symbol(`${featureKind}_export_${importedModule.name}_${key}`);
|
|
44
47
|
scope.register({
|
|
45
|
-
[symbol]:
|
|
46
|
-
|
|
47
|
-
|
|
48
|
+
[symbol]: this.createFeatureResolver({
|
|
49
|
+
feature,
|
|
50
|
+
featureKind,
|
|
51
|
+
key,
|
|
48
52
|
module: importedModule,
|
|
53
|
+
providerResolver,
|
|
54
|
+
resolutionScope: importedModuleScope.scope,
|
|
49
55
|
wrapForExport: true,
|
|
50
56
|
}),
|
|
51
57
|
});
|
|
@@ -70,10 +76,13 @@ export class KeyedFeatureRegistrar {
|
|
|
70
76
|
}
|
|
71
77
|
const symbol = Symbol(`${featureKind}_${module.name}_${key}`);
|
|
72
78
|
scope.register({
|
|
73
|
-
[symbol]:
|
|
74
|
-
|
|
75
|
-
|
|
79
|
+
[symbol]: this.createFeatureResolver({
|
|
80
|
+
feature,
|
|
81
|
+
featureKind,
|
|
82
|
+
key,
|
|
76
83
|
module,
|
|
84
|
+
providerResolver,
|
|
85
|
+
resolutionScope: scope,
|
|
77
86
|
}),
|
|
78
87
|
});
|
|
79
88
|
if (featureKind === "initializers") {
|
|
@@ -92,6 +101,28 @@ export class KeyedFeatureRegistrar {
|
|
|
92
101
|
}
|
|
93
102
|
return resolverMap;
|
|
94
103
|
}
|
|
104
|
+
createFeatureResolver({ feature, featureKind, key, module, providerResolver, resolutionScope, wrapForExport = false, }) {
|
|
105
|
+
const resolver = providerResolver.resolveClassProvider({
|
|
106
|
+
provider: feature,
|
|
107
|
+
resolutionScope,
|
|
108
|
+
module,
|
|
109
|
+
wrapForExport,
|
|
110
|
+
});
|
|
111
|
+
if (!isPreHandlerFeature(featureKind) || !this.devtoolsTracer) {
|
|
112
|
+
return resolver;
|
|
113
|
+
}
|
|
114
|
+
return this.devtoolsTracer.wrapResolver({
|
|
115
|
+
kind: "prehandler",
|
|
116
|
+
module,
|
|
117
|
+
options: this.getProviderOptions(featureKind),
|
|
118
|
+
className: getFeatureClassName(feature, key),
|
|
119
|
+
registrationKey: key,
|
|
120
|
+
resolver,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
get devtoolsTracer() {
|
|
124
|
+
return this.devtoolsProcessorRef.current?.tracer;
|
|
125
|
+
}
|
|
95
126
|
getProviderOptions(featureKind) {
|
|
96
127
|
if (featureKind === "initializers") {
|
|
97
128
|
return {
|
|
@@ -110,3 +141,13 @@ export class KeyedFeatureRegistrar {
|
|
|
110
141
|
ownerByInitializerToken.set(token, moduleName);
|
|
111
142
|
}
|
|
112
143
|
}
|
|
144
|
+
function isPreHandlerFeature(featureKind) {
|
|
145
|
+
return (featureKind === "queryPreHandlers" || featureKind === "commandPreHandlers");
|
|
146
|
+
}
|
|
147
|
+
function getFeatureClassName(feature, key) {
|
|
148
|
+
if (typeof feature === "function")
|
|
149
|
+
return feature.name || key;
|
|
150
|
+
if (hasUseClass(feature))
|
|
151
|
+
return feature.useClass.name || key;
|
|
152
|
+
return key;
|
|
153
|
+
}
|
|
@@ -53,22 +53,25 @@ export class ProviderResolver {
|
|
|
53
53
|
}
|
|
54
54
|
const resolverOptions = this.extractResolverOptions(module, provider);
|
|
55
55
|
if (GUARGS.isCostructorProvider(provider)) {
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
56
|
+
const baseResolver = Awilix.asClass(provider, resolverOptions);
|
|
57
|
+
const tracedResolver = !this.devtoolsTracer
|
|
58
|
+
? baseResolver
|
|
59
|
+
: this.devtoolsTracer.wrapResolver({
|
|
60
|
+
kind: "provider",
|
|
59
61
|
module,
|
|
60
|
-
|
|
62
|
+
moduleId,
|
|
61
63
|
options: resolverOptions,
|
|
64
|
+
className: provider.name,
|
|
65
|
+
registrationKey: key,
|
|
66
|
+
resolver: baseResolver,
|
|
62
67
|
});
|
|
63
|
-
if (!this.
|
|
64
|
-
return
|
|
65
|
-
return this.
|
|
66
|
-
kind: "provider",
|
|
68
|
+
if (!this.interceptorProcessor)
|
|
69
|
+
return tracedResolver;
|
|
70
|
+
return this.interceptorProcessor.createInterceptedProviderResolver({
|
|
67
71
|
module,
|
|
68
|
-
|
|
72
|
+
useClass: provider,
|
|
69
73
|
options: resolverOptions,
|
|
70
|
-
|
|
71
|
-
resolver,
|
|
74
|
+
resolver: tracedResolver,
|
|
72
75
|
});
|
|
73
76
|
}
|
|
74
77
|
if (GUARGS.isFactoryProvider(provider)) {
|
|
@@ -89,7 +92,8 @@ export class ProviderResolver {
|
|
|
89
92
|
module,
|
|
90
93
|
moduleId,
|
|
91
94
|
options: resolverOptions,
|
|
92
|
-
|
|
95
|
+
className: key,
|
|
96
|
+
registrationKey: key,
|
|
93
97
|
isFactory: true,
|
|
94
98
|
resolver,
|
|
95
99
|
});
|
|
@@ -98,23 +102,24 @@ export class ProviderResolver {
|
|
|
98
102
|
const classResolver = provider.allowCircular
|
|
99
103
|
? this.createProxyResolver(baseResolver, resolverOptions)
|
|
100
104
|
: baseResolver;
|
|
101
|
-
const
|
|
105
|
+
const tracedResolver = !this.devtoolsTracer
|
|
102
106
|
? classResolver
|
|
103
|
-
: this.
|
|
107
|
+
: this.devtoolsTracer.wrapResolver({
|
|
108
|
+
kind: "provider",
|
|
104
109
|
module,
|
|
105
|
-
|
|
110
|
+
moduleId,
|
|
106
111
|
options: resolverOptions,
|
|
112
|
+
className: provider.useClass.name,
|
|
113
|
+
registrationKey: key,
|
|
107
114
|
resolver: classResolver,
|
|
108
115
|
});
|
|
109
|
-
if (!this.
|
|
110
|
-
return
|
|
111
|
-
return this.
|
|
112
|
-
kind: "provider",
|
|
116
|
+
if (!this.interceptorProcessor)
|
|
117
|
+
return tracedResolver;
|
|
118
|
+
return this.interceptorProcessor.createInterceptedProviderResolver({
|
|
113
119
|
module,
|
|
114
|
-
|
|
120
|
+
useClass: provider.useClass,
|
|
115
121
|
options: resolverOptions,
|
|
116
|
-
|
|
117
|
-
resolver,
|
|
122
|
+
resolver: tracedResolver,
|
|
118
123
|
});
|
|
119
124
|
}
|
|
120
125
|
async resolveProviderAsync({ key, moduleId, provider, resolutionScope, module, }) {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export interface RouteRegistration {
|
|
2
2
|
method: string;
|
|
3
|
+
operationId: string;
|
|
3
4
|
path: string;
|
|
4
5
|
schema: RouteSchema;
|
|
5
6
|
}
|
|
@@ -12,13 +13,12 @@ export interface RouteSchema {
|
|
|
12
13
|
description?: string;
|
|
13
14
|
summary?: string;
|
|
14
15
|
tags?: string[];
|
|
15
|
-
operationId?: string;
|
|
16
16
|
deprecated?: boolean;
|
|
17
17
|
}
|
|
18
18
|
export declare function hasValidationSchema(schema: RouteSchema): boolean;
|
|
19
19
|
export declare class OpenAPIBuilder {
|
|
20
20
|
private routes;
|
|
21
|
-
registerRoute(method: string, path: string, schema: RouteSchema): void;
|
|
21
|
+
registerRoute(method: string, path: string, handlerName: string, schema: RouteSchema): void;
|
|
22
22
|
buildPaths(): Record<string, any>;
|
|
23
23
|
private buildOperation;
|
|
24
24
|
private buildParameters;
|
|
@@ -6,17 +6,22 @@ export function hasValidationSchema(schema) {
|
|
|
6
6
|
}
|
|
7
7
|
export class OpenAPIBuilder {
|
|
8
8
|
routes = [];
|
|
9
|
-
registerRoute(method, path, schema) {
|
|
10
|
-
this.routes.push({
|
|
9
|
+
registerRoute(method, path, handlerName, schema) {
|
|
10
|
+
this.routes.push({
|
|
11
|
+
method: method.toLowerCase(),
|
|
12
|
+
operationId: handlerName,
|
|
13
|
+
path,
|
|
14
|
+
schema,
|
|
15
|
+
});
|
|
11
16
|
}
|
|
12
17
|
buildPaths() {
|
|
13
18
|
return this.routes.reduce((paths, route) => {
|
|
14
19
|
paths[route.path] ??= {};
|
|
15
|
-
paths[route.path][route.method] = this.buildOperation(route.schema);
|
|
20
|
+
paths[route.path][route.method] = this.buildOperation(route.operationId, route.schema);
|
|
16
21
|
return paths;
|
|
17
22
|
}, {});
|
|
18
23
|
}
|
|
19
|
-
buildOperation(schema) {
|
|
24
|
+
buildOperation(operationId, schema) {
|
|
20
25
|
const parameters = this.buildParameters(schema);
|
|
21
26
|
const requestBody = this.buildRequestBody(schema);
|
|
22
27
|
const responses = this.buildResponses(schema);
|
|
@@ -24,7 +29,7 @@ export class OpenAPIBuilder {
|
|
|
24
29
|
...(schema.summary && { summary: schema.summary }),
|
|
25
30
|
...(schema.description && { description: schema.description }),
|
|
26
31
|
...(schema.tags && { tags: schema.tags }),
|
|
27
|
-
|
|
32
|
+
operationId,
|
|
28
33
|
...(schema.deprecated && { deprecated: schema.deprecated }),
|
|
29
34
|
...(parameters.length > 0 && { parameters }),
|
|
30
35
|
...(requestBody && { requestBody }),
|
|
@@ -9,12 +9,13 @@ export declare class Mediator<C extends AnyContract> {
|
|
|
9
9
|
private handlerType;
|
|
10
10
|
private moduleName;
|
|
11
11
|
private devtoolsProcessorRef;
|
|
12
|
-
constructor(middlewareResolvers: MiddlewareResolverMap, moduleName: string, handlerType
|
|
12
|
+
constructor(middlewareResolvers: MiddlewareResolverMap, moduleName: string, handlerType?: string, devtoolsProcessorRef?: DevtoolsProcessorRef);
|
|
13
13
|
register(key: string, executor: Executor): void;
|
|
14
14
|
execute<K extends C["key"], Name extends ExtractScenarioName<C, K>>(key: K, payload: ExtractPayload<C, K>, options: ExecuteRuntimeOptions<C, K> & {
|
|
15
15
|
scenario: Name;
|
|
16
16
|
}): Promise<ExecuteResponseByScenario<C, K, Name>>;
|
|
17
17
|
execute<K extends C["key"], TOptions extends ExecuteRuntimeOptions<C, K> = ExecuteRuntimeOptions<C, K>>(key: K, payload: ExtractPayload<C, K>, ...args: ExecuteArgs<C, K, TOptions>): Promise<ExecuteResponse<C, K>>;
|
|
18
|
+
private getMediatorName;
|
|
18
19
|
private executeMiddlewares;
|
|
19
20
|
private processMiddlewareResult;
|
|
20
21
|
private resolveApplicableMiddlewares;
|
|
@@ -7,7 +7,7 @@ export class Mediator {
|
|
|
7
7
|
handlerType;
|
|
8
8
|
moduleName;
|
|
9
9
|
devtoolsProcessorRef;
|
|
10
|
-
constructor(middlewareResolvers, moduleName, handlerType = "handler", devtoolsProcessorRef) {
|
|
10
|
+
constructor(middlewareResolvers, moduleName, handlerType = "handler", devtoolsProcessorRef = {}) {
|
|
11
11
|
this.handlerType = handlerType;
|
|
12
12
|
this.moduleName = moduleName;
|
|
13
13
|
this.middlewareResolvers = middlewareResolvers;
|
|
@@ -21,13 +21,13 @@ export class Mediator {
|
|
|
21
21
|
this.handlers.set(keyStr, executor);
|
|
22
22
|
}
|
|
23
23
|
async execute(key, payload, options) {
|
|
24
|
-
|
|
24
|
+
const execute = () => runInRequestScopeContext(async () => {
|
|
25
25
|
const keyStr = String(key);
|
|
26
26
|
const executor = this.handlers.get(keyStr);
|
|
27
27
|
if (!executor) {
|
|
28
28
|
throw new errors.HandlerNotRegisteredError(keyStr, this.moduleName);
|
|
29
29
|
}
|
|
30
|
-
const { executionContext, includePreHandlerKeys, excludePreHandlerKeys } = options ?? {};
|
|
30
|
+
const { executionContext, includePreHandlerKeys, excludePreHandlerKeys, } = options ?? {};
|
|
31
31
|
const applicableMiddlewares = this.resolveApplicableMiddlewares({
|
|
32
32
|
excludePreHandlerKeys,
|
|
33
33
|
includePreHandlerKeys,
|
|
@@ -44,6 +44,25 @@ export class Mediator {
|
|
|
44
44
|
}
|
|
45
45
|
return handlerResult;
|
|
46
46
|
});
|
|
47
|
+
return (this.devtoolsProcessorRef.current?.tracer.recordSpan({
|
|
48
|
+
kind: "mediator",
|
|
49
|
+
moduleName: this.moduleName,
|
|
50
|
+
className: this.getMediatorName(),
|
|
51
|
+
registrationKey: this.getMediatorName(),
|
|
52
|
+
methodName: "execute",
|
|
53
|
+
args: [key, payload, options],
|
|
54
|
+
callback: execute,
|
|
55
|
+
}) ?? execute());
|
|
56
|
+
}
|
|
57
|
+
getMediatorName() {
|
|
58
|
+
switch (this.handlerType) {
|
|
59
|
+
case "query":
|
|
60
|
+
return "QueryMediator";
|
|
61
|
+
case "command":
|
|
62
|
+
return "CommandMediator";
|
|
63
|
+
default:
|
|
64
|
+
return "Mediator";
|
|
65
|
+
}
|
|
47
66
|
}
|
|
48
67
|
async executeMiddlewares(middlewares, payload, executionContext) {
|
|
49
68
|
let context = {};
|
|
@@ -52,7 +71,8 @@ export class Mediator {
|
|
|
52
71
|
const result = await (this.devtoolsProcessorRef.current?.tracer.recordSpan({
|
|
53
72
|
kind: "prehandler",
|
|
54
73
|
moduleName: this.moduleName,
|
|
55
|
-
|
|
74
|
+
className: middleware.constructor.name,
|
|
75
|
+
registrationKey: middleware.constructor.name,
|
|
56
76
|
methodName: "execute",
|
|
57
77
|
args: [payload, context, executionContext],
|
|
58
78
|
callback: () => middleware.execute(payload, context, executionContext),
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { DecoratorState, DecoratorToken } from "../decorators/decorator-state.types.js";
|
|
2
|
+
export type HttpOperationRef = {
|
|
3
|
+
operationId: string;
|
|
4
|
+
serviceName: string;
|
|
5
|
+
transport: "http";
|
|
6
|
+
};
|
|
7
|
+
export type MessagingOperationRef = {
|
|
8
|
+
serviceName: string;
|
|
9
|
+
transport: "messaging";
|
|
10
|
+
type: string;
|
|
11
|
+
};
|
|
12
|
+
export type OperationRef = HttpOperationRef | MessagingOperationRef;
|
|
13
|
+
export type MessagingOperationInput = Omit<MessagingOperationRef, "transport">;
|
|
14
|
+
export type LocalMessagingOperationRef = Omit<MessagingOperationRef, "serviceName">;
|
|
15
|
+
export type LocalMessagingOperationInput = Pick<MessagingOperationRef, "type">;
|
|
16
|
+
export interface OperationCallDecoratorState extends DecoratorState<OperationRef[]> {
|
|
17
|
+
}
|
|
18
|
+
export interface OperationPublishDecoratorState extends DecoratorState<LocalMessagingOperationRef[]> {
|
|
19
|
+
}
|
|
20
|
+
export declare const OPERATION_CALL_DECORATOR_STATE_TOKEN: DecoratorToken<OperationCallDecoratorState>;
|
|
21
|
+
export declare const OPERATION_PUBLISH_DECORATOR_STATE_TOKEN: DecoratorToken<OperationPublishDecoratorState>;
|
|
22
|
+
export declare function callsOperation(operation: HttpOperationRef | MessagingOperationInput): <This, Args extends unknown[], Result>(_target: (this: This, ...args: Args) => Result, context: ClassMethodDecoratorContext<This, (this: This, ...args: Args) => Result>) => void;
|
|
23
|
+
export declare function publishesOperation(operation: LocalMessagingOperationInput): <This, Args extends unknown[], Result>(_target: (this: This, ...args: Args) => Result, context: ClassMethodDecoratorContext<This, (this: This, ...args: Args) => Result>) => void;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { createDecoratorStateUpdater } from "../decorators/decorator-state.js";
|
|
2
|
+
const callState = createDecoratorStateUpdater("operation-calls", {
|
|
3
|
+
method: () => [],
|
|
4
|
+
});
|
|
5
|
+
const publishState = createDecoratorStateUpdater("operation-publications", {
|
|
6
|
+
method: () => [],
|
|
7
|
+
});
|
|
8
|
+
export const OPERATION_CALL_DECORATOR_STATE_TOKEN = callState.token;
|
|
9
|
+
export const OPERATION_PUBLISH_DECORATOR_STATE_TOKEN = publishState.token;
|
|
10
|
+
export function callsOperation(operation) {
|
|
11
|
+
const operationRef = "operationId" in operation
|
|
12
|
+
? operation
|
|
13
|
+
: { ...operation, transport: "messaging" };
|
|
14
|
+
return (_target, context) => {
|
|
15
|
+
callState.update(context, {
|
|
16
|
+
method: (operations) => [...operations, operationRef],
|
|
17
|
+
}, callsOperation.name);
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export function publishesOperation(operation) {
|
|
21
|
+
const operationRef = {
|
|
22
|
+
...operation,
|
|
23
|
+
transport: "messaging",
|
|
24
|
+
};
|
|
25
|
+
return (_target, context) => {
|
|
26
|
+
publishState.update(context, {
|
|
27
|
+
method: (operations) => [...operations, operationRef],
|
|
28
|
+
}, publishesOperation.name);
|
|
29
|
+
};
|
|
30
|
+
}
|