@nestia/migrate 4.6.0 → 4.6.1-dev.20250117

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