@nestia/sdk 10.0.2 → 11.0.0-dev.20260305

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 (198) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +93 -93
  3. package/assets/bundle/api/HttpError.ts +1 -1
  4. package/assets/bundle/api/IConnection.ts +1 -1
  5. package/assets/bundle/api/Primitive.ts +1 -1
  6. package/assets/bundle/api/Resolved.ts +1 -1
  7. package/assets/bundle/api/index.ts +4 -4
  8. package/assets/bundle/api/module.ts +6 -6
  9. package/assets/bundle/distribute/README.md +37 -37
  10. package/assets/bundle/distribute/package.json +28 -28
  11. package/assets/bundle/distribute/tsconfig.json +109 -109
  12. package/assets/bundle/e2e/index.ts +42 -42
  13. package/assets/config/nestia.config.ts +97 -97
  14. package/lib/INestiaConfig.d.ts +1 -1
  15. package/lib/NestiaSdkApplication.js.map +1 -1
  16. package/lib/NestiaSwaggerComposer.d.ts +2 -1
  17. package/lib/NestiaSwaggerComposer.js +3 -3
  18. package/lib/NestiaSwaggerComposer.js.map +1 -1
  19. package/lib/analyses/AccessorAnalyzer.js +2 -2
  20. package/lib/analyses/AccessorAnalyzer.js.map +1 -1
  21. package/lib/analyses/DtoAnalyzer.js.map +1 -1
  22. package/lib/analyses/ImportAnalyzer.js.map +1 -1
  23. package/lib/analyses/ReflectHttpOperationAnalyzer.js +3 -3
  24. package/lib/analyses/ReflectHttpOperationAnalyzer.js.map +1 -1
  25. package/lib/analyses/ReflectHttpOperationExceptionAnalyzer.js +2 -2
  26. package/lib/analyses/ReflectHttpOperationExceptionAnalyzer.js.map +1 -1
  27. package/lib/analyses/ReflectHttpOperationParameterAnalyzer.js +7 -11
  28. package/lib/analyses/ReflectHttpOperationParameterAnalyzer.js.map +1 -1
  29. package/lib/analyses/ReflectHttpOperationResponseAnalyzer.js +6 -6
  30. package/lib/analyses/ReflectHttpOperationResponseAnalyzer.js.map +1 -1
  31. package/lib/analyses/TypedHttpRouteAnalyzer.d.ts +1 -1
  32. package/lib/analyses/TypedHttpRouteAnalyzer.js +6 -8
  33. package/lib/analyses/TypedHttpRouteAnalyzer.js.map +1 -1
  34. package/lib/executable/internal/NestiaConfigLoader.js +47 -43
  35. package/lib/executable/internal/NestiaConfigLoader.js.map +1 -1
  36. package/lib/executable/internal/NestiaSdkCommand.js.map +1 -1
  37. package/lib/executable/sdk.js +12 -12
  38. package/lib/generates/SwaggerGenerator.d.ts +1 -1
  39. package/lib/generates/SwaggerGenerator.js +6 -6
  40. package/lib/generates/SwaggerGenerator.js.map +1 -1
  41. package/lib/generates/internal/E2eFileProgrammer.js +8 -9
  42. package/lib/generates/internal/E2eFileProgrammer.js.map +1 -1
  43. package/lib/generates/internal/SdkAliasCollection.d.ts +2 -2
  44. package/lib/generates/internal/SdkAliasCollection.js +2 -2
  45. package/lib/generates/internal/SdkAliasCollection.js.map +1 -1
  46. package/lib/generates/internal/SdkDistributionComposer.js +1 -1
  47. package/lib/generates/internal/SdkHttpCloneProgrammer.js.map +1 -1
  48. package/lib/generates/internal/SdkHttpCloneReferencer.js.map +1 -1
  49. package/lib/generates/internal/SdkHttpFunctionProgrammer.js +11 -12
  50. package/lib/generates/internal/SdkHttpFunctionProgrammer.js.map +1 -1
  51. package/lib/generates/internal/SdkHttpNamespaceProgrammer.js +13 -16
  52. package/lib/generates/internal/SdkHttpNamespaceProgrammer.js.map +1 -1
  53. package/lib/generates/internal/SdkHttpRouteProgrammer.js +4 -4
  54. package/lib/generates/internal/SdkHttpRouteProgrammer.js.map +1 -1
  55. package/lib/generates/internal/SdkHttpSimulationProgrammer.js +12 -16
  56. package/lib/generates/internal/SdkHttpSimulationProgrammer.js.map +1 -1
  57. package/lib/generates/internal/SdkImportWizard.js +2 -2
  58. package/lib/generates/internal/SdkImportWizard.js.map +1 -1
  59. package/lib/generates/internal/SdkTypeProgrammer.d.ts +2 -3
  60. package/lib/generates/internal/SdkTypeProgrammer.js +6 -7
  61. package/lib/generates/internal/SdkTypeProgrammer.js.map +1 -1
  62. package/lib/generates/internal/SdkTypeTagProgrammer.d.ts +1 -1
  63. package/lib/generates/internal/SdkTypeTagProgrammer.js +11 -11
  64. package/lib/generates/internal/SdkTypeTagProgrammer.js.map +1 -1
  65. package/lib/generates/internal/SdkWebSocketNamespaceProgrammer.js +6 -8
  66. package/lib/generates/internal/SdkWebSocketNamespaceProgrammer.js.map +1 -1
  67. package/lib/generates/internal/SdkWebSocketRouteProgrammer.js +2 -2
  68. package/lib/generates/internal/SdkWebSocketRouteProgrammer.js.map +1 -1
  69. package/lib/generates/internal/SwaggerOperationComposer.d.ts +3 -3
  70. package/lib/generates/internal/SwaggerOperationComposer.js.map +1 -1
  71. package/lib/generates/internal/SwaggerOperationParameterComposer.d.ts +1 -1
  72. package/lib/generates/internal/SwaggerOperationParameterComposer.js +10 -8
  73. package/lib/generates/internal/SwaggerOperationParameterComposer.js.map +1 -1
  74. package/lib/generates/internal/SwaggerOperationResponseComposer.d.ts +3 -3
  75. package/lib/generates/internal/SwaggerOperationResponseComposer.js.map +1 -1
  76. package/lib/module.d.ts +1 -0
  77. package/lib/module.js +1 -0
  78. package/lib/module.js.map +1 -1
  79. package/lib/structures/IReflectHttpOperationException.d.ts +3 -4
  80. package/lib/structures/IReflectHttpOperationParameter.d.ts +3 -5
  81. package/lib/structures/IReflectHttpOperationSuccess.d.ts +3 -4
  82. package/lib/structures/ITypedApplication.d.ts +1 -1
  83. package/lib/structures/ITypedHttpRouteException.d.ts +2 -2
  84. package/lib/structures/ITypedHttpRouteParameter.d.ts +2 -2
  85. package/lib/structures/ITypedHttpRouteSuccess.d.ts +2 -2
  86. package/lib/transformers/IOperationMetadata.d.ts +2 -4
  87. package/lib/transformers/ISdkOperationTransformerContext.d.ts +1 -1
  88. package/lib/transformers/SdkOperationProgrammer.js +8 -10
  89. package/lib/transformers/SdkOperationProgrammer.js.map +1 -1
  90. package/lib/transformers/SdkOperationTransformer.js +5 -7
  91. package/lib/transformers/SdkOperationTransformer.js.map +1 -1
  92. package/lib/transformers/TextPlainValidator.d.ts +2 -2
  93. package/lib/transformers/TextPlainValidator.js.map +1 -1
  94. package/lib/utils/MetadataUtil.d.ts +2 -2
  95. package/lib/utils/MetadataUtil.js.map +1 -1
  96. package/lib/validators/HttpHeadersValidator.d.ts +2 -3
  97. package/lib/validators/HttpHeadersValidator.js +2 -2
  98. package/lib/validators/HttpHeadersValidator.js.map +1 -1
  99. package/lib/validators/HttpQueryValidator.d.ts +2 -3
  100. package/lib/validators/HttpQueryValidator.js +2 -2
  101. package/lib/validators/HttpQueryValidator.js.map +1 -1
  102. package/package.json +44 -30
  103. package/src/INestiaConfig.ts +267 -267
  104. package/src/NestiaSdkApplication.ts +307 -307
  105. package/src/NestiaSwaggerComposer.ts +140 -138
  106. package/src/analyses/AccessorAnalyzer.ts +67 -67
  107. package/src/analyses/ConfigAnalyzer.ts +155 -155
  108. package/src/analyses/DtoAnalyzer.ts +260 -260
  109. package/src/analyses/ExceptionAnalyzer.ts +154 -154
  110. package/src/analyses/GenericAnalyzer.ts +49 -49
  111. package/src/analyses/ImportAnalyzer.ts +126 -126
  112. package/src/analyses/PathAnalyzer.ts +69 -69
  113. package/src/analyses/ReflectControllerAnalyzer.ts +105 -105
  114. package/src/analyses/ReflectHttpOperationAnalyzer.ts +183 -183
  115. package/src/analyses/ReflectHttpOperationExceptionAnalyzer.ts +72 -71
  116. package/src/analyses/ReflectHttpOperationParameterAnalyzer.ts +350 -348
  117. package/src/analyses/ReflectHttpOperationResponseAnalyzer.ts +126 -127
  118. package/src/analyses/ReflectMetadataAnalyzer.ts +44 -44
  119. package/src/analyses/ReflectWebSocketOperationAnalyzer.ts +172 -172
  120. package/src/analyses/SecurityAnalyzer.ts +25 -25
  121. package/src/analyses/TypedHttpRouteAnalyzer.ts +208 -204
  122. package/src/analyses/TypedWebSocketRouteAnalyzer.ts +33 -33
  123. package/src/decorators/OperationMetadata.ts +15 -15
  124. package/src/executable/internal/CommandParser.ts +15 -15
  125. package/src/executable/internal/NestiaConfigLoader.ts +85 -78
  126. package/src/executable/internal/NestiaSdkCommand.ts +107 -103
  127. package/src/executable/sdk.ts +75 -75
  128. package/src/generates/CloneGenerator.ts +66 -66
  129. package/src/generates/E2eGenerator.ts +32 -32
  130. package/src/generates/SdkGenerator.ts +160 -160
  131. package/src/generates/SwaggerGenerator.ts +285 -284
  132. package/src/generates/internal/E2eFileProgrammer.ts +196 -197
  133. package/src/generates/internal/FilePrinter.ts +64 -64
  134. package/src/generates/internal/ImportDictionary.ts +192 -192
  135. package/src/generates/internal/SdkAliasCollection.ts +260 -261
  136. package/src/generates/internal/SdkDistributionComposer.ts +103 -103
  137. package/src/generates/internal/SdkFileProgrammer.ts +110 -110
  138. package/src/generates/internal/SdkHttpCloneProgrammer.ts +126 -124
  139. package/src/generates/internal/SdkHttpCloneReferencer.ts +77 -77
  140. package/src/generates/internal/SdkHttpFunctionProgrammer.ts +278 -279
  141. package/src/generates/internal/SdkHttpNamespaceProgrammer.ts +502 -500
  142. package/src/generates/internal/SdkHttpParameterProgrammer.ts +178 -178
  143. package/src/generates/internal/SdkHttpRouteProgrammer.ts +109 -108
  144. package/src/generates/internal/SdkHttpSimulationProgrammer.ts +312 -310
  145. package/src/generates/internal/SdkImportWizard.ts +62 -62
  146. package/src/generates/internal/SdkRouteDirectory.ts +18 -18
  147. package/src/generates/internal/SdkTypeProgrammer.ts +388 -385
  148. package/src/generates/internal/SdkTypeTagProgrammer.ts +114 -104
  149. package/src/generates/internal/SdkWebSocketNamespaceProgrammer.ts +379 -381
  150. package/src/generates/internal/SdkWebSocketParameterProgrammer.ts +87 -87
  151. package/src/generates/internal/SdkWebSocketRouteProgrammer.ts +302 -302
  152. package/src/generates/internal/SwaggerDescriptionComposer.ts +64 -64
  153. package/src/generates/internal/SwaggerOperationComposer.ts +119 -119
  154. package/src/generates/internal/SwaggerOperationParameterComposer.ts +161 -162
  155. package/src/generates/internal/SwaggerOperationResponseComposer.ts +110 -110
  156. package/src/index.ts +4 -4
  157. package/src/module.ts +4 -3
  158. package/src/structures/INestiaProject.ts +13 -13
  159. package/src/structures/INestiaSdkInput.ts +20 -20
  160. package/src/structures/IReflectApplication.ts +8 -8
  161. package/src/structures/IReflectController.ts +15 -15
  162. package/src/structures/IReflectHttpOperation.ts +26 -26
  163. package/src/structures/IReflectHttpOperationException.ts +18 -19
  164. package/src/structures/IReflectHttpOperationParameter.ts +79 -77
  165. package/src/structures/IReflectHttpOperationSuccess.ts +21 -22
  166. package/src/structures/IReflectImport.ts +6 -6
  167. package/src/structures/IReflectOperationError.ts +26 -26
  168. package/src/structures/IReflectType.ts +4 -4
  169. package/src/structures/IReflectWebSocketOperation.ts +17 -17
  170. package/src/structures/IReflectWebSocketOperationParameter.ts +36 -36
  171. package/src/structures/ITypedApplication.ts +11 -11
  172. package/src/structures/ITypedHttpRoute.ts +41 -41
  173. package/src/structures/ITypedHttpRouteException.ts +15 -15
  174. package/src/structures/ITypedHttpRouteParameter.ts +41 -41
  175. package/src/structures/ITypedHttpRouteSuccess.ts +22 -22
  176. package/src/structures/ITypedWebSocketRoute.ts +24 -24
  177. package/src/structures/ITypedWebSocketRouteParameter.ts +3 -3
  178. package/src/structures/MethodType.ts +5 -5
  179. package/src/structures/ParamCategory.ts +1 -1
  180. package/src/structures/TypeEntry.ts +22 -22
  181. package/src/transform.ts +9 -9
  182. package/src/transformers/IOperationMetadata.ts +46 -44
  183. package/src/transformers/ISdkOperationTransformerContext.ts +8 -8
  184. package/src/transformers/SdkOperationProgrammer.ts +240 -238
  185. package/src/transformers/SdkOperationTransformer.ts +248 -252
  186. package/src/transformers/TextPlainValidator.ts +17 -17
  187. package/src/typings/get-function-location.d.ts +7 -7
  188. package/src/utils/ArrayUtil.ts +26 -26
  189. package/src/utils/FileRetriever.ts +22 -22
  190. package/src/utils/MapUtil.ts +14 -14
  191. package/src/utils/MetadataUtil.ts +26 -26
  192. package/src/utils/PathUtil.ts +10 -10
  193. package/src/utils/SourceFinder.ts +63 -63
  194. package/src/utils/StringUtil.ts +17 -17
  195. package/src/utils/StripEnums.ts +5 -5
  196. package/src/utils/VersioningStrategy.ts +28 -28
  197. package/src/validators/HttpHeadersValidator.ts +36 -34
  198. package/src/validators/HttpQueryValidator.ts +36 -34
