@treatwell/moleculer-call-wrapper 1.0.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/LICENSE +21 -0
- package/README.md +216 -0
- package/dist/index.cjs +698 -0
- package/dist/index.d.cts +38 -0
- package/dist/index.d.mts +38 -0
- package/dist/index.mjs +694 -0
- package/package.json +58 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,698 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var promises = require('node:fs/promises');
|
|
4
|
+
var typescript = require('typescript');
|
|
5
|
+
var node_fs = require('node:fs');
|
|
6
|
+
var node_path = require('node:path');
|
|
7
|
+
var crypto = require('node:crypto');
|
|
8
|
+
var tsCloneNode = require('ts-clone-node');
|
|
9
|
+
|
|
10
|
+
const MOLECULER_NAME = "m";
|
|
11
|
+
function fillImports(context, sourceFile, node) {
|
|
12
|
+
node.forEachChild((n) => fillImports(context, sourceFile, n));
|
|
13
|
+
if (!typescript.isTypeReferenceNode(node)) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
let nodeName = node.typeName.getText();
|
|
17
|
+
if (typescript.isQualifiedName(node.typeName)) {
|
|
18
|
+
nodeName = node.typeName.left.getText();
|
|
19
|
+
}
|
|
20
|
+
let importName = node.typeName.getText();
|
|
21
|
+
const importDeclaration = sourceFile.statements.filter(typescript.isImportDeclaration).find((n) => {
|
|
22
|
+
if (n.importClause?.namedBindings && typescript.isNamedImports(n.importClause.namedBindings)) {
|
|
23
|
+
const importIdentifier = n.importClause.namedBindings.elements.find(
|
|
24
|
+
(c) => c.name.getText() === nodeName
|
|
25
|
+
);
|
|
26
|
+
if (importIdentifier?.propertyName) {
|
|
27
|
+
importName = importIdentifier.propertyName.getText();
|
|
28
|
+
}
|
|
29
|
+
return !!importIdentifier;
|
|
30
|
+
}
|
|
31
|
+
return false;
|
|
32
|
+
});
|
|
33
|
+
if (importDeclaration) {
|
|
34
|
+
let key = importDeclaration.moduleSpecifier.getText().slice(1, -1);
|
|
35
|
+
if (key.startsWith(".")) {
|
|
36
|
+
key = `./${node_path.relative(
|
|
37
|
+
node_path.dirname(context.wrapperPath),
|
|
38
|
+
node_path.resolve(node_path.dirname(context.currentFilePath), key)
|
|
39
|
+
).replace(/\\/g, "/")}`;
|
|
40
|
+
}
|
|
41
|
+
const name = addDepToImports(context.imports, key);
|
|
42
|
+
context.importMapping.set(node, `${name}.${importName}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function getUniqueNameFromKey(key) {
|
|
46
|
+
switch (key) {
|
|
47
|
+
case "@wavyapp/wavy-sdk":
|
|
48
|
+
return "sdk";
|
|
49
|
+
case "moleculer":
|
|
50
|
+
return MOLECULER_NAME;
|
|
51
|
+
default:
|
|
52
|
+
if (/^[a-zA-Z]\w+$/.test(key)) {
|
|
53
|
+
return key;
|
|
54
|
+
}
|
|
55
|
+
return `s${crypto.createHash("sha1").update(key).digest("hex").slice(0, 7)}`;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function addDepToImports(imports, key) {
|
|
59
|
+
let name = imports.get(key);
|
|
60
|
+
if (!name) {
|
|
61
|
+
name = getUniqueNameFromKey(key);
|
|
62
|
+
imports.set(key, name);
|
|
63
|
+
}
|
|
64
|
+
return name;
|
|
65
|
+
}
|
|
66
|
+
function importMappingCloneHook(importMapping) {
|
|
67
|
+
return (n) => {
|
|
68
|
+
if (typescript.isTypeReferenceNode(n) && importMapping.has(n)) {
|
|
69
|
+
return {
|
|
70
|
+
typeName: () => typescript.factory.createIdentifier(importMapping.get(n))
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
return void 0;
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function parseService(context, service) {
|
|
78
|
+
const actions = [];
|
|
79
|
+
const sourceFile = typescript.createSourceFile(
|
|
80
|
+
"service.ts",
|
|
81
|
+
node_fs.readFileSync(context.currentFilePath, "utf8"),
|
|
82
|
+
typescript.ScriptTarget.ESNext,
|
|
83
|
+
true
|
|
84
|
+
);
|
|
85
|
+
const handlers = findActionsTypes(context, sourceFile);
|
|
86
|
+
const version = service.version ? `v${service.version}.` : "";
|
|
87
|
+
Object.entries(service.schema.actions || {}).forEach(
|
|
88
|
+
([actionName, action]) => {
|
|
89
|
+
if (action === false || typeof action === "object" && action.visibility === "private") {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const { params, returnType, typeParameters } = handlers.get(actionName) || {};
|
|
93
|
+
if (typeParameters) {
|
|
94
|
+
typeParameters.forEach((tp) => fillImports(context, sourceFile, tp));
|
|
95
|
+
}
|
|
96
|
+
if (returnType) {
|
|
97
|
+
fillImports(context, sourceFile, returnType);
|
|
98
|
+
}
|
|
99
|
+
if (params) {
|
|
100
|
+
fillImports(context, sourceFile, params);
|
|
101
|
+
}
|
|
102
|
+
actions.push({
|
|
103
|
+
actionName: `${version}${service.name}.${actionName}`,
|
|
104
|
+
params,
|
|
105
|
+
returnType,
|
|
106
|
+
typeParameters
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
);
|
|
110
|
+
context.builtins.forEach(
|
|
111
|
+
(injectBuiltin) => injectBuiltin(context, actions, service, sourceFile)
|
|
112
|
+
);
|
|
113
|
+
return actions;
|
|
114
|
+
}
|
|
115
|
+
function findActionsTypes(context, sourceFile) {
|
|
116
|
+
const handlers = /* @__PURE__ */ new Map();
|
|
117
|
+
function visit(n) {
|
|
118
|
+
if (!typescript.isMethodDeclaration(n) || n.name.getText() !== "handler" || n.parameters.length !== 1) {
|
|
119
|
+
return n.forEachChild(visit);
|
|
120
|
+
}
|
|
121
|
+
if (!typescript.isPropertyAssignment(n.parent.parent)) {
|
|
122
|
+
return n.forEachChild(visit);
|
|
123
|
+
}
|
|
124
|
+
const ctxType = n.parameters[0].type;
|
|
125
|
+
if (!ctxType || !typescript.isTypeReferenceNode(ctxType)) {
|
|
126
|
+
return n.forEachChild(visit);
|
|
127
|
+
}
|
|
128
|
+
let paramsType;
|
|
129
|
+
if (ctxType.typeArguments?.length && ctxType.typeArguments[0].getText() !== "never") {
|
|
130
|
+
[paramsType] = ctxType.typeArguments;
|
|
131
|
+
}
|
|
132
|
+
handlers.set(n.parent.parent.name.getText(), {
|
|
133
|
+
params: paramsType,
|
|
134
|
+
returnType: n.type,
|
|
135
|
+
typeParameters: n.typeParameters
|
|
136
|
+
});
|
|
137
|
+
return void 0;
|
|
138
|
+
}
|
|
139
|
+
sourceFile.forEachChild(visit);
|
|
140
|
+
return handlers;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const ParamsActions = "Actions";
|
|
144
|
+
const NoParamsActions = "ActionsU";
|
|
145
|
+
function createWrapperParameters(action, importMapping, actionTypeGenericName) {
|
|
146
|
+
let actionParamType;
|
|
147
|
+
if (actionTypeGenericName) {
|
|
148
|
+
actionParamType = typescript.factory.createTypeReferenceNode(
|
|
149
|
+
typescript.factory.createIdentifier(actionTypeGenericName),
|
|
150
|
+
void 0
|
|
151
|
+
);
|
|
152
|
+
} else if (action.actionName) {
|
|
153
|
+
actionParamType = typescript.factory.createLiteralTypeNode(
|
|
154
|
+
typescript.factory.createStringLiteral(action.actionName, true)
|
|
155
|
+
);
|
|
156
|
+
} else {
|
|
157
|
+
actionParamType = typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.StringKeyword);
|
|
158
|
+
}
|
|
159
|
+
return [
|
|
160
|
+
typescript.factory.createParameterDeclaration(
|
|
161
|
+
void 0,
|
|
162
|
+
void 0,
|
|
163
|
+
"ctx",
|
|
164
|
+
void 0,
|
|
165
|
+
typescript.factory.createTypeReferenceNode(`${MOLECULER_NAME}.Context`)
|
|
166
|
+
),
|
|
167
|
+
typescript.factory.createParameterDeclaration(
|
|
168
|
+
void 0,
|
|
169
|
+
void 0,
|
|
170
|
+
"action",
|
|
171
|
+
void 0,
|
|
172
|
+
actionParamType
|
|
173
|
+
),
|
|
174
|
+
typescript.factory.createParameterDeclaration(
|
|
175
|
+
void 0,
|
|
176
|
+
void 0,
|
|
177
|
+
"params",
|
|
178
|
+
action.params ? void 0 : typescript.factory.createToken(typescript.SyntaxKind.QuestionToken),
|
|
179
|
+
tsCloneNode.cloneNode(action.params, {
|
|
180
|
+
hook: importMappingCloneHook(importMapping)
|
|
181
|
+
}) || typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UndefinedKeyword)
|
|
182
|
+
),
|
|
183
|
+
typescript.factory.createParameterDeclaration(
|
|
184
|
+
void 0,
|
|
185
|
+
void 0,
|
|
186
|
+
"meta",
|
|
187
|
+
typescript.factory.createToken(typescript.SyntaxKind.QuestionToken),
|
|
188
|
+
typescript.factory.createTypeReferenceNode(`${MOLECULER_NAME}.CallingOptions`)
|
|
189
|
+
)
|
|
190
|
+
];
|
|
191
|
+
}
|
|
192
|
+
function createWrapperReturnType(action, importMapping) {
|
|
193
|
+
if (!action.returnType) {
|
|
194
|
+
return typescript.factory.createTypeReferenceNode("Promise", [
|
|
195
|
+
typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.VoidKeyword)
|
|
196
|
+
]);
|
|
197
|
+
}
|
|
198
|
+
if (typescript.isTypeReferenceNode(action.returnType) && action.returnType.getSourceFile() && action.returnType.typeName.getText() === "Promise") {
|
|
199
|
+
return tsCloneNode.cloneNode(action.returnType, {
|
|
200
|
+
hook: importMappingCloneHook(importMapping)
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
return typescript.factory.createTypeReferenceNode("Promise", [
|
|
204
|
+
tsCloneNode.cloneNode(action.returnType, {
|
|
205
|
+
hook: importMappingCloneHook(importMapping)
|
|
206
|
+
})
|
|
207
|
+
]);
|
|
208
|
+
}
|
|
209
|
+
function createUnwrapReturnType(action, importMapping) {
|
|
210
|
+
if (!action.returnType) {
|
|
211
|
+
return typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.VoidKeyword);
|
|
212
|
+
}
|
|
213
|
+
if (typescript.isTypeReferenceNode(action.returnType) && action.returnType.getSourceFile() && action.returnType.typeName.getText() === "Promise" && action.returnType.typeArguments?.length) {
|
|
214
|
+
return tsCloneNode.cloneNode(action.returnType.typeArguments[0], {
|
|
215
|
+
hook: importMappingCloneHook(importMapping)
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
return tsCloneNode.cloneNode(action.returnType, {
|
|
219
|
+
hook: importMappingCloneHook(importMapping)
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
function createWrapperTypeParameters(action, importMapping) {
|
|
223
|
+
if (!action.typeParameters) {
|
|
224
|
+
return [];
|
|
225
|
+
}
|
|
226
|
+
return action.typeParameters.map(
|
|
227
|
+
(tp) => tsCloneNode.cloneNode(tp, { hook: importMappingCloneHook(importMapping) })
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
function createWrapperFunctionOverload(action, importMapping, name, actionTypeGenericName, block) {
|
|
231
|
+
return typescript.factory.createFunctionDeclaration(
|
|
232
|
+
[typescript.factory.createModifier(typescript.SyntaxKind.ExportKeyword)],
|
|
233
|
+
void 0,
|
|
234
|
+
name,
|
|
235
|
+
createWrapperTypeParameters(action, importMapping),
|
|
236
|
+
createWrapperParameters(action, importMapping, actionTypeGenericName),
|
|
237
|
+
createWrapperReturnType(action, importMapping),
|
|
238
|
+
block
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
function findUnusedTemplateName(action) {
|
|
242
|
+
const typeNames = new Set(action.typeParameters?.map((t) => t.name.getText()));
|
|
243
|
+
let actionTemplateName = "N";
|
|
244
|
+
while (typeNames.has(actionTemplateName)) {
|
|
245
|
+
actionTemplateName += "N";
|
|
246
|
+
}
|
|
247
|
+
return actionTemplateName;
|
|
248
|
+
}
|
|
249
|
+
function buildCallWrapperFile(actions, imports, importMapping) {
|
|
250
|
+
const stmts = [];
|
|
251
|
+
let sortedImports = [...imports.keys()].sort();
|
|
252
|
+
sortedImports = [
|
|
253
|
+
...sortedImports.filter((d) => d.startsWith("@")),
|
|
254
|
+
...sortedImports.filter((d) => !d.startsWith(".") && !d.startsWith("@")),
|
|
255
|
+
...sortedImports.filter((d) => d.startsWith("."))
|
|
256
|
+
];
|
|
257
|
+
for (const importPath of sortedImports) {
|
|
258
|
+
const importName = imports.get(importPath);
|
|
259
|
+
stmts.push(
|
|
260
|
+
typescript.factory.createImportDeclaration(
|
|
261
|
+
void 0,
|
|
262
|
+
typescript.factory.createImportClause(
|
|
263
|
+
true,
|
|
264
|
+
void 0,
|
|
265
|
+
typescript.factory.createNamespaceImport(typescript.factory.createIdentifier(importName))
|
|
266
|
+
),
|
|
267
|
+
typescript.factory.createStringLiteral(importPath, true)
|
|
268
|
+
)
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
const sortedActions = actions.sort(
|
|
272
|
+
(a, b) => (a.actionName || "").localeCompare(b.actionName || "")
|
|
273
|
+
);
|
|
274
|
+
const actionsProperties = [];
|
|
275
|
+
const actionsNoParamsProperties = [];
|
|
276
|
+
stmts.push(
|
|
277
|
+
typescript.factory.createInterfaceDeclaration(
|
|
278
|
+
void 0,
|
|
279
|
+
typescript.factory.createIdentifier(ParamsActions),
|
|
280
|
+
void 0,
|
|
281
|
+
void 0,
|
|
282
|
+
actionsProperties
|
|
283
|
+
)
|
|
284
|
+
);
|
|
285
|
+
stmts.push(
|
|
286
|
+
typescript.factory.createInterfaceDeclaration(
|
|
287
|
+
void 0,
|
|
288
|
+
typescript.factory.createIdentifier(NoParamsActions),
|
|
289
|
+
void 0,
|
|
290
|
+
void 0,
|
|
291
|
+
actionsNoParamsProperties
|
|
292
|
+
)
|
|
293
|
+
);
|
|
294
|
+
const callTStmts = [];
|
|
295
|
+
const callStmts = [];
|
|
296
|
+
for (const action of sortedActions) {
|
|
297
|
+
if (action.typeParameters?.length) {
|
|
298
|
+
const templateTypes = createWrapperTypeParameters(action, importMapping);
|
|
299
|
+
if (action.params) {
|
|
300
|
+
const actionTemplateName = findUnusedTemplateName(action);
|
|
301
|
+
action.params = typescript.factory.createConditionalTypeNode(
|
|
302
|
+
typescript.factory.createTypeReferenceNode(
|
|
303
|
+
typescript.factory.createIdentifier(actionTemplateName),
|
|
304
|
+
void 0
|
|
305
|
+
),
|
|
306
|
+
typescript.factory.createLiteralTypeNode(
|
|
307
|
+
typescript.factory.createStringLiteral(action.actionName || "", true)
|
|
308
|
+
),
|
|
309
|
+
action.params,
|
|
310
|
+
typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.NeverKeyword)
|
|
311
|
+
);
|
|
312
|
+
action.typeParameters = typescript.factory.createNodeArray([
|
|
313
|
+
...templateTypes,
|
|
314
|
+
typescript.factory.createTypeParameterDeclaration(
|
|
315
|
+
void 0,
|
|
316
|
+
typescript.factory.createIdentifier(actionTemplateName),
|
|
317
|
+
typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.StringKeyword),
|
|
318
|
+
typescript.factory.createLiteralTypeNode(
|
|
319
|
+
typescript.factory.createStringLiteral(action.actionName || "", true)
|
|
320
|
+
)
|
|
321
|
+
)
|
|
322
|
+
]);
|
|
323
|
+
callTStmts.push(
|
|
324
|
+
createWrapperFunctionOverload(
|
|
325
|
+
action,
|
|
326
|
+
importMapping,
|
|
327
|
+
"callT",
|
|
328
|
+
actionTemplateName
|
|
329
|
+
)
|
|
330
|
+
);
|
|
331
|
+
} else {
|
|
332
|
+
callStmts.push(
|
|
333
|
+
createWrapperFunctionOverload(action, importMapping, "call")
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
} else if (action.params) {
|
|
337
|
+
actionsProperties.push(
|
|
338
|
+
typescript.factory.createPropertySignature(
|
|
339
|
+
void 0,
|
|
340
|
+
typescript.factory.createStringLiteral(action.actionName || "", true),
|
|
341
|
+
void 0,
|
|
342
|
+
typescript.factory.createTupleTypeNode([
|
|
343
|
+
tsCloneNode.cloneNode(action.params, {
|
|
344
|
+
hook: importMappingCloneHook(importMapping)
|
|
345
|
+
}),
|
|
346
|
+
createUnwrapReturnType(action, importMapping)
|
|
347
|
+
])
|
|
348
|
+
)
|
|
349
|
+
);
|
|
350
|
+
} else {
|
|
351
|
+
actionsNoParamsProperties.push(
|
|
352
|
+
typescript.factory.createPropertySignature(
|
|
353
|
+
void 0,
|
|
354
|
+
typescript.factory.createStringLiteral(action.actionName || "", true),
|
|
355
|
+
void 0,
|
|
356
|
+
createUnwrapReturnType(action, importMapping)
|
|
357
|
+
)
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
callStmts.push(
|
|
362
|
+
createWrapperFunctionOverload(
|
|
363
|
+
{
|
|
364
|
+
typeParameters: typescript.factory.createNodeArray([
|
|
365
|
+
typescript.factory.createTypeParameterDeclaration(
|
|
366
|
+
void 0,
|
|
367
|
+
typescript.factory.createIdentifier("N"),
|
|
368
|
+
typescript.factory.createTypeOperatorNode(
|
|
369
|
+
typescript.SyntaxKind.KeyOfKeyword,
|
|
370
|
+
typescript.factory.createTypeReferenceNode(
|
|
371
|
+
typescript.factory.createIdentifier(ParamsActions),
|
|
372
|
+
void 0
|
|
373
|
+
)
|
|
374
|
+
)
|
|
375
|
+
)
|
|
376
|
+
]),
|
|
377
|
+
params: typescript.factory.createIndexedAccessTypeNode(
|
|
378
|
+
typescript.factory.createIndexedAccessTypeNode(
|
|
379
|
+
typescript.factory.createTypeReferenceNode(
|
|
380
|
+
typescript.factory.createIdentifier(ParamsActions)
|
|
381
|
+
),
|
|
382
|
+
typescript.factory.createTypeReferenceNode(typescript.factory.createIdentifier("N"))
|
|
383
|
+
),
|
|
384
|
+
typescript.factory.createLiteralTypeNode(typescript.factory.createNumericLiteral("0"))
|
|
385
|
+
),
|
|
386
|
+
returnType: typescript.factory.createIndexedAccessTypeNode(
|
|
387
|
+
typescript.factory.createIndexedAccessTypeNode(
|
|
388
|
+
typescript.factory.createTypeReferenceNode(
|
|
389
|
+
typescript.factory.createIdentifier(ParamsActions),
|
|
390
|
+
void 0
|
|
391
|
+
),
|
|
392
|
+
typescript.factory.createTypeReferenceNode(
|
|
393
|
+
typescript.factory.createIdentifier("N"),
|
|
394
|
+
void 0
|
|
395
|
+
)
|
|
396
|
+
),
|
|
397
|
+
typescript.factory.createLiteralTypeNode(typescript.factory.createNumericLiteral("1"))
|
|
398
|
+
)
|
|
399
|
+
},
|
|
400
|
+
importMapping,
|
|
401
|
+
"call",
|
|
402
|
+
"N"
|
|
403
|
+
)
|
|
404
|
+
);
|
|
405
|
+
callStmts.push(
|
|
406
|
+
createWrapperFunctionOverload(
|
|
407
|
+
{
|
|
408
|
+
typeParameters: typescript.factory.createNodeArray([
|
|
409
|
+
typescript.factory.createTypeParameterDeclaration(
|
|
410
|
+
void 0,
|
|
411
|
+
typescript.factory.createIdentifier("N"),
|
|
412
|
+
typescript.factory.createTypeOperatorNode(
|
|
413
|
+
typescript.SyntaxKind.KeyOfKeyword,
|
|
414
|
+
typescript.factory.createTypeReferenceNode(
|
|
415
|
+
typescript.factory.createIdentifier(NoParamsActions),
|
|
416
|
+
void 0
|
|
417
|
+
)
|
|
418
|
+
)
|
|
419
|
+
)
|
|
420
|
+
]),
|
|
421
|
+
returnType: typescript.factory.createIndexedAccessTypeNode(
|
|
422
|
+
typescript.factory.createTypeReferenceNode(
|
|
423
|
+
typescript.factory.createIdentifier(NoParamsActions),
|
|
424
|
+
void 0
|
|
425
|
+
),
|
|
426
|
+
typescript.factory.createTypeReferenceNode(
|
|
427
|
+
typescript.factory.createIdentifier("N"),
|
|
428
|
+
void 0
|
|
429
|
+
)
|
|
430
|
+
)
|
|
431
|
+
},
|
|
432
|
+
importMapping,
|
|
433
|
+
"call",
|
|
434
|
+
"N"
|
|
435
|
+
)
|
|
436
|
+
);
|
|
437
|
+
callStmts.push(
|
|
438
|
+
createWrapperFunctionOverload(
|
|
439
|
+
{
|
|
440
|
+
params: typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UnknownKeyword),
|
|
441
|
+
returnType: typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UnknownKeyword)
|
|
442
|
+
},
|
|
443
|
+
importMapping,
|
|
444
|
+
"call",
|
|
445
|
+
void 0,
|
|
446
|
+
typescript.factory.createBlock(
|
|
447
|
+
[
|
|
448
|
+
typescript.factory.createReturnStatement(
|
|
449
|
+
typescript.factory.createCallExpression(
|
|
450
|
+
typescript.factory.createPropertyAccessExpression(
|
|
451
|
+
typescript.factory.createIdentifier("ctx"),
|
|
452
|
+
typescript.factory.createIdentifier("call")
|
|
453
|
+
),
|
|
454
|
+
void 0,
|
|
455
|
+
[
|
|
456
|
+
typescript.factory.createIdentifier("action"),
|
|
457
|
+
typescript.factory.createIdentifier("params"),
|
|
458
|
+
typescript.factory.createIdentifier("meta")
|
|
459
|
+
]
|
|
460
|
+
)
|
|
461
|
+
)
|
|
462
|
+
],
|
|
463
|
+
true
|
|
464
|
+
)
|
|
465
|
+
)
|
|
466
|
+
);
|
|
467
|
+
callTStmts.push(
|
|
468
|
+
createWrapperFunctionOverload(
|
|
469
|
+
{
|
|
470
|
+
params: typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UnknownKeyword),
|
|
471
|
+
returnType: typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.UnknownKeyword)
|
|
472
|
+
},
|
|
473
|
+
importMapping,
|
|
474
|
+
"callT",
|
|
475
|
+
void 0,
|
|
476
|
+
typescript.factory.createBlock(
|
|
477
|
+
[
|
|
478
|
+
typescript.factory.createReturnStatement(
|
|
479
|
+
typescript.factory.createCallExpression(
|
|
480
|
+
typescript.factory.createPropertyAccessExpression(
|
|
481
|
+
typescript.factory.createIdentifier("ctx"),
|
|
482
|
+
typescript.factory.createIdentifier("call")
|
|
483
|
+
),
|
|
484
|
+
void 0,
|
|
485
|
+
[
|
|
486
|
+
typescript.factory.createIdentifier("action"),
|
|
487
|
+
typescript.factory.createIdentifier("params"),
|
|
488
|
+
typescript.factory.createIdentifier("meta")
|
|
489
|
+
]
|
|
490
|
+
)
|
|
491
|
+
)
|
|
492
|
+
],
|
|
493
|
+
true
|
|
494
|
+
)
|
|
495
|
+
)
|
|
496
|
+
);
|
|
497
|
+
const printer = typescript.createPrinter({ newLine: typescript.NewLineKind.LineFeed });
|
|
498
|
+
const sourceFile = typescript.factory.createSourceFile(
|
|
499
|
+
[...stmts, ...callStmts, ...callTStmts],
|
|
500
|
+
typescript.factory.createToken(typescript.SyntaxKind.EndOfFileToken),
|
|
501
|
+
typescript.NodeFlags.Const
|
|
502
|
+
);
|
|
503
|
+
let res = printer.printFile(sourceFile);
|
|
504
|
+
res = res.replace(/interface Actions/, "\ninterface Actions").replace(/export function call/, "\nexport function call").replace(/export function callT/, "\nexport function callT");
|
|
505
|
+
const eslintIgnoreRules = [
|
|
506
|
+
"@typescript-eslint/no-explicit-any",
|
|
507
|
+
"@typescript-eslint/no-unused-vars"
|
|
508
|
+
];
|
|
509
|
+
res = `/* eslint-disable ${eslintIgnoreRules.join(",")} */
|
|
510
|
+
${res}`;
|
|
511
|
+
return res;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
function injectDatabaseMixinV2Builtins(context, actions, service, sourceFile) {
|
|
515
|
+
const dbMixin = service.originalSchema.mixins?.find(
|
|
516
|
+
(m) => m.methods?._getDatabaseMixinCollection
|
|
517
|
+
);
|
|
518
|
+
if (!dbMixin) {
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
const types = findDatabaseMixinTypes(context, sourceFile);
|
|
522
|
+
if (!types) {
|
|
523
|
+
return;
|
|
524
|
+
}
|
|
525
|
+
const typeArguments = types.tenantField ? [types.entityType, types.tenantField] : [types.entityType];
|
|
526
|
+
const version = service.version ? `v${service.version}.` : "";
|
|
527
|
+
const dbActions = actions.filter((a) => a.actionName?.startsWith(`${version}${service.name}.`)).filter((a) => !a.params || !a.returnType);
|
|
528
|
+
const importName = addDepToImports(
|
|
529
|
+
context.imports,
|
|
530
|
+
"@treatwell/moleculer-essentials"
|
|
531
|
+
);
|
|
532
|
+
for (const action of dbActions) {
|
|
533
|
+
const actionName = action.actionName?.replace(
|
|
534
|
+
`${version}${service.name}.`,
|
|
535
|
+
""
|
|
536
|
+
);
|
|
537
|
+
if (!action.params) {
|
|
538
|
+
switch (actionName) {
|
|
539
|
+
case "find":
|
|
540
|
+
case "findStream": {
|
|
541
|
+
action.params = typescript.factory.createTypeReferenceNode(
|
|
542
|
+
`${importName}.DatabaseActionFindParams`,
|
|
543
|
+
typeArguments
|
|
544
|
+
);
|
|
545
|
+
break;
|
|
546
|
+
}
|
|
547
|
+
case "getInternal": {
|
|
548
|
+
action.params = typescript.factory.createTypeReferenceNode(
|
|
549
|
+
`${importName}.DatabaseActionGetInternalParams`,
|
|
550
|
+
typeArguments
|
|
551
|
+
);
|
|
552
|
+
break;
|
|
553
|
+
}
|
|
554
|
+
case "get": {
|
|
555
|
+
action.params = typescript.factory.createTypeReferenceNode(
|
|
556
|
+
`${importName}.DatabaseActionGetParams`,
|
|
557
|
+
typeArguments
|
|
558
|
+
);
|
|
559
|
+
break;
|
|
560
|
+
}
|
|
561
|
+
case "countInternal": {
|
|
562
|
+
action.params = typescript.factory.createTypeReferenceNode(
|
|
563
|
+
`${importName}.DatabaseActionCountInternalParams`,
|
|
564
|
+
typeArguments
|
|
565
|
+
);
|
|
566
|
+
break;
|
|
567
|
+
}
|
|
568
|
+
case "count": {
|
|
569
|
+
action.params = typescript.factory.createTypeReferenceNode(
|
|
570
|
+
`${importName}.DatabaseActionCountParams`,
|
|
571
|
+
typeArguments
|
|
572
|
+
);
|
|
573
|
+
break;
|
|
574
|
+
}
|
|
575
|
+
case "list": {
|
|
576
|
+
action.params = typescript.factory.createTypeReferenceNode(
|
|
577
|
+
`${importName}.DatabaseActionListParams`,
|
|
578
|
+
typeArguments
|
|
579
|
+
);
|
|
580
|
+
break;
|
|
581
|
+
}
|
|
582
|
+
case "create": {
|
|
583
|
+
action.params = typescript.factory.createTypeReferenceNode(
|
|
584
|
+
`${importName}.DatabaseActionCreateParams`,
|
|
585
|
+
[types.entityType]
|
|
586
|
+
);
|
|
587
|
+
break;
|
|
588
|
+
}
|
|
589
|
+
case "update": {
|
|
590
|
+
action.params = typescript.factory.createTypeReferenceNode(
|
|
591
|
+
`${importName}.DatabaseActionUpdateParams`,
|
|
592
|
+
typeArguments
|
|
593
|
+
);
|
|
594
|
+
break;
|
|
595
|
+
}
|
|
596
|
+
case "remove": {
|
|
597
|
+
action.params = typescript.factory.createTypeReferenceNode(
|
|
598
|
+
`${importName}.DatabaseActionRemoveParams`,
|
|
599
|
+
typeArguments
|
|
600
|
+
);
|
|
601
|
+
break;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
if (!action.returnType) {
|
|
606
|
+
switch (actionName) {
|
|
607
|
+
case "find": {
|
|
608
|
+
action.returnType = typescript.factory.createTypeReferenceNode(
|
|
609
|
+
`${importName}.DatabaseActionFindResult`,
|
|
610
|
+
[types.entityType]
|
|
611
|
+
);
|
|
612
|
+
break;
|
|
613
|
+
}
|
|
614
|
+
case "findStream": {
|
|
615
|
+
const streamImportName = addDepToImports(context.imports, "stream");
|
|
616
|
+
action.returnType = typescript.factory.createTypeReferenceNode(
|
|
617
|
+
`${streamImportName}.Readable`
|
|
618
|
+
);
|
|
619
|
+
break;
|
|
620
|
+
}
|
|
621
|
+
case "countInternal":
|
|
622
|
+
case "count": {
|
|
623
|
+
action.returnType = typescript.factory.createKeywordTypeNode(
|
|
624
|
+
typescript.SyntaxKind.NumberKeyword
|
|
625
|
+
);
|
|
626
|
+
break;
|
|
627
|
+
}
|
|
628
|
+
case "list": {
|
|
629
|
+
action.returnType = typescript.factory.createTypeReferenceNode(
|
|
630
|
+
`${importName}.DatabaseActionListResult`,
|
|
631
|
+
[types.entityType]
|
|
632
|
+
);
|
|
633
|
+
break;
|
|
634
|
+
}
|
|
635
|
+
case "create":
|
|
636
|
+
case "update":
|
|
637
|
+
case "remove":
|
|
638
|
+
case "getInternal":
|
|
639
|
+
case "get": {
|
|
640
|
+
action.returnType = typescript.factory.createTypeReferenceNode(
|
|
641
|
+
`${importName}.DatabaseActionEntityResult`,
|
|
642
|
+
[types.entityType]
|
|
643
|
+
);
|
|
644
|
+
break;
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
function findDatabaseMixinTypes(context, sourceFile) {
|
|
651
|
+
let res;
|
|
652
|
+
function visit(n) {
|
|
653
|
+
if (!typescript.isCallExpression(n) || n.expression.getText() !== "DatabaseMethodsMixin") {
|
|
654
|
+
n.forEachChild(visit);
|
|
655
|
+
return;
|
|
656
|
+
}
|
|
657
|
+
if (!n.typeArguments?.length) {
|
|
658
|
+
return;
|
|
659
|
+
}
|
|
660
|
+
res = { entityType: n.typeArguments[0], tenantField: n.typeArguments[1] };
|
|
661
|
+
}
|
|
662
|
+
visit(sourceFile);
|
|
663
|
+
if (res) {
|
|
664
|
+
fillImports(context, sourceFile, res.entityType);
|
|
665
|
+
if (res.tenantField) {
|
|
666
|
+
fillImports(context, sourceFile, res.tenantField);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
return res;
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
async function createWrapperCall(wrapperPath, services, svcFiles, additionalBuiltins) {
|
|
673
|
+
const currentContent = await promises.readFile(wrapperPath, "utf8");
|
|
674
|
+
const importMapping = /* @__PURE__ */ new Map();
|
|
675
|
+
const imports = /* @__PURE__ */ new Map();
|
|
676
|
+
addDepToImports(imports, "moleculer");
|
|
677
|
+
const builtins = [injectDatabaseMixinV2Builtins, ...additionalBuiltins];
|
|
678
|
+
const actions = services.flatMap(
|
|
679
|
+
(svc, idx) => parseService(
|
|
680
|
+
{
|
|
681
|
+
imports,
|
|
682
|
+
currentFilePath: svcFiles[idx],
|
|
683
|
+
wrapperPath,
|
|
684
|
+
importMapping,
|
|
685
|
+
builtins
|
|
686
|
+
},
|
|
687
|
+
svc
|
|
688
|
+
)
|
|
689
|
+
);
|
|
690
|
+
const wrapper = buildCallWrapperFile(actions, imports, importMapping);
|
|
691
|
+
if (currentContent !== wrapper && wrapper.trim()) {
|
|
692
|
+
await promises.writeFile(wrapperPath, wrapper);
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
exports.addDepToImports = addDepToImports;
|
|
697
|
+
exports.createWrapperCall = createWrapperCall;
|
|
698
|
+
exports.fillImports = fillImports;
|