@nestia/sdk 7.1.1-dev.20250714 → 7.2.0

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 (162) 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/analyses/DtoAnalyzer.d.ts +21 -0
  15. package/lib/analyses/DtoAnalyzer.js +208 -0
  16. package/lib/analyses/DtoAnalyzer.js.map +1 -0
  17. package/lib/analyses/ImportAnalyzer.d.ts +3 -8
  18. package/lib/analyses/ImportAnalyzer.js +84 -102
  19. package/lib/analyses/ImportAnalyzer.js.map +1 -1
  20. package/lib/analyses/ReflectHttpOperationAnalyzer.js +1 -1
  21. package/lib/analyses/ReflectHttpOperationAnalyzer.js.map +1 -1
  22. package/lib/analyses/ReflectWebSocketOperationAnalyzer.js +1 -1
  23. package/lib/analyses/ReflectWebSocketOperationAnalyzer.js.map +1 -1
  24. package/lib/executable/internal/NestiaConfigLoader.js +4 -4
  25. package/lib/executable/sdk.js +12 -12
  26. package/lib/generates/internal/E2eFileProgrammer.js +3 -9
  27. package/lib/generates/internal/E2eFileProgrammer.js.map +1 -1
  28. package/lib/generates/internal/ImportDictionary.d.ts +9 -11
  29. package/lib/generates/internal/ImportDictionary.js +55 -48
  30. package/lib/generates/internal/ImportDictionary.js.map +1 -1
  31. package/lib/generates/internal/SdkAliasCollection.js +24 -18
  32. package/lib/generates/internal/SdkAliasCollection.js.map +1 -1
  33. package/lib/generates/internal/SdkFileProgrammer.js +1 -7
  34. package/lib/generates/internal/SdkFileProgrammer.js.map +1 -1
  35. package/lib/generates/internal/SdkHttpCloneReferencer.js +3 -1
  36. package/lib/generates/internal/SdkHttpCloneReferencer.js.map +1 -1
  37. package/lib/generates/internal/SdkHttpSimulationProgrammer.js +4 -3
  38. package/lib/generates/internal/SdkHttpSimulationProgrammer.js.map +1 -1
  39. package/lib/generates/internal/SdkImportWizard.js +28 -21
  40. package/lib/generates/internal/SdkImportWizard.js.map +1 -1
  41. package/lib/generates/internal/SdkTypeProgrammer.js +3 -2
  42. package/lib/generates/internal/SdkTypeProgrammer.js.map +1 -1
  43. package/lib/generates/internal/SdkTypeTagProgrammer.js +10 -8
  44. package/lib/generates/internal/SdkTypeTagProgrammer.js.map +1 -1
  45. package/lib/generates/internal/SdkWebSocketNamespaceProgrammer.js +8 -6
  46. package/lib/generates/internal/SdkWebSocketNamespaceProgrammer.js.map +1 -1
  47. package/lib/generates/internal/SdkWebSocketRouteProgrammer.js +12 -9
  48. package/lib/generates/internal/SdkWebSocketRouteProgrammer.js.map +1 -1
  49. package/lib/structures/IReflectHttpOperation.d.ts +2 -2
  50. package/lib/structures/IReflectImport.d.ts +6 -0
  51. package/lib/structures/{IReflectTypeImport.js → IReflectImport.js} +1 -1
  52. package/lib/structures/IReflectImport.js.map +1 -0
  53. package/lib/structures/IReflectWebSocketOperation.d.ts +2 -2
  54. package/lib/structures/IReflectWebSocketOperationParameter.d.ts +2 -2
  55. package/lib/structures/ITypedHttpRoute.d.ts +2 -2
  56. package/lib/structures/ITypedWebSocketRoute.d.ts +2 -2
  57. package/lib/transformers/IOperationMetadata.d.ts +3 -3
  58. package/lib/transformers/SdkOperationProgrammer.d.ts +3 -1
  59. package/lib/transformers/SdkOperationProgrammer.js +57 -26
  60. package/lib/transformers/SdkOperationProgrammer.js.map +1 -1
  61. package/lib/transformers/SdkOperationTransformer.js +3 -3
  62. package/lib/transformers/SdkOperationTransformer.js.map +1 -1
  63. package/package.json +4 -4
  64. package/src/INestiaConfig.ts +269 -269
  65. package/src/NestiaSdkApplication.ts +307 -307
  66. package/src/NestiaSwaggerComposer.ts +138 -138
  67. package/src/analyses/AccessorAnalyzer.ts +67 -67
  68. package/src/analyses/ConfigAnalyzer.ts +155 -155
  69. package/src/analyses/DtoAnalyzer.ts +250 -0
  70. package/src/analyses/ExceptionAnalyzer.ts +154 -154
  71. package/src/analyses/GenericAnalyzer.ts +49 -49
  72. package/src/analyses/ImportAnalyzer.ts +126 -171
  73. package/src/analyses/PathAnalyzer.ts +69 -69
  74. package/src/analyses/ReflectControllerAnalyzer.ts +105 -105
  75. package/src/analyses/ReflectHttpOperationAnalyzer.ts +183 -183
  76. package/src/analyses/ReflectHttpOperationExceptionAnalyzer.ts +71 -71
  77. package/src/analyses/ReflectHttpOperationParameterAnalyzer.ts +348 -348
  78. package/src/analyses/ReflectHttpOperationResponseAnalyzer.ts +127 -127
  79. package/src/analyses/ReflectMetadataAnalyzer.ts +44 -44
  80. package/src/analyses/ReflectWebSocketOperationAnalyzer.ts +172 -172
  81. package/src/analyses/SecurityAnalyzer.ts +25 -25
  82. package/src/analyses/TypedHttpRouteAnalyzer.ts +204 -204
  83. package/src/analyses/TypedWebSocketRouteAnalyzer.ts +33 -33
  84. package/src/decorators/OperationMetadata.ts +15 -15
  85. package/src/executable/internal/CommandParser.ts +15 -15
  86. package/src/executable/internal/NestiaConfigLoader.ts +78 -78
  87. package/src/executable/internal/NestiaSdkCommand.ts +103 -103
  88. package/src/executable/sdk.ts +75 -75
  89. package/src/generates/CloneGenerator.ts +66 -66
  90. package/src/generates/E2eGenerator.ts +32 -32
  91. package/src/generates/SdkGenerator.ts +160 -160
  92. package/src/generates/SwaggerGenerator.ts +284 -284
  93. package/src/generates/internal/E2eFileProgrammer.ts +197 -205
  94. package/src/generates/internal/FilePrinter.ts +53 -53
  95. package/src/generates/internal/ImportDictionary.ts +190 -163
  96. package/src/generates/internal/SdkAliasCollection.ts +261 -255
  97. package/src/generates/internal/SdkDistributionComposer.ts +103 -103
  98. package/src/generates/internal/SdkFileProgrammer.ts +110 -116
  99. package/src/generates/internal/SdkHttpCloneProgrammer.ts +124 -124
  100. package/src/generates/internal/SdkHttpCloneReferencer.ts +77 -75
  101. package/src/generates/internal/SdkHttpFunctionProgrammer.ts +279 -279
  102. package/src/generates/internal/SdkHttpNamespaceProgrammer.ts +500 -500
  103. package/src/generates/internal/SdkHttpParameterProgrammer.ts +178 -178
  104. package/src/generates/internal/SdkHttpRouteProgrammer.ts +107 -107
  105. package/src/generates/internal/SdkHttpSimulationProgrammer.ts +310 -309
  106. package/src/generates/internal/SdkImportWizard.ts +62 -55
  107. package/src/generates/internal/SdkRouteDirectory.ts +18 -18
  108. package/src/generates/internal/SdkTypeProgrammer.ts +385 -384
  109. package/src/generates/internal/SdkTypeTagProgrammer.ts +104 -102
  110. package/src/generates/internal/SdkWebSocketNamespaceProgrammer.ts +368 -366
  111. package/src/generates/internal/SdkWebSocketParameterProgrammer.ts +87 -87
  112. package/src/generates/internal/SdkWebSocketRouteProgrammer.ts +282 -279
  113. package/src/generates/internal/SwaggerDescriptionComposer.ts +64 -64
  114. package/src/generates/internal/SwaggerOperationComposer.ts +119 -119
  115. package/src/generates/internal/SwaggerOperationParameterComposer.ts +177 -177
  116. package/src/generates/internal/SwaggerOperationResponseComposer.ts +110 -110
  117. package/src/index.ts +4 -4
  118. package/src/module.ts +3 -3
  119. package/src/structures/INestiaProject.ts +13 -13
  120. package/src/structures/INestiaSdkInput.ts +20 -20
  121. package/src/structures/IReflectApplication.ts +8 -8
  122. package/src/structures/IReflectController.ts +15 -15
  123. package/src/structures/IReflectHttpOperation.ts +26 -26
  124. package/src/structures/IReflectHttpOperationException.ts +19 -19
  125. package/src/structures/IReflectHttpOperationParameter.ts +77 -81
  126. package/src/structures/IReflectHttpOperationSuccess.ts +22 -22
  127. package/src/structures/IReflectImport.ts +6 -0
  128. package/src/structures/IReflectOperationError.ts +26 -26
  129. package/src/structures/IReflectType.ts +4 -4
  130. package/src/structures/IReflectWebSocketOperation.ts +17 -17
  131. package/src/structures/IReflectWebSocketOperationParameter.ts +36 -38
  132. package/src/structures/ITypedApplication.ts +11 -11
  133. package/src/structures/ITypedHttpRoute.ts +41 -41
  134. package/src/structures/ITypedHttpRouteException.ts +15 -15
  135. package/src/structures/ITypedHttpRouteParameter.ts +41 -41
  136. package/src/structures/ITypedHttpRouteSuccess.ts +22 -22
  137. package/src/structures/ITypedWebSocketRoute.ts +24 -24
  138. package/src/structures/ITypedWebSocketRouteParameter.ts +3 -3
  139. package/src/structures/MethodType.ts +5 -5
  140. package/src/structures/ParamCategory.ts +1 -1
  141. package/src/structures/TypeEntry.ts +22 -22
  142. package/src/transform.ts +9 -9
  143. package/src/transformers/IOperationMetadata.ts +44 -44
  144. package/src/transformers/ISdkOperationTransformerContext.ts +8 -8
  145. package/src/transformers/SdkOperationProgrammer.ts +238 -209
  146. package/src/transformers/SdkOperationTransformer.ts +252 -253
  147. package/src/transformers/TextPlainValidator.ts +17 -17
  148. package/src/typings/get-function-location.d.ts +7 -7
  149. package/src/utils/ArrayUtil.ts +26 -26
  150. package/src/utils/FileRetriever.ts +22 -22
  151. package/src/utils/MapUtil.ts +14 -14
  152. package/src/utils/MetadataUtil.ts +26 -26
  153. package/src/utils/PathUtil.ts +10 -10
  154. package/src/utils/SourceFinder.ts +66 -66
  155. package/src/utils/StringUtil.ts +17 -17
  156. package/src/utils/StripEnums.ts +5 -5
  157. package/src/utils/VersioningStrategy.ts +28 -28
  158. package/src/validators/HttpHeadersValidator.ts +34 -34
  159. package/src/validators/HttpQueryValidator.ts +34 -34
  160. package/lib/structures/IReflectTypeImport.d.ts +0 -4
  161. package/lib/structures/IReflectTypeImport.js.map +0 -1
  162. package/src/structures/IReflectTypeImport.ts +0 -4
