@nestbridge/core 2.6.1 → 3.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/CHANGELOG.md +17 -0
- package/README.md +19 -23
- package/build/nestbridge.core.d.ts +29 -15
- package/build/nestbridge.core.js +871 -284
- package/build/nestbridge.core.js.map +1 -1
- package/package.json +1 -1
package/build/nestbridge.core.js
CHANGED
|
@@ -1,7 +1,450 @@
|
|
|
1
|
-
import { SyntaxKind, isArrayLiteralExpression, isArrayTypeNode, isArrowFunction, isBinaryExpression, isBlock, isCallExpression, isClassDeclaration, isConditionalExpression, isDecorator, isDoStatement, isExportDeclaration, isExpressionStatement, isFalseLiteral, isForInStatement, isForOfStatement, isForStatement, isFunctionExpression, isIdentifier, isIfStatement, isImportDeclaration, isLiteralTypeNode, isMethodDeclaration, isNamedExports, isNamedImports, isNewExpression, isNullLiteral, isNumericLiteral, isObjectLiteralExpression, isParenthesizedExpression, isPropertyAccessExpression, isPropertyAssignment, isPropertyDeclaration, isReturnStatement, isSpreadElement, isStringLiteral, isStringLiteralLikeNode, isSwitchStatement, isTrueLiteral, isTypeReferenceNode, isUnionTypeNode, isVariableStatement, isWhileStatement } from "typescript/unstable/ast";
|
|
2
|
-
import { API } from "typescript/unstable/sync";
|
|
1
|
+
import { SyntaxKind, isArrayLiteralExpression, isArrayTypeNode, isArrowFunction, isBinaryExpression, isBlock, isCallExpression, isClassDeclaration, isClassExpression, isConditionalExpression, isDecorator, isDoStatement, isExportDeclaration, isExpressionStatement, isFalseLiteral, isForInStatement, isForOfStatement, isForStatement, isFunctionDeclaration, isFunctionExpression, isIdentifier, isIfStatement, isImportDeclaration, isLiteralTypeNode, isMethodDeclaration, isNamedExports, isNamedImports, isNewExpression, isNullLiteral, isNumericLiteral, isObjectLiteralExpression, isParenthesizedExpression, isPropertyAccessExpression, isPropertyAssignment, isPropertyDeclaration, isReturnStatement, isShorthandPropertyAssignment, isSpreadElement, isStringLiteral, isStringLiteralLikeNode, isSwitchStatement, isTrueLiteral, isTypeReferenceNode, isUnionTypeNode, isVariableStatement, isWhileStatement } from "typescript/unstable/ast";
|
|
3
2
|
import { existsSync } from "node:fs";
|
|
4
3
|
import { dirname, extname, relative, resolve } from "node:path";
|
|
4
|
+
import { API } from "typescript/unstable/sync";
|
|
5
|
+
//#region src/analysis/attach-response-wrappers.ts
|
|
6
|
+
var attachResponseWrappers = (controller, classDeclaration, interceptors) => {
|
|
7
|
+
const memberMethods = classDeclaration.members.filter((member) => isMethodDeclaration(member));
|
|
8
|
+
const methods = controller.methods.map((method) => {
|
|
9
|
+
const declaration = memberMethods.find((candidate) => candidate.name.getText() === method.name);
|
|
10
|
+
const responseWrapper = method.responseKind === "json" && declaration !== void 0 ? interceptors.responseWrapperOf(declaration) : void 0;
|
|
11
|
+
return responseWrapper === void 0 ? method : {
|
|
12
|
+
...method,
|
|
13
|
+
responseWrapper
|
|
14
|
+
};
|
|
15
|
+
});
|
|
16
|
+
return {
|
|
17
|
+
...controller,
|
|
18
|
+
methods
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/models/response-wrapper-substitution.ts
|
|
23
|
+
var WRAPPED_RESULT_SUBSTITUTION = "Awaited<Result>";
|
|
24
|
+
var UNRESOLVED_WRAPPED_RESULT_TYPE_SOURCE = "unknown";
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region src/analysis/unwrap-parenthesized-expression.ts
|
|
27
|
+
var unwrapParenthesizedExpression = (expression) => {
|
|
28
|
+
let unwrapped = expression;
|
|
29
|
+
while (isParenthesizedExpression(unwrapped)) unwrapped = unwrapped.expression;
|
|
30
|
+
return unwrapped;
|
|
31
|
+
};
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region src/analysis/resolve-sole-return-expression.ts
|
|
34
|
+
/**
|
|
35
|
+
* Accepts an expression body, or a block that holds nothing but simple `const` declarations and
|
|
36
|
+
* a single trailing `return`. A returned identifier resolves to its declared initializer.
|
|
37
|
+
*/
|
|
38
|
+
var resolveSoleReturnExpression = (body) => {
|
|
39
|
+
if (!isBlock(body)) return unwrapParenthesizedExpression(body);
|
|
40
|
+
const localDeclarations = /* @__PURE__ */ new Map();
|
|
41
|
+
let returnExpression;
|
|
42
|
+
let sawReturn = false;
|
|
43
|
+
for (const statement of body.statements) {
|
|
44
|
+
if (sawReturn) return;
|
|
45
|
+
if (isVariableStatement(statement)) {
|
|
46
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
47
|
+
if (!isIdentifier(declaration.name) || declaration.initializer === void 0) return;
|
|
48
|
+
localDeclarations.set(declaration.name.text, declaration.initializer);
|
|
49
|
+
}
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (isReturnStatement(statement)) {
|
|
53
|
+
sawReturn = true;
|
|
54
|
+
returnExpression = statement.expression;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (returnExpression === void 0) return;
|
|
60
|
+
return isIdentifier(returnExpression) ? localDeclarations.get(returnExpression.text) : returnExpression;
|
|
61
|
+
};
|
|
62
|
+
//#endregion
|
|
63
|
+
//#region src/analysis/synthesize-generic-object-type.ts
|
|
64
|
+
var NUMERIC_OPERATOR_KINDS = [
|
|
65
|
+
SyntaxKind.MinusToken,
|
|
66
|
+
SyntaxKind.AsteriskToken,
|
|
67
|
+
SyntaxKind.SlashToken,
|
|
68
|
+
SyntaxKind.PercentToken
|
|
69
|
+
];
|
|
70
|
+
var fail = (state, node, reason) => {
|
|
71
|
+
state.failure ??= {
|
|
72
|
+
node,
|
|
73
|
+
reason
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
var describeUntypedExpression = (expression) => isBinaryExpression(expression) && expression.operatorToken.kind === SyntaxKind.PlusToken ? `\`${expression.getText()}\` can be a string or a number, so its type is not guessed.` : `The type of \`${expression.getText()}\` cannot be determined statically.`;
|
|
77
|
+
var synthesizeExpressionType = (expression, parameterName, substitution, state) => {
|
|
78
|
+
if (isParenthesizedExpression(expression)) return synthesizeExpressionType(expression.expression, parameterName, substitution, state);
|
|
79
|
+
if (isIdentifier(expression) && expression.text === parameterName) {
|
|
80
|
+
state.substitutionCount += 1;
|
|
81
|
+
return substitution;
|
|
82
|
+
}
|
|
83
|
+
if (isObjectLiteralExpression(expression)) {
|
|
84
|
+
const memberTypes = [];
|
|
85
|
+
for (const property of expression.properties) {
|
|
86
|
+
if (isShorthandPropertyAssignment(property)) {
|
|
87
|
+
const shorthandName = property.name.getText();
|
|
88
|
+
if (shorthandName !== parameterName) return fail(state, property, `The type of \`${shorthandName}\` cannot be determined statically.`);
|
|
89
|
+
state.substitutionCount += 1;
|
|
90
|
+
memberTypes.push(`${shorthandName}: ${substitution}`);
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
if (!isPropertyAssignment(property) || !(isIdentifier(property.name) || isStringLiteral(property.name))) return fail(state, property, `\`${property.getText()}\` is not a plain \`name: value\` property.`);
|
|
94
|
+
const valueType = synthesizeExpressionType(property.initializer, parameterName, substitution, state);
|
|
95
|
+
if (valueType === void 0) return;
|
|
96
|
+
memberTypes.push(`${property.name.text}: ${valueType}`);
|
|
97
|
+
}
|
|
98
|
+
return `{ ${memberTypes.join("; ")} }`;
|
|
99
|
+
}
|
|
100
|
+
if (isStringLiteral(expression)) return "string";
|
|
101
|
+
if (isNumericLiteral(expression)) return "number";
|
|
102
|
+
if (isTrueLiteral(expression) || isFalseLiteral(expression)) return "boolean";
|
|
103
|
+
if (isNullLiteral(expression)) return "null";
|
|
104
|
+
if (isBinaryExpression(expression) && NUMERIC_OPERATOR_KINDS.includes(expression.operatorToken.kind)) return "number";
|
|
105
|
+
return fail(state, expression, describeUntypedExpression(expression));
|
|
106
|
+
};
|
|
107
|
+
var synthesizeGenericObjectType = (objectLiteral, parameterName, substitution) => {
|
|
108
|
+
const state = { substitutionCount: 0 };
|
|
109
|
+
const objectType = synthesizeExpressionType(objectLiteral, parameterName, substitution, state);
|
|
110
|
+
if (objectType === void 0) {
|
|
111
|
+
const failure = state.failure ?? {
|
|
112
|
+
node: objectLiteral,
|
|
113
|
+
reason: `The type of \`${objectLiteral.getText()}\` cannot be determined statically.`
|
|
114
|
+
};
|
|
115
|
+
return {
|
|
116
|
+
ok: false,
|
|
117
|
+
node: failure.node,
|
|
118
|
+
reason: failure.reason
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
if (state.substitutionCount !== 1) return {
|
|
122
|
+
ok: false,
|
|
123
|
+
node: objectLiteral,
|
|
124
|
+
reason: `The payload parameter \`${parameterName}\` must appear exactly once in the returned object, but appears ${state.substitutionCount} times.`
|
|
125
|
+
};
|
|
126
|
+
return {
|
|
127
|
+
ok: true,
|
|
128
|
+
typeSource: objectType
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
//#endregion
|
|
132
|
+
//#region src/analysis/read-map-operation.ts
|
|
133
|
+
var readMapOperation = (operator) => {
|
|
134
|
+
const [callback] = operator.arguments;
|
|
135
|
+
if (operator.arguments.length !== 1 || callback === void 0 || !(isArrowFunction(callback) || isFunctionExpression(callback))) return {
|
|
136
|
+
ok: false,
|
|
137
|
+
node: operator,
|
|
138
|
+
reason: "The `map` operator must take a single arrow or function expression."
|
|
139
|
+
};
|
|
140
|
+
const [parameter] = callback.parameters;
|
|
141
|
+
if (callback.parameters.length !== 1 || parameter === void 0 || !isIdentifier(parameter.name)) return {
|
|
142
|
+
ok: false,
|
|
143
|
+
node: callback,
|
|
144
|
+
reason: "The `map` callback must take exactly one plain parameter."
|
|
145
|
+
};
|
|
146
|
+
const returnExpression = resolveSoleReturnExpression(callback.body);
|
|
147
|
+
if (returnExpression === void 0 || !isObjectLiteralExpression(returnExpression)) return {
|
|
148
|
+
ok: false,
|
|
149
|
+
node: callback,
|
|
150
|
+
reason: "The `map` callback must return an object literal."
|
|
151
|
+
};
|
|
152
|
+
const synthesis = synthesizeGenericObjectType(returnExpression, parameter.name.text, WRAPPED_RESULT_SUBSTITUTION);
|
|
153
|
+
if (!synthesis.ok) return {
|
|
154
|
+
ok: false,
|
|
155
|
+
node: synthesis.node,
|
|
156
|
+
reason: synthesis.reason
|
|
157
|
+
};
|
|
158
|
+
return {
|
|
159
|
+
ok: true,
|
|
160
|
+
operation: {
|
|
161
|
+
parameterName: parameter.name.text,
|
|
162
|
+
returnExpression
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
};
|
|
166
|
+
//#endregion
|
|
167
|
+
//#region src/analysis/open-source-file.ts
|
|
168
|
+
var sharedApi;
|
|
169
|
+
/**
|
|
170
|
+
* The returned checker belongs to the snapshot opened for this file. Opening the next file
|
|
171
|
+
* replaces that snapshot, so finish every checker query before calling this again.
|
|
172
|
+
*/
|
|
173
|
+
var openSourceFile = (filePath) => {
|
|
174
|
+
sharedApi ??= new API();
|
|
175
|
+
sharedApi.updateSnapshot({ closeFiles: [filePath] });
|
|
176
|
+
const project = sharedApi.updateSnapshot({
|
|
177
|
+
openFiles: [filePath],
|
|
178
|
+
fileChanges: { changed: [filePath] }
|
|
179
|
+
}).getDefaultProjectForFile(filePath);
|
|
180
|
+
const sourceFile = project?.program.getSourceFile(filePath);
|
|
181
|
+
if (project === void 0 || sourceFile === void 0) throw new Error(`Unable to parse source file: ${filePath}`);
|
|
182
|
+
return {
|
|
183
|
+
sourceFile,
|
|
184
|
+
checker: project.checker
|
|
185
|
+
};
|
|
186
|
+
};
|
|
187
|
+
//#endregion
|
|
188
|
+
//#region src/analysis/parse-source-file.ts
|
|
189
|
+
var parseSourceFile = (filePath) => openSourceFile(filePath).sourceFile;
|
|
190
|
+
//#endregion
|
|
191
|
+
//#region src/analysis/resolve-type-declaration.ts
|
|
192
|
+
var parsedFilesByPath = /* @__PURE__ */ new Map();
|
|
193
|
+
/**
|
|
194
|
+
* Each analysis entry point starts from an empty cache, so a long-running watch session never
|
|
195
|
+
* resolves a class from a file version that has since changed.
|
|
196
|
+
*/
|
|
197
|
+
var clearParsedFileCache = () => {
|
|
198
|
+
parsedFilesByPath.clear();
|
|
199
|
+
};
|
|
200
|
+
var parseFile = (filePath) => {
|
|
201
|
+
const cached = parsedFilesByPath.get(filePath);
|
|
202
|
+
if (cached !== void 0) return cached;
|
|
203
|
+
const sourceFile = parseSourceFile(filePath);
|
|
204
|
+
parsedFilesByPath.set(filePath, sourceFile);
|
|
205
|
+
return sourceFile;
|
|
206
|
+
};
|
|
207
|
+
var resolveModuleFile = (fromFilePath, moduleSpecifierText) => {
|
|
208
|
+
if (!moduleSpecifierText.startsWith(".")) return;
|
|
209
|
+
const base = resolve(dirname(fromFilePath), moduleSpecifierText);
|
|
210
|
+
return [
|
|
211
|
+
`${base}.ts`,
|
|
212
|
+
`${base}.tsx`,
|
|
213
|
+
resolve(base, "index.ts")
|
|
214
|
+
].find((candidate) => existsSync(candidate));
|
|
215
|
+
};
|
|
216
|
+
var findExportedDeclaration = (sourceFile, name) => sourceFile.statements.find((statement) => isClassDeclaration(statement) && statement.name?.text === name);
|
|
217
|
+
var findImportSource = (sourceFile, localName) => {
|
|
218
|
+
for (const statement of sourceFile.statements) {
|
|
219
|
+
if (!isImportDeclaration(statement) || !isStringLiteralLikeNode(statement.moduleSpecifier)) continue;
|
|
220
|
+
const namedBindings = statement.importClause?.namedBindings;
|
|
221
|
+
if (namedBindings === void 0 || !isNamedImports(namedBindings)) continue;
|
|
222
|
+
const element = namedBindings.elements.find((candidate) => candidate.name.text === localName);
|
|
223
|
+
if (element !== void 0) return {
|
|
224
|
+
moduleSpecifierText: statement.moduleSpecifier.text,
|
|
225
|
+
importedName: element.propertyName?.text ?? element.name.text
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
var findReExportSource = (sourceFile, name) => {
|
|
230
|
+
for (const statement of sourceFile.statements) {
|
|
231
|
+
if (!isExportDeclaration(statement) || statement.moduleSpecifier === void 0 || !isStringLiteralLikeNode(statement.moduleSpecifier)) continue;
|
|
232
|
+
if (statement.exportClause === void 0) return statement.moduleSpecifier.text;
|
|
233
|
+
if (isNamedExports(statement.exportClause) && statement.exportClause.elements.some((element) => (element.propertyName?.text ?? element.name.text) === name)) return statement.moduleSpecifier.text;
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
var resolveClassDeclaration = (name, fromSourceFile, fromFilePath, visited = /* @__PURE__ */ new Set()) => {
|
|
237
|
+
if (visited.has(fromFilePath)) return;
|
|
238
|
+
visited.add(fromFilePath);
|
|
239
|
+
const direct = findExportedDeclaration(fromSourceFile, name);
|
|
240
|
+
if (direct !== void 0) return {
|
|
241
|
+
classDeclaration: direct,
|
|
242
|
+
sourceFile: fromSourceFile,
|
|
243
|
+
filePath: fromFilePath
|
|
244
|
+
};
|
|
245
|
+
const importSource = findImportSource(fromSourceFile, name);
|
|
246
|
+
if (importSource !== void 0) {
|
|
247
|
+
const resolvedPath = resolveModuleFile(fromFilePath, importSource.moduleSpecifierText);
|
|
248
|
+
if (resolvedPath !== void 0) {
|
|
249
|
+
const nextSourceFile = parseFile(resolvedPath);
|
|
250
|
+
const result = resolveClassDeclaration(importSource.importedName, nextSourceFile, resolvedPath, visited);
|
|
251
|
+
if (result !== void 0) return result;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
const reExportModuleSpecifierText = findReExportSource(fromSourceFile, name);
|
|
255
|
+
if (reExportModuleSpecifierText !== void 0) {
|
|
256
|
+
const resolvedPath = resolveModuleFile(fromFilePath, reExportModuleSpecifierText);
|
|
257
|
+
if (resolvedPath !== void 0) return resolveClassDeclaration(name, parseFile(resolvedPath), resolvedPath, visited);
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
//#endregion
|
|
261
|
+
//#region src/analysis/read-pipe-operator.ts
|
|
262
|
+
var RXJS_MODULE_SPECIFIERS = ["rxjs", "rxjs/operators"];
|
|
263
|
+
var MAP_OPERATOR_NAME = "map";
|
|
264
|
+
var SHAPE_NEUTRAL_OPERATOR_NAMES = [
|
|
265
|
+
"tap",
|
|
266
|
+
"timeout",
|
|
267
|
+
"finalize"
|
|
268
|
+
];
|
|
269
|
+
var readPipeOperator = (operator, sourceFile) => {
|
|
270
|
+
if (!isCallExpression(operator) || !isIdentifier(operator.expression)) return {
|
|
271
|
+
ok: false,
|
|
272
|
+
node: operator,
|
|
273
|
+
reason: "Pipe operators must be calls of operators imported by name from rxjs."
|
|
274
|
+
};
|
|
275
|
+
const importSource = findImportSource(sourceFile, operator.expression.text);
|
|
276
|
+
if (importSource === void 0 || !RXJS_MODULE_SPECIFIERS.includes(importSource.moduleSpecifierText)) return {
|
|
277
|
+
ok: false,
|
|
278
|
+
node: operator,
|
|
279
|
+
reason: `Operator \`${operator.expression.text}\` is not imported from rxjs, so its effect on the response shape is unknown.`
|
|
280
|
+
};
|
|
281
|
+
if (importSource.importedName === MAP_OPERATOR_NAME) return readMapOperation(operator);
|
|
282
|
+
if (SHAPE_NEUTRAL_OPERATOR_NAMES.includes(importSource.importedName)) return {
|
|
283
|
+
ok: true,
|
|
284
|
+
operation: void 0
|
|
285
|
+
};
|
|
286
|
+
return {
|
|
287
|
+
ok: false,
|
|
288
|
+
node: operator,
|
|
289
|
+
reason: `Operator \`${importSource.importedName}\` changes or hides the response shape.`
|
|
290
|
+
};
|
|
291
|
+
};
|
|
292
|
+
//#endregion
|
|
293
|
+
//#region src/analysis/contains-return-statement.ts
|
|
294
|
+
var isNestedFunctionScope = (node) => isArrowFunction(node) || isFunctionExpression(node) || isFunctionDeclaration(node) || isMethodDeclaration(node) || isClassDeclaration(node) || isClassExpression(node);
|
|
295
|
+
var containsReturnStatement = (node) => {
|
|
296
|
+
if (isReturnStatement(node)) return true;
|
|
297
|
+
if (isNestedFunctionScope(node)) return false;
|
|
298
|
+
let found = false;
|
|
299
|
+
node.forEachChild((child) => {
|
|
300
|
+
if (containsReturnStatement(child)) found = true;
|
|
301
|
+
});
|
|
302
|
+
return found;
|
|
303
|
+
};
|
|
304
|
+
//#endregion
|
|
305
|
+
//#region src/analysis/resolve-intercept-return-expression.ts
|
|
306
|
+
/**
|
|
307
|
+
* Any statements may precede the final `return` (logger setup, `const startedAt = Date.now()`),
|
|
308
|
+
* but none of them may return early, since the response shape would then depend on the branch.
|
|
309
|
+
* A returned identifier resolves to the initializer of the `const` declared with that name.
|
|
310
|
+
*/
|
|
311
|
+
var resolveInterceptReturnExpression = (body) => {
|
|
312
|
+
const finalStatement = body.statements[body.statements.length - 1];
|
|
313
|
+
if (finalStatement === void 0 || !isReturnStatement(finalStatement) || finalStatement.expression === void 0) return {
|
|
314
|
+
ok: false,
|
|
315
|
+
node: finalStatement ?? body,
|
|
316
|
+
reason: "The last statement of intercept() must be a `return` statement with a value."
|
|
317
|
+
};
|
|
318
|
+
const localDeclarations = /* @__PURE__ */ new Map();
|
|
319
|
+
for (const statement of body.statements) {
|
|
320
|
+
if (statement === finalStatement) continue;
|
|
321
|
+
if (containsReturnStatement(statement)) return {
|
|
322
|
+
ok: false,
|
|
323
|
+
node: statement,
|
|
324
|
+
reason: "intercept() returns before its last statement, so the response shape is ambiguous."
|
|
325
|
+
};
|
|
326
|
+
if (isVariableStatement(statement)) {
|
|
327
|
+
for (const declaration of statement.declarationList.declarations) if (isIdentifier(declaration.name) && declaration.initializer !== void 0) localDeclarations.set(declaration.name.text, declaration.initializer);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
const returned = unwrapParenthesizedExpression(finalStatement.expression);
|
|
331
|
+
if (!isIdentifier(returned)) return {
|
|
332
|
+
ok: true,
|
|
333
|
+
expression: returned
|
|
334
|
+
};
|
|
335
|
+
const initializer = localDeclarations.get(returned.text);
|
|
336
|
+
return initializer === void 0 ? {
|
|
337
|
+
ok: false,
|
|
338
|
+
node: returned,
|
|
339
|
+
reason: `\`${returned.text}\` is not a local \`const\` initialized inside intercept().`
|
|
340
|
+
} : {
|
|
341
|
+
ok: true,
|
|
342
|
+
expression: unwrapParenthesizedExpression(initializer)
|
|
343
|
+
};
|
|
344
|
+
};
|
|
345
|
+
//#endregion
|
|
346
|
+
//#region src/analysis/analyze-intercept-method.ts
|
|
347
|
+
var HANDLE_MEMBER_NAME = "handle";
|
|
348
|
+
var PIPE_MEMBER_NAME = "pipe";
|
|
349
|
+
var CALL_HANDLER_PARAMETER_INDEX = 1;
|
|
350
|
+
var isHandleCall = (expression, callHandlerName) => isCallExpression(expression) && expression.arguments.length === 0 && isPropertyAccessExpression(expression.expression) && expression.expression.name.text === HANDLE_MEMBER_NAME && isIdentifier(expression.expression.expression) && expression.expression.expression.text === callHandlerName;
|
|
351
|
+
var analyzeInterceptMethod = (method, sourceFile) => {
|
|
352
|
+
const callHandler = method.parameters[CALL_HANDLER_PARAMETER_INDEX];
|
|
353
|
+
if (method.body === void 0 || callHandler === void 0 || !isIdentifier(callHandler.name)) return {
|
|
354
|
+
ok: false,
|
|
355
|
+
node: method,
|
|
356
|
+
reason: "intercept() must have a body and name its CallHandler parameter."
|
|
357
|
+
};
|
|
358
|
+
const returned = resolveInterceptReturnExpression(method.body);
|
|
359
|
+
if (!returned.ok) return returned;
|
|
360
|
+
const callHandlerName = callHandler.name.text;
|
|
361
|
+
const expression = returned.expression;
|
|
362
|
+
if (isHandleCall(expression, callHandlerName)) return {
|
|
363
|
+
ok: true,
|
|
364
|
+
shape: { operations: [] }
|
|
365
|
+
};
|
|
366
|
+
if (!isCallExpression(expression) || !isPropertyAccessExpression(expression.expression) || expression.expression.name.text !== PIPE_MEMBER_NAME || !isHandleCall(expression.expression.expression, callHandlerName)) return {
|
|
367
|
+
ok: false,
|
|
368
|
+
node: expression,
|
|
369
|
+
reason: `intercept() must return \`${callHandlerName}.handle()\`, optionally piped through rxjs operators.`
|
|
370
|
+
};
|
|
371
|
+
const operations = [];
|
|
372
|
+
for (const operator of expression.arguments) {
|
|
373
|
+
const read = readPipeOperator(operator, sourceFile);
|
|
374
|
+
if (!read.ok) return read;
|
|
375
|
+
if (read.operation !== void 0) operations.push(read.operation);
|
|
376
|
+
}
|
|
377
|
+
return {
|
|
378
|
+
ok: true,
|
|
379
|
+
shape: { operations }
|
|
380
|
+
};
|
|
381
|
+
};
|
|
382
|
+
//#endregion
|
|
383
|
+
//#region src/analysis/analyze-interceptor-class.ts
|
|
384
|
+
var INTERCEPT_METHOD_NAME = "intercept";
|
|
385
|
+
var findInterceptMethod = (members) => members.find((member) => isMethodDeclaration(member) && isIdentifier(member.name) && member.name.text === INTERCEPT_METHOD_NAME);
|
|
386
|
+
var analyzeInterceptorClass = (resolved) => {
|
|
387
|
+
const interceptMethod = findInterceptMethod(resolved.classDeclaration.members);
|
|
388
|
+
if (interceptMethod === void 0) return {
|
|
389
|
+
ok: false,
|
|
390
|
+
location: resolved.classDeclaration,
|
|
391
|
+
found: void 0,
|
|
392
|
+
reason: "The class declares no intercept() method of its own."
|
|
393
|
+
};
|
|
394
|
+
const analysis = analyzeInterceptMethod(interceptMethod, resolved.sourceFile);
|
|
395
|
+
return analysis.ok ? analysis : {
|
|
396
|
+
ok: false,
|
|
397
|
+
location: interceptMethod,
|
|
398
|
+
found: analysis.node.getText(),
|
|
399
|
+
reason: analysis.reason
|
|
400
|
+
};
|
|
401
|
+
};
|
|
402
|
+
//#endregion
|
|
403
|
+
//#region src/analysis/compose-wrapper-type-source.ts
|
|
404
|
+
/**
|
|
405
|
+
* `shapes` run outermost first, so the type is built from the innermost interceptor outward,
|
|
406
|
+
* starting at the awaited server result. Returns `undefined` when no interceptor changes the
|
|
407
|
+
* shape.
|
|
408
|
+
*/
|
|
409
|
+
var composeWrapperTypeSource = (shapes) => {
|
|
410
|
+
const operations = [...shapes].reverse().flatMap((shape) => shape.operations);
|
|
411
|
+
if (operations.length === 0) return;
|
|
412
|
+
let typeSource = WRAPPED_RESULT_SUBSTITUTION;
|
|
413
|
+
for (const operation of operations) {
|
|
414
|
+
const synthesis = synthesizeGenericObjectType(operation.returnExpression, operation.parameterName, typeSource);
|
|
415
|
+
if (!synthesis.ok) throw new Error(`[nestbridge] An interceptor shape that was validated can no longer be typed: ${synthesis.reason}`);
|
|
416
|
+
typeSource = synthesis.typeSource;
|
|
417
|
+
}
|
|
418
|
+
return typeSource;
|
|
419
|
+
};
|
|
420
|
+
//#endregion
|
|
421
|
+
//#region src/analysis/node-location.ts
|
|
422
|
+
var locationOf = (sourceFile, node) => {
|
|
423
|
+
const { line, character } = sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile));
|
|
424
|
+
return {
|
|
425
|
+
line: line + 1,
|
|
426
|
+
column: character + 1
|
|
427
|
+
};
|
|
428
|
+
};
|
|
429
|
+
//#endregion
|
|
430
|
+
//#region src/analysis/create-interceptor-diagnostic.ts
|
|
431
|
+
var EXPECTED$2 = "An intercept() method that returns next.handle(), optionally piped through rxjs `map` operators that return object literals and the shape-neutral `tap`, `timeout` and `finalize` operators, registered as a class or `new Class()`. Routes under an interceptor that cannot be analyzed are declared as AbortablePromise<unknown>.";
|
|
432
|
+
var createInterceptorDiagnostic = (source) => {
|
|
433
|
+
const location = locationOf(source.sourceFile, source.location);
|
|
434
|
+
return {
|
|
435
|
+
code: "unsupported-interceptor",
|
|
436
|
+
title: "Unsupported interceptor.",
|
|
437
|
+
controllerName: source.controllerName,
|
|
438
|
+
memberName: source.memberName,
|
|
439
|
+
detail: source.detail,
|
|
440
|
+
found: source.found ?? source.location.getText(),
|
|
441
|
+
expected: EXPECTED$2,
|
|
442
|
+
filePath: source.filePath,
|
|
443
|
+
line: location.line,
|
|
444
|
+
column: location.column
|
|
445
|
+
};
|
|
446
|
+
};
|
|
447
|
+
//#endregion
|
|
5
448
|
//#region src/analysis/decorator-inspection.ts
|
|
6
449
|
var getDecorators = (node) => {
|
|
7
450
|
const decorators = node.modifiers?.filter(isDecorator);
|
|
@@ -12,6 +455,7 @@ var findDecorator = (node, decoratorName) => {
|
|
|
12
455
|
if (decorators === void 0) return;
|
|
13
456
|
return decorators.find((decorator) => decoratorNameOf(decorator) === decoratorName);
|
|
14
457
|
};
|
|
458
|
+
var findDecorators = (node, decoratorName) => (getDecorators(node) ?? []).filter((decorator) => decoratorNameOf(decorator) === decoratorName);
|
|
15
459
|
var hasAnyDecorator = (node) => getDecorators(node) !== void 0;
|
|
16
460
|
var findAnyDecorator = (node, decoratorNames) => {
|
|
17
461
|
const decorators = getDecorators(node);
|
|
@@ -23,24 +467,388 @@ var decoratorNameOf = (decorator) => {
|
|
|
23
467
|
if (isCallExpression(expression) && isIdentifier(expression.expression)) return expression.expression.text;
|
|
24
468
|
if (isIdentifier(expression)) return expression.text;
|
|
25
469
|
};
|
|
26
|
-
var readStaticStringArgument = (decorator) => {
|
|
27
|
-
const expression = decorator.expression;
|
|
28
|
-
if (!isCallExpression(expression)) return {
|
|
29
|
-
ok: true,
|
|
30
|
-
value: void 0
|
|
470
|
+
var readStaticStringArgument = (decorator) => {
|
|
471
|
+
const expression = decorator.expression;
|
|
472
|
+
if (!isCallExpression(expression)) return {
|
|
473
|
+
ok: true,
|
|
474
|
+
value: void 0
|
|
475
|
+
};
|
|
476
|
+
const [firstArgument] = expression.arguments;
|
|
477
|
+
if (firstArgument === void 0) return {
|
|
478
|
+
ok: true,
|
|
479
|
+
value: void 0
|
|
480
|
+
};
|
|
481
|
+
if (isStringLiteralLikeNode(firstArgument)) return {
|
|
482
|
+
ok: true,
|
|
483
|
+
value: firstArgument.text
|
|
484
|
+
};
|
|
485
|
+
return {
|
|
486
|
+
ok: false,
|
|
487
|
+
argumentText: firstArgument.getText()
|
|
488
|
+
};
|
|
489
|
+
};
|
|
490
|
+
//#endregion
|
|
491
|
+
//#region src/analysis/read-module-option.ts
|
|
492
|
+
var MODULE_DECORATOR_NAME$1 = "Module";
|
|
493
|
+
var hasModuleDecorator = (classDeclaration) => findDecorator(classDeclaration, MODULE_DECORATOR_NAME$1) !== void 0;
|
|
494
|
+
var readModuleOption = (classDeclaration, optionName) => {
|
|
495
|
+
const moduleDecorator = findDecorator(classDeclaration, MODULE_DECORATOR_NAME$1);
|
|
496
|
+
if (moduleDecorator === void 0 || !isCallExpression(moduleDecorator.expression)) return;
|
|
497
|
+
const [moduleOptions] = moduleDecorator.expression.arguments;
|
|
498
|
+
if (moduleOptions === void 0 || !isObjectLiteralExpression(moduleOptions)) return;
|
|
499
|
+
return moduleOptions.properties.find((property) => isPropertyAssignment(property) && isIdentifier(property.name) && property.name.text === optionName);
|
|
500
|
+
};
|
|
501
|
+
//#endregion
|
|
502
|
+
//#region src/analysis/find-app-interceptor-provider-registrations.ts
|
|
503
|
+
var PROVIDERS_OPTION_NAME = "providers";
|
|
504
|
+
var PROVIDE_PROPERTY_NAME$1 = "provide";
|
|
505
|
+
var USE_CLASS_PROPERTY_NAME$1 = "useClass";
|
|
506
|
+
var APP_INTERCEPTOR_LOCAL_NAME = "APP_INTERCEPTOR";
|
|
507
|
+
var NEST_CORE_MODULE_SPECIFIER$2 = "@nestjs/core";
|
|
508
|
+
var UNSUPPORTED_PROVIDER_PROPERTY_NAMES = [
|
|
509
|
+
"useFactory",
|
|
510
|
+
"useValue",
|
|
511
|
+
"useExisting"
|
|
512
|
+
];
|
|
513
|
+
var findProperty = (provider, name) => provider.properties.find((property) => isPropertyAssignment(property) && isIdentifier(property.name) && property.name.text === name);
|
|
514
|
+
var findUnsupportedProviderPropertyName = (provider) => UNSUPPORTED_PROVIDER_PROPERTY_NAMES.find((name) => findProperty(provider, name) !== void 0);
|
|
515
|
+
/**
|
|
516
|
+
* Reads the `{ provide: APP_INTERCEPTOR, useClass: X }` providers of one module, in the order
|
|
517
|
+
* they are declared. Only `useClass` can be analyzed: `useFactory`, `useValue`, `useExisting` and
|
|
518
|
+
* spread providers are reported as problems because the interceptor behind them is unknown.
|
|
519
|
+
*/
|
|
520
|
+
var findAppInterceptorProviderRegistrations = (moduleClassDeclaration, moduleSourceFile, moduleFilePath) => {
|
|
521
|
+
const registrations = {
|
|
522
|
+
references: [],
|
|
523
|
+
problems: []
|
|
524
|
+
};
|
|
525
|
+
const providersProperty = readModuleOption(moduleClassDeclaration, PROVIDERS_OPTION_NAME);
|
|
526
|
+
const appInterceptorImportSource = findImportSource(moduleSourceFile, APP_INTERCEPTOR_LOCAL_NAME);
|
|
527
|
+
if (providersProperty === void 0 || appInterceptorImportSource === void 0 || appInterceptorImportSource.moduleSpecifierText !== NEST_CORE_MODULE_SPECIFIER$2) return registrations;
|
|
528
|
+
const reportProblem = (node, detail) => {
|
|
529
|
+
registrations.problems.push({
|
|
530
|
+
node,
|
|
531
|
+
sourceFile: moduleSourceFile,
|
|
532
|
+
filePath: moduleFilePath,
|
|
533
|
+
detail
|
|
534
|
+
});
|
|
535
|
+
};
|
|
536
|
+
if (!isArrayLiteralExpression(providersProperty.initializer)) {
|
|
537
|
+
reportProblem(providersProperty.initializer, "The `providers` of a module registering APP_INTERCEPTOR must be an array literal.");
|
|
538
|
+
return registrations;
|
|
539
|
+
}
|
|
540
|
+
for (const element of providersProperty.initializer.elements) {
|
|
541
|
+
if (isSpreadElement(element)) {
|
|
542
|
+
reportProblem(element, "Spread providers cannot be analyzed, so an APP_INTERCEPTOR provider inside them would be missed.");
|
|
543
|
+
continue;
|
|
544
|
+
}
|
|
545
|
+
if (!isObjectLiteralExpression(element)) continue;
|
|
546
|
+
const provideProperty = findProperty(element, PROVIDE_PROPERTY_NAME$1);
|
|
547
|
+
if (provideProperty === void 0 || !isIdentifier(provideProperty.initializer) || provideProperty.initializer.text !== APP_INTERCEPTOR_LOCAL_NAME) continue;
|
|
548
|
+
const unsupportedPropertyName = findUnsupportedProviderPropertyName(element);
|
|
549
|
+
if (unsupportedPropertyName !== void 0) {
|
|
550
|
+
reportProblem(element, `\`${unsupportedPropertyName}\` interceptor providers can't be analysed; use \`useClass\`.`);
|
|
551
|
+
continue;
|
|
552
|
+
}
|
|
553
|
+
const useClassProperty = findProperty(element, USE_CLASS_PROPERTY_NAME$1);
|
|
554
|
+
if (useClassProperty === void 0 || !isIdentifier(useClassProperty.initializer)) {
|
|
555
|
+
reportProblem(element, "An APP_INTERCEPTOR provider must state its interceptor as `useClass: InterceptorClass`.");
|
|
556
|
+
continue;
|
|
557
|
+
}
|
|
558
|
+
registrations.references.push({
|
|
559
|
+
className: useClassProperty.initializer.text,
|
|
560
|
+
node: useClassProperty.initializer,
|
|
561
|
+
sourceFile: moduleSourceFile,
|
|
562
|
+
filePath: moduleFilePath
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
return registrations;
|
|
566
|
+
};
|
|
567
|
+
//#endregion
|
|
568
|
+
//#region src/analysis/read-imported-modules.ts
|
|
569
|
+
var IMPORTS_OPTION_NAME = "imports";
|
|
570
|
+
/**
|
|
571
|
+
* Only plain class identifiers that resolve to a local `@Module()` class are followed. Dynamic
|
|
572
|
+
* modules such as `FooModule.forRoot()`, `forwardRef()` calls, spreads and package imports are
|
|
573
|
+
* skipped because their providers cannot be read statically.
|
|
574
|
+
*/
|
|
575
|
+
var readImportedModules = (module) => {
|
|
576
|
+
const importsProperty = readModuleOption(module.classDeclaration, IMPORTS_OPTION_NAME);
|
|
577
|
+
if (importsProperty === void 0 || !isArrayLiteralExpression(importsProperty.initializer)) return [];
|
|
578
|
+
const importedModules = [];
|
|
579
|
+
for (const element of importsProperty.initializer.elements) {
|
|
580
|
+
if (!isIdentifier(element)) continue;
|
|
581
|
+
const resolved = resolveClassDeclaration(element.text, module.sourceFile, module.filePath);
|
|
582
|
+
if (resolved !== void 0 && hasModuleDecorator(resolved.classDeclaration)) importedModules.push(resolved);
|
|
583
|
+
}
|
|
584
|
+
return importedModules;
|
|
585
|
+
};
|
|
586
|
+
//#endregion
|
|
587
|
+
//#region src/analysis/collect-module-interceptor-registrations.ts
|
|
588
|
+
/**
|
|
589
|
+
* Nest scans modules depth-first starting at the root module, inserting a module before the
|
|
590
|
+
* modules it imports, and a module reached twice keeps its first position. Registering the
|
|
591
|
+
* `APP_INTERCEPTOR` providers module by module, each module's providers before its imports,
|
|
592
|
+
* reproduces the order in which Nest adds them to the global interceptor list.
|
|
593
|
+
*/
|
|
594
|
+
var collectModuleInterceptorRegistrations = (rootModule, contributingFiles) => {
|
|
595
|
+
const registrations = {
|
|
596
|
+
references: [],
|
|
597
|
+
problems: []
|
|
598
|
+
};
|
|
599
|
+
const visitedModules = /* @__PURE__ */ new Set();
|
|
600
|
+
const visit = (module) => {
|
|
601
|
+
const moduleKey = `${module.filePath}#${module.classDeclaration.name?.text}`;
|
|
602
|
+
if (visitedModules.has(moduleKey)) return;
|
|
603
|
+
visitedModules.add(moduleKey);
|
|
604
|
+
contributingFiles.add(module.filePath);
|
|
605
|
+
const moduleRegistrations = findAppInterceptorProviderRegistrations(module.classDeclaration, module.sourceFile, module.filePath);
|
|
606
|
+
registrations.references.push(...moduleRegistrations.references);
|
|
607
|
+
registrations.problems.push(...moduleRegistrations.problems);
|
|
608
|
+
for (const importedModule of readImportedModules(module)) visit(importedModule);
|
|
609
|
+
};
|
|
610
|
+
visit(rootModule);
|
|
611
|
+
return registrations;
|
|
612
|
+
};
|
|
613
|
+
//#endregion
|
|
614
|
+
//#region src/analysis/find-bootstrap-interceptor-registrations.ts
|
|
615
|
+
var USE_GLOBAL_INTERCEPTORS_METHOD_NAME = "useGlobalInterceptors";
|
|
616
|
+
var collectUseGlobalInterceptorsCalls = (node, calls) => {
|
|
617
|
+
if (isCallExpression(node) && isPropertyAccessExpression(node.expression) && node.expression.name.text === USE_GLOBAL_INTERCEPTORS_METHOD_NAME) calls.push(node);
|
|
618
|
+
node.forEachChild((child) => {
|
|
619
|
+
collectUseGlobalInterceptorsCalls(child, calls);
|
|
620
|
+
});
|
|
621
|
+
};
|
|
622
|
+
/**
|
|
623
|
+
* Returned in registration order: `useGlobalInterceptors` concatenates onto a single global
|
|
624
|
+
* list, so a later call (or a later argument within one call) registers an interceptor that wraps
|
|
625
|
+
* closer to the raw handler result.
|
|
626
|
+
*/
|
|
627
|
+
var findBootstrapInterceptorRegistrations = (sourceFile, filePath) => {
|
|
628
|
+
const calls = [];
|
|
629
|
+
collectUseGlobalInterceptorsCalls(sourceFile, calls);
|
|
630
|
+
const registrations = {
|
|
631
|
+
references: [],
|
|
632
|
+
problems: []
|
|
633
|
+
};
|
|
634
|
+
for (const call of calls) for (const argument of call.arguments) {
|
|
635
|
+
if (isNewExpression(argument) && isIdentifier(argument.expression)) {
|
|
636
|
+
registrations.references.push({
|
|
637
|
+
className: argument.expression.text,
|
|
638
|
+
node: argument,
|
|
639
|
+
sourceFile,
|
|
640
|
+
filePath
|
|
641
|
+
});
|
|
642
|
+
continue;
|
|
643
|
+
}
|
|
644
|
+
registrations.problems.push({
|
|
645
|
+
node: argument,
|
|
646
|
+
sourceFile,
|
|
647
|
+
filePath,
|
|
648
|
+
detail: "Global interceptors must be registered as `new InterceptorClass()` to be analyzed."
|
|
649
|
+
});
|
|
650
|
+
}
|
|
651
|
+
return registrations;
|
|
652
|
+
};
|
|
653
|
+
//#endregion
|
|
654
|
+
//#region src/analysis/find-root-module-class-name.ts
|
|
655
|
+
var NEST_FACTORY_LOCAL_NAME = "NestFactory";
|
|
656
|
+
var NEST_FACTORY_CREATE_METHOD_NAME = "create";
|
|
657
|
+
var NEST_CORE_MODULE_SPECIFIER$1 = "@nestjs/core";
|
|
658
|
+
var collectNestFactoryCreateCalls = (node, calls) => {
|
|
659
|
+
if (isCallExpression(node) && isPropertyAccessExpression(node.expression) && isIdentifier(node.expression.expression) && node.expression.expression.text === NEST_FACTORY_LOCAL_NAME && node.expression.name.text === NEST_FACTORY_CREATE_METHOD_NAME) calls.push(node);
|
|
660
|
+
node.forEachChild((child) => {
|
|
661
|
+
collectNestFactoryCreateCalls(child, calls);
|
|
662
|
+
});
|
|
663
|
+
};
|
|
664
|
+
var findRootModuleClassName = (sourceFile) => {
|
|
665
|
+
const nestFactoryImportSource = findImportSource(sourceFile, NEST_FACTORY_LOCAL_NAME);
|
|
666
|
+
if (nestFactoryImportSource === void 0 || nestFactoryImportSource.moduleSpecifierText !== NEST_CORE_MODULE_SPECIFIER$1) return;
|
|
667
|
+
const calls = [];
|
|
668
|
+
collectNestFactoryCreateCalls(sourceFile, calls);
|
|
669
|
+
if (calls.length !== 1) return;
|
|
670
|
+
const [call] = calls;
|
|
671
|
+
if (call === void 0) return;
|
|
672
|
+
const [moduleArgument] = call.arguments;
|
|
673
|
+
return moduleArgument !== void 0 && isIdentifier(moduleArgument) ? moduleArgument.text : void 0;
|
|
674
|
+
};
|
|
675
|
+
//#endregion
|
|
676
|
+
//#region src/analysis/find-global-interceptor-registrations.ts
|
|
677
|
+
/**
|
|
678
|
+
* The global interceptors in the order Nest runs them, outermost first: the `APP_INTERCEPTOR`
|
|
679
|
+
* providers of the root module and of the local modules it imports, then the interceptors passed
|
|
680
|
+
* to `useGlobalInterceptors()`.
|
|
681
|
+
*
|
|
682
|
+
* Verified against @nestjs/core 12.3.1: `NestFactory.create()` awaits `initialize()`, which calls
|
|
683
|
+
* `DependenciesScanner.applyApplicationProviders()` (nest-factory.js line 108) and pushes every
|
|
684
|
+
* `APP_INTERCEPTOR` instance onto the global list (scanner.js line 386,
|
|
685
|
+
* application-config.js line 60). `useGlobalInterceptors()` can only run once `create()` has
|
|
686
|
+
* returned, and it appends to the same list (application-config.js line 63). The first entry of
|
|
687
|
+
* the list is the outermost interceptor. Request- and transient-scoped `APP_INTERCEPTOR`
|
|
688
|
+
* providers are appended after all of them at request time and are not distinguished here.
|
|
689
|
+
*/
|
|
690
|
+
var findGlobalInterceptorRegistrations = (bootstrapFilePath, contributingFiles) => {
|
|
691
|
+
const bootstrapSourceFile = parseSourceFile(bootstrapFilePath);
|
|
692
|
+
contributingFiles.add(bootstrapFilePath);
|
|
693
|
+
const rootModuleClassName = findRootModuleClassName(bootstrapSourceFile);
|
|
694
|
+
const rootModule = rootModuleClassName === void 0 ? void 0 : resolveClassDeclaration(rootModuleClassName, bootstrapSourceFile, bootstrapFilePath);
|
|
695
|
+
const moduleRegistrations = rootModule === void 0 ? {
|
|
696
|
+
references: [],
|
|
697
|
+
problems: []
|
|
698
|
+
} : collectModuleInterceptorRegistrations(rootModule, contributingFiles);
|
|
699
|
+
const bootstrapRegistrations = findBootstrapInterceptorRegistrations(bootstrapSourceFile, bootstrapFilePath);
|
|
700
|
+
return {
|
|
701
|
+
references: [...moduleRegistrations.references, ...bootstrapRegistrations.references],
|
|
702
|
+
problems: [...moduleRegistrations.problems, ...bootstrapRegistrations.problems]
|
|
703
|
+
};
|
|
704
|
+
};
|
|
705
|
+
//#endregion
|
|
706
|
+
//#region src/analysis/find-use-interceptors-registrations.ts
|
|
707
|
+
var USE_INTERCEPTORS_DECORATOR_NAME = "UseInterceptors";
|
|
708
|
+
var NEST_COMMON_MODULE_SPECIFIER = "@nestjs/common";
|
|
709
|
+
/**
|
|
710
|
+
* Decorators are applied bottom-up and each one appends to the target's interceptor metadata, so
|
|
711
|
+
* stacked `@UseInterceptors()` decorators contribute in reverse source order.
|
|
712
|
+
*/
|
|
713
|
+
var findUseInterceptorsRegistrations = (target, sourceFile, filePath) => {
|
|
714
|
+
const registrations = {
|
|
715
|
+
references: [],
|
|
716
|
+
problems: []
|
|
717
|
+
};
|
|
718
|
+
const importSource = findImportSource(sourceFile, USE_INTERCEPTORS_DECORATOR_NAME);
|
|
719
|
+
if (importSource === void 0 || importSource.moduleSpecifierText !== NEST_COMMON_MODULE_SPECIFIER || importSource.importedName !== USE_INTERCEPTORS_DECORATOR_NAME) return registrations;
|
|
720
|
+
const decorators = findDecorators(target, USE_INTERCEPTORS_DECORATOR_NAME).reverse();
|
|
721
|
+
for (const decorator of decorators) {
|
|
722
|
+
if (!isCallExpression(decorator.expression)) continue;
|
|
723
|
+
for (const argument of decorator.expression.arguments) {
|
|
724
|
+
if (isIdentifier(argument)) {
|
|
725
|
+
registrations.references.push({
|
|
726
|
+
className: argument.text,
|
|
727
|
+
node: argument,
|
|
728
|
+
sourceFile,
|
|
729
|
+
filePath
|
|
730
|
+
});
|
|
731
|
+
continue;
|
|
732
|
+
}
|
|
733
|
+
if (isNewExpression(argument) && isIdentifier(argument.expression)) {
|
|
734
|
+
registrations.references.push({
|
|
735
|
+
className: argument.expression.text,
|
|
736
|
+
node: argument,
|
|
737
|
+
sourceFile,
|
|
738
|
+
filePath
|
|
739
|
+
});
|
|
740
|
+
continue;
|
|
741
|
+
}
|
|
742
|
+
registrations.problems.push({
|
|
743
|
+
node: argument,
|
|
744
|
+
sourceFile,
|
|
745
|
+
filePath,
|
|
746
|
+
detail: "@UseInterceptors() arguments must be interceptor classes or `new InterceptorClass()` to be analyzed."
|
|
747
|
+
});
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
return registrations;
|
|
751
|
+
};
|
|
752
|
+
//#endregion
|
|
753
|
+
//#region src/analysis/create-interceptor-analysis.ts
|
|
754
|
+
var REGISTRATION_SUBJECT = "(interceptors)";
|
|
755
|
+
var INTERCEPT_MEMBER_NAME = "intercept";
|
|
756
|
+
/**
|
|
757
|
+
* Resolves, for each JSON route, the interceptors that apply to it (global, then controller, then
|
|
758
|
+
* route, outermost first) and the response shape they give it. Every interceptor class is
|
|
759
|
+
* analyzed once and every problem is reported once, however many routes the interceptor reaches.
|
|
760
|
+
* Global interceptors are only read when the first JSON route asks for them.
|
|
761
|
+
*/
|
|
762
|
+
var createInterceptorAnalysis = (options) => {
|
|
763
|
+
const { bootstrapFilePath, diagnostics } = options;
|
|
764
|
+
const contributingFiles = /* @__PURE__ */ new Set();
|
|
765
|
+
const shapesByInterceptor = /* @__PURE__ */ new Map();
|
|
766
|
+
let globalRegistrations;
|
|
767
|
+
const reportRegistrationProblem = (problem) => {
|
|
768
|
+
diagnostics.push(createInterceptorDiagnostic({
|
|
769
|
+
controllerName: REGISTRATION_SUBJECT,
|
|
770
|
+
memberName: "",
|
|
771
|
+
detail: problem.detail,
|
|
772
|
+
location: problem.node,
|
|
773
|
+
sourceFile: problem.sourceFile,
|
|
774
|
+
filePath: problem.filePath
|
|
775
|
+
}));
|
|
31
776
|
};
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
777
|
+
const reportUnresolvedReference = (reference) => {
|
|
778
|
+
diagnostics.push(createInterceptorDiagnostic({
|
|
779
|
+
controllerName: reference.className,
|
|
780
|
+
memberName: "",
|
|
781
|
+
detail: `Interceptor \`${reference.className}\` does not resolve to a class declared in a local source file, so its effect on the response shape is unknown.`,
|
|
782
|
+
location: reference.node,
|
|
783
|
+
sourceFile: reference.sourceFile,
|
|
784
|
+
filePath: reference.filePath
|
|
785
|
+
}));
|
|
36
786
|
};
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
787
|
+
const resolveShape = (reference) => {
|
|
788
|
+
const resolved = resolveClassDeclaration(reference.className, reference.sourceFile, reference.filePath);
|
|
789
|
+
if (resolved === void 0) {
|
|
790
|
+
const location = locationOf(reference.sourceFile, reference.node);
|
|
791
|
+
const referenceKey = `${reference.filePath}:${location.line}:${location.column}`;
|
|
792
|
+
if (!shapesByInterceptor.has(referenceKey)) {
|
|
793
|
+
shapesByInterceptor.set(referenceKey, void 0);
|
|
794
|
+
reportUnresolvedReference(reference);
|
|
795
|
+
}
|
|
796
|
+
return;
|
|
797
|
+
}
|
|
798
|
+
contributingFiles.add(resolved.filePath);
|
|
799
|
+
const interceptorKey = `${resolved.filePath}#${resolved.classDeclaration.name?.text}`;
|
|
800
|
+
if (shapesByInterceptor.has(interceptorKey)) return shapesByInterceptor.get(interceptorKey);
|
|
801
|
+
const analysis = analyzeInterceptorClass(resolved);
|
|
802
|
+
if (!analysis.ok) diagnostics.push(createInterceptorDiagnostic({
|
|
803
|
+
controllerName: resolved.classDeclaration.name?.text ?? reference.className,
|
|
804
|
+
memberName: INTERCEPT_MEMBER_NAME,
|
|
805
|
+
detail: analysis.reason,
|
|
806
|
+
found: analysis.found,
|
|
807
|
+
location: analysis.location,
|
|
808
|
+
sourceFile: resolved.sourceFile,
|
|
809
|
+
filePath: resolved.filePath
|
|
810
|
+
}));
|
|
811
|
+
const shape = analysis.ok ? analysis.shape : void 0;
|
|
812
|
+
shapesByInterceptor.set(interceptorKey, shape);
|
|
813
|
+
return shape;
|
|
814
|
+
};
|
|
815
|
+
const readGlobalRegistrations = () => {
|
|
816
|
+
if (globalRegistrations === void 0) {
|
|
817
|
+
globalRegistrations = bootstrapFilePath === void 0 ? {
|
|
818
|
+
references: [],
|
|
819
|
+
problems: []
|
|
820
|
+
} : findGlobalInterceptorRegistrations(bootstrapFilePath, contributingFiles);
|
|
821
|
+
globalRegistrations.problems.forEach(reportRegistrationProblem);
|
|
822
|
+
}
|
|
823
|
+
return globalRegistrations;
|
|
824
|
+
};
|
|
825
|
+
const forController = (classDeclaration, sourceFile, filePath) => {
|
|
826
|
+
let controllerRegistrations;
|
|
827
|
+
const readControllerRegistrations = () => {
|
|
828
|
+
if (controllerRegistrations === void 0) {
|
|
829
|
+
controllerRegistrations = findUseInterceptorsRegistrations(classDeclaration, sourceFile, filePath);
|
|
830
|
+
controllerRegistrations.problems.forEach(reportRegistrationProblem);
|
|
831
|
+
}
|
|
832
|
+
return controllerRegistrations;
|
|
833
|
+
};
|
|
834
|
+
const responseWrapperOf = (method) => {
|
|
835
|
+
const methodRegistrations = findUseInterceptorsRegistrations(method, sourceFile, filePath);
|
|
836
|
+
methodRegistrations.problems.forEach(reportRegistrationProblem);
|
|
837
|
+
const chain = [
|
|
838
|
+
readGlobalRegistrations(),
|
|
839
|
+
readControllerRegistrations(),
|
|
840
|
+
methodRegistrations
|
|
841
|
+
];
|
|
842
|
+
const shapes = chain.flatMap(({ references }) => references.map(resolveShape));
|
|
843
|
+
if (chain.some(({ problems }) => problems.length > 0) || shapes.some((shape) => shape === void 0)) return { wrappedResultTypeSource: UNRESOLVED_WRAPPED_RESULT_TYPE_SOURCE };
|
|
844
|
+
const wrappedResultTypeSource = composeWrapperTypeSource(shapes.filter((shape) => shape !== void 0));
|
|
845
|
+
return wrappedResultTypeSource === void 0 ? void 0 : { wrappedResultTypeSource };
|
|
846
|
+
};
|
|
847
|
+
return { responseWrapperOf };
|
|
40
848
|
};
|
|
41
849
|
return {
|
|
42
|
-
|
|
43
|
-
|
|
850
|
+
forController,
|
|
851
|
+
contributingFiles: () => [...contributingFiles]
|
|
44
852
|
};
|
|
45
853
|
};
|
|
46
854
|
//#endregion
|
|
@@ -79,15 +887,6 @@ var findPayloadParameterIndex = (method) => {
|
|
|
79
887
|
return 0;
|
|
80
888
|
};
|
|
81
889
|
//#endregion
|
|
82
|
-
//#region src/analysis/node-location.ts
|
|
83
|
-
var locationOf = (sourceFile, node) => {
|
|
84
|
-
const { line, character } = sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile));
|
|
85
|
-
return {
|
|
86
|
-
line: line + 1,
|
|
87
|
-
column: character + 1
|
|
88
|
-
};
|
|
89
|
-
};
|
|
90
|
-
//#endregion
|
|
91
890
|
//#region src/analysis/read-literal-type.ts
|
|
92
891
|
var readLiteralType = (type, checker, location) => {
|
|
93
892
|
if (type.isStringLiteralType() || type.isNumberLiteralType() || type.isBooleanLiteralType()) return type.value;
|
|
@@ -566,47 +1365,33 @@ var findControllerClasses = (sourceFile) => {
|
|
|
566
1365
|
return sourceFile.statements.filter((statement) => isClassDeclaration(statement) && findDecorator(statement, "Controller") !== void 0);
|
|
567
1366
|
};
|
|
568
1367
|
//#endregion
|
|
569
|
-
//#region src/analysis/open-source-file.ts
|
|
570
|
-
var sharedApi;
|
|
571
|
-
/**
|
|
572
|
-
* The returned checker belongs to the snapshot opened for this file. Opening the next file
|
|
573
|
-
* replaces that snapshot, so finish every checker query before calling this again.
|
|
574
|
-
*/
|
|
575
|
-
var openSourceFile = (filePath) => {
|
|
576
|
-
sharedApi ??= new API();
|
|
577
|
-
sharedApi.updateSnapshot({ closeFiles: [filePath] });
|
|
578
|
-
const project = sharedApi.updateSnapshot({
|
|
579
|
-
openFiles: [filePath],
|
|
580
|
-
fileChanges: { changed: [filePath] }
|
|
581
|
-
}).getDefaultProjectForFile(filePath);
|
|
582
|
-
const sourceFile = project?.program.getSourceFile(filePath);
|
|
583
|
-
if (project === void 0 || sourceFile === void 0) throw new Error(`Unable to parse source file: ${filePath}`);
|
|
584
|
-
return {
|
|
585
|
-
sourceFile,
|
|
586
|
-
checker: project.checker
|
|
587
|
-
};
|
|
588
|
-
};
|
|
589
|
-
//#endregion
|
|
590
1368
|
//#region src/analysis/analyze-controllers.ts
|
|
591
1369
|
var analyzeControllers = (filePaths, options = {}) => {
|
|
592
1370
|
const diagnostics = [];
|
|
593
1371
|
const controllers = [];
|
|
1372
|
+
const interceptors = createInterceptorAnalysis({
|
|
1373
|
+
bootstrapFilePath: options.bootstrapFilePath,
|
|
1374
|
+
diagnostics
|
|
1375
|
+
});
|
|
1376
|
+
clearParsedFileCache();
|
|
594
1377
|
for (const filePath of filePaths) {
|
|
595
1378
|
const { sourceFile, checker } = openSourceFile(filePath);
|
|
596
|
-
|
|
597
|
-
|
|
1379
|
+
const extractedControllers = findControllerClasses(sourceFile).map((classDeclaration) => ({
|
|
1380
|
+
classDeclaration,
|
|
1381
|
+
controller: extractControllerDefinition(classDeclaration, {
|
|
598
1382
|
sourceFile,
|
|
599
1383
|
filePath,
|
|
600
1384
|
checker,
|
|
601
1385
|
messagePatterns: options.messagePatterns ?? false,
|
|
602
1386
|
diagnostics
|
|
603
|
-
})
|
|
604
|
-
|
|
605
|
-
}
|
|
1387
|
+
})
|
|
1388
|
+
}));
|
|
1389
|
+
for (const { classDeclaration, controller } of extractedControllers) if (controller !== void 0) controllers.push(attachResponseWrappers(controller, classDeclaration, interceptors.forController(classDeclaration, sourceFile, filePath)));
|
|
606
1390
|
}
|
|
607
1391
|
return {
|
|
608
1392
|
controllers,
|
|
609
|
-
diagnostics
|
|
1393
|
+
diagnostics,
|
|
1394
|
+
interceptorFiles: interceptors.contributingFiles()
|
|
610
1395
|
};
|
|
611
1396
|
};
|
|
612
1397
|
//#endregion
|
|
@@ -745,72 +1530,6 @@ var extractResolverArgumentDefinition = (parameter, index, context) => {
|
|
|
745
1530
|
};
|
|
746
1531
|
};
|
|
747
1532
|
//#endregion
|
|
748
|
-
//#region src/analysis/parse-source-file.ts
|
|
749
|
-
var parseSourceFile = (filePath) => openSourceFile(filePath).sourceFile;
|
|
750
|
-
//#endregion
|
|
751
|
-
//#region src/analysis/resolve-type-declaration.ts
|
|
752
|
-
var parsedFilesByPath = /* @__PURE__ */ new Map();
|
|
753
|
-
var parseFile = (filePath) => {
|
|
754
|
-
const cached = parsedFilesByPath.get(filePath);
|
|
755
|
-
if (cached !== void 0) return cached;
|
|
756
|
-
const sourceFile = parseSourceFile(filePath);
|
|
757
|
-
parsedFilesByPath.set(filePath, sourceFile);
|
|
758
|
-
return sourceFile;
|
|
759
|
-
};
|
|
760
|
-
var resolveModuleFile = (fromFilePath, moduleSpecifierText) => {
|
|
761
|
-
if (!moduleSpecifierText.startsWith(".")) return;
|
|
762
|
-
const base = resolve(dirname(fromFilePath), moduleSpecifierText);
|
|
763
|
-
return [
|
|
764
|
-
`${base}.ts`,
|
|
765
|
-
`${base}.tsx`,
|
|
766
|
-
resolve(base, "index.ts")
|
|
767
|
-
].find((candidate) => existsSync(candidate));
|
|
768
|
-
};
|
|
769
|
-
var findExportedDeclaration = (sourceFile, name) => sourceFile.statements.find((statement) => isClassDeclaration(statement) && statement.name?.text === name);
|
|
770
|
-
var findImportSource = (sourceFile, localName) => {
|
|
771
|
-
for (const statement of sourceFile.statements) {
|
|
772
|
-
if (!isImportDeclaration(statement) || !isStringLiteralLikeNode(statement.moduleSpecifier)) continue;
|
|
773
|
-
const namedBindings = statement.importClause?.namedBindings;
|
|
774
|
-
if (namedBindings === void 0 || !isNamedImports(namedBindings)) continue;
|
|
775
|
-
const element = namedBindings.elements.find((candidate) => candidate.name.text === localName);
|
|
776
|
-
if (element !== void 0) return {
|
|
777
|
-
moduleSpecifierText: statement.moduleSpecifier.text,
|
|
778
|
-
importedName: element.propertyName?.text ?? element.name.text
|
|
779
|
-
};
|
|
780
|
-
}
|
|
781
|
-
};
|
|
782
|
-
var findReExportSource = (sourceFile, name) => {
|
|
783
|
-
for (const statement of sourceFile.statements) {
|
|
784
|
-
if (!isExportDeclaration(statement) || statement.moduleSpecifier === void 0 || !isStringLiteralLikeNode(statement.moduleSpecifier)) continue;
|
|
785
|
-
if (statement.exportClause === void 0) return statement.moduleSpecifier.text;
|
|
786
|
-
if (isNamedExports(statement.exportClause) && statement.exportClause.elements.some((element) => (element.propertyName?.text ?? element.name.text) === name)) return statement.moduleSpecifier.text;
|
|
787
|
-
}
|
|
788
|
-
};
|
|
789
|
-
var resolveClassDeclaration = (name, fromSourceFile, fromFilePath, visited = /* @__PURE__ */ new Set()) => {
|
|
790
|
-
if (visited.has(fromFilePath)) return;
|
|
791
|
-
visited.add(fromFilePath);
|
|
792
|
-
const direct = findExportedDeclaration(fromSourceFile, name);
|
|
793
|
-
if (direct !== void 0) return {
|
|
794
|
-
classDeclaration: direct,
|
|
795
|
-
sourceFile: fromSourceFile,
|
|
796
|
-
filePath: fromFilePath
|
|
797
|
-
};
|
|
798
|
-
const importSource = findImportSource(fromSourceFile, name);
|
|
799
|
-
if (importSource !== void 0) {
|
|
800
|
-
const resolvedPath = resolveModuleFile(fromFilePath, importSource.moduleSpecifierText);
|
|
801
|
-
if (resolvedPath !== void 0) {
|
|
802
|
-
const nextSourceFile = parseFile(resolvedPath);
|
|
803
|
-
const result = resolveClassDeclaration(importSource.importedName, nextSourceFile, resolvedPath, visited);
|
|
804
|
-
if (result !== void 0) return result;
|
|
805
|
-
}
|
|
806
|
-
}
|
|
807
|
-
const reExportModuleSpecifierText = findReExportSource(fromSourceFile, name);
|
|
808
|
-
if (reExportModuleSpecifierText !== void 0) {
|
|
809
|
-
const resolvedPath = resolveModuleFile(fromFilePath, reExportModuleSpecifierText);
|
|
810
|
-
if (resolvedPath !== void 0) return resolveClassDeclaration(name, parseFile(resolvedPath), resolvedPath, visited);
|
|
811
|
-
}
|
|
812
|
-
};
|
|
813
|
-
//#endregion
|
|
814
1533
|
//#region src/analysis/extract-selection-fields.ts
|
|
815
1534
|
var PRIMITIVE_SCALAR_NAMES$1 = /* @__PURE__ */ new Set([
|
|
816
1535
|
"string",
|
|
@@ -1119,7 +1838,7 @@ var PROVIDERS_PROPERTY_NAME = "providers";
|
|
|
1119
1838
|
var PROVIDE_PROPERTY_NAME = "provide";
|
|
1120
1839
|
var USE_CLASS_PROPERTY_NAME = "useClass";
|
|
1121
1840
|
var APP_FILTER_LOCAL_NAME = "APP_FILTER";
|
|
1122
|
-
var NEST_CORE_MODULE_SPECIFIER
|
|
1841
|
+
var NEST_CORE_MODULE_SPECIFIER = "@nestjs/core";
|
|
1123
1842
|
var findAppFilterProviderClassName = (moduleClassDeclaration, moduleSourceFile) => {
|
|
1124
1843
|
const moduleDecorator = findDecorator(moduleClassDeclaration, MODULE_DECORATOR_NAME);
|
|
1125
1844
|
if (moduleDecorator === void 0 || !isCallExpression(moduleDecorator.expression)) return;
|
|
@@ -1128,7 +1847,7 @@ var findAppFilterProviderClassName = (moduleClassDeclaration, moduleSourceFile)
|
|
|
1128
1847
|
const providersProperty = moduleOptions.properties.find((property) => isPropertyAssignment(property) && isIdentifier(property.name) && property.name.text === PROVIDERS_PROPERTY_NAME);
|
|
1129
1848
|
if (providersProperty === void 0 || !isArrayLiteralExpression(providersProperty.initializer)) return;
|
|
1130
1849
|
const appFilterImportSource = findImportSource(moduleSourceFile, APP_FILTER_LOCAL_NAME);
|
|
1131
|
-
if (appFilterImportSource === void 0 || appFilterImportSource.moduleSpecifierText !== NEST_CORE_MODULE_SPECIFIER
|
|
1850
|
+
if (appFilterImportSource === void 0 || appFilterImportSource.moduleSpecifierText !== NEST_CORE_MODULE_SPECIFIER) return;
|
|
1132
1851
|
const classNames = /* @__PURE__ */ new Set();
|
|
1133
1852
|
for (const element of providersProperty.initializer.elements) {
|
|
1134
1853
|
if (isSpreadElement(element)) return;
|
|
@@ -1162,28 +1881,6 @@ var findGlobalFilterClassName = (sourceFile) => {
|
|
|
1162
1881
|
return classNames.size === 1 ? [...classNames][0] : void 0;
|
|
1163
1882
|
};
|
|
1164
1883
|
//#endregion
|
|
1165
|
-
//#region src/analysis/find-root-module-class-name.ts
|
|
1166
|
-
var NEST_FACTORY_LOCAL_NAME = "NestFactory";
|
|
1167
|
-
var NEST_FACTORY_CREATE_METHOD_NAME = "create";
|
|
1168
|
-
var NEST_CORE_MODULE_SPECIFIER = "@nestjs/core";
|
|
1169
|
-
var collectNestFactoryCreateCalls = (node, calls) => {
|
|
1170
|
-
if (isCallExpression(node) && isPropertyAccessExpression(node.expression) && isIdentifier(node.expression.expression) && node.expression.expression.text === NEST_FACTORY_LOCAL_NAME && node.expression.name.text === NEST_FACTORY_CREATE_METHOD_NAME) calls.push(node);
|
|
1171
|
-
node.forEachChild((child) => {
|
|
1172
|
-
collectNestFactoryCreateCalls(child, calls);
|
|
1173
|
-
});
|
|
1174
|
-
};
|
|
1175
|
-
var findRootModuleClassName = (sourceFile) => {
|
|
1176
|
-
const nestFactoryImportSource = findImportSource(sourceFile, NEST_FACTORY_LOCAL_NAME);
|
|
1177
|
-
if (nestFactoryImportSource === void 0 || nestFactoryImportSource.moduleSpecifierText !== NEST_CORE_MODULE_SPECIFIER) return;
|
|
1178
|
-
const calls = [];
|
|
1179
|
-
collectNestFactoryCreateCalls(sourceFile, calls);
|
|
1180
|
-
if (calls.length !== 1) return;
|
|
1181
|
-
const [call] = calls;
|
|
1182
|
-
if (call === void 0) return;
|
|
1183
|
-
const [moduleArgument] = call.arguments;
|
|
1184
|
-
return moduleArgument !== void 0 && isIdentifier(moduleArgument) ? moduleArgument.text : void 0;
|
|
1185
|
-
};
|
|
1186
|
-
//#endregion
|
|
1187
1884
|
//#region src/analysis/resolve-catch-method-body.ts
|
|
1188
1885
|
var RESPONSE_BODY_CALL_NAMES = /* @__PURE__ */ new Set(["json", "send"]);
|
|
1189
1886
|
var matchResponseBodyArgument = (expression) => {
|
|
@@ -1287,6 +1984,7 @@ var reconcileResolvedFilter = (viaGlobalFilters, viaAppFilterProvider) => {
|
|
|
1287
1984
|
return viaGlobalFilters ?? viaAppFilterProvider;
|
|
1288
1985
|
};
|
|
1289
1986
|
var detectErrorResponseShape = (bootstrapFilePath) => {
|
|
1987
|
+
clearParsedFileCache();
|
|
1290
1988
|
const bootstrapSourceFile = parseSourceFile(bootstrapFilePath);
|
|
1291
1989
|
const resolved = reconcileResolvedFilter(resolveViaGlobalFilters(bootstrapSourceFile, bootstrapFilePath), resolveViaAppFilterProvider(bootstrapSourceFile, bootstrapFilePath));
|
|
1292
1990
|
if (resolved === void 0 || findDecorator(resolved.classDeclaration, CATCH_DECORATOR_NAME) === void 0) return;
|
|
@@ -1444,136 +2142,6 @@ var detectGlobalPrefix = (bootstrapFilePath) => {
|
|
|
1444
2142
|
};
|
|
1445
2143
|
};
|
|
1446
2144
|
//#endregion
|
|
1447
|
-
//#region src/analysis/find-global-interceptor-class-names.ts
|
|
1448
|
-
var USE_GLOBAL_INTERCEPTORS_METHOD_NAME = "useGlobalInterceptors";
|
|
1449
|
-
var collectUseGlobalInterceptorsCalls = (node, calls) => {
|
|
1450
|
-
if (isCallExpression(node) && isPropertyAccessExpression(node.expression) && node.expression.name.text === USE_GLOBAL_INTERCEPTORS_METHOD_NAME) calls.push(node);
|
|
1451
|
-
node.forEachChild((child) => {
|
|
1452
|
-
collectUseGlobalInterceptorsCalls(child, calls);
|
|
1453
|
-
});
|
|
1454
|
-
};
|
|
1455
|
-
/**
|
|
1456
|
-
* Returned in registration order: `useGlobalInterceptors` concatenates onto a
|
|
1457
|
-
* single global list, so a later call (or a later argument within one call)
|
|
1458
|
-
* always registers an interceptor that wraps closer to the raw handler result.
|
|
1459
|
-
*/
|
|
1460
|
-
var findGlobalInterceptorClassNames = (sourceFile) => {
|
|
1461
|
-
const calls = [];
|
|
1462
|
-
collectUseGlobalInterceptorsCalls(sourceFile, calls);
|
|
1463
|
-
if (calls.length === 0) return;
|
|
1464
|
-
const classNames = [];
|
|
1465
|
-
for (const call of calls) for (const argument of call.arguments) {
|
|
1466
|
-
if (!isNewExpression(argument) || !isIdentifier(argument.expression)) return;
|
|
1467
|
-
classNames.push(argument.expression.text);
|
|
1468
|
-
}
|
|
1469
|
-
return classNames.length === 0 ? void 0 : classNames;
|
|
1470
|
-
};
|
|
1471
|
-
//#endregion
|
|
1472
|
-
//#region src/analysis/synthesize-generic-object-type.ts
|
|
1473
|
-
var synthesizeExpressionType = (expression, parameterName, substitution, state) => {
|
|
1474
|
-
if (isIdentifier(expression) && expression.text === parameterName) {
|
|
1475
|
-
state.substitutionCount += 1;
|
|
1476
|
-
return substitution;
|
|
1477
|
-
}
|
|
1478
|
-
if (isObjectLiteralExpression(expression)) {
|
|
1479
|
-
const memberTypes = [];
|
|
1480
|
-
for (const property of expression.properties) {
|
|
1481
|
-
if (!isPropertyAssignment(property) || !(isIdentifier(property.name) || isStringLiteral(property.name))) return;
|
|
1482
|
-
const valueType = synthesizeExpressionType(property.initializer, parameterName, substitution, state);
|
|
1483
|
-
if (valueType === void 0) return;
|
|
1484
|
-
memberTypes.push(`${property.name.text}: ${valueType}`);
|
|
1485
|
-
}
|
|
1486
|
-
return `{ ${memberTypes.join("; ")} }`;
|
|
1487
|
-
}
|
|
1488
|
-
if (isStringLiteral(expression)) return "string";
|
|
1489
|
-
if (isNumericLiteral(expression)) return "number";
|
|
1490
|
-
if (isTrueLiteral(expression) || isFalseLiteral(expression)) return "boolean";
|
|
1491
|
-
if (isNullLiteral(expression)) return "null";
|
|
1492
|
-
};
|
|
1493
|
-
var synthesizeGenericObjectType = (objectLiteral, parameterName, substitution) => {
|
|
1494
|
-
const state = { substitutionCount: 0 };
|
|
1495
|
-
const objectType = synthesizeExpressionType(objectLiteral, parameterName, substitution, state);
|
|
1496
|
-
return objectType !== void 0 && state.substitutionCount === 1 ? objectType : void 0;
|
|
1497
|
-
};
|
|
1498
|
-
//#endregion
|
|
1499
|
-
//#region src/analysis/detect-response-wrapper.ts
|
|
1500
|
-
var INTERCEPT_METHOD_NAME = "intercept";
|
|
1501
|
-
var PIPE_MEMBER_NAME = "pipe";
|
|
1502
|
-
var MAP_OPERATOR_NAME = "map";
|
|
1503
|
-
var RXJS_MODULE_SPECIFIER = "rxjs";
|
|
1504
|
-
var WRAPPED_RESULT_SUBSTITUTION = "Awaited<Result>";
|
|
1505
|
-
var unwrapParenthesizedExpression = (expression) => {
|
|
1506
|
-
let unwrapped = expression;
|
|
1507
|
-
while (isParenthesizedExpression(unwrapped)) unwrapped = unwrapped.expression;
|
|
1508
|
-
return unwrapped;
|
|
1509
|
-
};
|
|
1510
|
-
var resolveSoleReturnExpression = (body) => {
|
|
1511
|
-
if (!isBlock(body)) return unwrapParenthesizedExpression(body);
|
|
1512
|
-
const localDeclarations = /* @__PURE__ */ new Map();
|
|
1513
|
-
let returnExpression;
|
|
1514
|
-
let sawReturn = false;
|
|
1515
|
-
for (const statement of body.statements) {
|
|
1516
|
-
if (sawReturn) return;
|
|
1517
|
-
if (isVariableStatement(statement)) {
|
|
1518
|
-
for (const declaration of statement.declarationList.declarations) {
|
|
1519
|
-
if (!isIdentifier(declaration.name) || declaration.initializer === void 0) return;
|
|
1520
|
-
localDeclarations.set(declaration.name.text, declaration.initializer);
|
|
1521
|
-
}
|
|
1522
|
-
continue;
|
|
1523
|
-
}
|
|
1524
|
-
if (isReturnStatement(statement)) {
|
|
1525
|
-
sawReturn = true;
|
|
1526
|
-
returnExpression = statement.expression;
|
|
1527
|
-
continue;
|
|
1528
|
-
}
|
|
1529
|
-
return;
|
|
1530
|
-
}
|
|
1531
|
-
if (returnExpression === void 0) return;
|
|
1532
|
-
return isIdentifier(returnExpression) ? localDeclarations.get(returnExpression.text) : returnExpression;
|
|
1533
|
-
};
|
|
1534
|
-
var matchPipedMapCallback = (expression, interceptorSourceFile) => {
|
|
1535
|
-
if (!isCallExpression(expression) || !isPropertyAccessExpression(expression.expression) || expression.expression.name.text !== PIPE_MEMBER_NAME || expression.arguments.length !== 1) return;
|
|
1536
|
-
const [operator] = expression.arguments;
|
|
1537
|
-
if (operator === void 0 || !isCallExpression(operator) || !isIdentifier(operator.expression) || operator.expression.text !== MAP_OPERATOR_NAME || operator.arguments.length !== 1) return;
|
|
1538
|
-
const mapImportSource = findImportSource(interceptorSourceFile, operator.expression.text);
|
|
1539
|
-
if (mapImportSource === void 0 || mapImportSource.moduleSpecifierText !== RXJS_MODULE_SPECIFIER) return;
|
|
1540
|
-
const [callback] = operator.arguments;
|
|
1541
|
-
return callback !== void 0 && (isArrowFunction(callback) || isFunctionExpression(callback)) ? callback : void 0;
|
|
1542
|
-
};
|
|
1543
|
-
var findInterceptMethod = (members) => members.find((member) => isMethodDeclaration(member) && isIdentifier(member.name) && member.name.text === INTERCEPT_METHOD_NAME);
|
|
1544
|
-
var synthesizeInterceptorWrapperType = (interceptorClassName, bootstrapSourceFile, bootstrapFilePath, innerTypeSource) => {
|
|
1545
|
-
const resolved = resolveClassDeclaration(interceptorClassName, bootstrapSourceFile, bootstrapFilePath);
|
|
1546
|
-
if (resolved === void 0) return;
|
|
1547
|
-
const interceptMethod = findInterceptMethod(resolved.classDeclaration.members);
|
|
1548
|
-
if (interceptMethod?.body === void 0) return;
|
|
1549
|
-
const returnExpression = resolveSoleReturnExpression(interceptMethod.body);
|
|
1550
|
-
if (returnExpression === void 0) return;
|
|
1551
|
-
const mapCallback = matchPipedMapCallback(returnExpression, resolved.sourceFile);
|
|
1552
|
-
if (mapCallback === void 0 || mapCallback.parameters.length !== 1) return;
|
|
1553
|
-
const [callbackParameter] = mapCallback.parameters;
|
|
1554
|
-
if (callbackParameter === void 0 || !isIdentifier(callbackParameter.name)) return;
|
|
1555
|
-
const callbackReturnExpression = resolveSoleReturnExpression(mapCallback.body);
|
|
1556
|
-
if (callbackReturnExpression === void 0 || !isObjectLiteralExpression(callbackReturnExpression)) return;
|
|
1557
|
-
return synthesizeGenericObjectType(callbackReturnExpression, callbackParameter.name.text, innerTypeSource);
|
|
1558
|
-
};
|
|
1559
|
-
/**
|
|
1560
|
-
* Interceptors registered first wrap those registered after them, so the
|
|
1561
|
-
* composed type is built from the last-registered interceptor (closest to the
|
|
1562
|
-
* raw handler result) outward to the first-registered one.
|
|
1563
|
-
*/
|
|
1564
|
-
var detectResponseWrapper = (bootstrapFilePath) => {
|
|
1565
|
-
const bootstrapSourceFile = parseSourceFile(bootstrapFilePath);
|
|
1566
|
-
const interceptorClassNames = findGlobalInterceptorClassNames(bootstrapSourceFile);
|
|
1567
|
-
if (interceptorClassNames === void 0) return;
|
|
1568
|
-
let composedTypeSource = WRAPPED_RESULT_SUBSTITUTION;
|
|
1569
|
-
for (const interceptorClassName of [...interceptorClassNames].reverse()) {
|
|
1570
|
-
const wrapperTypeSource = synthesizeInterceptorWrapperType(interceptorClassName, bootstrapSourceFile, bootstrapFilePath, composedTypeSource);
|
|
1571
|
-
if (wrapperTypeSource === void 0) return;
|
|
1572
|
-
composedTypeSource = wrapperTypeSource;
|
|
1573
|
-
}
|
|
1574
|
-
return { wrappedResultTypeSource: composedTypeSource };
|
|
1575
|
-
};
|
|
1576
|
-
//#endregion
|
|
1577
2145
|
//#region src/analysis/detect-versioning.ts
|
|
1578
2146
|
var ENABLE_VERSIONING_METHOD_NAME = "enableVersioning";
|
|
1579
2147
|
var VERSIONING_TYPE_ENUM_NAME = "VersioningType";
|
|
@@ -1659,20 +2227,41 @@ var stripExtension = (filePath) => {
|
|
|
1659
2227
|
const extension = extname(filePath);
|
|
1660
2228
|
return extension.length > 0 ? filePath.slice(0, -extension.length) : filePath;
|
|
1661
2229
|
};
|
|
1662
|
-
var
|
|
2230
|
+
var dependsOnServerResult = (wrapper) => wrapper.wrappedResultTypeSource.includes(WRAPPED_RESULT_SUBSTITUTION);
|
|
2231
|
+
var wrapperOf = (method) => (method.responseKind ?? "json") === "json" ? method.responseWrapper : void 0;
|
|
2232
|
+
/**
|
|
2233
|
+
* Wrappers that depend on the server result get a generic alias, in order of first use, so the
|
|
2234
|
+
* first one keeps the name `__NestBridgeWrapped`. A wrapper that does not depend on it, such as
|
|
2235
|
+
* `unknown`, is written inline.
|
|
2236
|
+
*/
|
|
2237
|
+
var aliasedWrapperSourcesOf = (methods) => {
|
|
2238
|
+
const sources = /* @__PURE__ */ new Set();
|
|
2239
|
+
for (const method of methods) {
|
|
2240
|
+
const wrapper = wrapperOf(method);
|
|
2241
|
+
if (wrapper !== void 0 && dependsOnServerResult(wrapper)) sources.add(wrapper.wrappedResultTypeSource);
|
|
2242
|
+
}
|
|
2243
|
+
return [...sources];
|
|
2244
|
+
};
|
|
2245
|
+
var wrapperHelperName = (aliasIndex) => aliasIndex === 0 ? WRAPPED_RESULT_HELPER : `${WRAPPED_RESULT_HELPER}${aliasIndex + 1}`;
|
|
2246
|
+
var generateControllerDeclaration = (controller, outputFilePath) => {
|
|
1663
2247
|
const sourceFileSpecifier = importSpecifierFor(controller.sourceFile, outputFilePath);
|
|
1664
2248
|
const importSpecifier = stripExtension(sourceFileSpecifier);
|
|
1665
2249
|
const serverAlias = `__Server${controller.name}`;
|
|
1666
|
-
const
|
|
2250
|
+
const aliasedWrapperSources = aliasedWrapperSourcesOf(controller.methods);
|
|
2251
|
+
const resultTypeFor = (method) => {
|
|
2252
|
+
const serverMethod = `${serverAlias}['${method.name}']`;
|
|
2253
|
+
const wrapper = wrapperOf(method);
|
|
2254
|
+
if (wrapper === void 0) return `${REMOTE_RESULT_HELPERS[method.responseKind ?? "json"]}<${serverMethod}>`;
|
|
2255
|
+
if (!dependsOnServerResult(wrapper)) return `${ABORTABLE_PROMISE}<${wrapper.wrappedResultTypeSource}>`;
|
|
2256
|
+
return `${wrapperHelperName(aliasedWrapperSources.indexOf(wrapper.wrappedResultTypeSource))}<${serverMethod}>`;
|
|
2257
|
+
};
|
|
2258
|
+
const importedHelperFor = (method) => wrapperOf(method) === void 0 ? REMOTE_RESULT_HELPERS[method.responseKind ?? "json"] : ABORTABLE_PROMISE;
|
|
1667
2259
|
const parameterListFor = (method) => {
|
|
1668
2260
|
const serverParameters = `Parameters<${serverAlias}['${method.name}']>`;
|
|
1669
2261
|
if ((method.serverOnlyParameterIndexes ?? []).length === 0) return `...args: ${serverParameters}`;
|
|
1670
2262
|
return [...method.parameters ?? []].sort((a, b) => a.index - b.index).map((parameter) => `${parameter.parameterName}: ${serverParameters}[${parameter.index}]`).join(", ");
|
|
1671
2263
|
};
|
|
1672
|
-
const methodDeclarations = controller.methods.map((method) => {
|
|
1673
|
-
const helperName = helperNameFor(method);
|
|
1674
|
-
return ` ${method.name}(${parameterListFor(method)}): ${helperName}<${serverAlias}['${method.name}']>;`;
|
|
1675
|
-
});
|
|
2264
|
+
const methodDeclarations = controller.methods.map((method) => ` ${method.name}(${parameterListFor(method)}): ${resultTypeFor(method)};`);
|
|
1676
2265
|
const messageHandlers = controller.messageHandlers ?? [];
|
|
1677
2266
|
const messageHandlerDeclarations = messageHandlers.map((handler) => {
|
|
1678
2267
|
const serverMethod = `${serverAlias}['${handler.name}']`;
|
|
@@ -1686,12 +2275,10 @@ var generateControllerDeclaration = (controller, outputFilePath, responseWrapper
|
|
|
1686
2275
|
...methodDeclarations,
|
|
1687
2276
|
...messageHandlerDeclarations
|
|
1688
2277
|
].join("\n");
|
|
1689
|
-
const
|
|
1690
|
-
const hasWrappedMethod = rawHelperNames.includes(WRAPPED_RESULT_HELPER);
|
|
1691
|
-
const remoteHelperImports = Array.from(new Set(rawHelperNames.map((name) => name === WRAPPED_RESULT_HELPER ? ABORTABLE_PROMISE : name))).sort();
|
|
2278
|
+
const remoteHelperImports = Array.from(new Set(controller.methods.map((method) => importedHelperFor(method)))).sort();
|
|
1692
2279
|
const helperImports = remoteHelperImports.length === 0 && messageHandlers.length === 0 ? ["RemoteResult"] : remoteHelperImports;
|
|
1693
2280
|
const usesMessageResult = messageHandlers.some((handler) => handler.kind === "message");
|
|
1694
|
-
const wrappedResultTypeAliasLines =
|
|
2281
|
+
const wrappedResultTypeAliasLines = aliasedWrapperSources.length === 0 ? [] : [...aliasedWrapperSources.map((source, aliasIndex) => `type ${wrapperHelperName(aliasIndex)}<T> = T extends (...args: never[]) => infer Result ? ${ABORTABLE_PROMISE}<${source}> : never;`), ""];
|
|
1695
2282
|
return [
|
|
1696
2283
|
`import type { ${controller.name} as ${serverAlias} } from '${importSpecifier}';`,
|
|
1697
2284
|
...helperImports.length > 0 ? [`import type { ${helperImports.join(", ")} } from 'nestbridge';`] : [],
|
|
@@ -1746,8 +2333,8 @@ var NestBridgeDiagnosticError = class extends Error {
|
|
|
1746
2333
|
};
|
|
1747
2334
|
//#endregion
|
|
1748
2335
|
//#region src/version.ts
|
|
1749
|
-
var VERSION = "
|
|
2336
|
+
var VERSION = "3.0.0";
|
|
1750
2337
|
//#endregion
|
|
1751
|
-
export { NestBridgeDiagnosticError, VERSION, analyzeControllers, analyzeResolvers, applyApplicationRouting, detectErrorResponseShape, detectGlobalPrefix,
|
|
2338
|
+
export { NestBridgeDiagnosticError, VERSION, analyzeControllers, analyzeResolvers, applyApplicationRouting, detectErrorResponseShape, detectGlobalPrefix, detectVersioning, formatDiagnostic, generateControllerDeclaration, generateErrorBodyDeclaration };
|
|
1752
2339
|
|
|
1753
2340
|
//# sourceMappingURL=nestbridge.core.js.map
|