@nestia/migrate 12.0.0-rc.2 → 12.0.0-rc.3

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 (66) hide show
  1. package/LICENSE +21 -21
  2. package/lib/_virtual/_tstl.mjs +2 -2
  3. package/lib/_virtual/index.mjs +2 -2
  4. package/lib/_virtual/index2.mjs +2 -2
  5. package/lib/_virtual/index3.mjs +2 -2
  6. package/lib/archivers/NestiaMigrateFileArchiver2.mjs +2 -2
  7. package/lib/archivers/NestiaMigrateFileArchiver2.mjs.map +1 -1
  8. package/lib/bundles/NEST_TEMPLATE.js +47 -47
  9. package/lib/bundles/NEST_TEMPLATE.js.map +1 -1
  10. package/lib/bundles/NEST_TEMPLATE2.mjs +47 -47
  11. package/lib/bundles/NEST_TEMPLATE2.mjs.map +1 -1
  12. package/lib/bundles/SDK_TEMPLATE.js +20 -20
  13. package/lib/bundles/SDK_TEMPLATE.js.map +1 -1
  14. package/lib/bundles/SDK_TEMPLATE2.mjs +20 -20
  15. package/lib/bundles/SDK_TEMPLATE2.mjs.map +1 -1
  16. package/lib/index2.mjs +1 -1
  17. package/lib/module2.mjs +2 -2
  18. package/lib/programmers/NestiaMigrateApiProgrammer2.mjs +4 -4
  19. package/lib/programmers/NestiaMigrateApiProgrammer2.mjs.map +1 -1
  20. package/lib/programmers/index2.mjs +1 -1
  21. package/lib/structures/index2.mjs +1 -1
  22. package/package.json +6 -6
  23. package/src/NestiaMigrateApplication.ts +196 -196
  24. package/src/analyzers/NestiaMigrateControllerAnalyzer.ts +51 -51
  25. package/src/archivers/NestiaMigrateFileArchiver.ts +28 -28
  26. package/src/bundles/NEST_TEMPLATE.ts +47 -47
  27. package/src/bundles/SDK_TEMPLATE.ts +20 -20
  28. package/src/executable/NestiaMigrateCommander.ts +115 -115
  29. package/src/executable/NestiaMigrateInquirer.ts +106 -106
  30. package/src/executable/bundle.js +323 -349
  31. package/src/executable/migrate.ts +7 -7
  32. package/src/factories/ExpressionFactory.ts +23 -23
  33. package/src/factories/FormatCheatSheet.ts +71 -71
  34. package/src/factories/IdentifierFactory.ts +84 -84
  35. package/src/factories/LiteralFactory.ts +54 -54
  36. package/src/factories/StatementFactory.ts +56 -56
  37. package/src/factories/TypeFactory.ts +27 -27
  38. package/src/factories/TypeLiteralFactory.ts +62 -62
  39. package/src/index.ts +4 -4
  40. package/src/internal/ts.ts +75 -75
  41. package/src/module.ts +6 -6
  42. package/src/programmers/NestiaMigrateApiFileProgrammer.ts +55 -55
  43. package/src/programmers/NestiaMigrateApiFunctionProgrammer.ts +358 -358
  44. package/src/programmers/NestiaMigrateApiNamespaceProgrammer.ts +511 -511
  45. package/src/programmers/NestiaMigrateApiProgrammer.ts +108 -108
  46. package/src/programmers/NestiaMigrateApiSimulationProgrammer.ts +309 -309
  47. package/src/programmers/NestiaMigrateApiStartProgrammer.ts +198 -198
  48. package/src/programmers/NestiaMigrateDtoProgrammer.ts +117 -117
  49. package/src/programmers/NestiaMigrateE2eFileProgrammer.ts +155 -155
  50. package/src/programmers/NestiaMigrateE2eProgrammer.ts +48 -48
  51. package/src/programmers/NestiaMigrateImportProgrammer.ts +119 -119
  52. package/src/programmers/NestiaMigrateNestControllerProgrammer.ts +70 -70
  53. package/src/programmers/NestiaMigrateNestMethodProgrammer.ts +402 -402
  54. package/src/programmers/NestiaMigrateNestModuleProgrammer.ts +64 -64
  55. package/src/programmers/NestiaMigrateNestProgrammer.ts +89 -89
  56. package/src/programmers/NestiaMigrateSchemaProgrammer.ts +475 -475
  57. package/src/programmers/index.ts +15 -15
  58. package/src/structures/INestiaMigrateConfig.ts +19 -19
  59. package/src/structures/INestiaMigrateContext.ts +9 -9
  60. package/src/structures/INestiaMigrateController.ts +8 -8
  61. package/src/structures/INestiaMigrateFile.ts +5 -5
  62. package/src/structures/index.ts +4 -4
  63. package/src/utils/FilePrinter.ts +44 -44
  64. package/src/utils/MapUtil.ts +13 -13
  65. package/src/utils/StringUtil.ts +109 -109
  66. package/README.md +0 -93
