@nestia/sdk 3.0.0-dev.20231209 → 3.0.0-dev.20240412
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -9
- package/assets/config/nestia.config.ts +82 -79
- package/lib/INestiaConfig.d.ts +28 -6
- package/lib/NestiaSdkApplication.js +12 -10
- package/lib/NestiaSdkApplication.js.map +1 -1
- package/lib/analyses/ConfigAnalyzer.js +1 -1
- package/lib/analyses/ConfigAnalyzer.js.map +1 -1
- package/lib/analyses/ControllerAnalyzer.js +30 -15
- package/lib/analyses/ControllerAnalyzer.js.map +1 -1
- package/lib/analyses/ExceptionAnalyzer.js +35 -6
- package/lib/analyses/ExceptionAnalyzer.js.map +1 -1
- package/lib/analyses/ImportAnalyzer.d.ts +1 -2
- package/lib/analyses/ImportAnalyzer.js +2 -2
- package/lib/analyses/ImportAnalyzer.js.map +1 -1
- package/lib/analyses/PathAnalyzer.d.ts +2 -4
- package/lib/analyses/PathAnalyzer.js +27 -11
- package/lib/analyses/PathAnalyzer.js.map +1 -1
- package/lib/analyses/ReflectAnalyzer.js +34 -22
- package/lib/analyses/ReflectAnalyzer.js.map +1 -1
- package/lib/analyses/SecurityAnalyzer.js +13 -8
- package/lib/analyses/SecurityAnalyzer.js.map +1 -1
- package/lib/executable/internal/NestiaConfigLoader.js +300 -220
- package/lib/executable/internal/NestiaConfigLoader.js.map +1 -1
- package/lib/executable/sdk.js +11 -11
- package/lib/generates/CloneGenerator.d.ts +6 -0
- package/lib/generates/CloneGenerator.js +62 -0
- package/lib/generates/CloneGenerator.js.map +1 -0
- package/lib/generates/E2eGenerator.d.ts +2 -1
- package/lib/generates/E2eGenerator.js +2 -2
- package/lib/generates/E2eGenerator.js.map +1 -1
- package/lib/generates/SdkGenerator.js +3 -11
- package/lib/generates/SdkGenerator.js.map +1 -1
- package/lib/generates/SwaggerGenerator.d.ts +2 -0
- package/lib/generates/SwaggerGenerator.js +119 -62
- package/lib/generates/SwaggerGenerator.js.map +1 -1
- package/lib/generates/internal/E2eFileProgrammer.d.ts +2 -1
- package/lib/generates/internal/E2eFileProgrammer.js +49 -53
- package/lib/generates/internal/E2eFileProgrammer.js.map +1 -1
- package/lib/generates/internal/FilePrinter.d.ts +10 -0
- package/lib/generates/internal/FilePrinter.js +46 -0
- package/lib/generates/internal/FilePrinter.js.map +1 -0
- package/lib/{utils → generates/internal}/ImportDictionary.d.ts +2 -1
- package/lib/{utils → generates/internal}/ImportDictionary.js +20 -14
- package/lib/generates/internal/ImportDictionary.js.map +1 -0
- package/lib/generates/internal/SdkAliasCollection.d.ts +12 -0
- package/lib/generates/internal/SdkAliasCollection.js +97 -0
- package/lib/generates/internal/SdkAliasCollection.js.map +1 -0
- package/lib/generates/internal/SdkCloneProgrammer.d.ts +12 -0
- package/lib/generates/internal/SdkCloneProgrammer.js +99 -0
- package/lib/generates/internal/SdkCloneProgrammer.js.map +1 -0
- package/lib/generates/internal/SdkFileProgrammer.d.ts +2 -1
- package/lib/generates/internal/SdkFileProgrammer.js +27 -28
- package/lib/generates/internal/SdkFileProgrammer.js.map +1 -1
- package/lib/generates/internal/SdkFunctionProgrammer.d.ts +7 -2
- package/lib/generates/internal/SdkFunctionProgrammer.js +115 -322
- package/lib/generates/internal/SdkFunctionProgrammer.js.map +1 -1
- package/lib/generates/internal/SdkImportWizard.d.ts +1 -1
- package/lib/generates/internal/SdkNamespaceProgrammer.d.ts +11 -0
- package/lib/generates/internal/SdkNamespaceProgrammer.js +180 -0
- package/lib/generates/internal/SdkNamespaceProgrammer.js.map +1 -0
- package/lib/generates/internal/SdkRouteProgrammer.d.ts +7 -0
- package/lib/generates/internal/SdkRouteProgrammer.js +55 -0
- package/lib/generates/internal/SdkRouteProgrammer.js.map +1 -0
- package/lib/generates/internal/SdkSimulationProgrammer.d.ts +8 -2
- package/lib/generates/internal/SdkSimulationProgrammer.js +103 -89
- package/lib/generates/internal/SdkSimulationProgrammer.js.map +1 -1
- package/lib/generates/internal/SdkTypeProgrammer.d.ts +9 -0
- package/lib/generates/internal/SdkTypeProgrammer.js +228 -0
- package/lib/generates/internal/SdkTypeProgrammer.js.map +1 -0
- package/lib/generates/internal/SwaggerSchemaGenerator.d.ts +4 -4
- package/lib/generates/internal/SwaggerSchemaGenerator.js +30 -28
- package/lib/generates/internal/SwaggerSchemaGenerator.js.map +1 -1
- package/lib/structures/IController.d.ts +4 -2
- package/lib/structures/IRoute.d.ts +5 -4
- package/lib/structures/ISwaggerLazyProperty.d.ts +2 -2
- package/lib/structures/ISwaggerLazySchema.d.ts +2 -2
- package/lib/structures/ParamCategory.d.ts +1 -1
- package/lib/structures/TypeEntry.js +2 -2
- package/lib/structures/TypeEntry.js.map +1 -1
- package/lib/utils/StringUtil.d.ts +3 -0
- package/lib/utils/StringUtil.js +8 -0
- package/lib/utils/StringUtil.js.map +1 -0
- package/package.json +12 -16
- package/src/INestiaConfig.ts +30 -6
- package/src/NestiaSdkApplication.ts +255 -253
- package/src/analyses/AccessorAnalyzer.ts +60 -60
- package/src/analyses/ConfigAnalyzer.ts +147 -147
- package/src/analyses/ControllerAnalyzer.ts +42 -19
- package/src/analyses/ExceptionAnalyzer.ts +148 -115
- package/src/analyses/GenericAnalyzer.ts +51 -51
- package/src/analyses/ImportAnalyzer.ts +1 -2
- package/src/analyses/PathAnalyzer.ts +110 -98
- package/src/analyses/ReflectAnalyzer.ts +39 -35
- package/src/analyses/SecurityAnalyzer.ts +24 -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/CloneGenerator.ts +62 -0
- package/src/generates/E2eGenerator.ts +66 -64
- package/src/generates/SdkGenerator.ts +84 -96
- package/src/generates/SwaggerGenerator.ts +145 -53
- package/src/generates/internal/E2eFileProgrammer.ts +182 -123
- package/src/generates/internal/FilePrinter.ts +53 -0
- package/src/{utils → generates/internal}/ImportDictionary.ts +35 -13
- package/src/generates/internal/SdkAliasCollection.ts +152 -0
- package/src/generates/internal/SdkCloneProgrammer.ts +155 -0
- package/src/generates/internal/SdkDistributionComposer.ts +91 -91
- package/src/generates/internal/SdkFileProgrammer.ts +115 -106
- package/src/generates/internal/SdkFunctionProgrammer.ts +298 -518
- package/src/generates/internal/SdkImportWizard.ts +55 -55
- package/src/generates/internal/SdkNamespaceProgrammer.ts +510 -0
- package/src/generates/internal/SdkRouteDirectory.ts +17 -17
- package/src/generates/internal/SdkRouteProgrammer.ts +83 -0
- package/src/generates/internal/SdkSimulationProgrammer.ts +365 -133
- package/src/generates/internal/SdkTypeProgrammer.ts +386 -0
- package/src/generates/internal/SwaggerSchemaGenerator.ts +437 -427
- package/src/generates/internal/SwaggerSchemaValidator.ts +198 -198
- package/src/index.ts +4 -4
- package/src/module.ts +2 -2
- package/src/structures/IController.ts +94 -95
- package/src/structures/IErrorReport.ts +6 -6
- package/src/structures/INestiaProject.ts +13 -13
- package/src/structures/INormalizedInput.ts +20 -20
- package/src/structures/IRoute.ts +53 -53
- package/src/structures/ISwaggerLazyProperty.ts +2 -2
- package/src/structures/ISwaggerLazySchema.ts +2 -2
- 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 +1 -1
- package/src/utils/ArrayUtil.ts +26 -26
- package/src/utils/FileRetriever.ts +22 -22
- package/src/utils/MapUtil.ts +14 -14
- package/src/utils/PathUtil.ts +10 -10
- package/src/utils/SourceFinder.ts +66 -66
- package/src/utils/StringUtil.ts +6 -0
- package/src/utils/StripEnums.ts +5 -5
- package/assets/bundle/api/utils/NestiaSimulator.ts +0 -70
- package/lib/generates/internal/SdkDtoGenerator.d.ts +0 -9
- package/lib/generates/internal/SdkDtoGenerator.js +0 -294
- package/lib/generates/internal/SdkDtoGenerator.js.map +0 -1
- package/lib/generates/internal/SdkTypeDefiner.d.ts +0 -11
- package/lib/generates/internal/SdkTypeDefiner.js +0 -82
- package/lib/generates/internal/SdkTypeDefiner.js.map +0 -1
- package/lib/structures/ISwagger.d.ts +0 -72
- package/lib/structures/ISwagger.js +0 -3
- package/lib/structures/ISwagger.js.map +0 -1
- package/lib/structures/ISwaggerComponents.d.ts +0 -26
- package/lib/structures/ISwaggerComponents.js +0 -3
- package/lib/structures/ISwaggerComponents.js.map +0 -1
- package/lib/structures/ISwaggerInfo.d.ts +0 -71
- package/lib/structures/ISwaggerInfo.js +0 -3
- package/lib/structures/ISwaggerInfo.js.map +0 -1
- package/lib/structures/ISwaggerRoute.d.ts +0 -47
- package/lib/structures/ISwaggerRoute.js +0 -3
- package/lib/structures/ISwaggerRoute.js.map +0 -1
- package/lib/structures/ISwaggerSecurityScheme.d.ts +0 -56
- package/lib/structures/ISwaggerSecurityScheme.js +0 -3
- package/lib/structures/ISwaggerSecurityScheme.js.map +0 -1
- package/lib/utils/ImportDictionary.js.map +0 -1
- package/src/generates/internal/SdkDtoGenerator.ts +0 -424
- package/src/generates/internal/SdkTypeDefiner.ts +0 -119
- package/src/structures/ISwagger.ts +0 -91
- package/src/structures/ISwaggerComponents.ts +0 -29
- package/src/structures/ISwaggerInfo.ts +0 -80
- package/src/structures/ISwaggerRoute.ts +0 -51
- package/src/structures/ISwaggerSecurityScheme.ts +0 -65
|
@@ -1,115 +1,148 @@
|
|
|
1
|
-
import path from "path";
|
|
2
|
-
import ts from "typescript";
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
)
|
|
1
|
+
import path from "path";
|
|
2
|
+
import ts from "typescript";
|
|
3
|
+
import { MetadataCollection } from "typia/lib/factories/MetadataCollection";
|
|
4
|
+
import { MetadataFactory } from "typia/lib/factories/MetadataFactory";
|
|
5
|
+
import { Metadata } from "typia/lib/schemas/metadata/Metadata";
|
|
6
|
+
|
|
7
|
+
import { IController } from "../structures/IController";
|
|
8
|
+
import { INestiaProject } from "../structures/INestiaProject";
|
|
9
|
+
import { IRoute } from "../structures/IRoute";
|
|
10
|
+
import { ITypeTuple } from "../structures/ITypeTuple";
|
|
11
|
+
import { GenericAnalyzer } from "./GenericAnalyzer";
|
|
12
|
+
import { ImportAnalyzer } from "./ImportAnalyzer";
|
|
13
|
+
|
|
14
|
+
export namespace ExceptionAnalyzer {
|
|
15
|
+
export const analyze =
|
|
16
|
+
(project: INestiaProject) =>
|
|
17
|
+
(
|
|
18
|
+
genericDict: GenericAnalyzer.Dictionary,
|
|
19
|
+
importDict: ImportAnalyzer.Dictionary,
|
|
20
|
+
) =>
|
|
21
|
+
(controller: IController, func: IController.IFunction) =>
|
|
22
|
+
(
|
|
23
|
+
declaration: ts.MethodDeclaration,
|
|
24
|
+
): Record<number | "2XX" | "3XX" | "4XX" | "5XX", IRoute.IOutput> => {
|
|
25
|
+
const output: Record<
|
|
26
|
+
number | "2XX" | "3XX" | "4XX" | "5XX",
|
|
27
|
+
IRoute.IOutput
|
|
28
|
+
> = {} as any;
|
|
29
|
+
for (const decorator of declaration.modifiers ?? [])
|
|
30
|
+
if (ts.isDecorator(decorator))
|
|
31
|
+
analyzeTyped(project)(genericDict, importDict)(controller, func)(
|
|
32
|
+
output,
|
|
33
|
+
)(decorator);
|
|
34
|
+
return output;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const analyzeTyped =
|
|
38
|
+
(project: INestiaProject) =>
|
|
39
|
+
(
|
|
40
|
+
genericDict: GenericAnalyzer.Dictionary,
|
|
41
|
+
importDict: ImportAnalyzer.Dictionary,
|
|
42
|
+
) =>
|
|
43
|
+
(controller: IController, func: IController.IFunction) =>
|
|
44
|
+
(output: Record<number | "2XX" | "3XX" | "4XX" | "5XX", IRoute.IOutput>) =>
|
|
45
|
+
(decorator: ts.Decorator): boolean => {
|
|
46
|
+
// CHECK DECORATOR
|
|
47
|
+
if (!ts.isCallExpression(decorator.expression)) return false;
|
|
48
|
+
else if ((decorator.expression.typeArguments ?? []).length !== 1)
|
|
49
|
+
return false;
|
|
50
|
+
|
|
51
|
+
// CHECK SIGNATURE
|
|
52
|
+
const signature: ts.Signature | undefined =
|
|
53
|
+
project.checker.getResolvedSignature(decorator.expression);
|
|
54
|
+
if (!signature || !signature.declaration) return false;
|
|
55
|
+
else if (
|
|
56
|
+
path
|
|
57
|
+
.resolve(signature.declaration.getSourceFile().fileName)
|
|
58
|
+
.indexOf(TYPED_EXCEPTION_PATH) === -1
|
|
59
|
+
)
|
|
60
|
+
return false;
|
|
61
|
+
|
|
62
|
+
// GET TYPE INFO
|
|
63
|
+
const status: string | null = getStatus(project.checker)(
|
|
64
|
+
decorator.expression.arguments[0] ?? null,
|
|
65
|
+
);
|
|
66
|
+
if (status === null) return false;
|
|
67
|
+
|
|
68
|
+
const node: ts.TypeNode = decorator.expression.typeArguments![0];
|
|
69
|
+
const type: ts.Type = project.checker.getTypeFromTypeNode(node);
|
|
70
|
+
if (type.isTypeParameter()) {
|
|
71
|
+
project.errors.push({
|
|
72
|
+
file: controller.file,
|
|
73
|
+
controller: controller.name,
|
|
74
|
+
function: func.name,
|
|
75
|
+
message: "TypedException() without generic argument specification.",
|
|
76
|
+
});
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const tuple: ITypeTuple | null = ImportAnalyzer.analyze(
|
|
81
|
+
project.checker,
|
|
82
|
+
genericDict,
|
|
83
|
+
importDict,
|
|
84
|
+
type,
|
|
85
|
+
);
|
|
86
|
+
if (
|
|
87
|
+
tuple === null ||
|
|
88
|
+
(project.config.clone !== true &&
|
|
89
|
+
(tuple.typeName === "__type" || tuple.typeName === "__object"))
|
|
90
|
+
) {
|
|
91
|
+
project.errors.push({
|
|
92
|
+
file: controller.file,
|
|
93
|
+
controller: controller.name,
|
|
94
|
+
function: func.name,
|
|
95
|
+
message: "TypeException() with implicit (unnamed) type.",
|
|
96
|
+
});
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// DO ASSIGN
|
|
101
|
+
const matched: IController.IException[] = Object.entries(func.exceptions)
|
|
102
|
+
.filter(([key]) => status === key)
|
|
103
|
+
.map(([_key, value]) => value);
|
|
104
|
+
for (const m of matched)
|
|
105
|
+
output[m.status] = {
|
|
106
|
+
type: tuple.type,
|
|
107
|
+
typeName: tuple.typeName,
|
|
108
|
+
contentType: "application/json",
|
|
109
|
+
description: m.description,
|
|
110
|
+
};
|
|
111
|
+
return true;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const getStatus =
|
|
115
|
+
(checker: ts.TypeChecker) =>
|
|
116
|
+
(expression: ts.Expression | null): string | null => {
|
|
117
|
+
if (expression === null) return null;
|
|
118
|
+
|
|
119
|
+
const type: ts.Type = checker.getTypeAtLocation(expression);
|
|
120
|
+
const result = MetadataFactory.analyze(checker)({
|
|
121
|
+
escape: true,
|
|
122
|
+
constant: true,
|
|
123
|
+
absorb: true,
|
|
124
|
+
})(new MetadataCollection())(type);
|
|
125
|
+
if (false === result.success) return null;
|
|
126
|
+
|
|
127
|
+
const meta: Metadata = result.data;
|
|
128
|
+
if (meta.constants.length === 1)
|
|
129
|
+
return meta.constants[0].values[0].value.toString();
|
|
130
|
+
else if (meta.escaped && meta.escaped.returns.constants.length === 1)
|
|
131
|
+
return meta.escaped.returns.constants[0].values[0].value.toString();
|
|
132
|
+
else if (ts.isStringLiteral(expression)) return expression.text;
|
|
133
|
+
else if (ts.isNumericLiteral(expression)) {
|
|
134
|
+
const value: number = Number(expression.text.split("_").join(""));
|
|
135
|
+
if (false === isNaN(value)) return value.toString();
|
|
136
|
+
}
|
|
137
|
+
return null;
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const TYPED_EXCEPTION_PATH = path.join(
|
|
142
|
+
"node_modules",
|
|
143
|
+
"@nestia",
|
|
144
|
+
"core",
|
|
145
|
+
"lib",
|
|
146
|
+
"decorators",
|
|
147
|
+
"TypedException.d.ts",
|
|
148
|
+
);
|
|
@@ -1,51 +1,51 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
|
-
|
|
3
|
-
export namespace GenericAnalyzer {
|
|
4
|
-
export type Dictionary = WeakMap<ts.Type, ts.Type>;
|
|
5
|
-
|
|
6
|
-
export function analyze(
|
|
7
|
-
checker: ts.TypeChecker,
|
|
8
|
-
classNode: ts.ClassDeclaration,
|
|
9
|
-
): Dictionary {
|
|
10
|
-
const dict: Dictionary = new WeakMap();
|
|
11
|
-
explore(checker, dict, classNode);
|
|
12
|
-
return dict;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function explore(
|
|
16
|
-
checker: ts.TypeChecker,
|
|
17
|
-
dict: Dictionary,
|
|
18
|
-
classNode: ts.ClassDeclaration,
|
|
19
|
-
): void {
|
|
20
|
-
if (classNode.heritageClauses === undefined) return;
|
|
21
|
-
|
|
22
|
-
for (const heritage of classNode.heritageClauses)
|
|
23
|
-
for (const hType of heritage.types) {
|
|
24
|
-
// MUST BE CLASS
|
|
25
|
-
const expression: ts.Type = checker.getTypeAtLocation(hType.expression);
|
|
26
|
-
const superNode: ts.Declaration | undefined =
|
|
27
|
-
expression.symbol.getDeclarations()?.[0];
|
|
28
|
-
|
|
29
|
-
if (superNode === undefined || !ts.isClassDeclaration(superNode))
|
|
30
|
-
continue;
|
|
31
|
-
|
|
32
|
-
// SPECIFY GENERICS
|
|
33
|
-
const usages: ReadonlyArray<ts.TypeNode> = hType.typeArguments || [];
|
|
34
|
-
const parameters: ReadonlyArray<ts.TypeParameterDeclaration> =
|
|
35
|
-
superNode.typeParameters || [];
|
|
36
|
-
|
|
37
|
-
parameters.forEach((param, index) => {
|
|
38
|
-
const paramType: ts.Type = checker.getTypeAtLocation(param);
|
|
39
|
-
const usageType: ts.Type =
|
|
40
|
-
usages[index] !== undefined
|
|
41
|
-
? checker.getTypeAtLocation(usages[index])
|
|
42
|
-
: checker.getTypeAtLocation(param.default!);
|
|
43
|
-
|
|
44
|
-
dict.set(paramType, usageType);
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
// RECUSRIVE EXPLORATION
|
|
48
|
-
explore(checker, dict, superNode);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
export namespace GenericAnalyzer {
|
|
4
|
+
export type Dictionary = WeakMap<ts.Type, ts.Type>;
|
|
5
|
+
|
|
6
|
+
export function analyze(
|
|
7
|
+
checker: ts.TypeChecker,
|
|
8
|
+
classNode: ts.ClassDeclaration,
|
|
9
|
+
): Dictionary {
|
|
10
|
+
const dict: Dictionary = new WeakMap();
|
|
11
|
+
explore(checker, dict, classNode);
|
|
12
|
+
return dict;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function explore(
|
|
16
|
+
checker: ts.TypeChecker,
|
|
17
|
+
dict: Dictionary,
|
|
18
|
+
classNode: ts.ClassDeclaration,
|
|
19
|
+
): void {
|
|
20
|
+
if (classNode.heritageClauses === undefined) return;
|
|
21
|
+
|
|
22
|
+
for (const heritage of classNode.heritageClauses)
|
|
23
|
+
for (const hType of heritage.types) {
|
|
24
|
+
// MUST BE CLASS
|
|
25
|
+
const expression: ts.Type = checker.getTypeAtLocation(hType.expression);
|
|
26
|
+
const superNode: ts.Declaration | undefined =
|
|
27
|
+
expression.symbol.getDeclarations()?.[0];
|
|
28
|
+
|
|
29
|
+
if (superNode === undefined || !ts.isClassDeclaration(superNode))
|
|
30
|
+
continue;
|
|
31
|
+
|
|
32
|
+
// SPECIFY GENERICS
|
|
33
|
+
const usages: ReadonlyArray<ts.TypeNode> = hType.typeArguments || [];
|
|
34
|
+
const parameters: ReadonlyArray<ts.TypeParameterDeclaration> =
|
|
35
|
+
superNode.typeParameters || [];
|
|
36
|
+
|
|
37
|
+
parameters.forEach((param, index) => {
|
|
38
|
+
const paramType: ts.Type = checker.getTypeAtLocation(param);
|
|
39
|
+
const usageType: ts.Type =
|
|
40
|
+
usages[index] !== undefined
|
|
41
|
+
? checker.getTypeAtLocation(usages[index])
|
|
42
|
+
: checker.getTypeAtLocation(param.default!);
|
|
43
|
+
|
|
44
|
+
dict.set(paramType, usageType);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// RECUSRIVE EXPLORATION
|
|
48
|
+
explore(checker, dict, superNode);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -1,98 +1,110 @@
|
|
|
1
|
-
import { RequestMethod } from "@nestjs/common";
|
|
2
|
-
import path from "path";
|
|
3
|
-
import { Token, parse } from "path-to-regexp";
|
|
4
|
-
|
|
5
|
-
import { INormalizedInput } from "../structures/INormalizedInput";
|
|
6
|
-
|
|
7
|
-
export namespace PathAnalyzer {
|
|
8
|
-
export const combinate =
|
|
9
|
-
(globalPrefix: INormalizedInput["globalPrefix"]) =>
|
|
10
|
-
(versions: Array<string | null>) =>
|
|
11
|
-
(props: { path: string; method: string }): string[] => {
|
|
12
|
-
const out = (str: string) =>
|
|
13
|
-
versions.map((v) => (v === null ? str : join(v, str)));
|
|
14
|
-
if (!globalPrefix?.prefix.length) return out(props.path);
|
|
15
|
-
else if (!globalPrefix.exclude?.length)
|
|
16
|
-
return out(props.path).map((str) => join(globalPrefix.prefix, str));
|
|
17
|
-
return globalPrefix.exclude.some((exclude) =>
|
|
18
|
-
typeof exclude === "string"
|
|
19
|
-
? RegExp(exclude).test(props.path)
|
|
20
|
-
: METHOD(exclude.method) === props.method &&
|
|
21
|
-
RegExp(exclude.path).test(props.path),
|
|
22
|
-
)
|
|
23
|
-
? out(props.path)
|
|
24
|
-
: out(props.path).map((str) => join(globalPrefix.prefix, str));
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
export const join = (...args: string[]) =>
|
|
28
|
-
"/" +
|
|
29
|
-
_Trim(
|
|
30
|
-
path
|
|
31
|
-
.join(...args.filter((s) => !!s.length))
|
|
32
|
-
.split("\\")
|
|
33
|
-
.join("/"),
|
|
34
|
-
);
|
|
35
|
-
|
|
36
|
-
export const escape = (str: string
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
:
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
? "
|
|
96
|
-
: value === RequestMethod.
|
|
97
|
-
|
|
98
|
-
|
|
1
|
+
import { RequestMethod } from "@nestjs/common";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { Token, parse } from "path-to-regexp";
|
|
4
|
+
|
|
5
|
+
import { INormalizedInput } from "../structures/INormalizedInput";
|
|
6
|
+
|
|
7
|
+
export namespace PathAnalyzer {
|
|
8
|
+
export const combinate =
|
|
9
|
+
(globalPrefix: INormalizedInput["globalPrefix"]) =>
|
|
10
|
+
(versions: Array<string | null>) =>
|
|
11
|
+
(props: { path: string; method: string }): string[] => {
|
|
12
|
+
const out = (str: string) =>
|
|
13
|
+
versions.map((v) => (v === null ? str : join(v, str)));
|
|
14
|
+
if (!globalPrefix?.prefix.length) return out(props.path);
|
|
15
|
+
else if (!globalPrefix.exclude?.length)
|
|
16
|
+
return out(props.path).map((str) => join(globalPrefix.prefix, str));
|
|
17
|
+
return globalPrefix.exclude.some((exclude) =>
|
|
18
|
+
typeof exclude === "string"
|
|
19
|
+
? RegExp(exclude).test(props.path)
|
|
20
|
+
: METHOD(exclude.method) === props.method &&
|
|
21
|
+
RegExp(exclude.path).test(props.path),
|
|
22
|
+
)
|
|
23
|
+
? out(props.path)
|
|
24
|
+
: out(props.path).map((str) => join(globalPrefix.prefix, str));
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const join = (...args: string[]) =>
|
|
28
|
+
"/" +
|
|
29
|
+
_Trim(
|
|
30
|
+
path
|
|
31
|
+
.join(...args.filter((s) => !!s.length))
|
|
32
|
+
.split("\\")
|
|
33
|
+
.join("/"),
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
export const escape = (str: string): string | null => {
|
|
37
|
+
const args = _Parse(str);
|
|
38
|
+
if (args === null) return null;
|
|
39
|
+
return (
|
|
40
|
+
"/" +
|
|
41
|
+
args
|
|
42
|
+
.map((arg) => (arg.type === "param" ? `:${arg.value}` : arg.value))
|
|
43
|
+
.join("/")
|
|
44
|
+
);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export const parameters = (str: string): string[] | null => {
|
|
48
|
+
const args = _Parse(str);
|
|
49
|
+
if (args === null) return null;
|
|
50
|
+
return args.filter((arg) => arg.type === "param").map((arg) => arg.value);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
function _Parse(str: string): IArgument[] | null {
|
|
54
|
+
const tokens: Token[] | null = (() => {
|
|
55
|
+
try {
|
|
56
|
+
return parse(path.join(str).split("\\").join("/"));
|
|
57
|
+
} catch {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
})();
|
|
61
|
+
if (tokens === null) return null;
|
|
62
|
+
|
|
63
|
+
const output: IArgument[] = [];
|
|
64
|
+
for (const key of tokens) {
|
|
65
|
+
if (typeof key === "string")
|
|
66
|
+
output.push({
|
|
67
|
+
type: "path",
|
|
68
|
+
value: _Trim(key),
|
|
69
|
+
});
|
|
70
|
+
else if (typeof key.name === "number" || _Trim(key.name) === "")
|
|
71
|
+
return null;
|
|
72
|
+
else
|
|
73
|
+
output.push({
|
|
74
|
+
type: "param",
|
|
75
|
+
value: _Trim(key.name),
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
return output;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function _Trim(str: string): string {
|
|
82
|
+
if (str[0] === "/") str = str.substring(1);
|
|
83
|
+
if (str[str.length - 1] === "/") str = str.substring(0, str.length - 1);
|
|
84
|
+
return str;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
interface IArgument {
|
|
88
|
+
type: "param" | "path";
|
|
89
|
+
value: string;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const METHOD = (value: RequestMethod) =>
|
|
94
|
+
value === RequestMethod.ALL
|
|
95
|
+
? "all"
|
|
96
|
+
: value === RequestMethod.DELETE
|
|
97
|
+
? "delete"
|
|
98
|
+
: value === RequestMethod.GET
|
|
99
|
+
? "get"
|
|
100
|
+
: value === RequestMethod.HEAD
|
|
101
|
+
? "head"
|
|
102
|
+
: value === RequestMethod.OPTIONS
|
|
103
|
+
? "options"
|
|
104
|
+
: value === RequestMethod.PATCH
|
|
105
|
+
? "patch"
|
|
106
|
+
: value === RequestMethod.POST
|
|
107
|
+
? "post"
|
|
108
|
+
: value === RequestMethod.PUT
|
|
109
|
+
? "put"
|
|
110
|
+
: "unknown";
|