@nestia/sdk 3.0.0-dev.20231209 → 3.0.0-dev.20240412
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -9
- package/assets/config/nestia.config.ts +82 -79
- package/lib/INestiaConfig.d.ts +28 -6
- package/lib/NestiaSdkApplication.js +12 -10
- package/lib/NestiaSdkApplication.js.map +1 -1
- package/lib/analyses/ConfigAnalyzer.js +1 -1
- package/lib/analyses/ConfigAnalyzer.js.map +1 -1
- package/lib/analyses/ControllerAnalyzer.js +30 -15
- package/lib/analyses/ControllerAnalyzer.js.map +1 -1
- package/lib/analyses/ExceptionAnalyzer.js +35 -6
- package/lib/analyses/ExceptionAnalyzer.js.map +1 -1
- package/lib/analyses/ImportAnalyzer.d.ts +1 -2
- package/lib/analyses/ImportAnalyzer.js +2 -2
- package/lib/analyses/ImportAnalyzer.js.map +1 -1
- package/lib/analyses/PathAnalyzer.d.ts +2 -4
- package/lib/analyses/PathAnalyzer.js +27 -11
- package/lib/analyses/PathAnalyzer.js.map +1 -1
- package/lib/analyses/ReflectAnalyzer.js +34 -22
- package/lib/analyses/ReflectAnalyzer.js.map +1 -1
- package/lib/analyses/SecurityAnalyzer.js +13 -8
- package/lib/analyses/SecurityAnalyzer.js.map +1 -1
- package/lib/executable/internal/NestiaConfigLoader.js +300 -220
- package/lib/executable/internal/NestiaConfigLoader.js.map +1 -1
- package/lib/executable/sdk.js +11 -11
- package/lib/generates/CloneGenerator.d.ts +6 -0
- package/lib/generates/CloneGenerator.js +62 -0
- package/lib/generates/CloneGenerator.js.map +1 -0
- package/lib/generates/E2eGenerator.d.ts +2 -1
- package/lib/generates/E2eGenerator.js +2 -2
- package/lib/generates/E2eGenerator.js.map +1 -1
- package/lib/generates/SdkGenerator.js +3 -11
- package/lib/generates/SdkGenerator.js.map +1 -1
- package/lib/generates/SwaggerGenerator.d.ts +2 -0
- package/lib/generates/SwaggerGenerator.js +119 -62
- package/lib/generates/SwaggerGenerator.js.map +1 -1
- package/lib/generates/internal/E2eFileProgrammer.d.ts +2 -1
- package/lib/generates/internal/E2eFileProgrammer.js +49 -53
- package/lib/generates/internal/E2eFileProgrammer.js.map +1 -1
- package/lib/generates/internal/FilePrinter.d.ts +10 -0
- package/lib/generates/internal/FilePrinter.js +46 -0
- package/lib/generates/internal/FilePrinter.js.map +1 -0
- package/lib/{utils → generates/internal}/ImportDictionary.d.ts +2 -1
- package/lib/{utils → generates/internal}/ImportDictionary.js +20 -14
- package/lib/generates/internal/ImportDictionary.js.map +1 -0
- package/lib/generates/internal/SdkAliasCollection.d.ts +12 -0
- package/lib/generates/internal/SdkAliasCollection.js +97 -0
- package/lib/generates/internal/SdkAliasCollection.js.map +1 -0
- package/lib/generates/internal/SdkCloneProgrammer.d.ts +12 -0
- package/lib/generates/internal/SdkCloneProgrammer.js +99 -0
- package/lib/generates/internal/SdkCloneProgrammer.js.map +1 -0
- package/lib/generates/internal/SdkFileProgrammer.d.ts +2 -1
- package/lib/generates/internal/SdkFileProgrammer.js +27 -28
- package/lib/generates/internal/SdkFileProgrammer.js.map +1 -1
- package/lib/generates/internal/SdkFunctionProgrammer.d.ts +7 -2
- package/lib/generates/internal/SdkFunctionProgrammer.js +115 -322
- package/lib/generates/internal/SdkFunctionProgrammer.js.map +1 -1
- package/lib/generates/internal/SdkImportWizard.d.ts +1 -1
- package/lib/generates/internal/SdkNamespaceProgrammer.d.ts +11 -0
- package/lib/generates/internal/SdkNamespaceProgrammer.js +180 -0
- package/lib/generates/internal/SdkNamespaceProgrammer.js.map +1 -0
- package/lib/generates/internal/SdkRouteProgrammer.d.ts +7 -0
- package/lib/generates/internal/SdkRouteProgrammer.js +55 -0
- package/lib/generates/internal/SdkRouteProgrammer.js.map +1 -0
- package/lib/generates/internal/SdkSimulationProgrammer.d.ts +8 -2
- package/lib/generates/internal/SdkSimulationProgrammer.js +103 -89
- package/lib/generates/internal/SdkSimulationProgrammer.js.map +1 -1
- package/lib/generates/internal/SdkTypeProgrammer.d.ts +9 -0
- package/lib/generates/internal/SdkTypeProgrammer.js +228 -0
- package/lib/generates/internal/SdkTypeProgrammer.js.map +1 -0
- package/lib/generates/internal/SwaggerSchemaGenerator.d.ts +4 -4
- package/lib/generates/internal/SwaggerSchemaGenerator.js +30 -28
- package/lib/generates/internal/SwaggerSchemaGenerator.js.map +1 -1
- package/lib/structures/IController.d.ts +4 -2
- package/lib/structures/IRoute.d.ts +5 -4
- package/lib/structures/ISwaggerLazyProperty.d.ts +2 -2
- package/lib/structures/ISwaggerLazySchema.d.ts +2 -2
- package/lib/structures/ParamCategory.d.ts +1 -1
- package/lib/structures/TypeEntry.js +2 -2
- package/lib/structures/TypeEntry.js.map +1 -1
- package/lib/utils/StringUtil.d.ts +3 -0
- package/lib/utils/StringUtil.js +8 -0
- package/lib/utils/StringUtil.js.map +1 -0
- package/package.json +12 -16
- package/src/INestiaConfig.ts +30 -6
- package/src/NestiaSdkApplication.ts +255 -253
- package/src/analyses/AccessorAnalyzer.ts +60 -60
- package/src/analyses/ConfigAnalyzer.ts +147 -147
- package/src/analyses/ControllerAnalyzer.ts +42 -19
- package/src/analyses/ExceptionAnalyzer.ts +148 -115
- package/src/analyses/GenericAnalyzer.ts +51 -51
- package/src/analyses/ImportAnalyzer.ts +1 -2
- package/src/analyses/PathAnalyzer.ts +110 -98
- package/src/analyses/ReflectAnalyzer.ts +39 -35
- package/src/analyses/SecurityAnalyzer.ts +24 -20
- package/src/executable/internal/CommandParser.ts +15 -15
- package/src/executable/internal/NestiaConfigLoader.ts +67 -67
- package/src/executable/internal/NestiaSdkCommand.ts +60 -60
- package/src/executable/sdk.ts +73 -73
- package/src/generates/CloneGenerator.ts +62 -0
- package/src/generates/E2eGenerator.ts +66 -64
- package/src/generates/SdkGenerator.ts +84 -96
- package/src/generates/SwaggerGenerator.ts +145 -53
- package/src/generates/internal/E2eFileProgrammer.ts +182 -123
- package/src/generates/internal/FilePrinter.ts +53 -0
- package/src/{utils → generates/internal}/ImportDictionary.ts +35 -13
- package/src/generates/internal/SdkAliasCollection.ts +152 -0
- package/src/generates/internal/SdkCloneProgrammer.ts +155 -0
- package/src/generates/internal/SdkDistributionComposer.ts +91 -91
- package/src/generates/internal/SdkFileProgrammer.ts +115 -106
- package/src/generates/internal/SdkFunctionProgrammer.ts +298 -518
- package/src/generates/internal/SdkImportWizard.ts +55 -55
- package/src/generates/internal/SdkNamespaceProgrammer.ts +510 -0
- package/src/generates/internal/SdkRouteDirectory.ts +17 -17
- package/src/generates/internal/SdkRouteProgrammer.ts +83 -0
- package/src/generates/internal/SdkSimulationProgrammer.ts +365 -133
- package/src/generates/internal/SdkTypeProgrammer.ts +386 -0
- package/src/generates/internal/SwaggerSchemaGenerator.ts +437 -427
- package/src/generates/internal/SwaggerSchemaValidator.ts +198 -198
- package/src/index.ts +4 -4
- package/src/module.ts +2 -2
- package/src/structures/IController.ts +94 -95
- package/src/structures/IErrorReport.ts +6 -6
- package/src/structures/INestiaProject.ts +13 -13
- package/src/structures/INormalizedInput.ts +20 -20
- package/src/structures/IRoute.ts +53 -53
- package/src/structures/ISwaggerLazyProperty.ts +2 -2
- package/src/structures/ISwaggerLazySchema.ts +2 -2
- package/src/structures/ITypeTuple.ts +6 -6
- package/src/structures/MethodType.ts +5 -5
- package/src/structures/ParamCategory.ts +1 -1
- package/src/structures/TypeEntry.ts +1 -1
- package/src/utils/ArrayUtil.ts +26 -26
- package/src/utils/FileRetriever.ts +22 -22
- package/src/utils/MapUtil.ts +14 -14
- package/src/utils/PathUtil.ts +10 -10
- package/src/utils/SourceFinder.ts +66 -66
- package/src/utils/StringUtil.ts +6 -0
- package/src/utils/StripEnums.ts +5 -5
- package/assets/bundle/api/utils/NestiaSimulator.ts +0 -70
- package/lib/generates/internal/SdkDtoGenerator.d.ts +0 -9
- package/lib/generates/internal/SdkDtoGenerator.js +0 -294
- package/lib/generates/internal/SdkDtoGenerator.js.map +0 -1
- package/lib/generates/internal/SdkTypeDefiner.d.ts +0 -11
- package/lib/generates/internal/SdkTypeDefiner.js +0 -82
- package/lib/generates/internal/SdkTypeDefiner.js.map +0 -1
- package/lib/structures/ISwagger.d.ts +0 -72
- package/lib/structures/ISwagger.js +0 -3
- package/lib/structures/ISwagger.js.map +0 -1
- package/lib/structures/ISwaggerComponents.d.ts +0 -26
- package/lib/structures/ISwaggerComponents.js +0 -3
- package/lib/structures/ISwaggerComponents.js.map +0 -1
- package/lib/structures/ISwaggerInfo.d.ts +0 -71
- package/lib/structures/ISwaggerInfo.js +0 -3
- package/lib/structures/ISwaggerInfo.js.map +0 -1
- package/lib/structures/ISwaggerRoute.d.ts +0 -47
- package/lib/structures/ISwaggerRoute.js +0 -3
- package/lib/structures/ISwaggerRoute.js.map +0 -1
- package/lib/structures/ISwaggerSecurityScheme.d.ts +0 -56
- package/lib/structures/ISwaggerSecurityScheme.js +0 -3
- package/lib/structures/ISwaggerSecurityScheme.js.map +0 -1
- package/lib/utils/ImportDictionary.js.map +0 -1
- package/src/generates/internal/SdkDtoGenerator.ts +0 -424
- package/src/generates/internal/SdkTypeDefiner.ts +0 -119
- package/src/structures/ISwagger.ts +0 -91
- package/src/structures/ISwaggerComponents.ts +0 -29
- package/src/structures/ISwaggerInfo.ts +0 -80
- package/src/structures/ISwaggerRoute.ts +0 -51
- package/src/structures/ISwaggerSecurityScheme.ts +0 -65
|
@@ -1,518 +1,298 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
import { INestiaConfig } from "../../INestiaConfig";
|
|
6
|
-
import { IController } from "../../structures/IController";
|
|
7
|
-
import { IRoute } from "../../structures/IRoute";
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import { SdkImportWizard } from "./SdkImportWizard";
|
|
11
|
-
import {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
export
|
|
15
|
-
|
|
16
|
-
(
|
|
17
|
-
(
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
),
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
)
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
);
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
(importer: ImportDictionary) =>
|
|
300
|
-
(route: IRoute) =>
|
|
301
|
-
(props: {
|
|
302
|
-
query: IRoute.IParameter | undefined;
|
|
303
|
-
headers: IRoute.IParameter | undefined;
|
|
304
|
-
input: IRoute.IParameter | undefined;
|
|
305
|
-
}): string => {
|
|
306
|
-
// LIST UP TYPES
|
|
307
|
-
const types: Pair<string, string>[] = [];
|
|
308
|
-
if (props.headers !== undefined)
|
|
309
|
-
types.push(
|
|
310
|
-
new Pair(
|
|
311
|
-
"Headers",
|
|
312
|
-
SdkTypeDefiner.headers(config)(importer)(props.headers),
|
|
313
|
-
),
|
|
314
|
-
);
|
|
315
|
-
if (props.query !== undefined)
|
|
316
|
-
types.push(
|
|
317
|
-
new Pair(
|
|
318
|
-
"Query",
|
|
319
|
-
SdkTypeDefiner.query(config)(importer)(props.query),
|
|
320
|
-
),
|
|
321
|
-
);
|
|
322
|
-
if (props.input !== undefined)
|
|
323
|
-
types.push(
|
|
324
|
-
new Pair(
|
|
325
|
-
"Input",
|
|
326
|
-
SdkTypeDefiner.input(config)(importer)(props.input),
|
|
327
|
-
),
|
|
328
|
-
);
|
|
329
|
-
if (config.propagate === true || route.output.typeName !== "void")
|
|
330
|
-
types.push(
|
|
331
|
-
new Pair("Output", SdkTypeDefiner.output(config)(importer)(route)),
|
|
332
|
-
);
|
|
333
|
-
|
|
334
|
-
// PATH WITH PARAMETERS
|
|
335
|
-
const parameters: IRoute.IParameter[] = filter_path_parameters(route)(
|
|
336
|
-
props.query,
|
|
337
|
-
);
|
|
338
|
-
const path: string = compute_path({
|
|
339
|
-
path: route.path,
|
|
340
|
-
query: props.query,
|
|
341
|
-
parameters,
|
|
342
|
-
});
|
|
343
|
-
return (
|
|
344
|
-
`export namespace ${route.name} {\n` +
|
|
345
|
-
(types.length !== 0
|
|
346
|
-
? types
|
|
347
|
-
.map(
|
|
348
|
-
(tuple) => ` export type ${tuple.first} = ${tuple.second};`,
|
|
349
|
-
)
|
|
350
|
-
.join("\n") + "\n"
|
|
351
|
-
: "") +
|
|
352
|
-
"\n" +
|
|
353
|
-
[
|
|
354
|
-
"export const METADATA = {",
|
|
355
|
-
` method: "${route.method}",`,
|
|
356
|
-
` path: "${route.path}",`,
|
|
357
|
-
...(props.input
|
|
358
|
-
? [
|
|
359
|
-
`request: {`,
|
|
360
|
-
` type: "${
|
|
361
|
-
(props.input as IController.IBodyParameter).encrypted
|
|
362
|
-
? "application/octet-stream"
|
|
363
|
-
: (props.input as IController.IBodyParameter).contentType ??
|
|
364
|
-
"application/json"
|
|
365
|
-
}",`,
|
|
366
|
-
` encrypted: ${
|
|
367
|
-
props.input.custom &&
|
|
368
|
-
props.input.category === "body" &&
|
|
369
|
-
props.input.encrypted
|
|
370
|
-
}`,
|
|
371
|
-
`},`,
|
|
372
|
-
].map((str) => ` ${str}`)
|
|
373
|
-
: [" request: null,"]),
|
|
374
|
-
...(route.method !== "HEAD"
|
|
375
|
-
? [
|
|
376
|
-
`response: {`,
|
|
377
|
-
` type: "${route.output.contentType}",`,
|
|
378
|
-
` encrypted: ${route.encrypted},`,
|
|
379
|
-
`},`,
|
|
380
|
-
].map((str) => ` ${str}`)
|
|
381
|
-
: [" response: null,"]),
|
|
382
|
-
...(route.status
|
|
383
|
-
? [` status: ${route.status},`]
|
|
384
|
-
: [" status: null,"]),
|
|
385
|
-
...(route.output.contentType === "application/x-www-form-urlencoded"
|
|
386
|
-
? [
|
|
387
|
-
` parseQuery: (input: URLSearchParams) => ${SdkImportWizard.typia(
|
|
388
|
-
importer,
|
|
389
|
-
)}.http.assertQuery<${route.output.typeName}>(input),`,
|
|
390
|
-
]
|
|
391
|
-
: []),
|
|
392
|
-
"} as const;",
|
|
393
|
-
]
|
|
394
|
-
.map((line) => ` ${line}`)
|
|
395
|
-
.join("\n") +
|
|
396
|
-
"\n\n" +
|
|
397
|
-
` export const path = (${parameters
|
|
398
|
-
.map(
|
|
399
|
-
(param) =>
|
|
400
|
-
`${param.name}: ${
|
|
401
|
-
param.category === "query" &&
|
|
402
|
-
param.typeName === props.query?.typeName
|
|
403
|
-
? `${route.name}.Query`
|
|
404
|
-
: getTypeName(config)(importer)(param)
|
|
405
|
-
}`,
|
|
406
|
-
)
|
|
407
|
-
.join(", ")}): string => {\n` +
|
|
408
|
-
`${path};\n` +
|
|
409
|
-
` }\n` +
|
|
410
|
-
(config.simulate === true && route.output.typeName !== "void"
|
|
411
|
-
? ` export const random = (g?: Partial<${SdkImportWizard.typia(
|
|
412
|
-
importer,
|
|
413
|
-
)}.IRandomGenerator>): ${SdkTypeDefiner.responseBody(config)(
|
|
414
|
-
importer,
|
|
415
|
-
)(route)} =>\n` +
|
|
416
|
-
` ${SdkImportWizard.typia(
|
|
417
|
-
importer,
|
|
418
|
-
)}.random<${SdkTypeDefiner.responseBody(config)(importer)(
|
|
419
|
-
route,
|
|
420
|
-
)}>(g);\n`
|
|
421
|
-
: "") +
|
|
422
|
-
(config.simulate === true
|
|
423
|
-
? SdkSimulationProgrammer.generate(config)(importer)(route) + "\n"
|
|
424
|
-
: "") +
|
|
425
|
-
(config.json === true &&
|
|
426
|
-
route.parameters.find((param) => param.category === "body") !==
|
|
427
|
-
undefined
|
|
428
|
-
? ` export const stringify = (input: Input) => ${SdkImportWizard.typia(
|
|
429
|
-
importer,
|
|
430
|
-
)}.json.assertStringify(input);\n`
|
|
431
|
-
: "") +
|
|
432
|
-
"}"
|
|
433
|
-
);
|
|
434
|
-
};
|
|
435
|
-
|
|
436
|
-
const compute_path = (props: {
|
|
437
|
-
query: IRoute.IParameter | undefined;
|
|
438
|
-
parameters: IRoute.IParameter[];
|
|
439
|
-
path: string;
|
|
440
|
-
}): string => {
|
|
441
|
-
for (const param of props.parameters)
|
|
442
|
-
if (param.category === "param")
|
|
443
|
-
props.path = props.path.replace(
|
|
444
|
-
`:${param.field}`,
|
|
445
|
-
`\${encodeURIComponent(${param.name} ?? "null")}`,
|
|
446
|
-
);
|
|
447
|
-
|
|
448
|
-
// NO QUERY PARAMETER
|
|
449
|
-
const queryParams: IRoute.IParameter[] = props.parameters.filter(
|
|
450
|
-
(param) => param.category === "query" && param.field !== undefined,
|
|
451
|
-
);
|
|
452
|
-
if (props.query === undefined && queryParams.length === 0)
|
|
453
|
-
return `${space(8)}return \`${props.path}\``;
|
|
454
|
-
|
|
455
|
-
const computeName = (str: string): string =>
|
|
456
|
-
props.parameters
|
|
457
|
-
.filter((p) => p.category !== "headers")
|
|
458
|
-
.find((p) => p.name === str) !== undefined
|
|
459
|
-
? computeName("_" + str)
|
|
460
|
-
: str;
|
|
461
|
-
const variables: string = computeName("variables");
|
|
462
|
-
const search: string = computeName("search");
|
|
463
|
-
const encoded: string = computeName("encoded");
|
|
464
|
-
|
|
465
|
-
const wrapper = (expr: string) =>
|
|
466
|
-
[
|
|
467
|
-
`const ${variables}: Record<any, any> = ${expr};`,
|
|
468
|
-
`const ${search}: URLSearchParams = new URLSearchParams();`,
|
|
469
|
-
`for (const [key, value] of Object.entries(${variables}))`,
|
|
470
|
-
` if (value === undefined) continue;`,
|
|
471
|
-
` else if (Array.isArray(value))`,
|
|
472
|
-
` value.forEach((elem) => ${search}.append(key, String(elem)));`,
|
|
473
|
-
` else`,
|
|
474
|
-
` ${search}.set(key, String(value));`,
|
|
475
|
-
`const ${encoded}: string = ${search}.toString();`,
|
|
476
|
-
`return \`${props.path}\${${encoded}.length ? \`?\${${encoded}}\` : ""}\`;`,
|
|
477
|
-
]
|
|
478
|
-
.map((str) => `${space(8)}${str}`)
|
|
479
|
-
.join("\n");
|
|
480
|
-
|
|
481
|
-
if (props.query !== undefined && queryParams.length === 0)
|
|
482
|
-
return wrapper(`${props.query.name} as any`);
|
|
483
|
-
else if (props.query === undefined)
|
|
484
|
-
return wrapper(`
|
|
485
|
-
{
|
|
486
|
-
${rest_query_parameters(queryParams)}
|
|
487
|
-
} as any`);
|
|
488
|
-
|
|
489
|
-
return wrapper(`
|
|
490
|
-
{
|
|
491
|
-
...${props.query.name},
|
|
492
|
-
${rest_query_parameters(queryParams)},
|
|
493
|
-
} as any`);
|
|
494
|
-
};
|
|
495
|
-
|
|
496
|
-
const rest_query_parameters = (parameters: IRoute.IParameter[]): string =>
|
|
497
|
-
parameters
|
|
498
|
-
.filter((param) => param.category !== "headers")
|
|
499
|
-
.map((param) =>
|
|
500
|
-
param.name === param.field
|
|
501
|
-
? param.name
|
|
502
|
-
: `${
|
|
503
|
-
Escaper.variable(param.field!)
|
|
504
|
-
? param.field
|
|
505
|
-
: JSON.stringify(param.field)
|
|
506
|
-
}: ${param.name}`,
|
|
507
|
-
)
|
|
508
|
-
.join(`,\n${space(12)}`);
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
const space = (count: number) => " ".repeat(count);
|
|
512
|
-
const getTypeName =
|
|
513
|
-
(config: INestiaConfig) =>
|
|
514
|
-
(importer: ImportDictionary) =>
|
|
515
|
-
(p: IRoute.IParameter | IRoute.IOutput) =>
|
|
516
|
-
p.metadata
|
|
517
|
-
? SdkDtoGenerator.decode(config)(importer)(p.metadata)
|
|
518
|
-
: p.typeName;
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import typia from "typia";
|
|
3
|
+
import { IdentifierFactory } from "typia/lib/factories/IdentifierFactory";
|
|
4
|
+
|
|
5
|
+
import { INestiaConfig } from "../../INestiaConfig";
|
|
6
|
+
import { IController } from "../../structures/IController";
|
|
7
|
+
import { IRoute } from "../../structures/IRoute";
|
|
8
|
+
import { StringUtil } from "../../utils/StringUtil";
|
|
9
|
+
import { ImportDictionary } from "./ImportDictionary";
|
|
10
|
+
import { SdkImportWizard } from "./SdkImportWizard";
|
|
11
|
+
import { SdkTypeProgrammer } from "./SdkTypeProgrammer";
|
|
12
|
+
|
|
13
|
+
export namespace SdkFunctionProgrammer {
|
|
14
|
+
export const write =
|
|
15
|
+
(config: INestiaConfig) =>
|
|
16
|
+
(importer: ImportDictionary) =>
|
|
17
|
+
(
|
|
18
|
+
route: IRoute,
|
|
19
|
+
props: {
|
|
20
|
+
headers: IRoute.IParameter | undefined;
|
|
21
|
+
query: IRoute.IParameter | undefined;
|
|
22
|
+
input: IRoute.IParameter | undefined;
|
|
23
|
+
},
|
|
24
|
+
): ts.FunctionDeclaration => {
|
|
25
|
+
return ts.factory.createFunctionDeclaration(
|
|
26
|
+
[
|
|
27
|
+
ts.factory.createModifier(ts.SyntaxKind.ExportKeyword),
|
|
28
|
+
ts.factory.createModifier(ts.SyntaxKind.AsyncKeyword),
|
|
29
|
+
],
|
|
30
|
+
undefined,
|
|
31
|
+
route.name,
|
|
32
|
+
undefined,
|
|
33
|
+
[
|
|
34
|
+
IdentifierFactory.parameter(
|
|
35
|
+
"connection",
|
|
36
|
+
ts.factory.createTypeReferenceNode(
|
|
37
|
+
SdkImportWizard.IConnection(importer),
|
|
38
|
+
props.headers
|
|
39
|
+
? [ts.factory.createTypeReferenceNode(`${route.name}.Headers`)]
|
|
40
|
+
: undefined,
|
|
41
|
+
),
|
|
42
|
+
),
|
|
43
|
+
...route.parameters
|
|
44
|
+
.filter((p) => p.category !== "headers")
|
|
45
|
+
.map((p) =>
|
|
46
|
+
ts.factory.createParameterDeclaration(
|
|
47
|
+
[],
|
|
48
|
+
undefined,
|
|
49
|
+
p.name,
|
|
50
|
+
p.optional
|
|
51
|
+
? ts.factory.createToken(ts.SyntaxKind.QuestionToken)
|
|
52
|
+
: undefined,
|
|
53
|
+
config.primitive !== false &&
|
|
54
|
+
(p === props.query || p === props.input)
|
|
55
|
+
? ts.factory.createTypeReferenceNode(
|
|
56
|
+
`${route.name}.${p === props.query ? "Query" : "Input"}`,
|
|
57
|
+
)
|
|
58
|
+
: getTypeName(config)(importer)(p),
|
|
59
|
+
),
|
|
60
|
+
),
|
|
61
|
+
],
|
|
62
|
+
ts.factory.createTypeReferenceNode("Promise", [
|
|
63
|
+
getReturnType(config)(route),
|
|
64
|
+
]),
|
|
65
|
+
ts.factory.createBlock(
|
|
66
|
+
write_body(config)(importer)(route, props),
|
|
67
|
+
true,
|
|
68
|
+
),
|
|
69
|
+
);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const write_body =
|
|
73
|
+
(config: INestiaConfig) =>
|
|
74
|
+
(importer: ImportDictionary) =>
|
|
75
|
+
(
|
|
76
|
+
route: IRoute,
|
|
77
|
+
props: {
|
|
78
|
+
headers: IRoute.IParameter | undefined;
|
|
79
|
+
query: IRoute.IParameter | undefined;
|
|
80
|
+
input: IRoute.IParameter | undefined;
|
|
81
|
+
},
|
|
82
|
+
): ts.Statement[] => {
|
|
83
|
+
const encrypted: boolean =
|
|
84
|
+
route.encrypted === true ||
|
|
85
|
+
(props.input !== undefined &&
|
|
86
|
+
props.input.custom === true &&
|
|
87
|
+
props.input.category === "body" &&
|
|
88
|
+
props.input.encrypted === true);
|
|
89
|
+
const contentType: string | undefined =
|
|
90
|
+
props.input !== undefined
|
|
91
|
+
? typia.is<IController.IBodyParameter>(props.input)
|
|
92
|
+
? props.input.contentType
|
|
93
|
+
: "application/json"
|
|
94
|
+
: undefined;
|
|
95
|
+
|
|
96
|
+
const caller = () =>
|
|
97
|
+
ts.factory.createCallExpression(
|
|
98
|
+
IdentifierFactory.access(
|
|
99
|
+
ts.factory.createIdentifier(
|
|
100
|
+
SdkImportWizard.Fetcher(encrypted)(importer),
|
|
101
|
+
),
|
|
102
|
+
)(config.propagate ? "propagate" : "fetch"),
|
|
103
|
+
undefined,
|
|
104
|
+
[
|
|
105
|
+
contentType && contentType !== "multipart/form-data"
|
|
106
|
+
? ts.factory.createObjectLiteralExpression(
|
|
107
|
+
[
|
|
108
|
+
ts.factory.createSpreadAssignment(
|
|
109
|
+
ts.factory.createIdentifier("connection"),
|
|
110
|
+
),
|
|
111
|
+
ts.factory.createPropertyAssignment(
|
|
112
|
+
"headers",
|
|
113
|
+
ts.factory.createObjectLiteralExpression(
|
|
114
|
+
[
|
|
115
|
+
ts.factory.createSpreadAssignment(
|
|
116
|
+
IdentifierFactory.access(
|
|
117
|
+
ts.factory.createIdentifier("connection"),
|
|
118
|
+
)("headers"),
|
|
119
|
+
),
|
|
120
|
+
ts.factory.createPropertyAssignment(
|
|
121
|
+
ts.factory.createStringLiteral("Content-Type"),
|
|
122
|
+
ts.factory.createStringLiteral(contentType),
|
|
123
|
+
),
|
|
124
|
+
],
|
|
125
|
+
true,
|
|
126
|
+
),
|
|
127
|
+
),
|
|
128
|
+
],
|
|
129
|
+
true,
|
|
130
|
+
)
|
|
131
|
+
: ts.factory.createIdentifier("connection"),
|
|
132
|
+
ts.factory.createObjectLiteralExpression(
|
|
133
|
+
[
|
|
134
|
+
ts.factory.createSpreadAssignment(
|
|
135
|
+
IdentifierFactory.access(
|
|
136
|
+
ts.factory.createIdentifier(route.name),
|
|
137
|
+
)("METADATA"),
|
|
138
|
+
),
|
|
139
|
+
ts.factory.createPropertyAssignment(
|
|
140
|
+
"path",
|
|
141
|
+
ts.factory.createCallExpression(
|
|
142
|
+
IdentifierFactory.access(
|
|
143
|
+
ts.factory.createIdentifier(route.name),
|
|
144
|
+
)("path"),
|
|
145
|
+
undefined,
|
|
146
|
+
route.parameters
|
|
147
|
+
.filter(
|
|
148
|
+
(p) => p.category === "param" || p.category === "query",
|
|
149
|
+
)
|
|
150
|
+
.map((p) => ts.factory.createIdentifier(p.name)),
|
|
151
|
+
),
|
|
152
|
+
),
|
|
153
|
+
],
|
|
154
|
+
true,
|
|
155
|
+
),
|
|
156
|
+
...(props.input
|
|
157
|
+
? [ts.factory.createIdentifier(props.input.name)]
|
|
158
|
+
: []),
|
|
159
|
+
...(config.json &&
|
|
160
|
+
typia.is<IController.IBodyParameter>(props.input) &&
|
|
161
|
+
(props.input.contentType === "application/json" ||
|
|
162
|
+
props.input.encrypted === true)
|
|
163
|
+
? [ts.factory.createIdentifier(`${route.name}.stringify`)]
|
|
164
|
+
: []),
|
|
165
|
+
],
|
|
166
|
+
);
|
|
167
|
+
const output = (awaiter: boolean) =>
|
|
168
|
+
config.simulate
|
|
169
|
+
? ts.factory.createConditionalExpression(
|
|
170
|
+
ts.factory.createIdentifier("!!connection.simulate"),
|
|
171
|
+
undefined,
|
|
172
|
+
ts.factory.createCallExpression(
|
|
173
|
+
ts.factory.createIdentifier(`${route.name}.simulate`),
|
|
174
|
+
[],
|
|
175
|
+
[
|
|
176
|
+
ts.factory.createIdentifier("connection"),
|
|
177
|
+
...route.parameters
|
|
178
|
+
.filter((p) => p.category !== "headers")
|
|
179
|
+
.map((p) => ts.factory.createIdentifier(p.name)),
|
|
180
|
+
],
|
|
181
|
+
),
|
|
182
|
+
undefined,
|
|
183
|
+
awaiter ? ts.factory.createAwaitExpression(caller()) : caller(),
|
|
184
|
+
)
|
|
185
|
+
: awaiter
|
|
186
|
+
? ts.factory.createAwaitExpression(caller())
|
|
187
|
+
: caller();
|
|
188
|
+
return [
|
|
189
|
+
...(config.assert
|
|
190
|
+
? route.parameters
|
|
191
|
+
.filter((p) => p.category !== "headers")
|
|
192
|
+
.map((p) =>
|
|
193
|
+
ts.factory.createExpressionStatement(
|
|
194
|
+
ts.factory.createCallExpression(
|
|
195
|
+
IdentifierFactory.access(
|
|
196
|
+
ts.factory.createIdentifier(
|
|
197
|
+
SdkImportWizard.typia(importer),
|
|
198
|
+
),
|
|
199
|
+
)("assert"),
|
|
200
|
+
[
|
|
201
|
+
ts.factory.createTypeQueryNode(
|
|
202
|
+
ts.factory.createIdentifier(p.name),
|
|
203
|
+
),
|
|
204
|
+
],
|
|
205
|
+
[ts.factory.createIdentifier(p.name)],
|
|
206
|
+
),
|
|
207
|
+
),
|
|
208
|
+
)
|
|
209
|
+
: []),
|
|
210
|
+
...(route.setHeaders.length === 0
|
|
211
|
+
? [ts.factory.createReturnStatement(output(false))]
|
|
212
|
+
: write_set_headers(config)(route)(output(true))),
|
|
213
|
+
];
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
const write_set_headers =
|
|
217
|
+
(config: INestiaConfig) =>
|
|
218
|
+
(route: IRoute) =>
|
|
219
|
+
(condition: ts.Expression): ts.Statement[] => {
|
|
220
|
+
const accessor = (x: string) => (y: string) =>
|
|
221
|
+
x[0] === "[" ? `${x}${y}` : `${x}.${y}`;
|
|
222
|
+
const output: string = StringUtil.escapeDuplicate([
|
|
223
|
+
"connection",
|
|
224
|
+
...route.parameters.map((p) => p.name),
|
|
225
|
+
])("output");
|
|
226
|
+
const headers: string = accessor("connection")("headers");
|
|
227
|
+
const data: string = config.propagate ? accessor(output)("data") : output;
|
|
228
|
+
|
|
229
|
+
const assigners: ts.ExpressionStatement[] = [
|
|
230
|
+
ts.factory.createBinaryExpression(
|
|
231
|
+
ts.factory.createIdentifier(headers),
|
|
232
|
+
ts.factory.createToken(ts.SyntaxKind.QuestionQuestionEqualsToken),
|
|
233
|
+
ts.factory.createObjectLiteralExpression([]),
|
|
234
|
+
),
|
|
235
|
+
...route.setHeaders.map((tuple) =>
|
|
236
|
+
tuple.type === "assigner"
|
|
237
|
+
? ts.factory.createCallExpression(
|
|
238
|
+
ts.factory.createIdentifier("Object.assign"),
|
|
239
|
+
[],
|
|
240
|
+
[
|
|
241
|
+
ts.factory.createIdentifier(headers),
|
|
242
|
+
ts.factory.createIdentifier(accessor(data)(tuple.source)),
|
|
243
|
+
],
|
|
244
|
+
)
|
|
245
|
+
: ts.factory.createBinaryExpression(
|
|
246
|
+
ts.factory.createIdentifier(
|
|
247
|
+
accessor(headers)(tuple.target ?? tuple.source),
|
|
248
|
+
),
|
|
249
|
+
ts.factory.createToken(ts.SyntaxKind.EqualsToken),
|
|
250
|
+
ts.factory.createIdentifier(accessor(data)(tuple.source)),
|
|
251
|
+
),
|
|
252
|
+
),
|
|
253
|
+
].map(ts.factory.createExpressionStatement);
|
|
254
|
+
return [
|
|
255
|
+
ts.factory.createVariableStatement(
|
|
256
|
+
[],
|
|
257
|
+
ts.factory.createVariableDeclarationList(
|
|
258
|
+
[
|
|
259
|
+
ts.factory.createVariableDeclaration(
|
|
260
|
+
output,
|
|
261
|
+
undefined,
|
|
262
|
+
getReturnType(config)(route),
|
|
263
|
+
condition,
|
|
264
|
+
),
|
|
265
|
+
],
|
|
266
|
+
ts.NodeFlags.Const,
|
|
267
|
+
),
|
|
268
|
+
),
|
|
269
|
+
...(config.propagate
|
|
270
|
+
? [
|
|
271
|
+
ts.factory.createIfStatement(
|
|
272
|
+
ts.factory.createIdentifier(accessor(output)("success")),
|
|
273
|
+
assigners.length === 1
|
|
274
|
+
? assigners[0]
|
|
275
|
+
: ts.factory.createBlock(assigners, true),
|
|
276
|
+
undefined,
|
|
277
|
+
),
|
|
278
|
+
]
|
|
279
|
+
: assigners),
|
|
280
|
+
ts.factory.createReturnStatement(ts.factory.createIdentifier(output)),
|
|
281
|
+
];
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const getTypeName =
|
|
286
|
+
(config: INestiaConfig) =>
|
|
287
|
+
(importer: ImportDictionary) =>
|
|
288
|
+
(p: IRoute.IParameter | IRoute.IOutput) =>
|
|
289
|
+
p.metadata
|
|
290
|
+
? SdkTypeProgrammer.write(config)(importer)(p.metadata)
|
|
291
|
+
: ts.factory.createTypeReferenceNode(p.typeName);
|
|
292
|
+
|
|
293
|
+
const getReturnType = (config: INestiaConfig) => (route: IRoute) =>
|
|
294
|
+
ts.factory.createTypeReferenceNode(
|
|
295
|
+
config.propagate !== true && route.output.typeName === "void"
|
|
296
|
+
? "void"
|
|
297
|
+
: `${route.name}.Output`,
|
|
298
|
+
);
|