@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,279 +1,278 @@
1
- import ts from "typescript";
2
- import { IdentifierFactory } from "typia/lib/factories/IdentifierFactory";
3
- import { TypeFactory } from "typia/lib/factories/TypeFactory";
4
-
5
- import { INestiaProject } from "../../structures/INestiaProject";
6
- import { ITypedHttpRoute } from "../../structures/ITypedHttpRoute";
7
- import { StringUtil } from "../../utils/StringUtil";
8
- import { ImportDictionary } from "./ImportDictionary";
9
- import { SdkAliasCollection } from "./SdkAliasCollection";
10
- import { SdkHttpParameterProgrammer } from "./SdkHttpParameterProgrammer";
11
- import { SdkImportWizard } from "./SdkImportWizard";
12
-
13
- export namespace SdkHttpFunctionProgrammer {
14
- export const write =
15
- (project: INestiaProject) =>
16
- (importer: ImportDictionary) =>
17
- (route: ITypedHttpRoute): ts.FunctionDeclaration => {
18
- return ts.factory.createFunctionDeclaration(
19
- [
20
- ts.factory.createModifier(ts.SyntaxKind.ExportKeyword),
21
- ts.factory.createModifier(ts.SyntaxKind.AsyncKeyword),
22
- ],
23
- undefined,
24
- route.name,
25
- undefined,
26
- [
27
- IdentifierFactory.parameter(
28
- "connection",
29
- ts.factory.createTypeReferenceNode(
30
- SdkImportWizard.IConnection(importer),
31
- route.headerObject !== null
32
- ? [ts.factory.createTypeReferenceNode(`${route.name}.Headers`)]
33
- : undefined,
34
- ),
35
- ),
36
- ...SdkHttpParameterProgrammer.getParameterDeclarations({
37
- project,
38
- importer,
39
- route,
40
- body: true,
41
- prefix: true,
42
- }),
43
- ],
44
- ts.factory.createTypeReferenceNode("Promise", [
45
- project.config.propagate === true ||
46
- route.success.metadata.size() !== 0
47
- ? ts.factory.createTypeReferenceNode(`${route.name}.Output`)
48
- : ts.factory.createTypeReferenceNode("void"),
49
- ]),
50
- ts.factory.createBlock(writeBody(project)(importer)(route), true),
51
- );
52
- };
53
-
54
- const writeBody =
55
- (project: INestiaProject) =>
56
- (importer: ImportDictionary) =>
57
- (route: ITypedHttpRoute): ts.Statement[] => {
58
- const access = (name: string): ts.Expression =>
59
- project.config.keyword === true
60
- ? IdentifierFactory.access(ts.factory.createIdentifier("props"), name)
61
- : ts.factory.createIdentifier(name);
62
- const fetch = () =>
63
- ts.factory.createCallExpression(
64
- IdentifierFactory.access(
65
- ts.factory.createIdentifier(
66
- SdkImportWizard.Fetcher(
67
- !!route.body?.encrypted || route.success.encrypted,
68
- )(importer),
69
- ),
70
- project.config.propagate ? "propagate" : "fetch",
71
- ),
72
- project.config.propagate
73
- ? route.method.toLowerCase() === "get" ||
74
- route.method.toLowerCase() === "head"
75
- ? [TypeFactory.keyword("any")]
76
- : [TypeFactory.keyword("any"), TypeFactory.keyword("any")]
77
- : undefined,
78
- [
79
- route.body && route.body.contentType !== "multipart/form-data"
80
- ? ts.factory.createObjectLiteralExpression(
81
- [
82
- ts.factory.createSpreadAssignment(
83
- ts.factory.createIdentifier("connection"),
84
- ),
85
- ts.factory.createPropertyAssignment(
86
- "headers",
87
- ts.factory.createObjectLiteralExpression(
88
- [
89
- ts.factory.createSpreadAssignment(
90
- IdentifierFactory.access(
91
- ts.factory.createIdentifier("connection"),
92
- "headers",
93
- ),
94
- ),
95
- ts.factory.createPropertyAssignment(
96
- ts.factory.createStringLiteral("Content-Type"),
97
- ts.factory.createStringLiteral(
98
- route.body?.contentType ?? "application/json",
99
- ),
100
- ),
101
- ],
102
- true,
103
- ),
104
- ),
105
- ],
106
- true,
107
- )
108
- : ts.factory.createIdentifier("connection"),
109
- ts.factory.createObjectLiteralExpression(
110
- [
111
- ts.factory.createSpreadAssignment(
112
- IdentifierFactory.access(
113
- ts.factory.createIdentifier(route.name),
114
- "METADATA",
115
- ),
116
- ),
117
- ts.factory.createPropertyAssignment(
118
- "template",
119
- IdentifierFactory.access(
120
- IdentifierFactory.access(
121
- ts.factory.createIdentifier(route.name),
122
- "METADATA",
123
- ),
124
- "path",
125
- ),
126
- ),
127
- ts.factory.createPropertyAssignment(
128
- "path",
129
- ts.factory.createCallExpression(
130
- IdentifierFactory.access(
131
- ts.factory.createIdentifier(route.name),
132
- "path",
133
- ),
134
- undefined,
135
- SdkHttpParameterProgrammer.getArguments({
136
- project,
137
- route,
138
- body: false,
139
- }),
140
- ),
141
- ),
142
- ],
143
- true,
144
- ),
145
- ...(route.body ? [access(route.body.name)] : []),
146
- ...(project.config.json &&
147
- route.body !== null &&
148
- (route.body.contentType === "application/json" ||
149
- route.body.encrypted === true)
150
- ? [ts.factory.createIdentifier(`${route.name}.stringify`)]
151
- : []),
152
- ],
153
- );
154
- const output = (awaiter: boolean) =>
155
- project.config.simulate
156
- ? ts.factory.createConditionalExpression(
157
- ts.factory.createStrictEquality(
158
- ts.factory.createTrue(),
159
- ts.factory.createIdentifier("connection.simulate"),
160
- ),
161
- undefined,
162
- ts.factory.createCallExpression(
163
- ts.factory.createIdentifier(`${route.name}.simulate`),
164
- [],
165
- [
166
- ts.factory.createIdentifier("connection"),
167
- ...SdkHttpParameterProgrammer.getArguments({
168
- project,
169
- route,
170
- body: true,
171
- }),
172
- ],
173
- ),
174
- undefined,
175
- awaiter ? ts.factory.createAwaitExpression(fetch()) : fetch(),
176
- )
177
- : awaiter
178
- ? ts.factory.createAwaitExpression(fetch())
179
- : fetch();
180
- return [
181
- ...(project.config.assert
182
- ? SdkHttpParameterProgrammer.getSignificant(route, true).map((p) =>
183
- ts.factory.createExpressionStatement(
184
- ts.factory.createCallExpression(
185
- IdentifierFactory.access(
186
- ts.factory.createIdentifier(
187
- SdkImportWizard.typia(importer),
188
- ),
189
- "assert",
190
- ),
191
- [
192
- ts.factory.createTypeQueryNode(
193
- ts.factory.createIdentifier(p.name),
194
- ),
195
- ],
196
- [ts.factory.createIdentifier(p.name)],
197
- ),
198
- ),
199
- )
200
- : []),
201
- ...(route.success.setHeaders.length === 0
202
- ? [ts.factory.createReturnStatement(output(false))]
203
- : writeSetHeaders(project)(importer)(route)(output(true))),
204
- ];
205
- };
206
-
207
- const writeSetHeaders =
208
- (project: INestiaProject) =>
209
- (importer: ImportDictionary) =>
210
- (route: ITypedHttpRoute) =>
211
- (condition: ts.Expression): ts.Statement[] => {
212
- const accessor = (x: string) => (y: string) =>
213
- x[0] === "[" ? `${x}${y}` : `${x}.${y}`;
214
- const output: string = StringUtil.escapeDuplicate([
215
- "connection",
216
- ...SdkHttpParameterProgrammer.getSignificant(route, true).map(
217
- (p) => p.name,
218
- ),
219
- ])("output");
220
- const headers: string = accessor("connection")("headers");
221
- const data: string = project.config.propagate
222
- ? accessor(output)("data")
223
- : output;
224
-
225
- const assigners: ts.ExpressionStatement[] = [
226
- ts.factory.createBinaryExpression(
227
- ts.factory.createIdentifier(headers),
228
- ts.factory.createToken(ts.SyntaxKind.QuestionQuestionEqualsToken),
229
- ts.factory.createObjectLiteralExpression([]),
230
- ),
231
- ...route.success.setHeaders.map((tuple) =>
232
- tuple.type === "assigner"
233
- ? ts.factory.createCallExpression(
234
- ts.factory.createIdentifier("Object.assign"),
235
- [],
236
- [
237
- ts.factory.createIdentifier(headers),
238
- ts.factory.createIdentifier(accessor(data)(tuple.source)),
239
- ],
240
- )
241
- : ts.factory.createBinaryExpression(
242
- ts.factory.createIdentifier(
243
- accessor(headers)(tuple.target ?? tuple.source),
244
- ),
245
- ts.factory.createToken(ts.SyntaxKind.EqualsToken),
246
- ts.factory.createIdentifier(accessor(data)(tuple.source)),
247
- ),
248
- ),
249
- ].map(ts.factory.createExpressionStatement);
250
- return [
251
- ts.factory.createVariableStatement(
252
- [],
253
- ts.factory.createVariableDeclarationList(
254
- [
255
- ts.factory.createVariableDeclaration(
256
- output,
257
- undefined,
258
- SdkAliasCollection.response(project)(importer)(route),
259
- condition,
260
- ),
261
- ],
262
- ts.NodeFlags.Const,
263
- ),
264
- ),
265
- ...(project.config.propagate
266
- ? [
267
- ts.factory.createIfStatement(
268
- ts.factory.createIdentifier(accessor(output)("success")),
269
- assigners.length === 1
270
- ? assigners[0]
271
- : ts.factory.createBlock(assigners, true),
272
- undefined,
273
- ),
274
- ]
275
- : assigners),
276
- ts.factory.createReturnStatement(ts.factory.createIdentifier(output)),
277
- ];
278
- };
279
- }
1
+ import { IdentifierFactory, TypeFactory } from "@typia/core";
2
+ import ts from "typescript";
3
+
4
+ import { INestiaProject } from "../../structures/INestiaProject";
5
+ import { ITypedHttpRoute } from "../../structures/ITypedHttpRoute";
6
+ import { StringUtil } from "../../utils/StringUtil";
7
+ import { ImportDictionary } from "./ImportDictionary";
8
+ import { SdkAliasCollection } from "./SdkAliasCollection";
9
+ import { SdkHttpParameterProgrammer } from "./SdkHttpParameterProgrammer";
10
+ import { SdkImportWizard } from "./SdkImportWizard";
11
+
12
+ export namespace SdkHttpFunctionProgrammer {
13
+ export const write =
14
+ (project: INestiaProject) =>
15
+ (importer: ImportDictionary) =>
16
+ (route: ITypedHttpRoute): ts.FunctionDeclaration => {
17
+ return ts.factory.createFunctionDeclaration(
18
+ [
19
+ ts.factory.createModifier(ts.SyntaxKind.ExportKeyword),
20
+ ts.factory.createModifier(ts.SyntaxKind.AsyncKeyword),
21
+ ],
22
+ undefined,
23
+ route.name,
24
+ undefined,
25
+ [
26
+ IdentifierFactory.parameter(
27
+ "connection",
28
+ ts.factory.createTypeReferenceNode(
29
+ SdkImportWizard.IConnection(importer),
30
+ route.headerObject !== null
31
+ ? [ts.factory.createTypeReferenceNode(`${route.name}.Headers`)]
32
+ : undefined,
33
+ ),
34
+ ),
35
+ ...SdkHttpParameterProgrammer.getParameterDeclarations({
36
+ project,
37
+ importer,
38
+ route,
39
+ body: true,
40
+ prefix: true,
41
+ }),
42
+ ],
43
+ ts.factory.createTypeReferenceNode("Promise", [
44
+ project.config.propagate === true ||
45
+ route.success.metadata.size() !== 0
46
+ ? ts.factory.createTypeReferenceNode(`${route.name}.Output`)
47
+ : ts.factory.createTypeReferenceNode("void"),
48
+ ]),
49
+ ts.factory.createBlock(writeBody(project)(importer)(route), true),
50
+ );
51
+ };
52
+
53
+ const writeBody =
54
+ (project: INestiaProject) =>
55
+ (importer: ImportDictionary) =>
56
+ (route: ITypedHttpRoute): ts.Statement[] => {
57
+ const access = (name: string): ts.Expression =>
58
+ project.config.keyword === true
59
+ ? IdentifierFactory.access(ts.factory.createIdentifier("props"), name)
60
+ : ts.factory.createIdentifier(name);
61
+ const fetch = () =>
62
+ ts.factory.createCallExpression(
63
+ IdentifierFactory.access(
64
+ ts.factory.createIdentifier(
65
+ SdkImportWizard.Fetcher(
66
+ !!route.body?.encrypted || route.success.encrypted,
67
+ )(importer),
68
+ ),
69
+ project.config.propagate ? "propagate" : "fetch",
70
+ ),
71
+ project.config.propagate
72
+ ? route.method.toLowerCase() === "get" ||
73
+ route.method.toLowerCase() === "head"
74
+ ? [TypeFactory.keyword("any")]
75
+ : [TypeFactory.keyword("any"), TypeFactory.keyword("any")]
76
+ : undefined,
77
+ [
78
+ route.body && route.body.contentType !== "multipart/form-data"
79
+ ? ts.factory.createObjectLiteralExpression(
80
+ [
81
+ ts.factory.createSpreadAssignment(
82
+ ts.factory.createIdentifier("connection"),
83
+ ),
84
+ ts.factory.createPropertyAssignment(
85
+ "headers",
86
+ ts.factory.createObjectLiteralExpression(
87
+ [
88
+ ts.factory.createSpreadAssignment(
89
+ IdentifierFactory.access(
90
+ ts.factory.createIdentifier("connection"),
91
+ "headers",
92
+ ),
93
+ ),
94
+ ts.factory.createPropertyAssignment(
95
+ ts.factory.createStringLiteral("Content-Type"),
96
+ ts.factory.createStringLiteral(
97
+ route.body?.contentType ?? "application/json",
98
+ ),
99
+ ),
100
+ ],
101
+ true,
102
+ ),
103
+ ),
104
+ ],
105
+ true,
106
+ )
107
+ : ts.factory.createIdentifier("connection"),
108
+ ts.factory.createObjectLiteralExpression(
109
+ [
110
+ ts.factory.createSpreadAssignment(
111
+ IdentifierFactory.access(
112
+ ts.factory.createIdentifier(route.name),
113
+ "METADATA",
114
+ ),
115
+ ),
116
+ ts.factory.createPropertyAssignment(
117
+ "template",
118
+ IdentifierFactory.access(
119
+ IdentifierFactory.access(
120
+ ts.factory.createIdentifier(route.name),
121
+ "METADATA",
122
+ ),
123
+ "path",
124
+ ),
125
+ ),
126
+ ts.factory.createPropertyAssignment(
127
+ "path",
128
+ ts.factory.createCallExpression(
129
+ IdentifierFactory.access(
130
+ ts.factory.createIdentifier(route.name),
131
+ "path",
132
+ ),
133
+ undefined,
134
+ SdkHttpParameterProgrammer.getArguments({
135
+ project,
136
+ route,
137
+ body: false,
138
+ }),
139
+ ),
140
+ ),
141
+ ],
142
+ true,
143
+ ),
144
+ ...(route.body ? [access(route.body.name)] : []),
145
+ ...(project.config.json &&
146
+ route.body !== null &&
147
+ (route.body.contentType === "application/json" ||
148
+ route.body.encrypted === true)
149
+ ? [ts.factory.createIdentifier(`${route.name}.stringify`)]
150
+ : []),
151
+ ],
152
+ );
153
+ const output = (awaiter: boolean) =>
154
+ project.config.simulate
155
+ ? ts.factory.createConditionalExpression(
156
+ ts.factory.createStrictEquality(
157
+ ts.factory.createTrue(),
158
+ ts.factory.createIdentifier("connection.simulate"),
159
+ ),
160
+ undefined,
161
+ ts.factory.createCallExpression(
162
+ ts.factory.createIdentifier(`${route.name}.simulate`),
163
+ [],
164
+ [
165
+ ts.factory.createIdentifier("connection"),
166
+ ...SdkHttpParameterProgrammer.getArguments({
167
+ project,
168
+ route,
169
+ body: true,
170
+ }),
171
+ ],
172
+ ),
173
+ undefined,
174
+ awaiter ? ts.factory.createAwaitExpression(fetch()) : fetch(),
175
+ )
176
+ : awaiter
177
+ ? ts.factory.createAwaitExpression(fetch())
178
+ : fetch();
179
+ return [
180
+ ...(project.config.assert
181
+ ? SdkHttpParameterProgrammer.getSignificant(route, true).map((p) =>
182
+ ts.factory.createExpressionStatement(
183
+ ts.factory.createCallExpression(
184
+ IdentifierFactory.access(
185
+ ts.factory.createIdentifier(
186
+ SdkImportWizard.typia(importer),
187
+ ),
188
+ "assert",
189
+ ),
190
+ [
191
+ ts.factory.createTypeQueryNode(
192
+ ts.factory.createIdentifier(p.name),
193
+ ),
194
+ ],
195
+ [ts.factory.createIdentifier(p.name)],
196
+ ),
197
+ ),
198
+ )
199
+ : []),
200
+ ...(route.success.setHeaders.length === 0
201
+ ? [ts.factory.createReturnStatement(output(false))]
202
+ : writeSetHeaders(project)(importer)(route)(output(true))),
203
+ ];
204
+ };
205
+
206
+ const writeSetHeaders =
207
+ (project: INestiaProject) =>
208
+ (importer: ImportDictionary) =>
209
+ (route: ITypedHttpRoute) =>
210
+ (condition: ts.Expression): ts.Statement[] => {
211
+ const accessor = (x: string) => (y: string) =>
212
+ x[0] === "[" ? `${x}${y}` : `${x}.${y}`;
213
+ const output: string = StringUtil.escapeDuplicate([
214
+ "connection",
215
+ ...SdkHttpParameterProgrammer.getSignificant(route, true).map(
216
+ (p) => p.name,
217
+ ),
218
+ ])("output");
219
+ const headers: string = accessor("connection")("headers");
220
+ const data: string = project.config.propagate
221
+ ? accessor(output)("data")
222
+ : output;
223
+
224
+ const assigners: ts.ExpressionStatement[] = [
225
+ ts.factory.createBinaryExpression(
226
+ ts.factory.createIdentifier(headers),
227
+ ts.factory.createToken(ts.SyntaxKind.QuestionQuestionEqualsToken),
228
+ ts.factory.createObjectLiteralExpression([]),
229
+ ),
230
+ ...route.success.setHeaders.map((tuple) =>
231
+ tuple.type === "assigner"
232
+ ? ts.factory.createCallExpression(
233
+ ts.factory.createIdentifier("Object.assign"),
234
+ [],
235
+ [
236
+ ts.factory.createIdentifier(headers),
237
+ ts.factory.createIdentifier(accessor(data)(tuple.source)),
238
+ ],
239
+ )
240
+ : ts.factory.createBinaryExpression(
241
+ ts.factory.createIdentifier(
242
+ accessor(headers)(tuple.target ?? tuple.source),
243
+ ),
244
+ ts.factory.createToken(ts.SyntaxKind.EqualsToken),
245
+ ts.factory.createIdentifier(accessor(data)(tuple.source)),
246
+ ),
247
+ ),
248
+ ].map(ts.factory.createExpressionStatement);
249
+ return [
250
+ ts.factory.createVariableStatement(
251
+ [],
252
+ ts.factory.createVariableDeclarationList(
253
+ [
254
+ ts.factory.createVariableDeclaration(
255
+ output,
256
+ undefined,
257
+ SdkAliasCollection.response(project)(importer)(route),
258
+ condition,
259
+ ),
260
+ ],
261
+ ts.NodeFlags.Const,
262
+ ),
263
+ ),
264
+ ...(project.config.propagate
265
+ ? [
266
+ ts.factory.createIfStatement(
267
+ ts.factory.createIdentifier(accessor(output)("success")),
268
+ assigners.length === 1
269
+ ? assigners[0]!
270
+ : ts.factory.createBlock(assigners, true),
271
+ undefined,
272
+ ),
273
+ ]
274
+ : assigners),
275
+ ts.factory.createReturnStatement(ts.factory.createIdentifier(output)),
276
+ ];
277
+ };
278
+ }