@nestia/sdk 2.4.3 → 2.4.4
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/lib/INestiaConfig.d.ts +13 -0
- package/lib/analyses/ControllerAnalyzer.js +12 -1
- package/lib/analyses/ControllerAnalyzer.js.map +1 -1
- package/lib/analyses/PathAnalyzer.d.ts +2 -2
- package/lib/analyses/PathAnalyzer.js +27 -11
- package/lib/analyses/PathAnalyzer.js.map +1 -1
- package/lib/analyses/ReflectAnalyzer.js +11 -2
- package/lib/analyses/ReflectAnalyzer.js.map +1 -1
- package/lib/executable/internal/NestiaConfigLoader.js +5 -1
- package/lib/executable/internal/NestiaConfigLoader.js.map +1 -1
- package/lib/executable/sdk.js +11 -11
- package/lib/generates/SwaggerGenerator.js +16 -22
- package/lib/generates/SwaggerGenerator.js.map +1 -1
- package/lib/generates/internal/SwaggerSchemaGenerator.js +22 -15
- package/lib/generates/internal/SwaggerSchemaGenerator.js.map +1 -1
- package/lib/structures/ISwaggerComponents.d.ts +1 -1
- package/lib/structures/ISwaggerRoute.d.ts +3 -3
- package/package.json +5 -5
- package/src/INestiaConfig.ts +248 -234
- package/src/NestiaSdkApplication.ts +253 -253
- package/src/analyses/AccessorAnalyzer.ts +60 -60
- package/src/analyses/ConfigAnalyzer.ts +147 -147
- package/src/analyses/ControllerAnalyzer.ts +390 -379
- package/src/analyses/ExceptionAnalyzer.ts +115 -115
- package/src/analyses/GenericAnalyzer.ts +51 -51
- package/src/analyses/ImportAnalyzer.ts +138 -138
- package/src/analyses/PathAnalyzer.ts +110 -98
- package/src/analyses/ReflectAnalyzer.ts +11 -6
- package/src/analyses/SecurityAnalyzer.ts +20 -20
- package/src/executable/internal/CommandParser.ts +15 -15
- package/src/executable/internal/NestiaConfigLoader.ts +67 -67
- package/src/executable/internal/NestiaSdkCommand.ts +60 -60
- package/src/executable/sdk.ts +73 -73
- package/src/generates/E2eGenerator.ts +64 -64
- package/src/generates/SdkGenerator.ts +96 -96
- package/src/generates/SwaggerGenerator.ts +376 -372
- package/src/generates/internal/E2eFileProgrammer.ts +123 -123
- package/src/generates/internal/SdkDistributionComposer.ts +91 -91
- package/src/generates/internal/SdkDtoGenerator.ts +424 -424
- package/src/generates/internal/SdkFileProgrammer.ts +106 -106
- package/src/generates/internal/SdkImportWizard.ts +55 -55
- package/src/generates/internal/SdkRouteDirectory.ts +17 -17
- package/src/generates/internal/SdkSimulationProgrammer.ts +133 -133
- package/src/generates/internal/SdkTypeDefiner.ts +119 -119
- package/src/generates/internal/SwaggerSchemaGenerator.ts +18 -2
- package/src/generates/internal/SwaggerSchemaValidator.ts +198 -198
- package/src/index.ts +4 -4
- package/src/module.ts +2 -2
- package/src/structures/IErrorReport.ts +6 -6
- package/src/structures/INestiaProject.ts +13 -13
- package/src/structures/INormalizedInput.ts +20 -20
- package/src/structures/ISwagger.ts +91 -91
- package/src/structures/ISwaggerComponents.ts +29 -29
- package/src/structures/ISwaggerError.ts +8 -8
- package/src/structures/ISwaggerInfo.ts +80 -80
- package/src/structures/ISwaggerLazyProperty.ts +7 -7
- package/src/structures/ISwaggerLazySchema.ts +7 -7
- package/src/structures/ISwaggerRoute.ts +51 -51
- package/src/structures/ISwaggerSecurityScheme.ts +65 -65
- package/src/structures/ITypeTuple.ts +6 -6
- package/src/structures/MethodType.ts +5 -5
- package/src/structures/ParamCategory.ts +1 -1
- package/src/structures/TypeEntry.ts +22 -22
- package/src/utils/ArrayUtil.ts +26 -26
- package/src/utils/FileRetriever.ts +22 -22
- package/src/utils/ImportDictionary.ts +125 -125
- package/src/utils/MapUtil.ts +14 -14
- package/src/utils/PathUtil.ts +10 -10
- package/src/utils/SourceFinder.ts +66 -66
- package/src/utils/StripEnums.ts +5 -5
|
@@ -1,379 +1,390 @@
|
|
|
1
|
-
import { VERSION_NEUTRAL, VersionValue } from "@nestjs/common/interfaces";
|
|
2
|
-
import path from "path";
|
|
3
|
-
import { HashMap } from "tstl/container/HashMap";
|
|
4
|
-
import ts from "typescript";
|
|
5
|
-
import { CommentFactory } from "typia/lib/factories/CommentFactory";
|
|
6
|
-
|
|
7
|
-
import { IController } from "../structures/IController";
|
|
8
|
-
import { IErrorReport } from "../structures/IErrorReport";
|
|
9
|
-
import { INestiaProject } from "../structures/INestiaProject";
|
|
10
|
-
import { IRoute } from "../structures/IRoute";
|
|
11
|
-
import { ITypeTuple } from "../structures/ITypeTuple";
|
|
12
|
-
import { PathUtil } from "../utils/PathUtil";
|
|
13
|
-
import { ExceptionAnalyzer } from "./ExceptionAnalyzer";
|
|
14
|
-
import { GenericAnalyzer } from "./GenericAnalyzer";
|
|
15
|
-
import { ImportAnalyzer } from "./ImportAnalyzer";
|
|
16
|
-
import { PathAnalyzer } from "./PathAnalyzer";
|
|
17
|
-
import { SecurityAnalyzer } from "./SecurityAnalyzer";
|
|
18
|
-
|
|
19
|
-
export namespace ControllerAnalyzer {
|
|
20
|
-
export const analyze =
|
|
21
|
-
(project: INestiaProject) =>
|
|
22
|
-
async (
|
|
23
|
-
sourceFile: ts.SourceFile,
|
|
24
|
-
controller: IController,
|
|
25
|
-
): Promise<IRoute[]> => {
|
|
26
|
-
// FIND CONTROLLER CLASS
|
|
27
|
-
const ret: IRoute[] = [];
|
|
28
|
-
ts.forEachChild(sourceFile, (node) => {
|
|
29
|
-
if (
|
|
30
|
-
ts.isClassDeclaration(node) &&
|
|
31
|
-
node.name?.escapedText === controller.name
|
|
32
|
-
) {
|
|
33
|
-
// ANALYZE THE CONTROLLER
|
|
34
|
-
ret.push(..._Analyze_controller(project)(controller, node));
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
return ret;
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
/* ---------------------------------------------------------
|
|
42
|
-
CLASS
|
|
43
|
-
--------------------------------------------------------- */
|
|
44
|
-
const _Analyze_controller =
|
|
45
|
-
(project: INestiaProject) =>
|
|
46
|
-
(controller: IController, classNode: ts.ClassDeclaration): IRoute[] => {
|
|
47
|
-
const classType: ts.InterfaceType = project.checker.getTypeAtLocation(
|
|
48
|
-
classNode,
|
|
49
|
-
) as ts.InterfaceType;
|
|
50
|
-
const genericDict: GenericAnalyzer.Dictionary = GenericAnalyzer.analyze(
|
|
51
|
-
project.checker,
|
|
52
|
-
classNode,
|
|
53
|
-
);
|
|
54
|
-
|
|
55
|
-
const ret: IRoute[] = [];
|
|
56
|
-
for (const property of classType.getProperties()) {
|
|
57
|
-
// GET METHOD DECLARATION
|
|
58
|
-
const declaration: ts.Declaration | undefined =
|
|
59
|
-
(property.declarations || [])[0];
|
|
60
|
-
if (!declaration || !ts.isMethodDeclaration(declaration)) continue;
|
|
61
|
-
|
|
62
|
-
// IDENTIFIER MUST BE
|
|
63
|
-
const identifier = declaration.name;
|
|
64
|
-
if (!ts.isIdentifier(identifier)) continue;
|
|
65
|
-
|
|
66
|
-
// ANALYZED WITH THE REFLECTED-FUNCTION
|
|
67
|
-
const runtime: IController.IFunction | undefined =
|
|
68
|
-
controller.functions.find((f) => f.name === identifier.escapedText);
|
|
69
|
-
if (runtime === undefined) continue;
|
|
70
|
-
|
|
71
|
-
const routes: IRoute[] = _Analyze_function(project)(
|
|
72
|
-
controller,
|
|
73
|
-
genericDict,
|
|
74
|
-
runtime,
|
|
75
|
-
declaration,
|
|
76
|
-
property,
|
|
77
|
-
);
|
|
78
|
-
ret.push(...routes);
|
|
79
|
-
}
|
|
80
|
-
return ret;
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
/* ---------------------------------------------------------
|
|
84
|
-
FUNCTION
|
|
85
|
-
--------------------------------------------------------- */
|
|
86
|
-
const _Analyze_function =
|
|
87
|
-
(project: INestiaProject) =>
|
|
88
|
-
(
|
|
89
|
-
controller: IController,
|
|
90
|
-
genericDict: GenericAnalyzer.Dictionary,
|
|
91
|
-
func: IController.IFunction,
|
|
92
|
-
declaration: ts.MethodDeclaration,
|
|
93
|
-
symbol: ts.Symbol,
|
|
94
|
-
): IRoute[] => {
|
|
95
|
-
// PREPARE ASSETS
|
|
96
|
-
const type: ts.Type = project.checker.getTypeOfSymbolAtLocation(
|
|
97
|
-
symbol,
|
|
98
|
-
symbol.valueDeclaration!,
|
|
99
|
-
);
|
|
100
|
-
const signature: ts.Signature | undefined =
|
|
101
|
-
project.checker.getSignaturesOfType(type, ts.SignatureKind.Call)[0];
|
|
102
|
-
if (signature === undefined) {
|
|
103
|
-
project.errors.push({
|
|
104
|
-
file: controller.file,
|
|
105
|
-
controller: controller.name,
|
|
106
|
-
function: func.name,
|
|
107
|
-
message: "unable to get the type signature.",
|
|
108
|
-
});
|
|
109
|
-
return [];
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// SKIP @IGNORE TAG
|
|
113
|
-
const jsDocTags = signature.getJsDocTags();
|
|
114
|
-
if (jsDocTags.some((tag) => tag.name === "ignore")) return [];
|
|
115
|
-
|
|
116
|
-
// EXPLORE CHILDREN TYPES
|
|
117
|
-
const importDict: ImportAnalyzer.Dictionary = new HashMap();
|
|
118
|
-
const parameters: Array<IRoute.IParameter | null> = func.parameters.map(
|
|
119
|
-
(param) =>
|
|
120
|
-
_Analyze_parameter(project)(
|
|
121
|
-
genericDict,
|
|
122
|
-
importDict,
|
|
123
|
-
controller,
|
|
124
|
-
func.name,
|
|
125
|
-
param,
|
|
126
|
-
signature.getParameters()[param.index],
|
|
127
|
-
)!,
|
|
128
|
-
);
|
|
129
|
-
const outputType: ITypeTuple | null = ImportAnalyzer.analyze(
|
|
130
|
-
project.checker,
|
|
131
|
-
genericDict,
|
|
132
|
-
importDict,
|
|
133
|
-
signature.getReturnType(),
|
|
134
|
-
);
|
|
135
|
-
if (outputType === null || outputType.typeName === "__type") {
|
|
136
|
-
project.errors.push({
|
|
137
|
-
file: controller.file,
|
|
138
|
-
controller: controller.name,
|
|
139
|
-
function: func.name,
|
|
140
|
-
message: "implicit (unnamed) return type.",
|
|
141
|
-
});
|
|
142
|
-
return [];
|
|
143
|
-
} else if (
|
|
144
|
-
func.method === "HEAD" &&
|
|
145
|
-
outputType.typeName !== "void" &&
|
|
146
|
-
outputType.typeName !== "undefined"
|
|
147
|
-
) {
|
|
148
|
-
project.errors.push({
|
|
149
|
-
file: controller.file,
|
|
150
|
-
controller: controller.name,
|
|
151
|
-
function: func.name,
|
|
152
|
-
message: `HEAD method must return void type.`,
|
|
153
|
-
});
|
|
154
|
-
return [];
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
const exceptions = ExceptionAnalyzer.analyze(project)(
|
|
158
|
-
genericDict,
|
|
159
|
-
project.config.propagate === true ? importDict : new HashMap(),
|
|
160
|
-
)(
|
|
161
|
-
controller,
|
|
162
|
-
func,
|
|
163
|
-
)(declaration);
|
|
164
|
-
const imports: [string, string[]][] = importDict
|
|
165
|
-
.toJSON()
|
|
166
|
-
.map((pair) => [pair.first, pair.second.toJSON()]);
|
|
167
|
-
|
|
168
|
-
// CONSIDER SECURITY TAGS
|
|
169
|
-
const security: Record<string, string[]>[] = SecurityAnalyzer.merge(
|
|
170
|
-
...controller.security,
|
|
171
|
-
...func.security,
|
|
172
|
-
...jsDocTags
|
|
173
|
-
.filter((tag) => tag.name === "security")
|
|
174
|
-
.map((tag) =>
|
|
175
|
-
(tag.text ?? []).map((text) => {
|
|
176
|
-
const line: string[] = text.text
|
|
177
|
-
.split(" ")
|
|
178
|
-
.filter((s) => s.trim())
|
|
179
|
-
.filter((s) => !!s.length);
|
|
180
|
-
if (line.length === 0) return {};
|
|
181
|
-
return {
|
|
182
|
-
[line[0]]: line.slice(1),
|
|
183
|
-
};
|
|
184
|
-
}),
|
|
185
|
-
)
|
|
186
|
-
.flat(),
|
|
187
|
-
);
|
|
188
|
-
|
|
189
|
-
// CONSTRUCT COMMON DATA
|
|
190
|
-
const common: Omit<IRoute, "path" | "accessors"> = {
|
|
191
|
-
...func,
|
|
192
|
-
parameters: parameters.filter((p) => p !== null) as IRoute.IParameter[],
|
|
193
|
-
output: {
|
|
194
|
-
type: outputType.type,
|
|
195
|
-
typeName: outputType.typeName,
|
|
196
|
-
contentType: func.contentType,
|
|
197
|
-
},
|
|
198
|
-
imports,
|
|
199
|
-
status: func.status,
|
|
200
|
-
symbol: {
|
|
201
|
-
class: controller.name,
|
|
202
|
-
function: func.name,
|
|
203
|
-
},
|
|
204
|
-
location: (() => {
|
|
205
|
-
const file = declaration.getSourceFile();
|
|
206
|
-
const { line, character } = file.getLineAndCharacterOfPosition(
|
|
207
|
-
declaration.pos,
|
|
208
|
-
);
|
|
209
|
-
return `${path.relative(process.cwd(), file.fileName)}:${line + 1}:${
|
|
210
|
-
character + 1
|
|
211
|
-
}`;
|
|
212
|
-
})(),
|
|
213
|
-
description: CommentFactory.description(symbol),
|
|
214
|
-
operationId: jsDocTags
|
|
215
|
-
.find(({ name }) => name === "operationId")
|
|
216
|
-
?.text?.[0].text.split(" ")[0]
|
|
217
|
-
.trim(),
|
|
218
|
-
jsDocTags: jsDocTags,
|
|
219
|
-
setHeaders: jsDocTags
|
|
220
|
-
.filter(
|
|
221
|
-
(t) =>
|
|
222
|
-
t.text?.length &&
|
|
223
|
-
t.text[0].text &&
|
|
224
|
-
(t.name === "setHeader" || t.name === "assignHeaders"),
|
|
225
|
-
)
|
|
226
|
-
.map((t) =>
|
|
227
|
-
t.name === "setHeader"
|
|
228
|
-
? {
|
|
229
|
-
type: "setter",
|
|
230
|
-
source: t.text![0].text.split(" ")[0].trim(),
|
|
231
|
-
target: t.text![0].text.split(" ")[1]?.trim(),
|
|
232
|
-
}
|
|
233
|
-
: {
|
|
234
|
-
type: "assigner",
|
|
235
|
-
source: t.text![0].text,
|
|
236
|
-
},
|
|
237
|
-
),
|
|
238
|
-
security,
|
|
239
|
-
exceptions,
|
|
240
|
-
};
|
|
241
|
-
|
|
242
|
-
// CONFIGURE PATHS
|
|
243
|
-
const pathList: Set<string> = new Set();
|
|
244
|
-
const versions: Array<string | null> = _Analyze_versions(
|
|
245
|
-
project.input.versioning === undefined
|
|
246
|
-
? undefined
|
|
247
|
-
: func.versions ??
|
|
248
|
-
controller.versions ??
|
|
249
|
-
(project.input.versioning?.defaultVersion !== undefined
|
|
250
|
-
? Array.isArray(project.input.versioning?.defaultVersion)
|
|
251
|
-
? project.input.versioning?.defaultVersion
|
|
252
|
-
: [project.input.versioning?.defaultVersion]
|
|
253
|
-
: undefined) ??
|
|
254
|
-
undefined,
|
|
255
|
-
);
|
|
256
|
-
for (const prefix of controller.prefixes)
|
|
257
|
-
for (const cPath of controller.paths)
|
|
258
|
-
for (const filePath of func.paths)
|
|
259
|
-
pathList.add(PathAnalyzer.join(prefix, cPath, filePath));
|
|
260
|
-
|
|
261
|
-
return [...pathList]
|
|
262
|
-
.map((individual) =>
|
|
263
|
-
PathAnalyzer.combinate(project.input.globalPrefix)(
|
|
264
|
-
[...versions].map((v) =>
|
|
265
|
-
v === null
|
|
266
|
-
? null
|
|
267
|
-
: project.input.versioning?.prefix?.length
|
|
268
|
-
? `${project.input.versioning.prefix}${v}`
|
|
269
|
-
: v,
|
|
270
|
-
),
|
|
271
|
-
)({
|
|
272
|
-
method: func.method,
|
|
273
|
-
path: individual,
|
|
274
|
-
}),
|
|
275
|
-
)
|
|
276
|
-
.flat()
|
|
277
|
-
.
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
`
|
|
327
|
-
`
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
}
|
|
1
|
+
import { VERSION_NEUTRAL, VersionValue } from "@nestjs/common/interfaces";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { HashMap } from "tstl/container/HashMap";
|
|
4
|
+
import ts from "typescript";
|
|
5
|
+
import { CommentFactory } from "typia/lib/factories/CommentFactory";
|
|
6
|
+
|
|
7
|
+
import { IController } from "../structures/IController";
|
|
8
|
+
import { IErrorReport } from "../structures/IErrorReport";
|
|
9
|
+
import { INestiaProject } from "../structures/INestiaProject";
|
|
10
|
+
import { IRoute } from "../structures/IRoute";
|
|
11
|
+
import { ITypeTuple } from "../structures/ITypeTuple";
|
|
12
|
+
import { PathUtil } from "../utils/PathUtil";
|
|
13
|
+
import { ExceptionAnalyzer } from "./ExceptionAnalyzer";
|
|
14
|
+
import { GenericAnalyzer } from "./GenericAnalyzer";
|
|
15
|
+
import { ImportAnalyzer } from "./ImportAnalyzer";
|
|
16
|
+
import { PathAnalyzer } from "./PathAnalyzer";
|
|
17
|
+
import { SecurityAnalyzer } from "./SecurityAnalyzer";
|
|
18
|
+
|
|
19
|
+
export namespace ControllerAnalyzer {
|
|
20
|
+
export const analyze =
|
|
21
|
+
(project: INestiaProject) =>
|
|
22
|
+
async (
|
|
23
|
+
sourceFile: ts.SourceFile,
|
|
24
|
+
controller: IController,
|
|
25
|
+
): Promise<IRoute[]> => {
|
|
26
|
+
// FIND CONTROLLER CLASS
|
|
27
|
+
const ret: IRoute[] = [];
|
|
28
|
+
ts.forEachChild(sourceFile, (node) => {
|
|
29
|
+
if (
|
|
30
|
+
ts.isClassDeclaration(node) &&
|
|
31
|
+
node.name?.escapedText === controller.name
|
|
32
|
+
) {
|
|
33
|
+
// ANALYZE THE CONTROLLER
|
|
34
|
+
ret.push(..._Analyze_controller(project)(controller, node));
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
return ret;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/* ---------------------------------------------------------
|
|
42
|
+
CLASS
|
|
43
|
+
--------------------------------------------------------- */
|
|
44
|
+
const _Analyze_controller =
|
|
45
|
+
(project: INestiaProject) =>
|
|
46
|
+
(controller: IController, classNode: ts.ClassDeclaration): IRoute[] => {
|
|
47
|
+
const classType: ts.InterfaceType = project.checker.getTypeAtLocation(
|
|
48
|
+
classNode,
|
|
49
|
+
) as ts.InterfaceType;
|
|
50
|
+
const genericDict: GenericAnalyzer.Dictionary = GenericAnalyzer.analyze(
|
|
51
|
+
project.checker,
|
|
52
|
+
classNode,
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
const ret: IRoute[] = [];
|
|
56
|
+
for (const property of classType.getProperties()) {
|
|
57
|
+
// GET METHOD DECLARATION
|
|
58
|
+
const declaration: ts.Declaration | undefined =
|
|
59
|
+
(property.declarations || [])[0];
|
|
60
|
+
if (!declaration || !ts.isMethodDeclaration(declaration)) continue;
|
|
61
|
+
|
|
62
|
+
// IDENTIFIER MUST BE
|
|
63
|
+
const identifier = declaration.name;
|
|
64
|
+
if (!ts.isIdentifier(identifier)) continue;
|
|
65
|
+
|
|
66
|
+
// ANALYZED WITH THE REFLECTED-FUNCTION
|
|
67
|
+
const runtime: IController.IFunction | undefined =
|
|
68
|
+
controller.functions.find((f) => f.name === identifier.escapedText);
|
|
69
|
+
if (runtime === undefined) continue;
|
|
70
|
+
|
|
71
|
+
const routes: IRoute[] = _Analyze_function(project)(
|
|
72
|
+
controller,
|
|
73
|
+
genericDict,
|
|
74
|
+
runtime,
|
|
75
|
+
declaration,
|
|
76
|
+
property,
|
|
77
|
+
);
|
|
78
|
+
ret.push(...routes);
|
|
79
|
+
}
|
|
80
|
+
return ret;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/* ---------------------------------------------------------
|
|
84
|
+
FUNCTION
|
|
85
|
+
--------------------------------------------------------- */
|
|
86
|
+
const _Analyze_function =
|
|
87
|
+
(project: INestiaProject) =>
|
|
88
|
+
(
|
|
89
|
+
controller: IController,
|
|
90
|
+
genericDict: GenericAnalyzer.Dictionary,
|
|
91
|
+
func: IController.IFunction,
|
|
92
|
+
declaration: ts.MethodDeclaration,
|
|
93
|
+
symbol: ts.Symbol,
|
|
94
|
+
): IRoute[] => {
|
|
95
|
+
// PREPARE ASSETS
|
|
96
|
+
const type: ts.Type = project.checker.getTypeOfSymbolAtLocation(
|
|
97
|
+
symbol,
|
|
98
|
+
symbol.valueDeclaration!,
|
|
99
|
+
);
|
|
100
|
+
const signature: ts.Signature | undefined =
|
|
101
|
+
project.checker.getSignaturesOfType(type, ts.SignatureKind.Call)[0];
|
|
102
|
+
if (signature === undefined) {
|
|
103
|
+
project.errors.push({
|
|
104
|
+
file: controller.file,
|
|
105
|
+
controller: controller.name,
|
|
106
|
+
function: func.name,
|
|
107
|
+
message: "unable to get the type signature.",
|
|
108
|
+
});
|
|
109
|
+
return [];
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// SKIP @IGNORE TAG
|
|
113
|
+
const jsDocTags = signature.getJsDocTags();
|
|
114
|
+
if (jsDocTags.some((tag) => tag.name === "ignore")) return [];
|
|
115
|
+
|
|
116
|
+
// EXPLORE CHILDREN TYPES
|
|
117
|
+
const importDict: ImportAnalyzer.Dictionary = new HashMap();
|
|
118
|
+
const parameters: Array<IRoute.IParameter | null> = func.parameters.map(
|
|
119
|
+
(param) =>
|
|
120
|
+
_Analyze_parameter(project)(
|
|
121
|
+
genericDict,
|
|
122
|
+
importDict,
|
|
123
|
+
controller,
|
|
124
|
+
func.name,
|
|
125
|
+
param,
|
|
126
|
+
signature.getParameters()[param.index],
|
|
127
|
+
)!,
|
|
128
|
+
);
|
|
129
|
+
const outputType: ITypeTuple | null = ImportAnalyzer.analyze(
|
|
130
|
+
project.checker,
|
|
131
|
+
genericDict,
|
|
132
|
+
importDict,
|
|
133
|
+
signature.getReturnType(),
|
|
134
|
+
);
|
|
135
|
+
if (outputType === null || outputType.typeName === "__type") {
|
|
136
|
+
project.errors.push({
|
|
137
|
+
file: controller.file,
|
|
138
|
+
controller: controller.name,
|
|
139
|
+
function: func.name,
|
|
140
|
+
message: "implicit (unnamed) return type.",
|
|
141
|
+
});
|
|
142
|
+
return [];
|
|
143
|
+
} else if (
|
|
144
|
+
func.method === "HEAD" &&
|
|
145
|
+
outputType.typeName !== "void" &&
|
|
146
|
+
outputType.typeName !== "undefined"
|
|
147
|
+
) {
|
|
148
|
+
project.errors.push({
|
|
149
|
+
file: controller.file,
|
|
150
|
+
controller: controller.name,
|
|
151
|
+
function: func.name,
|
|
152
|
+
message: `HEAD method must return void type.`,
|
|
153
|
+
});
|
|
154
|
+
return [];
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const exceptions = ExceptionAnalyzer.analyze(project)(
|
|
158
|
+
genericDict,
|
|
159
|
+
project.config.propagate === true ? importDict : new HashMap(),
|
|
160
|
+
)(
|
|
161
|
+
controller,
|
|
162
|
+
func,
|
|
163
|
+
)(declaration);
|
|
164
|
+
const imports: [string, string[]][] = importDict
|
|
165
|
+
.toJSON()
|
|
166
|
+
.map((pair) => [pair.first, pair.second.toJSON()]);
|
|
167
|
+
|
|
168
|
+
// CONSIDER SECURITY TAGS
|
|
169
|
+
const security: Record<string, string[]>[] = SecurityAnalyzer.merge(
|
|
170
|
+
...controller.security,
|
|
171
|
+
...func.security,
|
|
172
|
+
...jsDocTags
|
|
173
|
+
.filter((tag) => tag.name === "security")
|
|
174
|
+
.map((tag) =>
|
|
175
|
+
(tag.text ?? []).map((text) => {
|
|
176
|
+
const line: string[] = text.text
|
|
177
|
+
.split(" ")
|
|
178
|
+
.filter((s) => s.trim())
|
|
179
|
+
.filter((s) => !!s.length);
|
|
180
|
+
if (line.length === 0) return {};
|
|
181
|
+
return {
|
|
182
|
+
[line[0]]: line.slice(1),
|
|
183
|
+
};
|
|
184
|
+
}),
|
|
185
|
+
)
|
|
186
|
+
.flat(),
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
// CONSTRUCT COMMON DATA
|
|
190
|
+
const common: Omit<IRoute, "path" | "accessors"> = {
|
|
191
|
+
...func,
|
|
192
|
+
parameters: parameters.filter((p) => p !== null) as IRoute.IParameter[],
|
|
193
|
+
output: {
|
|
194
|
+
type: outputType.type,
|
|
195
|
+
typeName: outputType.typeName,
|
|
196
|
+
contentType: func.contentType,
|
|
197
|
+
},
|
|
198
|
+
imports,
|
|
199
|
+
status: func.status,
|
|
200
|
+
symbol: {
|
|
201
|
+
class: controller.name,
|
|
202
|
+
function: func.name,
|
|
203
|
+
},
|
|
204
|
+
location: (() => {
|
|
205
|
+
const file = declaration.getSourceFile();
|
|
206
|
+
const { line, character } = file.getLineAndCharacterOfPosition(
|
|
207
|
+
declaration.pos,
|
|
208
|
+
);
|
|
209
|
+
return `${path.relative(process.cwd(), file.fileName)}:${line + 1}:${
|
|
210
|
+
character + 1
|
|
211
|
+
}`;
|
|
212
|
+
})(),
|
|
213
|
+
description: CommentFactory.description(symbol),
|
|
214
|
+
operationId: jsDocTags
|
|
215
|
+
.find(({ name }) => name === "operationId")
|
|
216
|
+
?.text?.[0].text.split(" ")[0]
|
|
217
|
+
.trim(),
|
|
218
|
+
jsDocTags: jsDocTags,
|
|
219
|
+
setHeaders: jsDocTags
|
|
220
|
+
.filter(
|
|
221
|
+
(t) =>
|
|
222
|
+
t.text?.length &&
|
|
223
|
+
t.text[0].text &&
|
|
224
|
+
(t.name === "setHeader" || t.name === "assignHeaders"),
|
|
225
|
+
)
|
|
226
|
+
.map((t) =>
|
|
227
|
+
t.name === "setHeader"
|
|
228
|
+
? {
|
|
229
|
+
type: "setter",
|
|
230
|
+
source: t.text![0].text.split(" ")[0].trim(),
|
|
231
|
+
target: t.text![0].text.split(" ")[1]?.trim(),
|
|
232
|
+
}
|
|
233
|
+
: {
|
|
234
|
+
type: "assigner",
|
|
235
|
+
source: t.text![0].text,
|
|
236
|
+
},
|
|
237
|
+
),
|
|
238
|
+
security,
|
|
239
|
+
exceptions,
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
// CONFIGURE PATHS
|
|
243
|
+
const pathList: Set<string> = new Set();
|
|
244
|
+
const versions: Array<string | null> = _Analyze_versions(
|
|
245
|
+
project.input.versioning === undefined
|
|
246
|
+
? undefined
|
|
247
|
+
: func.versions ??
|
|
248
|
+
controller.versions ??
|
|
249
|
+
(project.input.versioning?.defaultVersion !== undefined
|
|
250
|
+
? Array.isArray(project.input.versioning?.defaultVersion)
|
|
251
|
+
? project.input.versioning?.defaultVersion
|
|
252
|
+
: [project.input.versioning?.defaultVersion]
|
|
253
|
+
: undefined) ??
|
|
254
|
+
undefined,
|
|
255
|
+
);
|
|
256
|
+
for (const prefix of controller.prefixes)
|
|
257
|
+
for (const cPath of controller.paths)
|
|
258
|
+
for (const filePath of func.paths)
|
|
259
|
+
pathList.add(PathAnalyzer.join(prefix, cPath, filePath));
|
|
260
|
+
|
|
261
|
+
return [...pathList]
|
|
262
|
+
.map((individual) =>
|
|
263
|
+
PathAnalyzer.combinate(project.input.globalPrefix)(
|
|
264
|
+
[...versions].map((v) =>
|
|
265
|
+
v === null
|
|
266
|
+
? null
|
|
267
|
+
: project.input.versioning?.prefix?.length
|
|
268
|
+
? `${project.input.versioning.prefix}${v}`
|
|
269
|
+
: v,
|
|
270
|
+
),
|
|
271
|
+
)({
|
|
272
|
+
method: func.method,
|
|
273
|
+
path: individual,
|
|
274
|
+
}),
|
|
275
|
+
)
|
|
276
|
+
.flat()
|
|
277
|
+
.filter((path) => {
|
|
278
|
+
const escaped: string | null = PathAnalyzer.escape(path);
|
|
279
|
+
if (escaped === null)
|
|
280
|
+
project.errors.push({
|
|
281
|
+
file: controller.file,
|
|
282
|
+
controller: controller.name,
|
|
283
|
+
function: func.name,
|
|
284
|
+
message: `unable to escape the path "${path}".`,
|
|
285
|
+
});
|
|
286
|
+
return escaped !== null;
|
|
287
|
+
})
|
|
288
|
+
.map((path) => ({
|
|
289
|
+
...common,
|
|
290
|
+
path: PathAnalyzer.escape(path)!,
|
|
291
|
+
accessors: [...PathUtil.accessors(path), func.name],
|
|
292
|
+
}));
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
const _Analyze_parameter =
|
|
296
|
+
(project: INestiaProject) =>
|
|
297
|
+
(
|
|
298
|
+
genericDict: GenericAnalyzer.Dictionary,
|
|
299
|
+
importDict: ImportAnalyzer.Dictionary,
|
|
300
|
+
controller: IController,
|
|
301
|
+
funcName: string,
|
|
302
|
+
param: IController.IParameter,
|
|
303
|
+
symbol: ts.Symbol,
|
|
304
|
+
): IRoute.IParameter | null => {
|
|
305
|
+
const type: ts.Type = project.checker.getTypeOfSymbolAtLocation(
|
|
306
|
+
symbol,
|
|
307
|
+
symbol.valueDeclaration!,
|
|
308
|
+
);
|
|
309
|
+
const name: string = symbol.getEscapedName().toString();
|
|
310
|
+
|
|
311
|
+
const optional: boolean = !!project.checker.symbolToParameterDeclaration(
|
|
312
|
+
symbol,
|
|
313
|
+
undefined,
|
|
314
|
+
undefined,
|
|
315
|
+
)?.questionToken;
|
|
316
|
+
|
|
317
|
+
const errors: IErrorReport[] = [];
|
|
318
|
+
|
|
319
|
+
// DO NOT SUPPORT BODY PARAMETER
|
|
320
|
+
if (param.category === "body" && param.field !== undefined)
|
|
321
|
+
errors.push({
|
|
322
|
+
file: controller.file,
|
|
323
|
+
controller: controller.name,
|
|
324
|
+
function: funcName,
|
|
325
|
+
message:
|
|
326
|
+
`nestia does not support body field specification. ` +
|
|
327
|
+
`Therefore, erase the "${name}" parameter and ` +
|
|
328
|
+
`re-define a new body decorator accepting full structured message.`,
|
|
329
|
+
});
|
|
330
|
+
if (optional === true && param.category !== "query")
|
|
331
|
+
errors.push({
|
|
332
|
+
file: controller.file,
|
|
333
|
+
controller: controller.name,
|
|
334
|
+
function: funcName,
|
|
335
|
+
message:
|
|
336
|
+
`nestia does not support optional parameter except query parameter. ` +
|
|
337
|
+
`Therefore, erase question mark on the "${name}" parameter, ` +
|
|
338
|
+
`or re-define a new method without the "${name}" parameter.`,
|
|
339
|
+
});
|
|
340
|
+
if (
|
|
341
|
+
optional === true &&
|
|
342
|
+
param.category === "query" &&
|
|
343
|
+
param.field === undefined
|
|
344
|
+
)
|
|
345
|
+
errors.push({
|
|
346
|
+
file: controller.file,
|
|
347
|
+
controller: controller.name,
|
|
348
|
+
function: funcName,
|
|
349
|
+
message:
|
|
350
|
+
`nestia does not support optional query parameter without field specification. ` +
|
|
351
|
+
`Therefore, erase question mark on the "${name}" parameter, ` +
|
|
352
|
+
`or re-define re-define parameters for each query parameters.`,
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
// GET TYPE NAME
|
|
356
|
+
const tuple: ITypeTuple | null = ImportAnalyzer.analyze(
|
|
357
|
+
project.checker,
|
|
358
|
+
genericDict,
|
|
359
|
+
importDict,
|
|
360
|
+
type,
|
|
361
|
+
);
|
|
362
|
+
if (tuple === null || tuple.typeName === "__type")
|
|
363
|
+
errors.push({
|
|
364
|
+
file: controller.file,
|
|
365
|
+
controller: controller.name,
|
|
366
|
+
function: funcName,
|
|
367
|
+
message: `implicit (unnamed) parameter type from "${name}".`,
|
|
368
|
+
});
|
|
369
|
+
if (errors.length) {
|
|
370
|
+
project.errors.push(...errors);
|
|
371
|
+
return null;
|
|
372
|
+
}
|
|
373
|
+
return {
|
|
374
|
+
...param,
|
|
375
|
+
name,
|
|
376
|
+
optional,
|
|
377
|
+
type: tuple!.type,
|
|
378
|
+
typeName: tuple!.typeName,
|
|
379
|
+
};
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
function _Analyze_versions(
|
|
383
|
+
value:
|
|
384
|
+
| Array<Exclude<VersionValue, Array<string | typeof VERSION_NEUTRAL>>>
|
|
385
|
+
| undefined,
|
|
386
|
+
): Array<string | null> {
|
|
387
|
+
if (value === undefined) return [null];
|
|
388
|
+
return value.map((v) => (typeof v === "symbol" ? null : v));
|
|
389
|
+
}
|
|
390
|
+
}
|