@@ -1,310 +1,312 @@
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 { StatementFactory } from "typia/lib/factories/StatementFactory";
6
- import { TypeFactory } from "typia/lib/factories/TypeFactory";
7
-
8
- import { INestiaProject } from "../../structures/INestiaProject";
9
- import { ITypedHttpRoute } from "../../structures/ITypedHttpRoute";
10
- import { ImportDictionary } from "./ImportDictionary";
11
- import { SdkAliasCollection } from "./SdkAliasCollection";
12
- import { SdkHttpParameterProgrammer } from "./SdkHttpParameterProgrammer";
13
- import { SdkImportWizard } from "./SdkImportWizard";
14
-
15
- export namespace SdkHttpSimulationProgrammer {
16
- export const random =
17
- (project: INestiaProject) =>
18
- (importer: ImportDictionary) =>
19
- (route: ITypedHttpRoute): ts.VariableStatement => {
20
- const output = SdkAliasCollection.responseBody(project)(importer)(route);
21
- return constant("random")(
22
- ts.factory.createArrowFunction(
23
- undefined,
24
- undefined,
25
- [],
26
- project.config.primitive === false
27
- ? output
28
- : ts.factory.createTypeReferenceNode(
29
- SdkImportWizard.Resolved(importer),
30
- [output],
31
- ),
32
- undefined,
33
- ts.factory.createCallExpression(
34
- IdentifierFactory.access(
35
- ts.factory.createIdentifier(SdkImportWizard.typia(importer)),
36
- "random",
37
- ),
38
- [output],
39
- undefined,
40
- ),
41
- ),
42
- );
43
- };
44
-
45
- export const simulate =
46
- (project: INestiaProject) =>
47
- (importer: ImportDictionary) =>
48
- (route: ITypedHttpRoute): ts.VariableStatement => {
49
- const output: boolean =
50
- project.config.propagate === true ||
51
- route.success.metadata.size() !== 0;
52
- const caller = () =>
53
- ts.factory.createCallExpression(
54
- ts.factory.createIdentifier("random"),
55
- undefined,
56
- undefined,
57
- );
58
- return constant("simulate")(
59
- ts.factory.createArrowFunction(
60
- undefined,
61
- undefined,
62
- [
63
- IdentifierFactory.parameter(
64
- SdkHttpParameterProgrammer.getSignificant(route, true).length !==
65
- 0
66
- ? "connection"
67
- : "_connection",
68
- ts.factory.createTypeReferenceNode(
69
- SdkImportWizard.IConnection(importer),
70
- route.headerObject
71
- ? [
72
- ts.factory.createTypeReferenceNode(
73
- `${route.name}.Headers`,
74
- ),
75
- ]
76
- : [],
77
- ),
78
- ),
79
- ...SdkHttpParameterProgrammer.getParameterDeclarations({
80
- project,
81
- importer,
82
- route,
83
- body: true,
84
- prefix: false,
85
- }),
86
- ],
87
- ts.factory.createTypeReferenceNode(output ? "Output" : "void"),
88
- undefined,
89
- ts.factory.createBlock(
90
- [
91
- ...assert(project)(importer)(route),
92
- ts.factory.createReturnStatement(
93
- project.config.propagate
94
- ? ts.factory.createAsExpression(
95
- ts.factory.createObjectLiteralExpression(
96
- [
97
- ts.factory.createPropertyAssignment(
98
- "success",
99
- ts.factory.createTrue(),
100
- ),
101
- ts.factory.createPropertyAssignment(
102
- "status",
103
- ExpressionFactory.number(
104
- route.success.status ??
105
- (route.method === "POST" ? 201 : 200),
106
- ),
107
- ),
108
- ts.factory.createPropertyAssignment(
109
- "headers",
110
- LiteralFactory.write({
111
- "Content-Type": route.success.contentType,
112
- }),
113
- ),
114
- ts.factory.createPropertyAssignment("data", caller()),
115
- ],
116
- true,
117
- ),
118
- ts.factory.createTypeReferenceNode("Output"),
119
- )
120
- : caller(),
121
- ),
122
- ],
123
- true,
124
- ),
125
- ),
126
- );
127
- };
128
-
129
- const assert =
130
- (project: INestiaProject) =>
131
- (importer: ImportDictionary) =>
132
- (route: ITypedHttpRoute): ts.Statement[] => {
133
- const parameters = SdkHttpParameterProgrammer.getSignificant(route, true);
134
- if (parameters.length === 0) return [];
135
-
136
- const typia = SdkImportWizard.typia(importer);
137
- const validator = StatementFactory.constant({
138
- name: "assert",
139
- value: ts.factory.createCallExpression(
140
- IdentifierFactory.access(
141
- ts.factory.createIdentifier(
142
- importer.external({
143
- file: `@nestia/fetcher/lib/NestiaSimulator`,
144
- declaration: false,
145
- type: "element",
146
- name: "NestiaSimulator",
147
- }),
148
- ),
149
- "assert",
150
- ),
151
- undefined,
152
- [
153
- ts.factory.createObjectLiteralExpression(
154
- [
155
- ts.factory.createPropertyAssignment(
156
- "method",
157
- ts.factory.createIdentifier("METADATA.method"),
158
- ),
159
- ts.factory.createPropertyAssignment(
160
- "host",
161
- ts.factory.createIdentifier("connection.host"),
162
- ),
163
- ts.factory.createPropertyAssignment(
164
- "path",
165
- ts.factory.createCallExpression(
166
- ts.factory.createIdentifier("path"),
167
- undefined,
168
- SdkHttpParameterProgrammer.getArguments({
169
- project,
170
- route,
171
- body: false,
172
- }),
173
- ),
174
- ),
175
- ts.factory.createPropertyAssignment(
176
- "contentType",
177
- ts.factory.createIdentifier(
178
- JSON.stringify(route.success.contentType),
179
- ),
180
- ),
181
- ],
182
- true,
183
- ),
184
- ],
185
- ),
186
- });
187
- const individual = parameters
188
- .map((p) =>
189
- ts.factory.createCallExpression(
190
- (() => {
191
- const base = IdentifierFactory.access(
192
- ts.factory.createIdentifier("assert"),
193
- p.category,
194
- );
195
- if (p.category !== "param") return base;
196
- return ts.factory.createCallExpression(base, undefined, [
197
- ts.factory.createStringLiteral(p.name),
198
- ]);
199
- })(),
200
- undefined,
201
- [
202
- ts.factory.createArrowFunction(
203
- undefined,
204
- undefined,
205
- [],
206
- undefined,
207
- undefined,
208
- ts.factory.createCallExpression(
209
- IdentifierFactory.access(
210
- ts.factory.createIdentifier(typia),
211
- "assert",
212
- ),
213
- undefined,
214
- [
215
- project.config.keyword === true
216
- ? ts.factory.createIdentifier(`props.${p.name}`)
217
- : ts.factory.createIdentifier(p.name),
218
- ],
219
- ),
220
- ),
221
- ],
222
- ),
223
- )
224
- .map(ts.factory.createExpressionStatement);
225
- return [
226
- validator,
227
- ...(project.config.propagate !== true
228
- ? individual
229
- : [tryAndCatch(importer)(individual)]),
230
- ];
231
- };
232
-
233
- const tryAndCatch =
234
- (importer: ImportDictionary) => (individual: ts.Statement[]) =>
235
- ts.factory.createTryStatement(
236
- ts.factory.createBlock(individual, true),
237
- ts.factory.createCatchClause(
238
- "exp",
239
- ts.factory.createBlock(
240
- [
241
- ts.factory.createIfStatement(
242
- ts.factory.createLogicalNot(
243
- ts.factory.createCallExpression(
244
- IdentifierFactory.access(
245
- ts.factory.createIdentifier(
246
- SdkImportWizard.typia(importer),
247
- ),
248
- "is",
249
- ),
250
- [
251
- ts.factory.createTypeReferenceNode(
252
- SdkImportWizard.HttpError(importer),
253
- ),
254
- ],
255
- [ts.factory.createIdentifier("exp")],
256
- ),
257
- ),
258
- ts.factory.createThrowStatement(
259
- ts.factory.createIdentifier("exp"),
260
- ),
261
- ),
262
- ts.factory.createReturnStatement(
263
- ts.factory.createAsExpression(
264
- ts.factory.createObjectLiteralExpression(
265
- [
266
- ts.factory.createPropertyAssignment(
267
- "success",
268
- ts.factory.createFalse(),
269
- ),
270
- ts.factory.createPropertyAssignment(
271
- "status",
272
- ts.factory.createIdentifier("exp.status"),
273
- ),
274
- ts.factory.createPropertyAssignment(
275
- "headers",
276
- ts.factory.createIdentifier("exp.headers"),
277
- ),
278
- ts.factory.createPropertyAssignment(
279
- "data",
280
- ts.factory.createIdentifier("exp.toJSON().message"),
281
- ),
282
- ],
283
- true,
284
- ),
285
- TypeFactory.keyword("any"),
286
- ),
287
- ),
288
- ],
289
- true,
290
- ),
291
- ),
292
- undefined,
293
- );
294
- }
295
-
296
- const constant = (name: string) => (expression: ts.Expression) =>
297
- ts.factory.createVariableStatement(
298
- [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
299
- ts.factory.createVariableDeclarationList(
300
- [
301
- ts.factory.createVariableDeclaration(
302
- ts.factory.createIdentifier(name),
303
- undefined,
304
- undefined,
305
- expression,
306
- ),
307
- ],
308
- ts.NodeFlags.Const,
309
- ),
310
- );
1
+ import {
2
+ ExpressionFactory,
3
+ IdentifierFactory,
4
+ LiteralFactory,
5
+ StatementFactory,
6
+ TypeFactory,
7
+ } from "@typia/core";
8
+ import ts from "typescript";
9
+
10
+ import { INestiaProject } from "../../structures/INestiaProject";
11
+ import { ITypedHttpRoute } from "../../structures/ITypedHttpRoute";
12
+ import { ImportDictionary } from "./ImportDictionary";
13
+ import { SdkAliasCollection } from "./SdkAliasCollection";
14
+ import { SdkHttpParameterProgrammer } from "./SdkHttpParameterProgrammer";
15
+ import { SdkImportWizard } from "./SdkImportWizard";
16
+
17
+ export namespace SdkHttpSimulationProgrammer {
18
+ export const random =
19
+ (project: INestiaProject) =>
20
+ (importer: ImportDictionary) =>
21
+ (route: ITypedHttpRoute): ts.VariableStatement => {
22
+ const output = SdkAliasCollection.responseBody(project)(importer)(route);
23
+ return constant("random")(
24
+ ts.factory.createArrowFunction(
25
+ undefined,
26
+ undefined,
27
+ [],
28
+ project.config.primitive === false
29
+ ? output
30
+ : ts.factory.createTypeReferenceNode(
31
+ SdkImportWizard.Resolved(importer),
32
+ [output],
33
+ ),
34
+ undefined,
35
+ ts.factory.createCallExpression(
36
+ IdentifierFactory.access(
37
+ ts.factory.createIdentifier(SdkImportWizard.typia(importer)),
38
+ "random",
39
+ ),
40
+ [output],
41
+ undefined,
42
+ ),
43
+ ),
44
+ );
45
+ };
46
+
47
+ export const simulate =
48
+ (project: INestiaProject) =>
49
+ (importer: ImportDictionary) =>
50
+ (route: ITypedHttpRoute): ts.VariableStatement => {
51
+ const output: boolean =
52
+ project.config.propagate === true ||
53
+ route.success.metadata.size() !== 0;
54
+ const caller = () =>
55
+ ts.factory.createCallExpression(
56
+ ts.factory.createIdentifier("random"),
57
+ undefined,
58
+ undefined,
59
+ );
60
+ return constant("simulate")(
61
+ ts.factory.createArrowFunction(
62
+ undefined,
63
+ undefined,
64
+ [
65
+ IdentifierFactory.parameter(
66
+ SdkHttpParameterProgrammer.getSignificant(route, true).length !==
67
+ 0
68
+ ? "connection"
69
+ : "_connection",
70
+ ts.factory.createTypeReferenceNode(
71
+ SdkImportWizard.IConnection(importer),
72
+ route.headerObject
73
+ ? [
74
+ ts.factory.createTypeReferenceNode(
75
+ `${route.name}.Headers`,
76
+ ),
77
+ ]
78
+ : [],
79
+ ),
80
+ ),
81
+ ...SdkHttpParameterProgrammer.getParameterDeclarations({
82
+ project,
83
+ importer,
84
+ route,
85
+ body: true,
86
+ prefix: false,
87
+ }),
88
+ ],
89
+ ts.factory.createTypeReferenceNode(output ? "Output" : "void"),
90
+ undefined,
91
+ ts.factory.createBlock(
92
+ [
93
+ ...assert(project)(importer)(route),
94
+ ts.factory.createReturnStatement(
95
+ project.config.propagate
96
+ ? ts.factory.createAsExpression(
97
+ ts.factory.createObjectLiteralExpression(
98
+ [
99
+ ts.factory.createPropertyAssignment(
100
+ "success",
101
+ ts.factory.createTrue(),
102
+ ),
103
+ ts.factory.createPropertyAssignment(
104
+ "status",
105
+ ExpressionFactory.number(
106
+ route.success.status ??
107
+ (route.method === "POST" ? 201 : 200),
108
+ ),
109
+ ),
110
+ ts.factory.createPropertyAssignment(
111
+ "headers",
112
+ LiteralFactory.write({
113
+ "Content-Type": route.success.contentType,
114
+ }),
115
+ ),
116
+ ts.factory.createPropertyAssignment("data", caller()),
117
+ ],
118
+ true,
119
+ ),
120
+ ts.factory.createTypeReferenceNode("Output"),
121
+ )
122
+ : caller(),
123
+ ),
124
+ ],
125
+ true,
126
+ ),
127
+ ),
128
+ );
129
+ };
130
+
131
+ const assert =
132
+ (project: INestiaProject) =>
133
+ (importer: ImportDictionary) =>
134
+ (route: ITypedHttpRoute): ts.Statement[] => {
135
+ const parameters = SdkHttpParameterProgrammer.getSignificant(route, true);
136
+ if (parameters.length === 0) return [];
137
+
138
+ const typia = SdkImportWizard.typia(importer);
139
+ const validator = StatementFactory.constant({
140
+ name: "assert",
141
+ value: ts.factory.createCallExpression(
142
+ IdentifierFactory.access(
143
+ ts.factory.createIdentifier(
144
+ importer.external({
145
+ file: `@nestia/fetcher`,
146
+ declaration: false,
147
+ type: "element",
148
+ name: "NestiaSimulator",
149
+ }),
150
+ ),
151
+ "assert",
152
+ ),
153
+ undefined,
154
+ [
155
+ ts.factory.createObjectLiteralExpression(
156
+ [
157
+ ts.factory.createPropertyAssignment(
158
+ "method",
159
+ ts.factory.createIdentifier("METADATA.method"),
160
+ ),
161
+ ts.factory.createPropertyAssignment(
162
+ "host",
163
+ ts.factory.createIdentifier("connection.host"),
164
+ ),
165
+ ts.factory.createPropertyAssignment(
166
+ "path",
167
+ ts.factory.createCallExpression(
168
+ ts.factory.createIdentifier("path"),
169
+ undefined,
170
+ SdkHttpParameterProgrammer.getArguments({
171
+ project,
172
+ route,
173
+ body: false,
174
+ }),
175
+ ),
176
+ ),
177
+ ts.factory.createPropertyAssignment(
178
+ "contentType",
179
+ ts.factory.createIdentifier(
180
+ JSON.stringify(route.success.contentType),
181
+ ),
182
+ ),
183
+ ],
184
+ true,
185
+ ),
186
+ ],
187
+ ),
188
+ });
189
+ const individual = parameters
190
+ .map((p) =>
191
+ ts.factory.createCallExpression(
192
+ (() => {
193
+ const base = IdentifierFactory.access(
194
+ ts.factory.createIdentifier("assert"),
195
+ p.category,
196
+ );
197
+ if (p.category !== "param") return base;
198
+ return ts.factory.createCallExpression(base, undefined, [
199
+ ts.factory.createStringLiteral(p.name),
200
+ ]);
201
+ })(),
202
+ undefined,
203
+ [
204
+ ts.factory.createArrowFunction(
205
+ undefined,
206
+ undefined,
207
+ [],
208
+ undefined,
209
+ undefined,
210
+ ts.factory.createCallExpression(
211
+ IdentifierFactory.access(
212
+ ts.factory.createIdentifier(typia),
213
+ "assert",
214
+ ),
215
+ undefined,
216
+ [
217
+ project.config.keyword === true
218
+ ? ts.factory.createIdentifier(`props.${p.name}`)
219
+ : ts.factory.createIdentifier(p.name),
220
+ ],
221
+ ),
222
+ ),
223
+ ],
224
+ ),
225
+ )
226
+ .map(ts.factory.createExpressionStatement);
227
+ return [
228
+ validator,
229
+ ...(project.config.propagate !== true
230
+ ? individual
231
+ : [tryAndCatch(importer)(individual)]),
232
+ ];
233
+ };
234
+
235
+ const tryAndCatch =
236
+ (importer: ImportDictionary) => (individual: ts.Statement[]) =>
237
+ ts.factory.createTryStatement(
238
+ ts.factory.createBlock(individual, true),
239
+ ts.factory.createCatchClause(
240
+ "exp",
241
+ ts.factory.createBlock(
242
+ [
243
+ ts.factory.createIfStatement(
244
+ ts.factory.createLogicalNot(
245
+ ts.factory.createCallExpression(
246
+ IdentifierFactory.access(
247
+ ts.factory.createIdentifier(
248
+ SdkImportWizard.typia(importer),
249
+ ),
250
+ "is",
251
+ ),
252
+ [
253
+ ts.factory.createTypeReferenceNode(
254
+ SdkImportWizard.HttpError(importer),
255
+ ),
256
+ ],
257
+ [ts.factory.createIdentifier("exp")],
258
+ ),
259
+ ),
260
+ ts.factory.createThrowStatement(
261
+ ts.factory.createIdentifier("exp"),
262
+ ),
263
+ ),
264
+ ts.factory.createReturnStatement(
265
+ ts.factory.createAsExpression(
266
+ ts.factory.createObjectLiteralExpression(
267
+ [
268
+ ts.factory.createPropertyAssignment(
269
+ "success",
270
+ ts.factory.createFalse(),
271
+ ),
272
+ ts.factory.createPropertyAssignment(
273
+ "status",
274
+ ts.factory.createIdentifier("exp.status"),
275
+ ),
276
+ ts.factory.createPropertyAssignment(
277
+ "headers",
278
+ ts.factory.createIdentifier("exp.headers"),
279
+ ),
280
+ ts.factory.createPropertyAssignment(
281
+ "data",
282
+ ts.factory.createIdentifier("exp.toJSON().message"),
283
+ ),
284
+ ],
285
+ true,
286
+ ),
287
+ TypeFactory.keyword("any"),
288
+ ),
289
+ ),
290
+ ],
291
+ true,
292
+ ),
293
+ ),
294
+ undefined,
295
+ );
296
+ }
297
+
298
+ const constant = (name: string) => (expression: ts.Expression) =>
299
+ ts.factory.createVariableStatement(
300
+ [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
301
+ ts.factory.createVariableDeclarationList(
302
+ [
303
+ ts.factory.createVariableDeclaration(
304
+ ts.factory.createIdentifier(name),
305
+ undefined,
306
+ undefined,
307
+ expression,
308
+ ),
309
+ ],
310
+ ts.NodeFlags.Const,
311
+ ),
312
+ );