@@ -1,358 +1,358 @@
1
- import { SyntaxKind, factory } from "@ttsc/factory";
2
- import { IHttpMigrateRoute, OpenApi } from "@typia/interface";
3
- import * as typiaUtils from "@typia/utils";
4
-
5
- import { IdentifierFactory } from "../factories/IdentifierFactory";
6
- import { StatementFactory } from "../factories/StatementFactory";
7
- import ts from "../internal/ts";
8
- import { INestiaMigrateConfig } from "../structures/INestiaMigrateConfig";
9
- import { FilePrinter } from "../utils/FilePrinter";
10
- import { NestiaMigrateImportProgrammer } from "./NestiaMigrateImportProgrammer";
11
- import { NestiaMigrateSchemaProgrammer } from "./NestiaMigrateSchemaProgrammer";
12
-
13
- const { NamingConvention } =
14
- (typiaUtils as { default?: typeof typiaUtils }).default ?? typiaUtils;
15
-
16
- export namespace NestiaMigrateApiFunctionProgrammer {
17
- export interface IContext {
18
- config: INestiaMigrateConfig;
19
- components: OpenApi.IComponents;
20
- importer: NestiaMigrateImportProgrammer;
21
- route: IHttpMigrateRoute;
22
- }
23
-
24
- export const write = (ctx: IContext): ts.FunctionDeclaration =>
25
- FilePrinter.description(
26
- factory.createFunctionDeclaration(
27
- [
28
- factory.createModifier(SyntaxKind.ExportKeyword),
29
- factory.createModifier(SyntaxKind.AsyncKeyword),
30
- ],
31
- undefined,
32
- ctx.route.accessor.at(-1)!,
33
- undefined,
34
- writeParameterDeclarations(ctx),
35
- factory.createTypeReferenceNode("Promise", [
36
- factory.createTypeReferenceNode(
37
- ctx.route.success === null
38
- ? "void"
39
- : `${ctx.route.accessor.at(-1)!}.Response`,
40
- ),
41
- ]),
42
- factory.createBlock(writeBody(ctx), true),
43
- ),
44
- writeDescription(ctx.config, ctx.route),
45
- );
46
-
47
- export const writeParameterDeclarations = (
48
- ctx: IContext,
49
- connectionName?: string,
50
- ): ts.ParameterDeclaration[] => {
51
- const connection: ts.ParameterDeclaration = IdentifierFactory.parameter(
52
- connectionName ?? "connection",
53
- factory.createTypeReferenceNode(
54
- ctx.importer.external({
55
- type: "instance",
56
- library: "@nestia/fetcher",
57
- name: "IConnection",
58
- }),
59
- ctx.route.headers
60
- ? [
61
- factory.createTypeReferenceNode(
62
- `${ctx.route.accessor.at(-1)!}.Headers`,
63
- ),
64
- ]
65
- : undefined,
66
- ),
67
- );
68
- if (ctx.config.keyword === true) {
69
- const isProps: boolean =
70
- ctx.route.parameters.length > 0 ||
71
- !!ctx.route.query ||
72
- !!ctx.route.body;
73
- if (isProps === false) return [connection];
74
- return [
75
- connection,
76
- factory.createParameterDeclaration(
77
- undefined,
78
- undefined,
79
- "props",
80
- undefined,
81
- factory.createTypeReferenceNode(
82
- `${ctx.route.accessor.at(-1)!}.Props`,
83
- ),
84
- ),
85
- ];
86
- }
87
- return [
88
- connection,
89
- ...ctx.route.parameters.map((p) =>
90
- IdentifierFactory.parameter(
91
- p.key,
92
- NestiaMigrateSchemaProgrammer.write({
93
- components: ctx.components,
94
- importer: ctx.importer,
95
- schema: p.schema,
96
- }),
97
- ),
98
- ),
99
- ...(ctx.route.query
100
- ? [
101
- IdentifierFactory.parameter(
102
- ctx.route.query.key,
103
- factory.createTypeReferenceNode(
104
- `${ctx.route.accessor.at(-1)!}.Query`,
105
- ),
106
- ),
107
- ]
108
- : []),
109
- ...(ctx.route.body
110
- ? [
111
- IdentifierFactory.parameter(
112
- ctx.route.body.key,
113
- factory.createTypeReferenceNode(
114
- `${ctx.route.accessor.at(-1)!}.Body`,
115
- ),
116
- (ctx.route.body.type === "application/json" ||
117
- ctx.route.body.type === "text/plain") &&
118
- ctx.route.operation().requestBody?.required === false
119
- ? factory.createToken(SyntaxKind.QuestionToken)
120
- : undefined,
121
- ),
122
- ]
123
- : []),
124
- ];
125
- };
126
-
127
- const writeDescription = (
128
- config: INestiaMigrateConfig,
129
- route: IHttpMigrateRoute,
130
- ): string => {
131
- const comment: string = route.comment();
132
- return [
133
- config.keyword === true
134
- ? comment.split("@param ").join("@param props.")
135
- : comment,
136
- `@path ${route.emendedPath}`,
137
- `@accessor api.functional.${route.accessor.join(".")}`,
138
- `@${config.author?.tag ?? "nestia"} ${config.author?.value ?? "Generated by Nestia - https://github.com/samchon/nestia"}`,
139
- ].join("\n");
140
- };
141
-
142
- const writeBody = (ctx: IContext): ts.Statement[] => {
143
- const encrypted: boolean = !!ctx.route.success?.["x-nestia-encrypted"];
144
- const contentType: string = ctx.route.body?.type ?? "application/json";
145
-
146
- const property = (key: string): ts.Expression =>
147
- ctx.config.keyword === true
148
- ? IdentifierFactory.access(factory.createIdentifier("props"), key)
149
- : factory.createIdentifier(key);
150
- const fetch = () =>
151
- factory.createAwaitExpression(
152
- factory.createCallExpression(
153
- IdentifierFactory.access(
154
- factory.createIdentifier(
155
- ctx.importer.external({
156
- type: "instance",
157
- library: encrypted
158
- ? "@nestia/fetcher/lib/EncryptedFetcher"
159
- : `@nestia/fetcher`,
160
- name: encrypted ? "EncryptedFetcher" : "PlainFetcher",
161
- }),
162
- ),
163
- "fetch",
164
- ),
165
- undefined,
166
- [
167
- contentType && contentType !== "multipart/form-data"
168
- ? factory.createObjectLiteralExpression(
169
- [
170
- factory.createSpreadAssignment(
171
- factory.createIdentifier("connection"),
172
- ),
173
- factory.createPropertyAssignment(
174
- "headers",
175
- factory.createObjectLiteralExpression(
176
- [
177
- factory.createSpreadAssignment(
178
- IdentifierFactory.access(
179
- factory.createIdentifier("connection"),
180
- "headers",
181
- ),
182
- ),
183
- factory.createPropertyAssignment(
184
- factory.createStringLiteral("Content-Type"),
185
- factory.createStringLiteral(contentType),
186
- ),
187
- ],
188
- true,
189
- ),
190
- ),
191
- ],
192
- true,
193
- )
194
- : factory.createIdentifier("connection"),
195
- factory.createObjectLiteralExpression(
196
- [
197
- factory.createSpreadAssignment(
198
- IdentifierFactory.access(
199
- factory.createIdentifier(ctx.route.accessor.at(-1)!),
200
- "METADATA",
201
- ),
202
- ),
203
- factory.createPropertyAssignment(
204
- "path",
205
- factory.createCallExpression(
206
- IdentifierFactory.access(
207
- factory.createIdentifier(ctx.route.accessor.at(-1)!),
208
- "path",
209
- ),
210
- undefined,
211
- getArguments(ctx, false),
212
- ),
213
- ),
214
- factory.createPropertyAssignment(
215
- "status",
216
- factory.createNull(),
217
- ),
218
- ],
219
- true,
220
- ),
221
- ...(ctx.route.body ? [property(ctx.route.body.key)] : []),
222
- ],
223
- ),
224
- );
225
-
226
- const value: ts.Expression =
227
- ctx.config.simulate !== true
228
- ? fetch()
229
- : factory.createConditionalExpression(
230
- factory.createStrictEquality(
231
- factory.createTrue(),
232
- factory.createIdentifier("connection.simulate"),
233
- ),
234
- undefined,
235
- factory.createCallExpression(
236
- factory.createIdentifier(
237
- `${ctx.route.accessor.at(-1)!}.simulate`,
238
- ),
239
- [],
240
- [
241
- factory.createIdentifier("connection"),
242
- ...getArguments(ctx, true),
243
- ],
244
- ),
245
- undefined,
246
- fetch(),
247
- );
248
- const headers: Array<IAssignHeader | ISetHeader> = getHeaders(
249
- ctx.route.comment(),
250
- );
251
- if (headers.length === 0) return [factory.createReturnStatement(value)];
252
- return [
253
- StatementFactory.constant({
254
- name: "output",
255
- type: factory.createTypeReferenceNode(
256
- `${ctx.route.accessor.at(-1)!}.Response`,
257
- ),
258
- value,
259
- }),
260
- factory.createExpressionStatement(
261
- factory.createBinaryExpression(
262
- factory.createIdentifier("connection.headers"),
263
- factory.createToken(SyntaxKind.QuestionQuestionEqualsToken),
264
- factory.createObjectLiteralExpression([]),
265
- ),
266
- ),
267
- ...headers.map((h) =>
268
- factory.createExpressionStatement(
269
- h.type === "assign"
270
- ? factory.createCallExpression(
271
- factory.createIdentifier("Object.assign"),
272
- undefined,
273
- [
274
- factory.createIdentifier("connection.headers"),
275
- factory.createIdentifier(`output.${h.accessor}`),
276
- ],
277
- )
278
- : factory.createBinaryExpression(
279
- factory.createIdentifier(
280
- `connection.headers${
281
- NamingConvention.variable(h.property)
282
- ? `.${h.property}`
283
- : `[${JSON.stringify(h.property)}]`
284
- }`,
285
- ),
286
- factory.createToken(SyntaxKind.EqualsToken),
287
- factory.createIdentifier(`output.${h.accessor}`),
288
- ),
289
- ),
290
- ),
291
- factory.createReturnStatement(factory.createIdentifier("output")),
292
- ];
293
- };
294
-
295
- const getArguments = (ctx: IContext, body: boolean): ts.Expression[] => {
296
- if (
297
- ctx.route.parameters.length === 0 &&
298
- ctx.route.query === null &&
299
- (body === false || ctx.route.body === null)
300
- )
301
- return [];
302
- else if (ctx.config.keyword === true)
303
- return [factory.createIdentifier("props")];
304
- return [
305
- ...ctx.route.parameters.map((p) => factory.createIdentifier(p.key)),
306
- ...(ctx.route.query
307
- ? [factory.createIdentifier(ctx.route.query.key)]
308
- : []),
309
- ...(body && ctx.route.body
310
- ? [factory.createIdentifier(ctx.route.body.key)]
311
- : []),
312
- ];
313
- };
314
-
315
- const getHeaders = (
316
- description: string,
317
- ): Array<IAssignHeader | ISetHeader> => {
318
- const directives: Array<IAssignHeader | ISetHeader> = [];
319
- for (const line of description.split("\n").map((l) => l.trim())) {
320
- const setHeaderTag: string | undefined = [
321
- "@setHeader ",
322
- "@setHeaders ",
323
- ].find((tag) => line.startsWith(tag));
324
- if (setHeaderTag !== undefined) {
325
- const parts: string[] = line
326
- .substring(setHeaderTag.length)
327
- .trim()
328
- .split(/\s+/);
329
- if (parts.length >= 2)
330
- directives.push({
331
- type: "set",
332
- accessor: parts[0]!,
333
- property: parts[1]!,
334
- });
335
- } else if (line.startsWith("@assignHeaders ")) {
336
- const accessor: string = line
337
- .substring("@assignHeaders ".length)
338
- .trim();
339
- if (accessor.length !== 0)
340
- directives.push({
341
- type: "assign",
342
- accessor,
343
- });
344
- }
345
- }
346
- return directives;
347
- };
348
- }
349
-
350
- interface IAssignHeader {
351
- type: "assign";
352
- accessor: string;
353
- }
354
- interface ISetHeader {
355
- type: "set";
356
- accessor: string;
357
- property: string;
358
- }
1
+ import { SyntaxKind, factory } from "@ttsc/factory";
2
+ import { IHttpMigrateRoute, OpenApi } from "@typia/interface";
3
+ import * as typiaUtils from "@typia/utils";
4
+
5
+ import { IdentifierFactory } from "../factories/IdentifierFactory";
6
+ import { StatementFactory } from "../factories/StatementFactory";
7
+ import ts from "../internal/ts";
8
+ import { INestiaMigrateConfig } from "../structures/INestiaMigrateConfig";
9
+ import { FilePrinter } from "../utils/FilePrinter";
10
+ import { NestiaMigrateImportProgrammer } from "./NestiaMigrateImportProgrammer";
11
+ import { NestiaMigrateSchemaProgrammer } from "./NestiaMigrateSchemaProgrammer";
12
+
13
+ const { NamingConvention } =
14
+ (typiaUtils as { default?: typeof typiaUtils }).default ?? typiaUtils;
15
+
16
+ export namespace NestiaMigrateApiFunctionProgrammer {
17
+ export interface IContext {
18
+ config: INestiaMigrateConfig;
19
+ components: OpenApi.IComponents;
20
+ importer: NestiaMigrateImportProgrammer;
21
+ route: IHttpMigrateRoute;
22
+ }
23
+
24
+ export const write = (ctx: IContext): ts.FunctionDeclaration =>
25
+ FilePrinter.description(
26
+ factory.createFunctionDeclaration(
27
+ [
28
+ factory.createModifier(SyntaxKind.ExportKeyword),
29
+ factory.createModifier(SyntaxKind.AsyncKeyword),
30
+ ],
31
+ undefined,
32
+ ctx.route.accessor.at(-1)!,
33
+ undefined,
34
+ writeParameterDeclarations(ctx),
35
+ factory.createTypeReferenceNode("Promise", [
36
+ factory.createTypeReferenceNode(
37
+ ctx.route.success === null
38
+ ? "void"
39
+ : `${ctx.route.accessor.at(-1)!}.Response`,
40
+ ),
41
+ ]),
42
+ factory.createBlock(writeBody(ctx), true),
43
+ ),
44
+ writeDescription(ctx.config, ctx.route),
45
+ );
46
+
47
+ export const writeParameterDeclarations = (
48
+ ctx: IContext,
49
+ connectionName?: string,
50
+ ): ts.ParameterDeclaration[] => {
51
+ const connection: ts.ParameterDeclaration = IdentifierFactory.parameter(
52
+ connectionName ?? "connection",
53
+ factory.createTypeReferenceNode(
54
+ ctx.importer.external({
55
+ type: "instance",
56
+ library: "@nestia/fetcher",
57
+ name: "IConnection",
58
+ }),
59
+ ctx.route.headers
60
+ ? [
61
+ factory.createTypeReferenceNode(
62
+ `${ctx.route.accessor.at(-1)!}.Headers`,
63
+ ),
64
+ ]
65
+ : undefined,
66
+ ),
67
+ );
68
+ if (ctx.config.keyword === true) {
69
+ const isProps: boolean =
70
+ ctx.route.parameters.length > 0 ||
71
+ !!ctx.route.query ||
72
+ !!ctx.route.body;
73
+ if (isProps === false) return [connection];
74
+ return [
75
+ connection,
76
+ factory.createParameterDeclaration(
77
+ undefined,
78
+ undefined,
79
+ "props",
80
+ undefined,
81
+ factory.createTypeReferenceNode(
82
+ `${ctx.route.accessor.at(-1)!}.Props`,
83
+ ),
84
+ ),
85
+ ];
86
+ }
87
+ return [
88
+ connection,
89
+ ...ctx.route.parameters.map((p) =>
90
+ IdentifierFactory.parameter(
91
+ p.key,
92
+ NestiaMigrateSchemaProgrammer.write({
93
+ components: ctx.components,
94
+ importer: ctx.importer,
95
+ schema: p.schema,
96
+ }),
97
+ ),
98
+ ),
99
+ ...(ctx.route.query
100
+ ? [
101
+ IdentifierFactory.parameter(
102
+ ctx.route.query.key,
103
+ factory.createTypeReferenceNode(
104
+ `${ctx.route.accessor.at(-1)!}.Query`,
105
+ ),
106
+ ),
107
+ ]
108
+ : []),
109
+ ...(ctx.route.body
110
+ ? [
111
+ IdentifierFactory.parameter(
112
+ ctx.route.body.key,
113
+ factory.createTypeReferenceNode(
114
+ `${ctx.route.accessor.at(-1)!}.Body`,
115
+ ),
116
+ (ctx.route.body.type === "application/json" ||
117
+ ctx.route.body.type === "text/plain") &&
118
+ ctx.route.operation().requestBody?.required === false
119
+ ? factory.createToken(SyntaxKind.QuestionToken)
120
+ : undefined,
121
+ ),
122
+ ]
123
+ : []),
124
+ ];
125
+ };
126
+
127
+ const writeDescription = (
128
+ config: INestiaMigrateConfig,
129
+ route: IHttpMigrateRoute,
130
+ ): string => {
131
+ const comment: string = route.comment();
132
+ return [
133
+ config.keyword === true
134
+ ? comment.split("@param ").join("@param props.")
135
+ : comment,
136
+ `@path ${route.emendedPath}`,
137
+ `@accessor api.functional.${route.accessor.join(".")}`,
138
+ `@${config.author?.tag ?? "nestia"} ${config.author?.value ?? "Generated by Nestia - https://github.com/samchon/nestia"}`,
139
+ ].join("\n");
140
+ };
141
+
142
+ const writeBody = (ctx: IContext): ts.Statement[] => {
143
+ const encrypted: boolean = !!ctx.route.success?.["x-nestia-encrypted"];
144
+ const contentType: string = ctx.route.body?.type ?? "application/json";
145
+
146
+ const property = (key: string): ts.Expression =>
147
+ ctx.config.keyword === true
148
+ ? IdentifierFactory.access(factory.createIdentifier("props"), key)
149
+ : factory.createIdentifier(key);
150
+ const fetch = () =>
151
+ factory.createAwaitExpression(
152
+ factory.createCallExpression(
153
+ IdentifierFactory.access(
154
+ factory.createIdentifier(
155
+ ctx.importer.external({
156
+ type: "instance",
157
+ library: encrypted
158
+ ? "@nestia/fetcher/lib/EncryptedFetcher"
159
+ : `@nestia/fetcher`,
160
+ name: encrypted ? "EncryptedFetcher" : "PlainFetcher",
161
+ }),
162
+ ),
163
+ "fetch",
164
+ ),
165
+ undefined,
166
+ [
167
+ contentType && contentType !== "multipart/form-data"
168
+ ? factory.createObjectLiteralExpression(
169
+ [
170
+ factory.createSpreadAssignment(
171
+ factory.createIdentifier("connection"),
172
+ ),
173
+ factory.createPropertyAssignment(
174
+ "headers",
175
+ factory.createObjectLiteralExpression(
176
+ [
177
+ factory.createSpreadAssignment(
178
+ IdentifierFactory.access(
179
+ factory.createIdentifier("connection"),
180
+ "headers",
181
+ ),
182
+ ),
183
+ factory.createPropertyAssignment(
184
+ factory.createStringLiteral("Content-Type"),
185
+ factory.createStringLiteral(contentType),
186
+ ),
187
+ ],
188
+ true,
189
+ ),
190
+ ),
191
+ ],
192
+ true,
193
+ )
194
+ : factory.createIdentifier("connection"),
195
+ factory.createObjectLiteralExpression(
196
+ [
197
+ factory.createSpreadAssignment(
198
+ IdentifierFactory.access(
199
+ factory.createIdentifier(ctx.route.accessor.at(-1)!),
200
+ "METADATA",
201
+ ),
202
+ ),
203
+ factory.createPropertyAssignment(
204
+ "path",
205
+ factory.createCallExpression(
206
+ IdentifierFactory.access(
207
+ factory.createIdentifier(ctx.route.accessor.at(-1)!),
208
+ "path",
209
+ ),
210
+ undefined,
211
+ getArguments(ctx, false),
212
+ ),
213
+ ),
214
+ factory.createPropertyAssignment(
215
+ "status",
216
+ factory.createNull(),
217
+ ),
218
+ ],
219
+ true,
220
+ ),
221
+ ...(ctx.route.body ? [property(ctx.route.body.key)] : []),
222
+ ],
223
+ ),
224
+ );
225
+
226
+ const value: ts.Expression =
227
+ ctx.config.simulate !== true
228
+ ? fetch()
229
+ : factory.createConditionalExpression(
230
+ factory.createStrictEquality(
231
+ factory.createTrue(),
232
+ factory.createIdentifier("connection.simulate"),
233
+ ),
234
+ undefined,
235
+ factory.createCallExpression(
236
+ factory.createIdentifier(
237
+ `${ctx.route.accessor.at(-1)!}.simulate`,
238
+ ),
239
+ [],
240
+ [
241
+ factory.createIdentifier("connection"),
242
+ ...getArguments(ctx, true),
243
+ ],
244
+ ),
245
+ undefined,
246
+ fetch(),
247
+ );
248
+ const headers: Array<IAssignHeader | ISetHeader> = getHeaders(
249
+ ctx.route.comment(),
250
+ );
251
+ if (headers.length === 0) return [factory.createReturnStatement(value)];
252
+ return [
253
+ StatementFactory.constant({
254
+ name: "output",
255
+ type: factory.createTypeReferenceNode(
256
+ `${ctx.route.accessor.at(-1)!}.Response`,
257
+ ),
258
+ value,
259
+ }),
260
+ factory.createExpressionStatement(
261
+ factory.createBinaryExpression(
262
+ factory.createIdentifier("connection.headers"),
263
+ factory.createToken(SyntaxKind.QuestionQuestionEqualsToken),
264
+ factory.createObjectLiteralExpression([]),
265
+ ),
266
+ ),
267
+ ...headers.map((h) =>
268
+ factory.createExpressionStatement(
269
+ h.type === "assign"
270
+ ? factory.createCallExpression(
271
+ factory.createIdentifier("Object.assign"),
272
+ undefined,
273
+ [
274
+ factory.createIdentifier("connection.headers"),
275
+ factory.createIdentifier(`output.${h.accessor}`),
276
+ ],
277
+ )
278
+ : factory.createBinaryExpression(
279
+ factory.createIdentifier(
280
+ `connection.headers${
281
+ NamingConvention.variable(h.property)
282
+ ? `.${h.property}`
283
+ : `[${JSON.stringify(h.property)}]`
284
+ }`,
285
+ ),
286
+ factory.createToken(SyntaxKind.EqualsToken),
287
+ factory.createIdentifier(`output.${h.accessor}`),
288
+ ),
289
+ ),
290
+ ),
291
+ factory.createReturnStatement(factory.createIdentifier("output")),
292
+ ];
293
+ };
294
+
295
+ const getArguments = (ctx: IContext, body: boolean): ts.Expression[] => {
296
+ if (
297
+ ctx.route.parameters.length === 0 &&
298
+ ctx.route.query === null &&
299
+ (body === false || ctx.route.body === null)
300
+ )
301
+ return [];
302
+ else if (ctx.config.keyword === true)
303
+ return [factory.createIdentifier("props")];
304
+ return [
305
+ ...ctx.route.parameters.map((p) => factory.createIdentifier(p.key)),
306
+ ...(ctx.route.query
307
+ ? [factory.createIdentifier(ctx.route.query.key)]
308
+ : []),
309
+ ...(body && ctx.route.body
310
+ ? [factory.createIdentifier(ctx.route.body.key)]
311
+ : []),
312
+ ];
313
+ };
314
+
315
+ const getHeaders = (
316
+ description: string,
317
+ ): Array<IAssignHeader | ISetHeader> => {
318
+ const directives: Array<IAssignHeader | ISetHeader> = [];
319
+ for (const line of description.split("\n").map((l) => l.trim())) {
320
+ const setHeaderTag: string | undefined = [
321
+ "@setHeader ",
322
+ "@setHeaders ",
323
+ ].find((tag) => line.startsWith(tag));
324
+ if (setHeaderTag !== undefined) {
325
+ const parts: string[] = line
326
+ .substring(setHeaderTag.length)
327
+ .trim()
328
+ .split(/\s+/);
329
+ if (parts.length >= 2)
330
+ directives.push({
331
+ type: "set",
332
+ accessor: parts[0]!,
333
+ property: parts[1]!,
334
+ });
335
+ } else if (line.startsWith("@assignHeaders ")) {
336
+ const accessor: string = line
337
+ .substring("@assignHeaders ".length)
338
+ .trim();
339
+ if (accessor.length !== 0)
340
+ directives.push({
341
+ type: "assign",
342
+ accessor,
343
+ });
344
+ }
345
+ }
346
+ return directives;
347
+ };
348
+ }
349
+
350
+ interface IAssignHeader {
351
+ type: "assign";
352
+ accessor: string;
353
+ }
354
+ interface ISetHeader {
355
+ type: "set";
356
+ accessor: string;
357
+ property: string;
358
+ }