@nestia/migrate 0.7.5 → 0.7.6

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.
Files changed (114) hide show
  1. package/lib/MigrateApplication.d.ts +9 -2
  2. package/lib/MigrateApplication.js +14 -8
  3. package/lib/MigrateApplication.js.map +1 -1
  4. package/lib/analyzers/MigrateAnalyzer.js +2 -2
  5. package/lib/analyzers/MigrateAnalyzer.js.map +1 -1
  6. package/lib/analyzers/{ControllerAnalyzer.d.ts → MigrateControllerAnalyzer.d.ts} +1 -1
  7. package/lib/analyzers/{ControllerAnalyzer.js → MigrateControllerAnalyzer.js} +11 -9
  8. package/lib/analyzers/MigrateControllerAnalyzer.js.map +1 -0
  9. package/lib/analyzers/{MethodAnalyzer.d.ts → MigrateMethodAnalyzer.d.ts} +1 -1
  10. package/lib/analyzers/{MethodAnalyzer.js → MigrateMethodAnalyzer.js} +26 -25
  11. package/lib/analyzers/MigrateMethodAnalyzer.js.map +1 -0
  12. package/lib/archivers/{FileArchiver.d.ts → MigrateFileArchiver.d.ts} +1 -1
  13. package/lib/archivers/{FileArchiver.js → MigrateFileArchiver.js} +6 -6
  14. package/lib/archivers/MigrateFileArchiver.js.map +1 -0
  15. package/lib/bundles/SDK_TEMPLATE.js +0 -5
  16. package/lib/bundles/SDK_TEMPLATE.js.map +1 -1
  17. package/lib/executable/bundle.js +1 -0
  18. package/lib/executable/bundle.js.map +1 -1
  19. package/lib/internal/MigrateCommander.js +3 -3
  20. package/lib/internal/MigrateCommander.js.map +1 -1
  21. package/lib/programmers/{ApiFileProgrammer.d.ts → MigrateApiFileProgrammer.d.ts} +1 -1
  22. package/lib/programmers/MigrateApiFileProgrammer.js +28 -0
  23. package/lib/programmers/MigrateApiFileProgrammer.js.map +1 -0
  24. package/lib/programmers/{ApiFunctionProgrammer.d.ts → MigrateApiFunctionProgrammer.d.ts} +4 -4
  25. package/lib/programmers/{ApiFunctionProgrammer.js → MigrateApiFunctionProgrammer.js} +10 -10
  26. package/lib/programmers/MigrateApiFunctionProgrammer.js.map +1 -0
  27. package/lib/programmers/{ApiNamespaceProgrammer.d.ts → MigrateApiNamespaceProgrammer.d.ts} +3 -3
  28. package/lib/programmers/{ApiNamespaceProgrammer.js → MigrateApiNamespaceProgrammer.js} +19 -19
  29. package/lib/programmers/MigrateApiNamespaceProgrammer.js.map +1 -0
  30. package/lib/programmers/{ApiProgrammer.d.ts → MigrateApiProgrammer.d.ts} +1 -1
  31. package/lib/programmers/{ApiProgrammer.js → MigrateApiProgrammer.js} +25 -20
  32. package/lib/programmers/MigrateApiProgrammer.js.map +1 -0
  33. package/lib/programmers/{ApiSimulatationProgrammer.d.ts → MigrateApiSimulatationProgrammer.d.ts} +4 -4
  34. package/lib/programmers/{ApiSimulatationProgrammer.js → MigrateApiSimulatationProgrammer.js} +16 -16
  35. package/lib/programmers/MigrateApiSimulatationProgrammer.js.map +1 -0
  36. package/lib/programmers/{DtoProgrammer.d.ts → MigrateDtoProgrammer.d.ts} +3 -3
  37. package/lib/programmers/{DtoProgrammer.js → MigrateDtoProgrammer.js} +8 -8
  38. package/lib/programmers/MigrateDtoProgrammer.js.map +1 -0
  39. package/lib/programmers/{ImportProgrammer.d.ts → MigrateImportProgrammer.d.ts} +3 -3
  40. package/lib/programmers/{ImportProgrammer.js → MigrateImportProgrammer.js} +7 -5
  41. package/lib/programmers/MigrateImportProgrammer.js.map +1 -0
  42. package/lib/programmers/{NestControllerProgrammer.d.ts → MigrateNestControllerProgrammer.d.ts} +1 -1
  43. package/lib/programmers/{NestControllerProgrammer.js → MigrateNestControllerProgrammer.js} +11 -11
  44. package/lib/programmers/MigrateNestControllerProgrammer.js.map +1 -0
  45. package/lib/programmers/{NestMethodProgrammer.d.ts → MigrateNestMethodProgrammer.d.ts} +3 -3
  46. package/lib/programmers/{NestMethodProgrammer.js → MigrateNestMethodProgrammer.js} +13 -11
  47. package/lib/programmers/MigrateNestMethodProgrammer.js.map +1 -0
  48. package/lib/programmers/{NestModuleProgrammer.d.ts → MigrateNestModuleProgrammer.d.ts} +1 -1
  49. package/lib/programmers/{NestModuleProgrammer.js → MigrateNestModuleProgrammer.js} +8 -8
  50. package/lib/programmers/MigrateNestModuleProgrammer.js.map +1 -0
  51. package/lib/programmers/{NestProgrammer.d.ts → MigrateNestProgrammer.d.ts} +1 -1
  52. package/lib/programmers/{NestProgrammer.js → MigrateNestProgrammer.js} +17 -15
  53. package/lib/programmers/MigrateNestProgrammer.js.map +1 -0
  54. package/lib/programmers/{SchemaProgrammer.d.ts → MigrateSchemaProgrammer.d.ts} +3 -3
  55. package/lib/programmers/{SchemaProgrammer.js → MigrateSchemaProgrammer.js} +23 -23
  56. package/lib/programmers/MigrateSchemaProgrammer.js.map +1 -0
  57. package/lib/structures/IMigrateRoute.d.ts +1 -0
  58. package/lib/utils/FilePrinter.d.ts +1 -1
  59. package/lib/utils/FilePrinter.js +1 -1
  60. package/lib/utils/FilePrinter.js.map +1 -1
  61. package/lib/utils/{JsonTypeChecker.d.ts → SwaggerTypeChecker.d.ts} +1 -1
  62. package/lib/utils/SwaggerTypeChecker.js +34 -0
  63. package/lib/utils/SwaggerTypeChecker.js.map +1 -0
  64. package/package.json +68 -68
  65. package/src/IMigrateConfig.ts +4 -4
  66. package/src/MigrateApplication.ts +48 -35
  67. package/src/analyzers/MigrateAnalyzer.ts +13 -13
  68. package/src/analyzers/{ControllerAnalyzer.ts → MigrateControllerAnalyzer.ts} +122 -123
  69. package/src/analyzers/{MethodAnalyzer.ts → MigrateMethodAnalyzer.ts} +348 -347
  70. package/src/archivers/{FileArchiver.ts → MigrateFileArchiver.ts} +38 -38
  71. package/src/bundles/SDK_TEMPLATE.ts +0 -5
  72. package/src/executable/bundle.ts +109 -108
  73. package/src/executable/migrate.ts +7 -7
  74. package/src/internal/MigrateCommander.ts +51 -52
  75. package/src/internal/MigrateInquirer.ts +79 -79
  76. package/src/module.ts +4 -4
  77. package/src/programmers/{ApiFileProgrammer.ts → MigrateApiFileProgrammer.ts} +53 -53
  78. package/src/programmers/{ApiFunctionProgrammer.ts → MigrateApiFunctionProgrammer.ts} +203 -203
  79. package/src/programmers/{ApiNamespaceProgrammer.ts → MigrateApiNamespaceProgrammer.ts} +430 -418
  80. package/src/programmers/{ApiProgrammer.ts → MigrateApiProgrammer.ts} +132 -128
  81. package/src/programmers/{ApiSimulatationProgrammer.ts → MigrateApiSimulatationProgrammer.ts} +328 -328
  82. package/src/programmers/{DtoProgrammer.ts → MigrateDtoProgrammer.ts} +78 -74
  83. package/src/programmers/{ImportProgrammer.ts → MigrateImportProgrammer.ts} +116 -114
  84. package/src/programmers/{NestControllerProgrammer.ts → MigrateNestControllerProgrammer.ts} +50 -48
  85. package/src/programmers/{NestMethodProgrammer.ts → MigrateNestMethodProgrammer.ts} +249 -243
  86. package/src/programmers/{NestModuleProgrammer.ts → MigrateNestModuleProgrammer.ts} +62 -62
  87. package/src/programmers/{NestProgrammer.ts → MigrateNestProgrammer.ts} +74 -74
  88. package/src/programmers/{SchemaProgrammer.ts → MigrateSchemaProgrammer.ts} +257 -255
  89. package/src/structures/IMigrateProgram.ts +9 -9
  90. package/src/structures/IMigrateRoute.ts +46 -45
  91. package/src/structures/ISwaggeSchema.ts +90 -90
  92. package/src/structures/ISwaggerInfo.ts +80 -80
  93. package/src/utils/FilePrinter.ts +36 -36
  94. package/src/utils/SetupWizard.ts +12 -12
  95. package/src/utils/StringUtil.ts +60 -60
  96. package/src/utils/{JsonTypeChecker.ts → SwaggerTypeChecker.ts} +67 -67
  97. package/lib/analyzers/ControllerAnalyzer.js.map +0 -1
  98. package/lib/analyzers/MethodAnalyzer.js.map +0 -1
  99. package/lib/archivers/FileArchiver.js.map +0 -1
  100. package/lib/programmers/ApiFileProgrammer.js +0 -28
  101. package/lib/programmers/ApiFileProgrammer.js.map +0 -1
  102. package/lib/programmers/ApiFunctionProgrammer.js.map +0 -1
  103. package/lib/programmers/ApiNamespaceProgrammer.js.map +0 -1
  104. package/lib/programmers/ApiProgrammer.js.map +0 -1
  105. package/lib/programmers/ApiSimulatationProgrammer.js.map +0 -1
  106. package/lib/programmers/DtoProgrammer.js.map +0 -1
  107. package/lib/programmers/ImportProgrammer.js.map +0 -1
  108. package/lib/programmers/NestControllerProgrammer.js.map +0 -1
  109. package/lib/programmers/NestMethodProgrammer.js.map +0 -1
  110. package/lib/programmers/NestModuleProgrammer.js.map +0 -1
  111. package/lib/programmers/NestProgrammer.js.map +0 -1
  112. package/lib/programmers/SchemaProgrammer.js.map +0 -1
  113. package/lib/utils/JsonTypeChecker.js +0 -34
  114. package/lib/utils/JsonTypeChecker.js.map +0 -1
