@nestia/migrate 0.7.6 → 0.7.7
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/MigrateApplication.js +7 -4
- package/lib/MigrateApplication.js.map +1 -1
- package/lib/analyzers/MigrateAnalyzer.d.ts +2 -3
- package/lib/analyzers/MigrateAnalyzer.js +1 -5
- package/lib/analyzers/MigrateAnalyzer.js.map +1 -1
- package/lib/analyzers/MigrateControllerAnalyzer.d.ts +2 -2
- package/lib/analyzers/MigrateControllerAnalyzer.js +22 -13
- package/lib/analyzers/MigrateControllerAnalyzer.js.map +1 -1
- package/lib/analyzers/MigrateMethodAnalyzer.d.ts +2 -2
- package/lib/analyzers/MigrateMethodAnalyzer.js +16 -16
- package/lib/analyzers/MigrateMethodAnalyzer.js.map +1 -1
- package/lib/module.d.ts +8 -0
- package/lib/module.js +8 -0
- package/lib/module.js.map +1 -1
- package/lib/programmers/MigrateApiFileProgrammer.d.ts +2 -2
- package/lib/programmers/MigrateApiFileProgrammer.js.map +1 -1
- package/lib/programmers/MigrateApiFunctionProgrammer.d.ts +2 -2
- package/lib/programmers/MigrateApiFunctionProgrammer.js.map +1 -1
- package/lib/programmers/MigrateApiNamespaceProgrammer.d.ts +2 -2
- package/lib/programmers/MigrateApiNamespaceProgrammer.js.map +1 -1
- package/lib/programmers/MigrateApiProgrammer.js +3 -3
- package/lib/programmers/MigrateApiProgrammer.js.map +1 -1
- package/lib/programmers/MigrateImportProgrammer.js +5 -5
- package/lib/programmers/MigrateImportProgrammer.js.map +1 -1
- package/lib/programmers/MigrateSchemaProgrammer.d.ts +1 -1
- package/lib/structures/IMigrateDto.d.ts +1 -1
- package/lib/structures/IMigrateProgram.d.ts +20 -4
- package/lib/structures/IMigrateRoute.d.ts +1 -1
- package/lib/structures/ISwaggerComponents.d.ts +1 -1
- package/lib/structures/ISwaggerRoute.d.ts +1 -1
- package/lib/structures/{ISwaggeSchema.js → ISwaggerSchema.js} +1 -1
- package/lib/structures/ISwaggerSchema.js.map +1 -0
- package/lib/utils/SwaggerTypeChecker.d.ts +1 -1
- package/package.json +1 -1
- package/src/MigrateApplication.ts +54 -48
- package/src/analyzers/MigrateAnalyzer.ts +9 -13
- package/src/analyzers/MigrateControllerAnalyzer.ts +139 -122
- package/src/analyzers/MigrateMethodAnalyzer.ts +350 -348
- package/src/archivers/MigrateFileArchiver.ts +38 -38
- package/src/executable/bundle.ts +109 -109
- package/src/internal/MigrateCommander.ts +51 -51
- package/src/module.ts +10 -0
- package/src/programmers/MigrateApiFileProgrammer.ts +53 -53
- package/src/programmers/MigrateApiFunctionProgrammer.ts +203 -203
- package/src/programmers/MigrateApiNamespaceProgrammer.ts +430 -430
- package/src/programmers/MigrateApiProgrammer.ts +132 -132
- package/src/programmers/MigrateApiSimulatationProgrammer.ts +328 -328
- package/src/programmers/MigrateDtoProgrammer.ts +78 -78
- package/src/programmers/MigrateImportProgrammer.ts +114 -116
- package/src/programmers/MigrateNestControllerProgrammer.ts +50 -50
- package/src/programmers/MigrateNestMethodProgrammer.ts +249 -249
- package/src/programmers/MigrateNestModuleProgrammer.ts +62 -62
- package/src/programmers/MigrateNestProgrammer.ts +74 -74
- package/src/programmers/MigrateSchemaProgrammer.ts +257 -257
- package/src/structures/IMigrateDto.ts +1 -1
- package/src/structures/IMigrateProgram.ts +20 -4
- package/src/structures/IMigrateRoute.ts +46 -46
- package/src/structures/ISwaggerComponents.ts +1 -1
- package/src/structures/ISwaggerRoute.ts +1 -1
- package/src/utils/FilePrinter.ts +36 -36
- package/src/utils/SwaggerTypeChecker.ts +67 -67
- package/lib/IMigrateConfig.d.ts +0 -4
- package/lib/IMigrateConfig.js +0 -3
- package/lib/IMigrateConfig.js.map +0 -1
- package/lib/structures/ISwaggeSchema.js.map +0 -1
- package/src/IMigrateConfig.ts +0 -4
- /package/lib/structures/{ISwaggeSchema.d.ts → ISwaggerSchema.d.ts} +0 -0
- /package/src/structures/{ISwaggeSchema.ts → ISwaggerSchema.ts} +0 -0
@@ -1,74 +1,74 @@
|
|
1
|
-
import ts from "typescript";
|
2
|
-
|
3
|
-
import { IMigrateFile } from "../structures/IMigrateFile";
|
4
|
-
import { IMigrateProgram } from "../structures/IMigrateProgram";
|
5
|
-
import { FilePrinter } from "../utils/FilePrinter";
|
6
|
-
import { MigrateDtoProgrammer } from "./MigrateDtoProgrammer";
|
7
|
-
import { MigrateImportProgrammer } from "./MigrateImportProgrammer";
|
8
|
-
import { MigrateNestControllerProgrammer } from "./MigrateNestControllerProgrammer";
|
9
|
-
import { MigrateNestModuleProgrammer } from "./MigrateNestModuleProgrammer";
|
10
|
-
|
11
|
-
export namespace MigrateNestProgrammer {
|
12
|
-
export const write = (program: IMigrateProgram): IMigrateFile[] =>
|
13
|
-
[
|
14
|
-
{
|
15
|
-
location: "src",
|
16
|
-
file: "MyModule.ts",
|
17
|
-
statements: MigrateNestModuleProgrammer.write(program.controllers),
|
18
|
-
},
|
19
|
-
...program.controllers.map((c) => ({
|
20
|
-
location: c.location,
|
21
|
-
file: `${c.name}.ts`,
|
22
|
-
statements: MigrateNestControllerProgrammer.write(
|
23
|
-
program.swagger.components,
|
24
|
-
)(c),
|
25
|
-
})),
|
26
|
-
...[
|
27
|
-
...MigrateDtoProgrammer.write(program.swagger.components).entries(),
|
28
|
-
].map(([key, value]) => ({
|
29
|
-
location: "src/api/structures",
|
30
|
-
file: `${key}.ts`,
|
31
|
-
statements: writeDtoFile(key, value),
|
32
|
-
})),
|
33
|
-
].map((o) => ({
|
34
|
-
location: o.location,
|
35
|
-
file: o.file,
|
36
|
-
content: FilePrinter.write({ statements: o.statements }),
|
37
|
-
}));
|
38
|
-
|
39
|
-
const writeDtoFile = (
|
40
|
-
key: string,
|
41
|
-
modulo: MigrateDtoProgrammer.IModule,
|
42
|
-
): ts.Statement[] => {
|
43
|
-
const importer = new MigrateImportProgrammer();
|
44
|
-
const statements: ts.Statement[] = iterate(importer)(modulo);
|
45
|
-
if (statements.length === 0) return [];
|
46
|
-
|
47
|
-
return [
|
48
|
-
...importer.toStatements((name) => `./${name}`, key),
|
49
|
-
...(importer.empty() ? [] : [FilePrinter.newLine()]),
|
50
|
-
...statements,
|
51
|
-
];
|
52
|
-
};
|
53
|
-
|
54
|
-
const iterate =
|
55
|
-
(importer: MigrateImportProgrammer) =>
|
56
|
-
(modulo: MigrateDtoProgrammer.IModule): ts.Statement[] => {
|
57
|
-
const output: ts.Statement[] = [];
|
58
|
-
if (modulo.programmer !== null) output.push(modulo.programmer(importer));
|
59
|
-
if (modulo.children.size) {
|
60
|
-
const internal: ts.Statement[] = [];
|
61
|
-
for (const child of modulo.children.values())
|
62
|
-
internal.push(...iterate(importer)(child));
|
63
|
-
output.push(
|
64
|
-
ts.factory.createModuleDeclaration(
|
65
|
-
[ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
|
66
|
-
ts.factory.createIdentifier(modulo.name),
|
67
|
-
ts.factory.createModuleBlock(internal),
|
68
|
-
ts.NodeFlags.Namespace,
|
69
|
-
),
|
70
|
-
);
|
71
|
-
}
|
72
|
-
return output;
|
73
|
-
};
|
74
|
-
}
|
1
|
+
import ts from "typescript";
|
2
|
+
|
3
|
+
import { IMigrateFile } from "../structures/IMigrateFile";
|
4
|
+
import { IMigrateProgram } from "../structures/IMigrateProgram";
|
5
|
+
import { FilePrinter } from "../utils/FilePrinter";
|
6
|
+
import { MigrateDtoProgrammer } from "./MigrateDtoProgrammer";
|
7
|
+
import { MigrateImportProgrammer } from "./MigrateImportProgrammer";
|
8
|
+
import { MigrateNestControllerProgrammer } from "./MigrateNestControllerProgrammer";
|
9
|
+
import { MigrateNestModuleProgrammer } from "./MigrateNestModuleProgrammer";
|
10
|
+
|
11
|
+
export namespace MigrateNestProgrammer {
|
12
|
+
export const write = (program: IMigrateProgram): IMigrateFile[] =>
|
13
|
+
[
|
14
|
+
{
|
15
|
+
location: "src",
|
16
|
+
file: "MyModule.ts",
|
17
|
+
statements: MigrateNestModuleProgrammer.write(program.controllers),
|
18
|
+
},
|
19
|
+
...program.controllers.map((c) => ({
|
20
|
+
location: c.location,
|
21
|
+
file: `${c.name}.ts`,
|
22
|
+
statements: MigrateNestControllerProgrammer.write(
|
23
|
+
program.swagger.components,
|
24
|
+
)(c),
|
25
|
+
})),
|
26
|
+
...[
|
27
|
+
...MigrateDtoProgrammer.write(program.swagger.components).entries(),
|
28
|
+
].map(([key, value]) => ({
|
29
|
+
location: "src/api/structures",
|
30
|
+
file: `${key}.ts`,
|
31
|
+
statements: writeDtoFile(key, value),
|
32
|
+
})),
|
33
|
+
].map((o) => ({
|
34
|
+
location: o.location,
|
35
|
+
file: o.file,
|
36
|
+
content: FilePrinter.write({ statements: o.statements }),
|
37
|
+
}));
|
38
|
+
|
39
|
+
const writeDtoFile = (
|
40
|
+
key: string,
|
41
|
+
modulo: MigrateDtoProgrammer.IModule,
|
42
|
+
): ts.Statement[] => {
|
43
|
+
const importer = new MigrateImportProgrammer();
|
44
|
+
const statements: ts.Statement[] = iterate(importer)(modulo);
|
45
|
+
if (statements.length === 0) return [];
|
46
|
+
|
47
|
+
return [
|
48
|
+
...importer.toStatements((name) => `./${name}`, key),
|
49
|
+
...(importer.empty() ? [] : [FilePrinter.newLine()]),
|
50
|
+
...statements,
|
51
|
+
];
|
52
|
+
};
|
53
|
+
|
54
|
+
const iterate =
|
55
|
+
(importer: MigrateImportProgrammer) =>
|
56
|
+
(modulo: MigrateDtoProgrammer.IModule): ts.Statement[] => {
|
57
|
+
const output: ts.Statement[] = [];
|
58
|
+
if (modulo.programmer !== null) output.push(modulo.programmer(importer));
|
59
|
+
if (modulo.children.size) {
|
60
|
+
const internal: ts.Statement[] = [];
|
61
|
+
for (const child of modulo.children.values())
|
62
|
+
internal.push(...iterate(importer)(child));
|
63
|
+
output.push(
|
64
|
+
ts.factory.createModuleDeclaration(
|
65
|
+
[ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
|
66
|
+
ts.factory.createIdentifier(modulo.name),
|
67
|
+
ts.factory.createModuleBlock(internal),
|
68
|
+
ts.NodeFlags.Namespace,
|
69
|
+
),
|
70
|
+
);
|
71
|
+
}
|
72
|
+
return output;
|
73
|
+
};
|
74
|
+
}
|
@@ -1,257 +1,257 @@
|
|
1
|
-
import ts from "typescript";
|
2
|
-
import { ExpressionFactory } from "typia/lib/factories/ExpressionFactory";
|
3
|
-
import { TypeFactory } from "typia/lib/factories/TypeFactory";
|
4
|
-
import { FormatCheatSheet } from "typia/lib/tags/internal/FormatCheatSheet";
|
5
|
-
import { Escaper } from "typia/lib/utils/Escaper";
|
6
|
-
|
7
|
-
import { ISwaggerSchema } from "../structures/
|
8
|
-
import { ISwaggerComponents } from "../structures/ISwaggerComponents";
|
9
|
-
import { FilePrinter } from "../utils/FilePrinter";
|
10
|
-
import { SwaggerSwaggerChecker } from "../utils/SwaggerTypeChecker";
|
11
|
-
import { MigrateImportProgrammer } from "./MigrateImportProgrammer";
|
12
|
-
|
13
|
-
export namespace MigrateSchemaProgrammer {
|
14
|
-
/* -----------------------------------------------------------
|
15
|
-
FACADE
|
16
|
-
----------------------------------------------------------- */
|
17
|
-
export const write =
|
18
|
-
(components: ISwaggerComponents) =>
|
19
|
-
(importer: MigrateImportProgrammer) =>
|
20
|
-
(schema: ISwaggerSchema): ts.TypeNode => {
|
21
|
-
const union: ts.TypeNode[] = [];
|
22
|
-
if (SwaggerSwaggerChecker.isUnknown(schema))
|
23
|
-
return TypeFactory.keyword("any");
|
24
|
-
else if (SwaggerSwaggerChecker.isNullOnly(schema))
|
25
|
-
return createNode("null");
|
26
|
-
else if (SwaggerSwaggerChecker.isNullable(components)(schema))
|
27
|
-
union.push(createNode("null"));
|
28
|
-
|
29
|
-
const type: ts.TypeNode = (() => {
|
30
|
-
// ATOMIC
|
31
|
-
if (SwaggerSwaggerChecker.isBoolean(schema))
|
32
|
-
return writeBoolean(schema);
|
33
|
-
else if (SwaggerSwaggerChecker.isInteger(schema))
|
34
|
-
return writeInteger(importer)(schema);
|
35
|
-
else if (SwaggerSwaggerChecker.isNumber(schema))
|
36
|
-
return writeNumber(importer)(schema);
|
37
|
-
// INSTANCES
|
38
|
-
else if (SwaggerSwaggerChecker.isString(schema))
|
39
|
-
return writeString(importer)(schema);
|
40
|
-
else if (SwaggerSwaggerChecker.isArray(schema))
|
41
|
-
return writeArray(components)(importer)(schema);
|
42
|
-
else if (SwaggerSwaggerChecker.isObject(schema))
|
43
|
-
return writeObject(components)(importer)(schema);
|
44
|
-
else if (SwaggerSwaggerChecker.isReference(schema))
|
45
|
-
return writeReference(importer)(schema);
|
46
|
-
// NESTED UNION
|
47
|
-
else if (SwaggerSwaggerChecker.isAnyOf(schema))
|
48
|
-
return writeUnion(components)(importer)(schema.anyOf);
|
49
|
-
else if (SwaggerSwaggerChecker.isOneOf(schema))
|
50
|
-
return writeUnion(components)(importer)(schema.oneOf);
|
51
|
-
else return TypeFactory.keyword("any");
|
52
|
-
})();
|
53
|
-
union.push(type);
|
54
|
-
|
55
|
-
if (union.length === 0) return TypeFactory.keyword("any");
|
56
|
-
else if (union.length === 1) return union[0];
|
57
|
-
return ts.factory.createUnionTypeNode(union);
|
58
|
-
};
|
59
|
-
|
60
|
-
/* -----------------------------------------------------------
|
61
|
-
ATOMICS
|
62
|
-
----------------------------------------------------------- */
|
63
|
-
const writeBoolean = (schema: ISwaggerSchema.IBoolean): ts.TypeNode => {
|
64
|
-
if (schema.enum?.length)
|
65
|
-
return ts.factory.createLiteralTypeNode(
|
66
|
-
schema.enum[0] ? ts.factory.createTrue() : ts.factory.createFalse(),
|
67
|
-
);
|
68
|
-
return TypeFactory.keyword("boolean");
|
69
|
-
};
|
70
|
-
|
71
|
-
const writeInteger =
|
72
|
-
(importer: MigrateImportProgrammer) =>
|
73
|
-
(schema: ISwaggerSchema.IInteger): ts.TypeNode =>
|
74
|
-
writeNumeric(() => [
|
75
|
-
TypeFactory.keyword("number"),
|
76
|
-
importer.tag("Type", "int32"),
|
77
|
-
])(importer)(schema);
|
78
|
-
|
79
|
-
const writeNumber =
|
80
|
-
(importer: MigrateImportProgrammer) =>
|
81
|
-
(schema: ISwaggerSchema.INumber): ts.TypeNode =>
|
82
|
-
writeNumeric(() => [TypeFactory.keyword("number")])(importer)(schema);
|
83
|
-
|
84
|
-
const writeNumeric =
|
85
|
-
(factory: () => ts.TypeNode[]) =>
|
86
|
-
(importer: MigrateImportProgrammer) =>
|
87
|
-
(schema: ISwaggerSchema.IInteger | ISwaggerSchema.INumber): ts.TypeNode => {
|
88
|
-
if (schema.enum?.length)
|
89
|
-
return ts.factory.createUnionTypeNode(
|
90
|
-
schema.enum.map((i) =>
|
91
|
-
ts.factory.createLiteralTypeNode(ExpressionFactory.number(i)),
|
92
|
-
),
|
93
|
-
);
|
94
|
-
const intersection: ts.TypeNode[] = factory();
|
95
|
-
if (schema.default !== undefined)
|
96
|
-
intersection.push(importer.tag("Default", schema.default));
|
97
|
-
if (schema.minimum !== undefined)
|
98
|
-
intersection.push(
|
99
|
-
importer.tag(
|
100
|
-
schema.exclusiveMinimum ? "ExclusiveMinimum" : "Minimum",
|
101
|
-
schema.minimum,
|
102
|
-
),
|
103
|
-
);
|
104
|
-
if (schema.maximum !== undefined)
|
105
|
-
intersection.push(
|
106
|
-
importer.tag(
|
107
|
-
schema.exclusiveMaximum ? "ExclusiveMaximum" : "Maximum",
|
108
|
-
schema.maximum,
|
109
|
-
),
|
110
|
-
);
|
111
|
-
if (schema.multipleOf !== undefined)
|
112
|
-
intersection.push(importer.tag("MultipleOf", schema.multipleOf));
|
113
|
-
|
114
|
-
return intersection.length === 1
|
115
|
-
? intersection[0]
|
116
|
-
: ts.factory.createIntersectionTypeNode(intersection);
|
117
|
-
};
|
118
|
-
|
119
|
-
const writeString =
|
120
|
-
(importer: MigrateImportProgrammer) =>
|
121
|
-
(schema: ISwaggerSchema.IString): ts.TypeNode => {
|
122
|
-
if (schema.format === "binary")
|
123
|
-
return ts.factory.createTypeReferenceNode("File");
|
124
|
-
|
125
|
-
const intersection: ts.TypeNode[] = [TypeFactory.keyword("string")];
|
126
|
-
if (schema.default !== undefined)
|
127
|
-
intersection.push(importer.tag("Default", schema.default));
|
128
|
-
if (schema.minLength !== undefined)
|
129
|
-
intersection.push(importer.tag("MinLength", schema.minLength));
|
130
|
-
if (schema.maxLength !== undefined)
|
131
|
-
intersection.push(importer.tag("MaxLength", schema.maxLength));
|
132
|
-
if (schema.pattern !== undefined)
|
133
|
-
intersection.push(importer.tag("Pattern", schema.pattern));
|
134
|
-
if (
|
135
|
-
schema.format !== undefined &&
|
136
|
-
(FormatCheatSheet as Record<string, string>)[schema.format] !==
|
137
|
-
undefined
|
138
|
-
)
|
139
|
-
intersection.push(importer.tag("Format", schema.format));
|
140
|
-
return intersection.length === 1
|
141
|
-
? intersection[0]
|
142
|
-
: ts.factory.createIntersectionTypeNode(intersection);
|
143
|
-
};
|
144
|
-
|
145
|
-
/* -----------------------------------------------------------
|
146
|
-
INSTANCES
|
147
|
-
----------------------------------------------------------- */
|
148
|
-
const writeArray =
|
149
|
-
(components: ISwaggerComponents) =>
|
150
|
-
(importer: MigrateImportProgrammer) =>
|
151
|
-
(schema: ISwaggerSchema.IArray): ts.TypeNode => {
|
152
|
-
const intersection: ts.TypeNode[] = [
|
153
|
-
ts.factory.createArrayTypeNode(
|
154
|
-
write(components)(importer)(schema.items),
|
155
|
-
),
|
156
|
-
];
|
157
|
-
if (schema.minItems !== undefined)
|
158
|
-
intersection.push(importer.tag("MinItems", schema.minItems));
|
159
|
-
if (schema.maxItems !== undefined)
|
160
|
-
intersection.push(importer.tag("MaxItems", schema.maxItems));
|
161
|
-
return intersection.length === 1
|
162
|
-
? intersection[0]
|
163
|
-
: ts.factory.createIntersectionTypeNode(intersection);
|
164
|
-
};
|
165
|
-
|
166
|
-
const writeObject =
|
167
|
-
(components: ISwaggerComponents) =>
|
168
|
-
(importer: MigrateImportProgrammer) =>
|
169
|
-
(schema: ISwaggerSchema.IObject): ts.TypeNode => {
|
170
|
-
const regular = () =>
|
171
|
-
ts.factory.createTypeLiteralNode(
|
172
|
-
Object.entries(schema.properties ?? []).map(([key, value]) =>
|
173
|
-
writeRegularProperty(components)(importer)(schema.required ?? [])(
|
174
|
-
key,
|
175
|
-
value,
|
176
|
-
),
|
177
|
-
),
|
178
|
-
);
|
179
|
-
const dynamic = () =>
|
180
|
-
ts.factory.createTypeLiteralNode([
|
181
|
-
writeDynamicProperty(components)(importer)(
|
182
|
-
schema.additionalProperties as ISwaggerSchema,
|
183
|
-
),
|
184
|
-
]);
|
185
|
-
return !!schema.properties?.length &&
|
186
|
-
typeof schema.additionalProperties === "object"
|
187
|
-
? ts.factory.createIntersectionTypeNode([regular(), dynamic()])
|
188
|
-
: typeof schema.additionalProperties === "object"
|
189
|
-
? dynamic()
|
190
|
-
: regular();
|
191
|
-
};
|
192
|
-
|
193
|
-
const writeRegularProperty =
|
194
|
-
(components: ISwaggerComponents) =>
|
195
|
-
(importer: MigrateImportProgrammer) =>
|
196
|
-
(required: string[]) =>
|
197
|
-
(key: string, value: ISwaggerSchema) =>
|
198
|
-
FilePrinter.description(
|
199
|
-
ts.factory.createPropertySignature(
|
200
|
-
undefined,
|
201
|
-
Escaper.variable(key)
|
202
|
-
? ts.factory.createIdentifier(key)
|
203
|
-
: ts.factory.createStringLiteral(key),
|
204
|
-
required.includes(key)
|
205
|
-
? undefined
|
206
|
-
: ts.factory.createToken(ts.SyntaxKind.QuestionToken),
|
207
|
-
write(components)(importer)(value),
|
208
|
-
),
|
209
|
-
writeComment(value),
|
210
|
-
);
|
211
|
-
|
212
|
-
const writeDynamicProperty =
|
213
|
-
(components: ISwaggerComponents) =>
|
214
|
-
(importer: MigrateImportProgrammer) =>
|
215
|
-
(value: ISwaggerSchema) =>
|
216
|
-
FilePrinter.description(
|
217
|
-
ts.factory.createIndexSignature(
|
218
|
-
undefined,
|
219
|
-
[
|
220
|
-
ts.factory.createParameterDeclaration(
|
221
|
-
undefined,
|
222
|
-
undefined,
|
223
|
-
ts.factory.createIdentifier("key"),
|
224
|
-
undefined,
|
225
|
-
TypeFactory.keyword("string"),
|
226
|
-
),
|
227
|
-
],
|
228
|
-
write(components)(importer)(value),
|
229
|
-
),
|
230
|
-
writeComment(value),
|
231
|
-
);
|
232
|
-
|
233
|
-
const writeReference =
|
234
|
-
(importer: MigrateImportProgrammer) =>
|
235
|
-
(schema: ISwaggerSchema.IReference): ts.TypeReferenceNode =>
|
236
|
-
importer.dto(schema.$ref.split("/").at(-1)!);
|
237
|
-
|
238
|
-
/* -----------------------------------------------------------
|
239
|
-
UNIONS
|
240
|
-
----------------------------------------------------------- */
|
241
|
-
const writeUnion =
|
242
|
-
(components: ISwaggerComponents) =>
|
243
|
-
(importer: MigrateImportProgrammer) =>
|
244
|
-
(elements: ISwaggerSchema[]): ts.UnionTypeNode =>
|
245
|
-
ts.factory.createUnionTypeNode(elements.map(write(components)(importer)));
|
246
|
-
}
|
247
|
-
const createNode = (text: string) => ts.factory.createTypeReferenceNode(text);
|
248
|
-
const writeComment = (schema: ISwaggerSchema): string =>
|
249
|
-
[
|
250
|
-
...(schema.description?.length ? [schema.description] : []),
|
251
|
-
...(schema.description?.length &&
|
252
|
-
(schema.title !== undefined || schema.deprecated === true)
|
253
|
-
? [""]
|
254
|
-
: []),
|
255
|
-
...(schema.title !== undefined ? [`@title ${schema.title}`] : []),
|
256
|
-
...(schema.deprecated === true ? [`@deprecated`] : []),
|
257
|
-
].join("\n");
|
1
|
+
import ts from "typescript";
|
2
|
+
import { ExpressionFactory } from "typia/lib/factories/ExpressionFactory";
|
3
|
+
import { TypeFactory } from "typia/lib/factories/TypeFactory";
|
4
|
+
import { FormatCheatSheet } from "typia/lib/tags/internal/FormatCheatSheet";
|
5
|
+
import { Escaper } from "typia/lib/utils/Escaper";
|
6
|
+
|
7
|
+
import { ISwaggerSchema } from "../structures/ISwaggerSchema";
|
8
|
+
import { ISwaggerComponents } from "../structures/ISwaggerComponents";
|
9
|
+
import { FilePrinter } from "../utils/FilePrinter";
|
10
|
+
import { SwaggerSwaggerChecker } from "../utils/SwaggerTypeChecker";
|
11
|
+
import { MigrateImportProgrammer } from "./MigrateImportProgrammer";
|
12
|
+
|
13
|
+
export namespace MigrateSchemaProgrammer {
|
14
|
+
/* -----------------------------------------------------------
|
15
|
+
FACADE
|
16
|
+
----------------------------------------------------------- */
|
17
|
+
export const write =
|
18
|
+
(components: ISwaggerComponents) =>
|
19
|
+
(importer: MigrateImportProgrammer) =>
|
20
|
+
(schema: ISwaggerSchema): ts.TypeNode => {
|
21
|
+
const union: ts.TypeNode[] = [];
|
22
|
+
if (SwaggerSwaggerChecker.isUnknown(schema))
|
23
|
+
return TypeFactory.keyword("any");
|
24
|
+
else if (SwaggerSwaggerChecker.isNullOnly(schema))
|
25
|
+
return createNode("null");
|
26
|
+
else if (SwaggerSwaggerChecker.isNullable(components)(schema))
|
27
|
+
union.push(createNode("null"));
|
28
|
+
|
29
|
+
const type: ts.TypeNode = (() => {
|
30
|
+
// ATOMIC
|
31
|
+
if (SwaggerSwaggerChecker.isBoolean(schema))
|
32
|
+
return writeBoolean(schema);
|
33
|
+
else if (SwaggerSwaggerChecker.isInteger(schema))
|
34
|
+
return writeInteger(importer)(schema);
|
35
|
+
else if (SwaggerSwaggerChecker.isNumber(schema))
|
36
|
+
return writeNumber(importer)(schema);
|
37
|
+
// INSTANCES
|
38
|
+
else if (SwaggerSwaggerChecker.isString(schema))
|
39
|
+
return writeString(importer)(schema);
|
40
|
+
else if (SwaggerSwaggerChecker.isArray(schema))
|
41
|
+
return writeArray(components)(importer)(schema);
|
42
|
+
else if (SwaggerSwaggerChecker.isObject(schema))
|
43
|
+
return writeObject(components)(importer)(schema);
|
44
|
+
else if (SwaggerSwaggerChecker.isReference(schema))
|
45
|
+
return writeReference(importer)(schema);
|
46
|
+
// NESTED UNION
|
47
|
+
else if (SwaggerSwaggerChecker.isAnyOf(schema))
|
48
|
+
return writeUnion(components)(importer)(schema.anyOf);
|
49
|
+
else if (SwaggerSwaggerChecker.isOneOf(schema))
|
50
|
+
return writeUnion(components)(importer)(schema.oneOf);
|
51
|
+
else return TypeFactory.keyword("any");
|
52
|
+
})();
|
53
|
+
union.push(type);
|
54
|
+
|
55
|
+
if (union.length === 0) return TypeFactory.keyword("any");
|
56
|
+
else if (union.length === 1) return union[0];
|
57
|
+
return ts.factory.createUnionTypeNode(union);
|
58
|
+
};
|
59
|
+
|
60
|
+
/* -----------------------------------------------------------
|
61
|
+
ATOMICS
|
62
|
+
----------------------------------------------------------- */
|
63
|
+
const writeBoolean = (schema: ISwaggerSchema.IBoolean): ts.TypeNode => {
|
64
|
+
if (schema.enum?.length)
|
65
|
+
return ts.factory.createLiteralTypeNode(
|
66
|
+
schema.enum[0] ? ts.factory.createTrue() : ts.factory.createFalse(),
|
67
|
+
);
|
68
|
+
return TypeFactory.keyword("boolean");
|
69
|
+
};
|
70
|
+
|
71
|
+
const writeInteger =
|
72
|
+
(importer: MigrateImportProgrammer) =>
|
73
|
+
(schema: ISwaggerSchema.IInteger): ts.TypeNode =>
|
74
|
+
writeNumeric(() => [
|
75
|
+
TypeFactory.keyword("number"),
|
76
|
+
importer.tag("Type", "int32"),
|
77
|
+
])(importer)(schema);
|
78
|
+
|
79
|
+
const writeNumber =
|
80
|
+
(importer: MigrateImportProgrammer) =>
|
81
|
+
(schema: ISwaggerSchema.INumber): ts.TypeNode =>
|
82
|
+
writeNumeric(() => [TypeFactory.keyword("number")])(importer)(schema);
|
83
|
+
|
84
|
+
const writeNumeric =
|
85
|
+
(factory: () => ts.TypeNode[]) =>
|
86
|
+
(importer: MigrateImportProgrammer) =>
|
87
|
+
(schema: ISwaggerSchema.IInteger | ISwaggerSchema.INumber): ts.TypeNode => {
|
88
|
+
if (schema.enum?.length)
|
89
|
+
return ts.factory.createUnionTypeNode(
|
90
|
+
schema.enum.map((i) =>
|
91
|
+
ts.factory.createLiteralTypeNode(ExpressionFactory.number(i)),
|
92
|
+
),
|
93
|
+
);
|
94
|
+
const intersection: ts.TypeNode[] = factory();
|
95
|
+
if (schema.default !== undefined)
|
96
|
+
intersection.push(importer.tag("Default", schema.default));
|
97
|
+
if (schema.minimum !== undefined)
|
98
|
+
intersection.push(
|
99
|
+
importer.tag(
|
100
|
+
schema.exclusiveMinimum ? "ExclusiveMinimum" : "Minimum",
|
101
|
+
schema.minimum,
|
102
|
+
),
|
103
|
+
);
|
104
|
+
if (schema.maximum !== undefined)
|
105
|
+
intersection.push(
|
106
|
+
importer.tag(
|
107
|
+
schema.exclusiveMaximum ? "ExclusiveMaximum" : "Maximum",
|
108
|
+
schema.maximum,
|
109
|
+
),
|
110
|
+
);
|
111
|
+
if (schema.multipleOf !== undefined)
|
112
|
+
intersection.push(importer.tag("MultipleOf", schema.multipleOf));
|
113
|
+
|
114
|
+
return intersection.length === 1
|
115
|
+
? intersection[0]
|
116
|
+
: ts.factory.createIntersectionTypeNode(intersection);
|
117
|
+
};
|
118
|
+
|
119
|
+
const writeString =
|
120
|
+
(importer: MigrateImportProgrammer) =>
|
121
|
+
(schema: ISwaggerSchema.IString): ts.TypeNode => {
|
122
|
+
if (schema.format === "binary")
|
123
|
+
return ts.factory.createTypeReferenceNode("File");
|
124
|
+
|
125
|
+
const intersection: ts.TypeNode[] = [TypeFactory.keyword("string")];
|
126
|
+
if (schema.default !== undefined)
|
127
|
+
intersection.push(importer.tag("Default", schema.default));
|
128
|
+
if (schema.minLength !== undefined)
|
129
|
+
intersection.push(importer.tag("MinLength", schema.minLength));
|
130
|
+
if (schema.maxLength !== undefined)
|
131
|
+
intersection.push(importer.tag("MaxLength", schema.maxLength));
|
132
|
+
if (schema.pattern !== undefined)
|
133
|
+
intersection.push(importer.tag("Pattern", schema.pattern));
|
134
|
+
if (
|
135
|
+
schema.format !== undefined &&
|
136
|
+
(FormatCheatSheet as Record<string, string>)[schema.format] !==
|
137
|
+
undefined
|
138
|
+
)
|
139
|
+
intersection.push(importer.tag("Format", schema.format));
|
140
|
+
return intersection.length === 1
|
141
|
+
? intersection[0]
|
142
|
+
: ts.factory.createIntersectionTypeNode(intersection);
|
143
|
+
};
|
144
|
+
|
145
|
+
/* -----------------------------------------------------------
|
146
|
+
INSTANCES
|
147
|
+
----------------------------------------------------------- */
|
148
|
+
const writeArray =
|
149
|
+
(components: ISwaggerComponents) =>
|
150
|
+
(importer: MigrateImportProgrammer) =>
|
151
|
+
(schema: ISwaggerSchema.IArray): ts.TypeNode => {
|
152
|
+
const intersection: ts.TypeNode[] = [
|
153
|
+
ts.factory.createArrayTypeNode(
|
154
|
+
write(components)(importer)(schema.items),
|
155
|
+
),
|
156
|
+
];
|
157
|
+
if (schema.minItems !== undefined)
|
158
|
+
intersection.push(importer.tag("MinItems", schema.minItems));
|
159
|
+
if (schema.maxItems !== undefined)
|
160
|
+
intersection.push(importer.tag("MaxItems", schema.maxItems));
|
161
|
+
return intersection.length === 1
|
162
|
+
? intersection[0]
|
163
|
+
: ts.factory.createIntersectionTypeNode(intersection);
|
164
|
+
};
|
165
|
+
|
166
|
+
const writeObject =
|
167
|
+
(components: ISwaggerComponents) =>
|
168
|
+
(importer: MigrateImportProgrammer) =>
|
169
|
+
(schema: ISwaggerSchema.IObject): ts.TypeNode => {
|
170
|
+
const regular = () =>
|
171
|
+
ts.factory.createTypeLiteralNode(
|
172
|
+
Object.entries(schema.properties ?? []).map(([key, value]) =>
|
173
|
+
writeRegularProperty(components)(importer)(schema.required ?? [])(
|
174
|
+
key,
|
175
|
+
value,
|
176
|
+
),
|
177
|
+
),
|
178
|
+
);
|
179
|
+
const dynamic = () =>
|
180
|
+
ts.factory.createTypeLiteralNode([
|
181
|
+
writeDynamicProperty(components)(importer)(
|
182
|
+
schema.additionalProperties as ISwaggerSchema,
|
183
|
+
),
|
184
|
+
]);
|
185
|
+
return !!schema.properties?.length &&
|
186
|
+
typeof schema.additionalProperties === "object"
|
187
|
+
? ts.factory.createIntersectionTypeNode([regular(), dynamic()])
|
188
|
+
: typeof schema.additionalProperties === "object"
|
189
|
+
? dynamic()
|
190
|
+
: regular();
|
191
|
+
};
|
192
|
+
|
193
|
+
const writeRegularProperty =
|
194
|
+
(components: ISwaggerComponents) =>
|
195
|
+
(importer: MigrateImportProgrammer) =>
|
196
|
+
(required: string[]) =>
|
197
|
+
(key: string, value: ISwaggerSchema) =>
|
198
|
+
FilePrinter.description(
|
199
|
+
ts.factory.createPropertySignature(
|
200
|
+
undefined,
|
201
|
+
Escaper.variable(key)
|
202
|
+
? ts.factory.createIdentifier(key)
|
203
|
+
: ts.factory.createStringLiteral(key),
|
204
|
+
required.includes(key)
|
205
|
+
? undefined
|
206
|
+
: ts.factory.createToken(ts.SyntaxKind.QuestionToken),
|
207
|
+
write(components)(importer)(value),
|
208
|
+
),
|
209
|
+
writeComment(value),
|
210
|
+
);
|
211
|
+
|
212
|
+
const writeDynamicProperty =
|
213
|
+
(components: ISwaggerComponents) =>
|
214
|
+
(importer: MigrateImportProgrammer) =>
|
215
|
+
(value: ISwaggerSchema) =>
|
216
|
+
FilePrinter.description(
|
217
|
+
ts.factory.createIndexSignature(
|
218
|
+
undefined,
|
219
|
+
[
|
220
|
+
ts.factory.createParameterDeclaration(
|
221
|
+
undefined,
|
222
|
+
undefined,
|
223
|
+
ts.factory.createIdentifier("key"),
|
224
|
+
undefined,
|
225
|
+
TypeFactory.keyword("string"),
|
226
|
+
),
|
227
|
+
],
|
228
|
+
write(components)(importer)(value),
|
229
|
+
),
|
230
|
+
writeComment(value),
|
231
|
+
);
|
232
|
+
|
233
|
+
const writeReference =
|
234
|
+
(importer: MigrateImportProgrammer) =>
|
235
|
+
(schema: ISwaggerSchema.IReference): ts.TypeReferenceNode =>
|
236
|
+
importer.dto(schema.$ref.split("/").at(-1)!);
|
237
|
+
|
238
|
+
/* -----------------------------------------------------------
|
239
|
+
UNIONS
|
240
|
+
----------------------------------------------------------- */
|
241
|
+
const writeUnion =
|
242
|
+
(components: ISwaggerComponents) =>
|
243
|
+
(importer: MigrateImportProgrammer) =>
|
244
|
+
(elements: ISwaggerSchema[]): ts.UnionTypeNode =>
|
245
|
+
ts.factory.createUnionTypeNode(elements.map(write(components)(importer)));
|
246
|
+
}
|
247
|
+
const createNode = (text: string) => ts.factory.createTypeReferenceNode(text);
|
248
|
+
const writeComment = (schema: ISwaggerSchema): string =>
|
249
|
+
[
|
250
|
+
...(schema.description?.length ? [schema.description] : []),
|
251
|
+
...(schema.description?.length &&
|
252
|
+
(schema.title !== undefined || schema.deprecated === true)
|
253
|
+
? [""]
|
254
|
+
: []),
|
255
|
+
...(schema.title !== undefined ? [`@title ${schema.title}`] : []),
|
256
|
+
...(schema.deprecated === true ? [`@deprecated`] : []),
|
257
|
+
].join("\n");
|