@@ -1,279 +1,282 @@
1
- import ts from "typescript";
2
- import { IJsDocTagInfo } from "typia";
3
- import { IdentifierFactory } from "typia/lib/factories/IdentifierFactory";
4
-
5
- import { INestiaProject } from "../../structures/INestiaProject";
6
- import { ITypedWebSocketRoute } from "../../structures/ITypedWebSocketRoute";
7
- import { FilePrinter } from "./FilePrinter";
8
- import { ImportDictionary } from "./ImportDictionary";
9
- import { SdkImportWizard } from "./SdkImportWizard";
10
- import { SdkWebSocketNamespaceProgrammer } from "./SdkWebSocketNamespaceProgrammer";
11
- import { SdkWebSocketParameterProgrammer } from "./SdkWebSocketParameterProgrammer";
12
-
13
- export namespace SdkWebSocketRouteProgrammer {
14
- export const write =
15
- (project: INestiaProject) =>
16
- (importer: ImportDictionary) =>
17
- (route: ITypedWebSocketRoute): ts.Statement[] => [
18
- FilePrinter.description(
19
- writeFunction(project)(importer)(route),
20
- writeDescription(route),
21
- ),
22
- SdkWebSocketNamespaceProgrammer.write(project)(importer)(route),
23
- ];
24
-
25
- const writeDescription = (route: ITypedWebSocketRoute): string => {
26
- // MAIN DESCRIPTION
27
- const comments: string[] = route.description
28
- ? route.description.split("\n")
29
- : [];
30
-
31
- // COMMENT TAGS
32
- const tags: IJsDocTagInfo[] = route.jsDocTags.filter(
33
- (tag) =>
34
- tag.name !== "param" ||
35
- [...route.pathParameters, ...(route.query ? [route.query] : [])]
36
- .filter((p) => p.category === "param" || p.category === "query")
37
- .some((p) => p.name === tag.text?.[0]?.text),
38
- );
39
- if (tags.length !== 0) {
40
- const content: string[] = tags.map((t) =>
41
- t.text?.length
42
- ? `@${t.name} ${t.text.map((e) => e.text).join("")}`
43
- : `@${t.name}`,
44
- );
45
- comments.push("", ...new Set(content));
46
- }
47
-
48
- // POSTFIX
49
- if (!!comments.length) comments.push("");
50
- comments.push(
51
- `@controller ${route.controller.class.name}.${route.name}`,
52
- `@path ${route.path}`,
53
- `@nestia Generated by Nestia - https://github.com/samchon/nestia`,
54
- );
55
- return comments.join("\n");
56
- };
57
-
58
- const writeFunction =
59
- (project: INestiaProject) =>
60
- (importer: ImportDictionary) =>
61
- (route: ITypedWebSocketRoute): ts.FunctionDeclaration => {
62
- return ts.factory.createFunctionDeclaration(
63
- [
64
- ts.factory.createModifier(ts.SyntaxKind.ExportKeyword),
65
- ts.factory.createModifier(ts.SyntaxKind.AsyncKeyword),
66
- ],
67
- undefined,
68
- route.name,
69
- undefined,
70
- [
71
- IdentifierFactory.parameter(
72
- "connection",
73
- ts.factory.createTypeReferenceNode(
74
- SdkImportWizard.IConnection(importer),
75
- [ts.factory.createTypeReferenceNode(`${route.name}.Header`)],
76
- ),
77
- ),
78
- ...SdkWebSocketParameterProgrammer.getParameterDeclarations({
79
- project,
80
- route,
81
- provider: true,
82
- prefix: true,
83
- }),
84
- ],
85
- ts.factory.createTypeReferenceNode("Promise", [
86
- ts.factory.createTypeReferenceNode(`${route.name}.Output`),
87
- ]),
88
- ts.factory.createBlock(
89
- writeFunctionBody(project)(importer)(route),
90
- true,
91
- ),
92
- );
93
- };
94
-
95
- const writeFunctionBody =
96
- (project: INestiaProject) =>
97
- (importer: ImportDictionary) =>
98
- (route: ITypedWebSocketRoute): ts.Statement[] => {
99
- const access = (key: string) =>
100
- project.config.keyword === true
101
- ? ts.factory.createPropertyAccessExpression(
102
- ts.factory.createIdentifier("props"),
103
- key,
104
- )
105
- : ts.factory.createIdentifier(key);
106
- return [
107
- local("connector")(
108
- ts.factory.createTypeReferenceNode(
109
- importer.external({
110
- type: false,
111
- library: "tgrid",
112
- instance: "WebSocketConnector",
113
- }),
114
- [
115
- ts.factory.createTypeReferenceNode(`${route.name}.Header`),
116
- ts.factory.createTypeReferenceNode(`${route.name}.Provider`),
117
- ts.factory.createTypeReferenceNode(`${route.name}.Listener`),
118
- ],
119
- ),
120
- )(
121
- ts.factory.createNewExpression(
122
- ts.factory.createIdentifier(
123
- importer.external({
124
- type: false,
125
- library: "tgrid",
126
- instance: "WebSocketConnector",
127
- }),
128
- ),
129
- undefined,
130
- [
131
- ts.factory.createAsExpression(
132
- ts.factory.createBinaryExpression(
133
- ts.factory.createPropertyAccessExpression(
134
- ts.factory.createIdentifier("connection"),
135
- "headers",
136
- ),
137
- ts.factory.createToken(ts.SyntaxKind.QuestionQuestionToken),
138
- ts.factory.createObjectLiteralExpression([], false),
139
- ),
140
- ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword),
141
- ),
142
- access("provider"),
143
- ],
144
- ),
145
- ),
146
- ts.factory.createExpressionStatement(
147
- ts.factory.createAwaitExpression(
148
- ts.factory.createCallExpression(
149
- ts.factory.createPropertyAccessExpression(
150
- ts.factory.createIdentifier("connector"),
151
- "connect",
152
- ),
153
- undefined,
154
- [
155
- joinPath(
156
- ts.factory.createCallExpression(
157
- ts.factory.createPropertyAccessExpression(
158
- ts.factory.createIdentifier(route.name),
159
- "path",
160
- ),
161
- [],
162
- project.config.keyword === true &&
163
- SdkWebSocketParameterProgrammer.isPathEmpty(route) ===
164
- false
165
- ? [ts.factory.createIdentifier("props")]
166
- : SdkWebSocketParameterProgrammer.getEntries({
167
- project,
168
- route,
169
- provider: false,
170
- prefix: true,
171
- }).map((p) => ts.factory.createIdentifier(p.key)),
172
- ),
173
- ),
174
- ],
175
- ),
176
- ),
177
- ),
178
- local("driver")(
179
- ts.factory.createTypeReferenceNode(
180
- importer.external({
181
- type: true,
182
- library: "tgrid",
183
- instance: "Driver",
184
- }),
185
- [ts.factory.createTypeReferenceNode(`${route.name}.Listener`)],
186
- ),
187
- )(
188
- ts.factory.createCallExpression(
189
- ts.factory.createPropertyAccessExpression(
190
- ts.factory.createIdentifier("connector"),
191
- "getDriver",
192
- ),
193
- undefined,
194
- undefined,
195
- ),
196
- ),
197
- ts.factory.createReturnStatement(
198
- ts.factory.createObjectLiteralExpression(
199
- [
200
- ts.factory.createShorthandPropertyAssignment("connector"),
201
- ts.factory.createShorthandPropertyAssignment("driver"),
202
- ],
203
- true,
204
- ),
205
- ),
206
- ];
207
- };
208
- }
209
-
210
- const local =
211
- (name: string) => (type: ts.TypeNode) => (expression: ts.Expression) =>
212
- ts.factory.createVariableStatement(
213
- [],
214
- ts.factory.createVariableDeclarationList(
215
- [
216
- ts.factory.createVariableDeclaration(
217
- name,
218
- undefined,
219
- type,
220
- expression,
221
- ),
222
- ],
223
- ts.NodeFlags.Const,
224
- ),
225
- );
226
-
227
- const joinPath = (caller: ts.Expression) =>
228
- ts.factory.createTemplateExpression(ts.factory.createTemplateHead("", ""), [
229
- ts.factory.createTemplateSpan(
230
- ts.factory.createConditionalExpression(
231
- ts.factory.createCallExpression(
232
- ts.factory.createPropertyAccessExpression(
233
- ts.factory.createPropertyAccessExpression(
234
- ts.factory.createIdentifier("connection"),
235
- ts.factory.createIdentifier("host"),
236
- ),
237
- ts.factory.createIdentifier("endsWith"),
238
- ),
239
- undefined,
240
- [ts.factory.createStringLiteral("/")],
241
- ),
242
- ts.factory.createToken(ts.SyntaxKind.QuestionToken),
243
- ts.factory.createCallExpression(
244
- ts.factory.createPropertyAccessExpression(
245
- ts.factory.createPropertyAccessExpression(
246
- ts.factory.createIdentifier("connection"),
247
- ts.factory.createIdentifier("host"),
248
- ),
249
- ts.factory.createIdentifier("substring"),
250
- ),
251
- undefined,
252
- [
253
- ts.factory.createNumericLiteral("0"),
254
- ts.factory.createBinaryExpression(
255
- ts.factory.createPropertyAccessExpression(
256
- ts.factory.createPropertyAccessExpression(
257
- ts.factory.createIdentifier("connection"),
258
- ts.factory.createIdentifier("host"),
259
- ),
260
- ts.factory.createIdentifier("length"),
261
- ),
262
- ts.factory.createToken(ts.SyntaxKind.MinusToken),
263
- ts.factory.createNumericLiteral("1"),
264
- ),
265
- ],
266
- ),
267
- ts.factory.createToken(ts.SyntaxKind.ColonToken),
268
- ts.factory.createPropertyAccessExpression(
269
- ts.factory.createIdentifier("connection"),
270
- ts.factory.createIdentifier("host"),
271
- ),
272
- ),
273
- ts.factory.createTemplateMiddle("", ""),
274
- ),
275
- ts.factory.createTemplateSpan(
276
- caller,
277
- ts.factory.createTemplateTail("", ""),
278
- ),
279
- ]);
1
+ import ts from "typescript";
2
+ import { IJsDocTagInfo } from "typia";
3
+ import { IdentifierFactory } from "typia/lib/factories/IdentifierFactory";
4
+
5
+ import { INestiaProject } from "../../structures/INestiaProject";
6
+ import { ITypedWebSocketRoute } from "../../structures/ITypedWebSocketRoute";
7
+ import { FilePrinter } from "./FilePrinter";
8
+ import { ImportDictionary } from "./ImportDictionary";
9
+ import { SdkImportWizard } from "./SdkImportWizard";
10
+ import { SdkWebSocketNamespaceProgrammer } from "./SdkWebSocketNamespaceProgrammer";
11
+ import { SdkWebSocketParameterProgrammer } from "./SdkWebSocketParameterProgrammer";
12
+
13
+ export namespace SdkWebSocketRouteProgrammer {
14
+ export const write =
15
+ (project: INestiaProject) =>
16
+ (importer: ImportDictionary) =>
17
+ (route: ITypedWebSocketRoute): ts.Statement[] => [
18
+ FilePrinter.description(
19
+ writeFunction(project)(importer)(route),
20
+ writeDescription(route),
21
+ ),
22
+ SdkWebSocketNamespaceProgrammer.write(project)(importer)(route),
23
+ ];
24
+
25
+ const writeDescription = (route: ITypedWebSocketRoute): string => {
26
+ // MAIN DESCRIPTION
27
+ const comments: string[] = route.description
28
+ ? route.description.split("\n")
29
+ : [];
30
+
31
+ // COMMENT TAGS
32
+ const tags: IJsDocTagInfo[] = route.jsDocTags.filter(
33
+ (tag) =>
34
+ tag.name !== "param" ||
35
+ [...route.pathParameters, ...(route.query ? [route.query] : [])]
36
+ .filter((p) => p.category === "param" || p.category === "query")
37
+ .some((p) => p.name === tag.text?.[0]?.text),
38
+ );
39
+ if (tags.length !== 0) {
40
+ const content: string[] = tags.map((t) =>
41
+ t.text?.length
42
+ ? `@${t.name} ${t.text.map((e) => e.text).join("")}`
43
+ : `@${t.name}`,
44
+ );
45
+ comments.push("", ...new Set(content));
46
+ }
47
+
48
+ // POSTFIX
49
+ if (!!comments.length) comments.push("");
50
+ comments.push(
51
+ `@controller ${route.controller.class.name}.${route.name}`,
52
+ `@path ${route.path}`,
53
+ `@nestia Generated by Nestia - https://github.com/samchon/nestia`,
54
+ );
55
+ return comments.join("\n");
56
+ };
57
+
58
+ const writeFunction =
59
+ (project: INestiaProject) =>
60
+ (importer: ImportDictionary) =>
61
+ (route: ITypedWebSocketRoute): ts.FunctionDeclaration => {
62
+ return ts.factory.createFunctionDeclaration(
63
+ [
64
+ ts.factory.createModifier(ts.SyntaxKind.ExportKeyword),
65
+ ts.factory.createModifier(ts.SyntaxKind.AsyncKeyword),
66
+ ],
67
+ undefined,
68
+ route.name,
69
+ undefined,
70
+ [
71
+ IdentifierFactory.parameter(
72
+ "connection",
73
+ ts.factory.createTypeReferenceNode(
74
+ SdkImportWizard.IConnection(importer),
75
+ [ts.factory.createTypeReferenceNode(`${route.name}.Header`)],
76
+ ),
77
+ ),
78
+ ...SdkWebSocketParameterProgrammer.getParameterDeclarations({
79
+ project,
80
+ route,
81
+ provider: true,
82
+ prefix: true,
83
+ }),
84
+ ],
85
+ ts.factory.createTypeReferenceNode("Promise", [
86
+ ts.factory.createTypeReferenceNode(`${route.name}.Output`),
87
+ ]),
88
+ ts.factory.createBlock(
89
+ writeFunctionBody(project)(importer)(route),
90
+ true,
91
+ ),
92
+ );
93
+ };
94
+
95
+ const writeFunctionBody =
96
+ (project: INestiaProject) =>
97
+ (importer: ImportDictionary) =>
98
+ (route: ITypedWebSocketRoute): ts.Statement[] => {
99
+ const access = (key: string) =>
100
+ project.config.keyword === true
101
+ ? ts.factory.createPropertyAccessExpression(
102
+ ts.factory.createIdentifier("props"),
103
+ key,
104
+ )
105
+ : ts.factory.createIdentifier(key);
106
+ return [
107
+ local("connector")(
108
+ ts.factory.createTypeReferenceNode(
109
+ importer.external({
110
+ declaration: false,
111
+ file: "tgrid",
112
+ type: "element",
113
+ name: "WebSocketConnector",
114
+ }),
115
+ [
116
+ ts.factory.createTypeReferenceNode(`${route.name}.Header`),
117
+ ts.factory.createTypeReferenceNode(`${route.name}.Provider`),
118
+ ts.factory.createTypeReferenceNode(`${route.name}.Listener`),
119
+ ],
120
+ ),
121
+ )(
122
+ ts.factory.createNewExpression(
123
+ ts.factory.createIdentifier(
124
+ importer.external({
125
+ declaration: false,
126
+ file: "tgrid",
127
+ type: "element",
128
+ name: "WebSocketConnector",
129
+ }),
130
+ ),
131
+ undefined,
132
+ [
133
+ ts.factory.createAsExpression(
134
+ ts.factory.createBinaryExpression(
135
+ ts.factory.createPropertyAccessExpression(
136
+ ts.factory.createIdentifier("connection"),
137
+ "headers",
138
+ ),
139
+ ts.factory.createToken(ts.SyntaxKind.QuestionQuestionToken),
140
+ ts.factory.createObjectLiteralExpression([], false),
141
+ ),
142
+ ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword),
143
+ ),
144
+ access("provider"),
145
+ ],
146
+ ),
147
+ ),
148
+ ts.factory.createExpressionStatement(
149
+ ts.factory.createAwaitExpression(
150
+ ts.factory.createCallExpression(
151
+ ts.factory.createPropertyAccessExpression(
152
+ ts.factory.createIdentifier("connector"),
153
+ "connect",
154
+ ),
155
+ undefined,
156
+ [
157
+ joinPath(
158
+ ts.factory.createCallExpression(
159
+ ts.factory.createPropertyAccessExpression(
160
+ ts.factory.createIdentifier(route.name),
161
+ "path",
162
+ ),
163
+ [],
164
+ project.config.keyword === true &&
165
+ SdkWebSocketParameterProgrammer.isPathEmpty(route) ===
166
+ false
167
+ ? [ts.factory.createIdentifier("props")]
168
+ : SdkWebSocketParameterProgrammer.getEntries({
169
+ project,
170
+ route,
171
+ provider: false,
172
+ prefix: true,
173
+ }).map((p) => ts.factory.createIdentifier(p.key)),
174
+ ),
175
+ ),
176
+ ],
177
+ ),
178
+ ),
179
+ ),
180
+ local("driver")(
181
+ ts.factory.createTypeReferenceNode(
182
+ importer.external({
183
+ declaration: true,
184
+ file: "tgrid",
185
+ type: "element",
186
+ name: "Driver",
187
+ }),
188
+ [ts.factory.createTypeReferenceNode(`${route.name}.Listener`)],
189
+ ),
190
+ )(
191
+ ts.factory.createCallExpression(
192
+ ts.factory.createPropertyAccessExpression(
193
+ ts.factory.createIdentifier("connector"),
194
+ "getDriver",
195
+ ),
196
+ undefined,
197
+ undefined,
198
+ ),
199
+ ),
200
+ ts.factory.createReturnStatement(
201
+ ts.factory.createObjectLiteralExpression(
202
+ [
203
+ ts.factory.createShorthandPropertyAssignment("connector"),
204
+ ts.factory.createShorthandPropertyAssignment("driver"),
205
+ ],
206
+ true,
207
+ ),
208
+ ),
209
+ ];
210
+ };
211
+ }
212
+
213
+ const local =
214
+ (name: string) => (type: ts.TypeNode) => (expression: ts.Expression) =>
215
+ ts.factory.createVariableStatement(
216
+ [],
217
+ ts.factory.createVariableDeclarationList(
218
+ [
219
+ ts.factory.createVariableDeclaration(
220
+ name,
221
+ undefined,
222
+ type,
223
+ expression,
224
+ ),
225
+ ],
226
+ ts.NodeFlags.Const,
227
+ ),
228
+ );
229
+
230
+ const joinPath = (caller: ts.Expression) =>
231
+ ts.factory.createTemplateExpression(ts.factory.createTemplateHead("", ""), [
232
+ ts.factory.createTemplateSpan(
233
+ ts.factory.createConditionalExpression(
234
+ ts.factory.createCallExpression(
235
+ ts.factory.createPropertyAccessExpression(
236
+ ts.factory.createPropertyAccessExpression(
237
+ ts.factory.createIdentifier("connection"),
238
+ ts.factory.createIdentifier("host"),
239
+ ),
240
+ ts.factory.createIdentifier("endsWith"),
241
+ ),
242
+ undefined,
243
+ [ts.factory.createStringLiteral("/")],
244
+ ),
245
+ ts.factory.createToken(ts.SyntaxKind.QuestionToken),
246
+ ts.factory.createCallExpression(
247
+ ts.factory.createPropertyAccessExpression(
248
+ ts.factory.createPropertyAccessExpression(
249
+ ts.factory.createIdentifier("connection"),
250
+ ts.factory.createIdentifier("host"),
251
+ ),
252
+ ts.factory.createIdentifier("substring"),
253
+ ),
254
+ undefined,
255
+ [
256
+ ts.factory.createNumericLiteral("0"),
257
+ ts.factory.createBinaryExpression(
258
+ ts.factory.createPropertyAccessExpression(
259
+ ts.factory.createPropertyAccessExpression(
260
+ ts.factory.createIdentifier("connection"),
261
+ ts.factory.createIdentifier("host"),
262
+ ),
263
+ ts.factory.createIdentifier("length"),
264
+ ),
265
+ ts.factory.createToken(ts.SyntaxKind.MinusToken),
266
+ ts.factory.createNumericLiteral("1"),
267
+ ),
268
+ ],
269
+ ),
270
+ ts.factory.createToken(ts.SyntaxKind.ColonToken),
271
+ ts.factory.createPropertyAccessExpression(
272
+ ts.factory.createIdentifier("connection"),
273
+ ts.factory.createIdentifier("host"),
274
+ ),
275
+ ),
276
+ ts.factory.createTemplateMiddle("", ""),
277
+ ),
278
+ ts.factory.createTemplateSpan(
279
+ caller,
280
+ ts.factory.createTemplateTail("", ""),
281
+ ),
282
+ ]);