@@ -1,418 +1,430 @@
1
- import ts from "typescript";
2
- import { ExpressionFactory } from "typia/lib/factories/ExpressionFactory";
3
- import { IdentifierFactory } from "typia/lib/factories/IdentifierFactory";
4
- import { LiteralFactory } from "typia/lib/factories/LiteralFactory";
5
- import { TypeFactory } from "typia/lib/factories/TypeFactory";
6
-
7
- import { IMigrateConfig } from "../IMigrateConfig";
8
- import { IMigrateController } from "../structures/IMigrateController";
9
- import { IMigrateRoute } from "../structures/IMigrateRoute";
10
- import { ISwaggerComponents } from "../structures/ISwaggerComponents";
11
- import { FilePrinter } from "../utils/FilePrinter";
12
- import { ApiSimulatationProgrammer } from "./ApiSimulatationProgrammer";
13
- import { ImportProgrammer } from "./ImportProgrammer";
14
- import { SchemaProgrammer } from "./SchemaProgrammer";
15
-
16
- export namespace ApiNamespaceProgrammer {
17
- export interface IProps {
18
- controller: IMigrateController;
19
- route: IMigrateRoute;
20
- alias: string;
21
- }
22
-
23
- export const write =
24
- (config: IMigrateConfig) =>
25
- (components: ISwaggerComponents) =>
26
- (importer: ImportProgrammer) =>
27
- (props: IProps): ts.ModuleDeclaration => {
28
- const types = writeTypes(components)(importer)(props.route);
29
- return ts.factory.createModuleDeclaration(
30
- [ts.factory.createToken(ts.SyntaxKind.ExportKeyword)],
31
- ts.factory.createIdentifier(props.alias),
32
- ts.factory.createModuleBlock([
33
- ...types,
34
- ...(types.length ? [FilePrinter.enter()] : []),
35
- writeMetadata(components)(importer)(props),
36
- FilePrinter.enter(),
37
- writePath(components)(importer)(props),
38
- ...(config.simulate
39
- ? [
40
- ApiSimulatationProgrammer.random(components)(importer)(props),
41
- ApiSimulatationProgrammer.simulate(components)(importer)(props),
42
- ]
43
- : []),
44
- ]),
45
- ts.NodeFlags.Namespace,
46
- );
47
- };
48
-
49
- export const writePathCallExpression = (props: IProps) =>
50
- ts.factory.createCallExpression(
51
- ts.factory.createIdentifier(`${props.alias}.path`),
52
- undefined,
53
- [
54
- ...props.route.parameters.map((p) =>
55
- ts.factory.createIdentifier(p.key),
56
- ),
57
- ...(props.route.query
58
- ? [ts.factory.createIdentifier(props.route.query.key)]
59
- : []),
60
- ],
61
- );
62
-
63
- const writeTypes =
64
- (components: ISwaggerComponents) =>
65
- (importer: ImportProgrammer) =>
66
- (route: IMigrateRoute): ts.TypeAliasDeclaration[] => {
67
- const array: ts.TypeAliasDeclaration[] = [];
68
- const declare = (name: string, type: ts.TypeNode) =>
69
- array.push(
70
- ts.factory.createTypeAliasDeclaration(
71
- [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
72
- name,
73
- undefined,
74
- type,
75
- ),
76
- );
77
- if (route.headers)
78
- declare(
79
- "Headers",
80
- SchemaProgrammer.write(components)(importer)(route.headers.schema),
81
- );
82
- if (route.query)
83
- declare(
84
- "Query",
85
- SchemaProgrammer.write(components)(importer)(route.query.schema),
86
- );
87
- if (route.body)
88
- declare(
89
- "Input",
90
- SchemaProgrammer.write(components)(importer)(route.body.schema),
91
- );
92
- if (route.success)
93
- declare(
94
- "Output",
95
- SchemaProgrammer.write(components)(importer)(route.success.schema),
96
- );
97
- return array;
98
- };
99
-
100
- const writeMetadata =
101
- (components: ISwaggerComponents) =>
102
- (importer: ImportProgrammer) =>
103
- (props: IProps): ts.VariableStatement =>
104
- constant("METADATA")(
105
- ts.factory.createAsExpression(
106
- ts.factory.createObjectLiteralExpression(
107
- [
108
- ts.factory.createPropertyAssignment(
109
- "method",
110
- ts.factory.createStringLiteral(
111
- props.route.method.toUpperCase(),
112
- ),
113
- ),
114
- ts.factory.createPropertyAssignment(
115
- "path",
116
- ts.factory.createStringLiteral(getPath(props)),
117
- ),
118
- ts.factory.createPropertyAssignment(
119
- "request",
120
- props.route.body
121
- ? LiteralFactory.generate({
122
- type: props.route.body.type,
123
- encrypted: !!props.route.body["x-nestia-encrypted"],
124
- })
125
- : ts.factory.createNull(),
126
- ),
127
- ts.factory.createPropertyAssignment(
128
- "response",
129
- props.route.method.toUpperCase() !== "HEAD"
130
- ? LiteralFactory.generate({
131
- type: props.route.success?.type ?? "application/json",
132
- encrypted: !!props.route.success?.["x-nestia-encrypted"],
133
- })
134
- : ts.factory.createNull(),
135
- ),
136
- ...(props.route.success?.type ===
137
- "application/x-www-form-urlencoded"
138
- ? [
139
- ts.factory.createPropertyAssignment(
140
- "parseQuery",
141
- ts.factory.createCallExpression(
142
- ts.factory.createIdentifier(
143
- `${importer.external({
144
- type: "default",
145
- library: "typia",
146
- name: "typia",
147
- })}.http.createAssertQuery`,
148
- ),
149
- [
150
- SchemaProgrammer.write(components)(importer)(
151
- props.route.success.schema,
152
- ),
153
- ],
154
- undefined,
155
- ),
156
- ),
157
- ]
158
- : []),
159
- ],
160
- true,
161
- ),
162
- ts.factory.createTypeReferenceNode(
163
- ts.factory.createIdentifier("const"),
164
- ),
165
- ),
166
- );
167
-
168
- const writePath =
169
- (components: ISwaggerComponents) =>
170
- (importer: ImportProgrammer) =>
171
- (props: IProps): ts.VariableStatement => {
172
- const out = (body: ts.ConciseBody) =>
173
- constant("path")(
174
- ts.factory.createArrowFunction(
175
- [],
176
- [],
177
- [
178
- ...props.route.parameters.map((p) =>
179
- IdentifierFactory.parameter(
180
- p.key,
181
- SchemaProgrammer.write(components)(importer)(p.schema),
182
- ),
183
- ),
184
- ...(props.route.query
185
- ? [
186
- IdentifierFactory.parameter(
187
- props.route.query.key,
188
- ts.factory.createTypeReferenceNode(
189
- `${props.alias}.Query`,
190
- ),
191
- ),
192
- ]
193
- : []),
194
- ],
195
- undefined,
196
- undefined,
197
- body,
198
- ),
199
- );
200
- const template = () => {
201
- const path: string = getPath(props);
202
- const splitted: string[] = path.split(":");
203
- if (splitted.length === 1) return ts.factory.createStringLiteral(path);
204
- return ts.factory.createTemplateExpression(
205
- ts.factory.createTemplateHead(splitted[0]),
206
- splitted.slice(1).map((s, i, arr) => {
207
- const name: string = s.split("/")[0];
208
- return ts.factory.createTemplateSpan(
209
- ts.factory.createCallExpression(
210
- ts.factory.createIdentifier("encodeURIComponent"),
211
- undefined,
212
- [
213
- ts.factory.createBinaryExpression(
214
- ts.factory.createIdentifier(
215
- props.route.parameters.find((p) => p.key === name)!.key,
216
- ),
217
- ts.factory.createToken(ts.SyntaxKind.QuestionQuestionToken),
218
- ts.factory.createStringLiteral("null"),
219
- ),
220
- ],
221
- ),
222
- (i !== arr.length - 1
223
- ? ts.factory.createTemplateMiddle
224
- : ts.factory.createTemplateTail)(s.substring(name.length)),
225
- );
226
- }),
227
- );
228
- };
229
- if (!props.route.query) return out(template());
230
-
231
- const computeName = (str: string): string =>
232
- props.route.parameters.find((p) => p.key === str) !== undefined
233
- ? computeName("_" + str)
234
- : str;
235
- const variables: string = computeName("variables");
236
- return out(
237
- ts.factory.createBlock(
238
- [
239
- local(variables)("URLSearchParams")(
240
- ts.factory.createNewExpression(
241
- ts.factory.createIdentifier("URLSearchParams"),
242
- [],
243
- [],
244
- ),
245
- ),
246
- ts.factory.createForOfStatement(
247
- undefined,
248
- ts.factory.createVariableDeclarationList(
249
- [
250
- ts.factory.createVariableDeclaration(
251
- ts.factory.createArrayBindingPattern([
252
- ts.factory.createBindingElement(
253
- undefined,
254
- undefined,
255
- ts.factory.createIdentifier("key"),
256
- undefined,
257
- ),
258
- ts.factory.createBindingElement(
259
- undefined,
260
- undefined,
261
- ts.factory.createIdentifier("value"),
262
- undefined,
263
- ),
264
- ]),
265
- undefined,
266
- undefined,
267
- undefined,
268
- ),
269
- ],
270
- ts.NodeFlags.Const,
271
- ),
272
- ts.factory.createCallExpression(
273
- ts.factory.createIdentifier("Object.entries"),
274
- undefined,
275
- [
276
- ts.factory.createAsExpression(
277
- ts.factory.createIdentifier(props.route.query.key),
278
- TypeFactory.keyword("any"),
279
- ),
280
- ],
281
- ),
282
- ts.factory.createIfStatement(
283
- ts.factory.createStrictEquality(
284
- ts.factory.createIdentifier("undefined"),
285
- ts.factory.createIdentifier("value"),
286
- ),
287
- ts.factory.createContinueStatement(),
288
- ts.factory.createIfStatement(
289
- ts.factory.createCallExpression(
290
- ts.factory.createIdentifier("Array.isArray"),
291
- undefined,
292
- [ts.factory.createIdentifier("value")],
293
- ),
294
- ts.factory.createExpressionStatement(
295
- ts.factory.createCallExpression(
296
- ts.factory.createPropertyAccessExpression(
297
- ts.factory.createIdentifier("value"),
298
- ts.factory.createIdentifier("forEach"),
299
- ),
300
- undefined,
301
- [
302
- ts.factory.createArrowFunction(
303
- undefined,
304
- undefined,
305
- [IdentifierFactory.parameter("elem")],
306
- undefined,
307
- undefined,
308
- ts.factory.createCallExpression(
309
- IdentifierFactory.access(
310
- ts.factory.createIdentifier(variables),
311
- )("append"),
312
- undefined,
313
- [
314
- ts.factory.createIdentifier("key"),
315
- ts.factory.createCallExpression(
316
- ts.factory.createIdentifier("String"),
317
- undefined,
318
- [ts.factory.createIdentifier("elem")],
319
- ),
320
- ],
321
- ),
322
- ),
323
- ],
324
- ),
325
- ),
326
- ts.factory.createExpressionStatement(
327
- ts.factory.createCallExpression(
328
- IdentifierFactory.access(
329
- ts.factory.createIdentifier(variables),
330
- )("set"),
331
- undefined,
332
- [
333
- ts.factory.createIdentifier("key"),
334
- ts.factory.createCallExpression(
335
- ts.factory.createIdentifier("String"),
336
- undefined,
337
- [ts.factory.createIdentifier("value")],
338
- ),
339
- ],
340
- ),
341
- ),
342
- ),
343
- ),
344
- ),
345
- local("location")("string")(template()),
346
- ts.factory.createReturnStatement(
347
- ts.factory.createConditionalExpression(
348
- ts.factory.createStrictEquality(
349
- ExpressionFactory.number(0),
350
- IdentifierFactory.access(
351
- ts.factory.createIdentifier(variables),
352
- )("size"),
353
- ),
354
- undefined,
355
- ts.factory.createIdentifier("location"),
356
- undefined,
357
- ts.factory.createTemplateExpression(
358
- ts.factory.createTemplateHead(""),
359
- [
360
- ts.factory.createTemplateSpan(
361
- ts.factory.createIdentifier("location"),
362
- ts.factory.createTemplateMiddle("?"),
363
- ),
364
- ts.factory.createTemplateSpan(
365
- ts.factory.createCallExpression(
366
- IdentifierFactory.access(
367
- ts.factory.createIdentifier(variables),
368
- )("toString"),
369
- undefined,
370
- undefined,
371
- ),
372
- ts.factory.createTemplateTail(""),
373
- ),
374
- ],
375
- ),
376
- ),
377
- ),
378
- ],
379
- true,
380
- ),
381
- );
382
- };
383
- }
384
-
385
- const constant = (name: string) => (expression: ts.Expression) =>
386
- ts.factory.createVariableStatement(
387
- [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
388
- ts.factory.createVariableDeclarationList(
389
- [
390
- ts.factory.createVariableDeclaration(
391
- name,
392
- undefined,
393
- undefined,
394
- expression,
395
- ),
396
- ],
397
- ts.NodeFlags.Const,
398
- ),
399
- );
400
- const getPath = (props: ApiNamespaceProgrammer.IProps) =>
401
- [...props.controller.path.split("/"), ...props.route.path.split("/")]
402
- .filter((str) => !!str.length)
403
- .join("/");
404
- const local = (name: string) => (type: string) => (expression: ts.Expression) =>
405
- ts.factory.createVariableStatement(
406
- [],
407
- ts.factory.createVariableDeclarationList(
408
- [
409
- ts.factory.createVariableDeclaration(
410
- name,
411
- undefined,
412
- ts.factory.createTypeReferenceNode(type),
413
- expression,
414
- ),
415
- ],
416
- ts.NodeFlags.Const,
417
- ),
418
- );
1
+ import ts from "typescript";
2
+ import { ExpressionFactory } from "typia/lib/factories/ExpressionFactory";
3
+ import { IdentifierFactory } from "typia/lib/factories/IdentifierFactory";
4
+ import { LiteralFactory } from "typia/lib/factories/LiteralFactory";
5
+ import { TypeFactory } from "typia/lib/factories/TypeFactory";
6
+
7
+ import { IMigrateConfig } from "../IMigrateConfig";
8
+ import { IMigrateController } from "../structures/IMigrateController";
9
+ import { IMigrateRoute } from "../structures/IMigrateRoute";
10
+ import { ISwaggerComponents } from "../structures/ISwaggerComponents";
11
+ import { FilePrinter } from "../utils/FilePrinter";
12
+ import { MigrateApiSimulatationProgrammer } from "./MigrateApiSimulatationProgrammer";
13
+ import { MigrateImportProgrammer } from "./MigrateImportProgrammer";
14
+ import { MigrateSchemaProgrammer } from "./MigrateSchemaProgrammer";
15
+
16
+ export namespace MigrateApiNamespaceProgrammer {
17
+ export interface IProps {
18
+ controller: IMigrateController;
19
+ route: IMigrateRoute;
20
+ alias: string;
21
+ }
22
+
23
+ export const write =
24
+ (config: IMigrateConfig) =>
25
+ (components: ISwaggerComponents) =>
26
+ (importer: MigrateImportProgrammer) =>
27
+ (props: IProps): ts.ModuleDeclaration => {
28
+ const types = writeTypes(components)(importer)(props.route);
29
+ return ts.factory.createModuleDeclaration(
30
+ [ts.factory.createToken(ts.SyntaxKind.ExportKeyword)],
31
+ ts.factory.createIdentifier(props.alias),
32
+ ts.factory.createModuleBlock([
33
+ ...types,
34
+ ...(types.length ? [FilePrinter.newLine()] : []),
35
+ writeMetadata(components)(importer)(props),
36
+ FilePrinter.newLine(),
37
+ writePath(components)(importer)(props),
38
+ ...(config.simulate
39
+ ? [
40
+ MigrateApiSimulatationProgrammer.random(components)(importer)(
41
+ props,
42
+ ),
43
+ MigrateApiSimulatationProgrammer.simulate(components)(importer)(
44
+ props,
45
+ ),
46
+ ]
47
+ : []),
48
+ ]),
49
+ ts.NodeFlags.Namespace,
50
+ );
51
+ };
52
+
53
+ export const writePathCallExpression = (props: IProps) =>
54
+ ts.factory.createCallExpression(
55
+ ts.factory.createIdentifier(`${props.alias}.path`),
56
+ undefined,
57
+ [
58
+ ...props.route.parameters.map((p) =>
59
+ ts.factory.createIdentifier(p.key),
60
+ ),
61
+ ...(props.route.query
62
+ ? [ts.factory.createIdentifier(props.route.query.key)]
63
+ : []),
64
+ ],
65
+ );
66
+
67
+ const writeTypes =
68
+ (components: ISwaggerComponents) =>
69
+ (importer: MigrateImportProgrammer) =>
70
+ (route: IMigrateRoute): ts.TypeAliasDeclaration[] => {
71
+ const array: ts.TypeAliasDeclaration[] = [];
72
+ const declare = (name: string, type: ts.TypeNode) =>
73
+ array.push(
74
+ ts.factory.createTypeAliasDeclaration(
75
+ [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
76
+ name,
77
+ undefined,
78
+ type,
79
+ ),
80
+ );
81
+ if (route.headers)
82
+ declare(
83
+ "Headers",
84
+ MigrateSchemaProgrammer.write(components)(importer)(
85
+ route.headers.schema,
86
+ ),
87
+ );
88
+ if (route.query)
89
+ declare(
90
+ "Query",
91
+ MigrateSchemaProgrammer.write(components)(importer)(
92
+ route.query.schema,
93
+ ),
94
+ );
95
+ if (route.body)
96
+ declare(
97
+ "Input",
98
+ MigrateSchemaProgrammer.write(components)(importer)(
99
+ route.body.schema,
100
+ ),
101
+ );
102
+ if (route.success)
103
+ declare(
104
+ "Output",
105
+ MigrateSchemaProgrammer.write(components)(importer)(
106
+ route.success.schema,
107
+ ),
108
+ );
109
+ return array;
110
+ };
111
+
112
+ const writeMetadata =
113
+ (components: ISwaggerComponents) =>
114
+ (importer: MigrateImportProgrammer) =>
115
+ (props: IProps): ts.VariableStatement =>
116
+ constant("METADATA")(
117
+ ts.factory.createAsExpression(
118
+ ts.factory.createObjectLiteralExpression(
119
+ [
120
+ ts.factory.createPropertyAssignment(
121
+ "method",
122
+ ts.factory.createStringLiteral(
123
+ props.route.method.toUpperCase(),
124
+ ),
125
+ ),
126
+ ts.factory.createPropertyAssignment(
127
+ "path",
128
+ ts.factory.createStringLiteral(getPath(props)),
129
+ ),
130
+ ts.factory.createPropertyAssignment(
131
+ "request",
132
+ props.route.body
133
+ ? LiteralFactory.generate({
134
+ type: props.route.body.type,
135
+ encrypted: !!props.route.body["x-nestia-encrypted"],
136
+ })
137
+ : ts.factory.createNull(),
138
+ ),
139
+ ts.factory.createPropertyAssignment(
140
+ "response",
141
+ props.route.method.toUpperCase() !== "HEAD"
142
+ ? LiteralFactory.generate({
143
+ type: props.route.success?.type ?? "application/json",
144
+ encrypted: !!props.route.success?.["x-nestia-encrypted"],
145
+ })
146
+ : ts.factory.createNull(),
147
+ ),
148
+ ...(props.route.success?.type ===
149
+ "application/x-www-form-urlencoded"
150
+ ? [
151
+ ts.factory.createPropertyAssignment(
152
+ "parseQuery",
153
+ ts.factory.createCallExpression(
154
+ ts.factory.createIdentifier(
155
+ `${importer.external({
156
+ type: "default",
157
+ library: "typia",
158
+ name: "typia",
159
+ })}.http.createAssertQuery`,
160
+ ),
161
+ [
162
+ MigrateSchemaProgrammer.write(components)(importer)(
163
+ props.route.success.schema,
164
+ ),
165
+ ],
166
+ undefined,
167
+ ),
168
+ ),
169
+ ]
170
+ : []),
171
+ ],
172
+ true,
173
+ ),
174
+ ts.factory.createTypeReferenceNode(
175
+ ts.factory.createIdentifier("const"),
176
+ ),
177
+ ),
178
+ );
179
+
180
+ const writePath =
181
+ (components: ISwaggerComponents) =>
182
+ (importer: MigrateImportProgrammer) =>
183
+ (props: IProps): ts.VariableStatement => {
184
+ const out = (body: ts.ConciseBody) =>
185
+ constant("path")(
186
+ ts.factory.createArrowFunction(
187
+ [],
188
+ [],
189
+ [
190
+ ...props.route.parameters.map((p) =>
191
+ IdentifierFactory.parameter(
192
+ p.key,
193
+ MigrateSchemaProgrammer.write(components)(importer)(p.schema),
194
+ ),
195
+ ),
196
+ ...(props.route.query
197
+ ? [
198
+ IdentifierFactory.parameter(
199
+ props.route.query.key,
200
+ ts.factory.createTypeReferenceNode(
201
+ `${props.alias}.Query`,
202
+ ),
203
+ ),
204
+ ]
205
+ : []),
206
+ ],
207
+ undefined,
208
+ undefined,
209
+ body,
210
+ ),
211
+ );
212
+ const template = () => {
213
+ const path: string = getPath(props);
214
+ const splitted: string[] = path.split(":");
215
+ if (splitted.length === 1) return ts.factory.createStringLiteral(path);
216
+ return ts.factory.createTemplateExpression(
217
+ ts.factory.createTemplateHead(splitted[0]),
218
+ splitted.slice(1).map((s, i, arr) => {
219
+ const name: string = s.split("/")[0];
220
+ return ts.factory.createTemplateSpan(
221
+ ts.factory.createCallExpression(
222
+ ts.factory.createIdentifier("encodeURIComponent"),
223
+ undefined,
224
+ [
225
+ ts.factory.createBinaryExpression(
226
+ ts.factory.createIdentifier(
227
+ props.route.parameters.find((p) => p.key === name)!.key,
228
+ ),
229
+ ts.factory.createToken(ts.SyntaxKind.QuestionQuestionToken),
230
+ ts.factory.createStringLiteral("null"),
231
+ ),
232
+ ],
233
+ ),
234
+ (i !== arr.length - 1
235
+ ? ts.factory.createTemplateMiddle
236
+ : ts.factory.createTemplateTail)(s.substring(name.length)),
237
+ );
238
+ }),
239
+ );
240
+ };
241
+ if (!props.route.query) return out(template());
242
+
243
+ const computeName = (str: string): string =>
244
+ props.route.parameters.find((p) => p.key === str) !== undefined
245
+ ? computeName("_" + str)
246
+ : str;
247
+ const variables: string = computeName("variables");
248
+ return out(
249
+ ts.factory.createBlock(
250
+ [
251
+ local(variables)("URLSearchParams")(
252
+ ts.factory.createNewExpression(
253
+ ts.factory.createIdentifier("URLSearchParams"),
254
+ [],
255
+ [],
256
+ ),
257
+ ),
258
+ ts.factory.createForOfStatement(
259
+ undefined,
260
+ ts.factory.createVariableDeclarationList(
261
+ [
262
+ ts.factory.createVariableDeclaration(
263
+ ts.factory.createArrayBindingPattern([
264
+ ts.factory.createBindingElement(
265
+ undefined,
266
+ undefined,
267
+ ts.factory.createIdentifier("key"),
268
+ undefined,
269
+ ),
270
+ ts.factory.createBindingElement(
271
+ undefined,
272
+ undefined,
273
+ ts.factory.createIdentifier("value"),
274
+ undefined,
275
+ ),
276
+ ]),
277
+ undefined,
278
+ undefined,
279
+ undefined,
280
+ ),
281
+ ],
282
+ ts.NodeFlags.Const,
283
+ ),
284
+ ts.factory.createCallExpression(
285
+ ts.factory.createIdentifier("Object.entries"),
286
+ undefined,
287
+ [
288
+ ts.factory.createAsExpression(
289
+ ts.factory.createIdentifier(props.route.query.key),
290
+ TypeFactory.keyword("any"),
291
+ ),
292
+ ],
293
+ ),
294
+ ts.factory.createIfStatement(
295
+ ts.factory.createStrictEquality(
296
+ ts.factory.createIdentifier("undefined"),
297
+ ts.factory.createIdentifier("value"),
298
+ ),
299
+ ts.factory.createContinueStatement(),
300
+ ts.factory.createIfStatement(
301
+ ts.factory.createCallExpression(
302
+ ts.factory.createIdentifier("Array.isArray"),
303
+ undefined,
304
+ [ts.factory.createIdentifier("value")],
305
+ ),
306
+ ts.factory.createExpressionStatement(
307
+ ts.factory.createCallExpression(
308
+ ts.factory.createPropertyAccessExpression(
309
+ ts.factory.createIdentifier("value"),
310
+ ts.factory.createIdentifier("forEach"),
311
+ ),
312
+ undefined,
313
+ [
314
+ ts.factory.createArrowFunction(
315
+ undefined,
316
+ undefined,
317
+ [IdentifierFactory.parameter("elem")],
318
+ undefined,
319
+ undefined,
320
+ ts.factory.createCallExpression(
321
+ IdentifierFactory.access(
322
+ ts.factory.createIdentifier(variables),
323
+ )("append"),
324
+ undefined,
325
+ [
326
+ ts.factory.createIdentifier("key"),
327
+ ts.factory.createCallExpression(
328
+ ts.factory.createIdentifier("String"),
329
+ undefined,
330
+ [ts.factory.createIdentifier("elem")],
331
+ ),
332
+ ],
333
+ ),
334
+ ),
335
+ ],
336
+ ),
337
+ ),
338
+ ts.factory.createExpressionStatement(
339
+ ts.factory.createCallExpression(
340
+ IdentifierFactory.access(
341
+ ts.factory.createIdentifier(variables),
342
+ )("set"),
343
+ undefined,
344
+ [
345
+ ts.factory.createIdentifier("key"),
346
+ ts.factory.createCallExpression(
347
+ ts.factory.createIdentifier("String"),
348
+ undefined,
349
+ [ts.factory.createIdentifier("value")],
350
+ ),
351
+ ],
352
+ ),
353
+ ),
354
+ ),
355
+ ),
356
+ ),
357
+ local("location")("string")(template()),
358
+ ts.factory.createReturnStatement(
359
+ ts.factory.createConditionalExpression(
360
+ ts.factory.createStrictEquality(
361
+ ExpressionFactory.number(0),
362
+ IdentifierFactory.access(
363
+ ts.factory.createIdentifier(variables),
364
+ )("size"),
365
+ ),
366
+ undefined,
367
+ ts.factory.createIdentifier("location"),
368
+ undefined,
369
+ ts.factory.createTemplateExpression(
370
+ ts.factory.createTemplateHead(""),
371
+ [
372
+ ts.factory.createTemplateSpan(
373
+ ts.factory.createIdentifier("location"),
374
+ ts.factory.createTemplateMiddle("?"),
375
+ ),
376
+ ts.factory.createTemplateSpan(
377
+ ts.factory.createCallExpression(
378
+ IdentifierFactory.access(
379
+ ts.factory.createIdentifier(variables),
380
+ )("toString"),
381
+ undefined,
382
+ undefined,
383
+ ),
384
+ ts.factory.createTemplateTail(""),
385
+ ),
386
+ ],
387
+ ),
388
+ ),
389
+ ),
390
+ ],
391
+ true,
392
+ ),
393
+ );
394
+ };
395
+ }
396
+
397
+ const constant = (name: string) => (expression: ts.Expression) =>
398
+ ts.factory.createVariableStatement(
399
+ [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
400
+ ts.factory.createVariableDeclarationList(
401
+ [
402
+ ts.factory.createVariableDeclaration(
403
+ name,
404
+ undefined,
405
+ undefined,
406
+ expression,
407
+ ),
408
+ ],
409
+ ts.NodeFlags.Const,
410
+ ),
411
+ );
412
+ const getPath = (props: MigrateApiNamespaceProgrammer.IProps) =>
413
+ [...props.controller.path.split("/"), ...props.route.path.split("/")]
414
+ .filter((str) => !!str.length)
415
+ .join("/");
416
+ const local = (name: string) => (type: string) => (expression: ts.Expression) =>
417
+ ts.factory.createVariableStatement(
418
+ [],
419
+ ts.factory.createVariableDeclarationList(
420
+ [
421
+ ts.factory.createVariableDeclaration(
422
+ name,
423
+ undefined,
424
+ ts.factory.createTypeReferenceNode(type),
425
+ expression,
426
+ ),
427
+ ],
428
+ ts.NodeFlags.Const,
429
+ ),
430
+ );