@nestia/sdk 13.0.4 → 14.0.1

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 (186) hide show
  1. package/README.md +1 -1
  2. package/assets/bundle/distribute/package.json +2 -2
  3. package/assets/bundle/distribute/tsconfig.json +15 -105
  4. package/lib/INestiaConfig.d.ts +19 -10
  5. package/lib/NestiaSdkApplication.js +58 -27
  6. package/lib/NestiaSdkApplication.js.map +1 -1
  7. package/lib/NestiaSwaggerComposer.js +11 -10
  8. package/lib/NestiaSwaggerComposer.js.map +1 -1
  9. package/lib/analyses/ConfigAnalyzer.js +4 -4
  10. package/lib/analyses/ConfigAnalyzer.js.map +1 -1
  11. package/lib/analyses/ParameterNameAnalyzer.d.ts +20 -0
  12. package/lib/analyses/ParameterNameAnalyzer.js +48 -0
  13. package/lib/analyses/ParameterNameAnalyzer.js.map +1 -0
  14. package/lib/analyses/PathAnalyzer.d.ts +30 -0
  15. package/lib/analyses/PathAnalyzer.js +108 -16
  16. package/lib/analyses/PathAnalyzer.js.map +1 -1
  17. package/lib/analyses/ReflectControllerAnalyzer.js +2 -1
  18. package/lib/analyses/ReflectControllerAnalyzer.js.map +1 -1
  19. package/lib/analyses/ReflectHttpOperationAnalyzer.js +7 -7
  20. package/lib/analyses/ReflectHttpOperationAnalyzer.js.map +1 -1
  21. package/lib/analyses/ReflectHttpOperationParameterAnalyzer.js +122 -23
  22. package/lib/analyses/ReflectHttpOperationParameterAnalyzer.js.map +1 -1
  23. package/lib/analyses/ReflectHttpOperationResponseAnalyzer.js +5 -2
  24. package/lib/analyses/ReflectHttpOperationResponseAnalyzer.js.map +1 -1
  25. package/lib/analyses/ReflectMcpOperationAnalyzer.js +5 -2
  26. package/lib/analyses/ReflectMcpOperationAnalyzer.js.map +1 -1
  27. package/lib/analyses/ReflectWebSocketOperationAnalyzer.js +6 -3
  28. package/lib/analyses/ReflectWebSocketOperationAnalyzer.js.map +1 -1
  29. package/lib/analyses/SecurityAnalyzer.d.ts +10 -0
  30. package/lib/analyses/SecurityAnalyzer.js +24 -19
  31. package/lib/analyses/SecurityAnalyzer.js.map +1 -1
  32. package/lib/analyses/SwaggerExampleAnalyzer.d.ts +15 -0
  33. package/lib/analyses/SwaggerExampleAnalyzer.js +20 -0
  34. package/lib/analyses/SwaggerExampleAnalyzer.js.map +1 -0
  35. package/lib/analyses/TypedHttpRouteAnalyzer.js +30 -21
  36. package/lib/analyses/TypedHttpRouteAnalyzer.js.map +1 -1
  37. package/lib/analyses/TypedWebSocketRouteAnalyzer.js +1 -1
  38. package/lib/analyses/TypedWebSocketRouteAnalyzer.js.map +1 -1
  39. package/lib/executable/internal/NestiaSdkWatcher.js +3 -3
  40. package/lib/executable/internal/NestiaSdkWatcher.js.map +1 -1
  41. package/lib/executable/sdk.js +16 -1
  42. package/lib/executable/sdk.js.map +1 -1
  43. package/lib/factories/ExpressionFactory.d.ts +3 -0
  44. package/lib/factories/ExpressionFactory.js +6 -2
  45. package/lib/factories/ExpressionFactory.js.map +1 -1
  46. package/lib/factories/LiteralFactory.js +3 -1
  47. package/lib/factories/LiteralFactory.js.map +1 -1
  48. package/lib/generates/SdkGenerator.js +3 -3
  49. package/lib/generates/SdkGenerator.js.map +1 -1
  50. package/lib/generates/SwaggerGenerator.js +135 -27
  51. package/lib/generates/SwaggerGenerator.js.map +1 -1
  52. package/lib/generates/internal/FilePrinter.d.ts +14 -0
  53. package/lib/generates/internal/FilePrinter.js +31 -0
  54. package/lib/generates/internal/FilePrinter.js.map +1 -1
  55. package/lib/generates/internal/ImportDictionary.d.ts +2 -0
  56. package/lib/generates/internal/ImportDictionary.js +13 -0
  57. package/lib/generates/internal/ImportDictionary.js.map +1 -1
  58. package/lib/generates/internal/SdkAliasCollection.d.ts +2 -1
  59. package/lib/generates/internal/SdkAliasCollection.js +8 -4
  60. package/lib/generates/internal/SdkAliasCollection.js.map +1 -1
  61. package/lib/generates/internal/SdkDistributionComposer.js +72 -14
  62. package/lib/generates/internal/SdkDistributionComposer.js.map +1 -1
  63. package/lib/generates/internal/SdkFileProgrammer.js +83 -12
  64. package/lib/generates/internal/SdkFileProgrammer.js.map +1 -1
  65. package/lib/generates/internal/SdkHttpFunctionProgrammer.js +21 -23
  66. package/lib/generates/internal/SdkHttpFunctionProgrammer.js.map +1 -1
  67. package/lib/generates/internal/SdkHttpNamespaceProgrammer.js +27 -34
  68. package/lib/generates/internal/SdkHttpNamespaceProgrammer.js.map +1 -1
  69. package/lib/generates/internal/SdkHttpParameterProgrammer.d.ts +40 -1
  70. package/lib/generates/internal/SdkHttpParameterProgrammer.js +105 -6
  71. package/lib/generates/internal/SdkHttpParameterProgrammer.js.map +1 -1
  72. package/lib/generates/internal/SdkHttpRouteProgrammer.js +16 -32
  73. package/lib/generates/internal/SdkHttpRouteProgrammer.js.map +1 -1
  74. package/lib/generates/internal/SdkHttpSimulationProgrammer.js +18 -14
  75. package/lib/generates/internal/SdkHttpSimulationProgrammer.js.map +1 -1
  76. package/lib/generates/internal/SdkMcpRouteProgrammer.js +39 -11
  77. package/lib/generates/internal/SdkMcpRouteProgrammer.js.map +1 -1
  78. package/lib/generates/internal/SdkPathTemplate.d.ts +21 -0
  79. package/lib/generates/internal/SdkPathTemplate.js +50 -0
  80. package/lib/generates/internal/SdkPathTemplate.js.map +1 -0
  81. package/lib/generates/internal/SdkTypeProgrammer.js +15 -15
  82. package/lib/generates/internal/SdkTypeProgrammer.js.map +1 -1
  83. package/lib/generates/internal/SdkTypeTagProgrammer.d.ts +20 -1
  84. package/lib/generates/internal/SdkTypeTagProgrammer.js +386 -67
  85. package/lib/generates/internal/SdkTypeTagProgrammer.js.map +1 -1
  86. package/lib/generates/internal/SdkWebSocketNamespaceProgrammer.js +27 -36
  87. package/lib/generates/internal/SdkWebSocketNamespaceProgrammer.js.map +1 -1
  88. package/lib/generates/internal/SdkWebSocketParameterProgrammer.d.ts +35 -1
  89. package/lib/generates/internal/SdkWebSocketParameterProgrammer.js +72 -5
  90. package/lib/generates/internal/SdkWebSocketParameterProgrammer.js.map +1 -1
  91. package/lib/generates/internal/SdkWebSocketRouteProgrammer.js +57 -33
  92. package/lib/generates/internal/SdkWebSocketRouteProgrammer.js.map +1 -1
  93. package/lib/generates/internal/SwaggerOperationComposer.js +19 -8
  94. package/lib/generates/internal/SwaggerOperationComposer.js.map +1 -1
  95. package/lib/generates/internal/SwaggerOperationParameterComposer.d.ts +1 -1
  96. package/lib/generates/internal/SwaggerOperationParameterComposer.js +201 -19
  97. package/lib/generates/internal/SwaggerOperationParameterComposer.js.map +1 -1
  98. package/lib/generates/internal/SwaggerOperationResponseComposer.d.ts +2 -0
  99. package/lib/generates/internal/SwaggerOperationResponseComposer.js +39 -7
  100. package/lib/generates/internal/SwaggerOperationResponseComposer.js.map +1 -1
  101. package/lib/internal/legacy.d.ts +49 -18
  102. package/lib/internal/legacy.js +84 -25
  103. package/lib/internal/legacy.js.map +1 -1
  104. package/lib/structures/IOperationMetadata.d.ts +22 -0
  105. package/lib/structures/IReflectHttpOperationException.d.ts +3 -2
  106. package/lib/structures/IReflectHttpOperationParameter.d.ts +8 -3
  107. package/lib/structures/IReflectHttpOperationSuccess.d.ts +4 -3
  108. package/lib/structures/ITypedHttpRoute.d.ts +8 -0
  109. package/lib/structures/ITypedHttpRouteException.d.ts +3 -1
  110. package/lib/structures/ITypedHttpRouteParameter.d.ts +3 -1
  111. package/lib/structures/ITypedHttpRouteSuccess.d.ts +3 -1
  112. package/lib/structures/ITypedWebSocketRoute.d.ts +8 -0
  113. package/lib/utils/SourceFinder.d.ts +10 -0
  114. package/lib/utils/SourceFinder.js +27 -4
  115. package/lib/utils/SourceFinder.js.map +1 -1
  116. package/lib/utils/VersioningStrategy.d.ts +13 -1
  117. package/lib/utils/VersioningStrategy.js +24 -12
  118. package/lib/utils/VersioningStrategy.js.map +1 -1
  119. package/native/go.mod +1 -1
  120. package/native/go.sum +2 -2
  121. package/native/sdk/sdk_http_rules.go +210 -0
  122. package/native/sdk/sdk_metadata_json.go +73 -3
  123. package/native/sdk/sdk_transform.go +801 -49
  124. package/package.json +8 -8
  125. package/src/INestiaConfig.ts +19 -10
  126. package/src/NestiaSdkApplication.ts +63 -34
  127. package/src/NestiaSwaggerComposer.ts +11 -8
  128. package/src/analyses/ConfigAnalyzer.ts +3 -3
  129. package/src/analyses/ParameterNameAnalyzer.ts +51 -0
  130. package/src/analyses/PathAnalyzer.ts +106 -14
  131. package/src/analyses/ReflectControllerAnalyzer.ts +2 -1
  132. package/src/analyses/ReflectHttpOperationAnalyzer.ts +4 -4
  133. package/src/analyses/ReflectHttpOperationParameterAnalyzer.ts +122 -39
  134. package/src/analyses/ReflectHttpOperationResponseAnalyzer.ts +6 -7
  135. package/src/analyses/ReflectMcpOperationAnalyzer.ts +6 -2
  136. package/src/analyses/ReflectWebSocketOperationAnalyzer.ts +7 -3
  137. package/src/analyses/SecurityAnalyzer.ts +30 -20
  138. package/src/analyses/SwaggerExampleAnalyzer.ts +23 -0
  139. package/src/analyses/TypedHttpRouteAnalyzer.ts +29 -20
  140. package/src/analyses/TypedWebSocketRouteAnalyzer.ts +1 -0
  141. package/src/executable/internal/NestiaSdkWatcher.ts +2 -2
  142. package/src/executable/sdk.ts +24 -1
  143. package/src/factories/ExpressionFactory.ts +8 -2
  144. package/src/factories/LiteralFactory.ts +7 -2
  145. package/src/generates/SdkGenerator.ts +3 -3
  146. package/src/generates/SwaggerGenerator.ts +173 -32
  147. package/src/generates/internal/FilePrinter.ts +28 -0
  148. package/src/generates/internal/ImportDictionary.ts +11 -0
  149. package/src/generates/internal/SdkAliasCollection.ts +11 -4
  150. package/src/generates/internal/SdkDistributionComposer.ts +89 -15
  151. package/src/generates/internal/SdkFileProgrammer.ts +108 -16
  152. package/src/generates/internal/SdkHttpFunctionProgrammer.ts +28 -30
  153. package/src/generates/internal/SdkHttpNamespaceProgrammer.ts +34 -64
  154. package/src/generates/internal/SdkHttpParameterProgrammer.ts +193 -13
  155. package/src/generates/internal/SdkHttpRouteProgrammer.ts +17 -31
  156. package/src/generates/internal/SdkHttpSimulationProgrammer.ts +51 -14
  157. package/src/generates/internal/SdkMcpRouteProgrammer.ts +112 -15
  158. package/src/generates/internal/SdkPathTemplate.ts +73 -0
  159. package/src/generates/internal/SdkTypeProgrammer.ts +19 -22
  160. package/src/generates/internal/SdkTypeTagProgrammer.ts +506 -78
  161. package/src/generates/internal/SdkWebSocketNamespaceProgrammer.ts +43 -68
  162. package/src/generates/internal/SdkWebSocketParameterProgrammer.ts +126 -5
  163. package/src/generates/internal/SdkWebSocketRouteProgrammer.ts +71 -48
  164. package/src/generates/internal/SwaggerOperationComposer.ts +20 -6
  165. package/src/generates/internal/SwaggerOperationParameterComposer.ts +279 -30
  166. package/src/generates/internal/SwaggerOperationResponseComposer.ts +55 -11
  167. package/src/internal/legacy.ts +114 -26
  168. package/src/structures/IOperationMetadata.ts +28 -0
  169. package/src/structures/IReflectHttpOperationException.ts +7 -2
  170. package/src/structures/IReflectHttpOperationParameter.ts +9 -2
  171. package/src/structures/IReflectHttpOperationSuccess.ts +8 -3
  172. package/src/structures/ITypedHttpRoute.ts +8 -0
  173. package/src/structures/ITypedHttpRouteException.ts +4 -1
  174. package/src/structures/ITypedHttpRouteParameter.ts +3 -1
  175. package/src/structures/ITypedHttpRouteSuccess.ts +4 -1
  176. package/src/structures/ITypedWebSocketRoute.ts +8 -0
  177. package/src/utils/SourceFinder.ts +25 -4
  178. package/src/utils/VersioningStrategy.ts +28 -13
  179. package/lib/validators/HttpHeadersValidator.d.ts +0 -11
  180. package/lib/validators/HttpHeadersValidator.js +0 -15
  181. package/lib/validators/HttpHeadersValidator.js.map +0 -1
  182. package/lib/validators/HttpQueryValidator.d.ts +0 -10
  183. package/lib/validators/HttpQueryValidator.js +0 -14
  184. package/lib/validators/HttpQueryValidator.js.map +0 -1
  185. package/src/validators/HttpHeadersValidator.ts +0 -12
  186. package/src/validators/HttpQueryValidator.ts +0 -11
@@ -12,6 +12,7 @@ import {
12
12
  import { IdentifierFactory } from "../../factories/IdentifierFactory";
13
13
  import { INestiaProject } from "../../structures/INestiaProject";
14
14
  import { ITypedMcpRoute } from "../../structures/ITypedMcpRoute";
15
+ import { StringUtil } from "../../utils/StringUtil";
15
16
  import { FilePrinter } from "./FilePrinter";
16
17
  import { ImportDictionary } from "./ImportDictionary";
17
18
 
@@ -39,6 +40,16 @@ export namespace SdkMcpRouteProgrammer {
39
40
  (_project: INestiaProject) =>
40
41
  (importer: ImportDictionary) =>
41
42
  (route: ITypedMcpRoute): Node => {
43
+ // The body reads `<route>.METADATA` next to these, so they yield to a
44
+ // tool of the same name, as the HTTP and WebSocket SDKs' own do.
45
+ const own = StringUtil.escapeDuplicate([route.name]);
46
+ const names = {
47
+ client: own("client"),
48
+ args: own("args"),
49
+ raw: own("raw"),
50
+ result: own("result"),
51
+ first: own("first"),
52
+ };
42
53
  const clientType = factory.createTypeReferenceNode(
43
54
  importer.external({
44
55
  declaration: true,
@@ -75,7 +86,7 @@ export namespace SdkMcpRouteProgrammer {
75
86
  factory.createParameterDeclaration(
76
87
  undefined,
77
88
  undefined,
78
- "client",
89
+ names.client,
79
90
  undefined,
80
91
  clientType,
81
92
  undefined,
@@ -86,7 +97,7 @@ export namespace SdkMcpRouteProgrammer {
86
97
  factory.createParameterDeclaration(
87
98
  undefined,
88
99
  undefined,
89
- "args",
100
+ names.args,
90
101
  undefined,
91
102
  inputRef,
92
103
  undefined,
@@ -110,7 +121,7 @@ export namespace SdkMcpRouteProgrammer {
110
121
  "arguments",
111
122
  factory.createAsExpression(
112
123
  factory.createAsExpression(
113
- factory.createIdentifier("args"),
124
+ factory.createIdentifier(names.args),
114
125
  factory.createKeywordTypeNode(SyntaxKind.AnyKeyword),
115
126
  ),
116
127
  factory.createTypeReferenceNode("Record", [
@@ -128,13 +139,13 @@ export namespace SdkMcpRouteProgrammer {
128
139
  factory.createVariableDeclarationList(
129
140
  [
130
141
  factory.createVariableDeclaration(
131
- "raw",
142
+ names.raw,
132
143
  undefined,
133
144
  undefined,
134
145
  factory.createAwaitExpression(
135
146
  factory.createCallExpression(
136
147
  factory.createPropertyAccessExpression(
137
- factory.createIdentifier("client"),
148
+ factory.createIdentifier(names.client),
138
149
  "callTool",
139
150
  ),
140
151
  undefined,
@@ -165,7 +176,7 @@ export namespace SdkMcpRouteProgrammer {
165
176
  ),
166
177
  undefined,
167
178
  [
168
- factory.createIdentifier("raw"),
179
+ factory.createIdentifier(names.raw),
169
180
  factory.createStringLiteral("toolResult"),
170
181
  ],
171
182
  ),
@@ -179,11 +190,11 @@ export namespace SdkMcpRouteProgrammer {
179
190
  factory.createVariableDeclarationList(
180
191
  [
181
192
  factory.createVariableDeclaration(
182
- "result",
193
+ names.result,
183
194
  undefined,
184
195
  factory.createTypeReferenceNode(callToolResultTypeName),
185
196
  factory.createAsExpression(
186
- factory.createIdentifier("raw"),
197
+ factory.createIdentifier(names.raw),
187
198
  factory.createTypeReferenceNode(callToolResultTypeName),
188
199
  ),
189
200
  ),
@@ -194,13 +205,13 @@ export namespace SdkMcpRouteProgrammer {
194
205
  factory.createIfStatement(
195
206
  factory.createBinaryExpression(
196
207
  factory.createPropertyAccessExpression(
197
- factory.createIdentifier("result"),
208
+ factory.createIdentifier(names.result),
198
209
  "isError",
199
210
  ),
200
211
  factory.createToken(SyntaxKind.EqualsEqualsEqualsToken),
201
212
  factory.createTrue(),
202
213
  ),
203
- throwToolError(toolNameExpr, '" returned isError'),
214
+ throwToolErrorWithText(toolNameExpr, names.result),
204
215
  ),
205
216
  ...(isVoid
206
217
  ? [factory.createReturnStatement()]
@@ -210,13 +221,13 @@ export namespace SdkMcpRouteProgrammer {
210
221
  factory.createVariableDeclarationList(
211
222
  [
212
223
  factory.createVariableDeclaration(
213
- "first",
224
+ names.first,
214
225
  undefined,
215
226
  undefined,
216
227
  factory.createCallExpression(
217
228
  factory.createPropertyAccessExpression(
218
229
  factory.createPropertyAccessExpression(
219
- factory.createIdentifier("result"),
230
+ factory.createIdentifier(names.result),
220
231
  "content",
221
232
  ),
222
233
  "find",
@@ -249,7 +260,7 @@ export namespace SdkMcpRouteProgrammer {
249
260
  ),
250
261
  factory.createIfStatement(
251
262
  factory.createBinaryExpression(
252
- factory.createIdentifier("first"),
263
+ factory.createIdentifier(names.first),
253
264
  factory.createToken(SyntaxKind.EqualsEqualsEqualsToken),
254
265
  factory.createIdentifier("undefined"),
255
266
  ),
@@ -258,7 +269,7 @@ export namespace SdkMcpRouteProgrammer {
258
269
  factory.createIfStatement(
259
270
  factory.createBinaryExpression(
260
271
  factory.createPropertyAccessExpression(
261
- factory.createIdentifier("first"),
272
+ factory.createIdentifier(names.first),
262
273
  "type",
263
274
  ),
264
275
  factory.createToken(SyntaxKind.EqualsEqualsEqualsToken),
@@ -274,7 +285,7 @@ export namespace SdkMcpRouteProgrammer {
274
285
  undefined,
275
286
  [
276
287
  factory.createPropertyAccessExpression(
277
- factory.createIdentifier("first"),
288
+ factory.createIdentifier(names.first),
278
289
  "text",
279
290
  ),
280
291
  ],
@@ -423,6 +434,92 @@ export namespace SdkMcpRouteProgrammer {
423
434
  const isVoidName = (name: string): boolean =>
424
435
  name === "void" || name === "undefined";
425
436
 
437
+ /**
438
+ * The error of a tool that answered `isError`, carrying the text content the
439
+ * tool put its reason in, as `McpAdaptor` does an `HttpException`'s message.
440
+ */
441
+ const throwToolErrorWithText = (
442
+ toolNameExpr: Expression,
443
+ result: string,
444
+ ): Statement => {
445
+ const entry = (): Expression => factory.createIdentifier("entry");
446
+ return factory.createThrowStatement(
447
+ factory.createNewExpression(
448
+ factory.createIdentifier("Error"),
449
+ undefined,
450
+ [
451
+ factory.createTemplateExpression(
452
+ factory.createTemplateHead('MCP tool "'),
453
+ [
454
+ factory.createTemplateSpan(
455
+ toolNameExpr,
456
+ factory.createTemplateMiddle('" returned isError: '),
457
+ ),
458
+ factory.createTemplateSpan(
459
+ factory.createCallExpression(
460
+ factory.createPropertyAccessExpression(
461
+ factory.createCallExpression(
462
+ factory.createPropertyAccessExpression(
463
+ factory.createCallExpression(
464
+ factory.createPropertyAccessExpression(
465
+ factory.createPropertyAccessExpression(
466
+ factory.createIdentifier(result),
467
+ "content",
468
+ ),
469
+ "filter",
470
+ ),
471
+ undefined,
472
+ [
473
+ factory.createArrowFunction(
474
+ undefined,
475
+ undefined,
476
+ [IdentifierFactory.parameter("entry")],
477
+ undefined,
478
+ undefined,
479
+ factory.createBinaryExpression(
480
+ factory.createPropertyAccessExpression(
481
+ entry(),
482
+ "type",
483
+ ),
484
+ factory.createToken(
485
+ SyntaxKind.EqualsEqualsEqualsToken,
486
+ ),
487
+ factory.createStringLiteral("text"),
488
+ ),
489
+ ),
490
+ ],
491
+ ),
492
+ "map",
493
+ ),
494
+ undefined,
495
+ [
496
+ factory.createArrowFunction(
497
+ undefined,
498
+ undefined,
499
+ [IdentifierFactory.parameter("entry")],
500
+ undefined,
501
+ undefined,
502
+ factory.createPropertyAccessExpression(
503
+ entry(),
504
+ "text",
505
+ ),
506
+ ),
507
+ ],
508
+ ),
509
+ "join",
510
+ ),
511
+ undefined,
512
+ [factory.createStringLiteral("\n")],
513
+ ),
514
+ factory.createTemplateTail(""),
515
+ ),
516
+ ],
517
+ ),
518
+ ],
519
+ ),
520
+ );
521
+ };
522
+
426
523
  const throwToolError = (
427
524
  toolNameExpr: Expression,
428
525
  suffix: string,
@@ -0,0 +1,73 @@
1
+ import { type Expression, factory } from "@ttsc/factory";
2
+
3
+ import { PathAnalyzer } from "../../analyses/PathAnalyzer";
4
+ import { IdentifierFactory } from "../../factories/IdentifierFactory";
5
+ import { ImportDictionary } from "./ImportDictionary";
6
+
7
+ export namespace SdkPathTemplate {
8
+ /**
9
+ * The expression an SDK function builds its route's path with: the literal
10
+ * text of the path, each parameter filled in by `PathParameter.encode()` of
11
+ * `@nestia/fetcher` with the value `argument` names for it: URI-encoded, or
12
+ * `"null"` when nullish, and refused when it is a dot segment (`.`, `..`) the
13
+ * URL would resolve away.
14
+ *
15
+ * The positions come from {@link PathAnalyzer.segments}, as the server's
16
+ * router reads the path, so a parameter with literal text beside it in its
17
+ * segment, such as `/files/:id.json` or `/range/:from-:to`, is filled in
18
+ * where it stands.
19
+ */
20
+ export const compose = (props: {
21
+ importer: ImportDictionary;
22
+ path: string;
23
+ argument: (name: string) => Expression;
24
+ }): Expression => {
25
+ const segments: PathAnalyzer.ISegment[] | null = PathAnalyzer.segments(
26
+ props.path,
27
+ );
28
+ if (segments === null) return factory.createStringLiteral(props.path);
29
+ // the literal text as the router reads it, an escaped `\:` unescaped
30
+ else if (segments.every((segment) => segment.type === "literal"))
31
+ return factory.createStringLiteral(
32
+ segments
33
+ .map((segment) => (segment.type === "literal" ? segment.value : ""))
34
+ .join(""),
35
+ );
36
+
37
+ // a template holds a literal between every two expressions
38
+ const literals: string[] = [""];
39
+ const parameters: string[] = [];
40
+ for (const segment of segments)
41
+ if (segment.type === "literal")
42
+ literals[literals.length - 1] += segment.value;
43
+ else {
44
+ parameters.push(segment.name);
45
+ literals.push("");
46
+ }
47
+ return factory.createTemplateExpression(
48
+ factory.createTemplateHead(literals[0]!),
49
+ parameters.map((name, i) =>
50
+ factory.createTemplateSpan(
51
+ factory.createCallExpression(
52
+ IdentifierFactory.access(
53
+ factory.createIdentifier(
54
+ props.importer.external({
55
+ declaration: false,
56
+ file: "@nestia/fetcher",
57
+ type: "element",
58
+ name: "PathParameter",
59
+ }),
60
+ ),
61
+ "encode",
62
+ ),
63
+ undefined,
64
+ [factory.createStringLiteral(name), props.argument(name)],
65
+ ),
66
+ (i !== parameters.length - 1
67
+ ? factory.createTemplateMiddle
68
+ : factory.createTemplateTail)(literals[i + 1]!),
69
+ ),
70
+ ),
71
+ );
72
+ };
73
+ }
@@ -3,6 +3,7 @@ import { NamingConvention } from "@typia/utils";
3
3
  import { IJsDocTagInfo, IMetadataTypeTag } from "typia";
4
4
 
5
5
  import { ExpressionFactory } from "../../factories/ExpressionFactory";
6
+ import { LiteralFactory } from "../../factories/LiteralFactory";
6
7
  import { TypeFactory } from "../../factories/TypeFactory";
7
8
  import {
8
9
  MetadataAliasType,
@@ -14,6 +15,7 @@ import {
14
15
  MetadataProperty,
15
16
  MetadataSchema,
16
17
  MetadataTuple,
18
+ decodeMetadataValue,
17
19
  isRequiredOf,
18
20
  isSoleLiteralOf,
19
21
  sizeOf,
@@ -43,7 +45,7 @@ export namespace SdkTypeProgrammer {
43
45
 
44
46
  // ATOMIC TYPES
45
47
  for (const c of meta.constants)
46
- for (const value of c.values) union.push(write_constant(value));
48
+ for (const value of c.values) union.push(write_constant(c.type, value));
47
49
  for (const tpl of meta.templates)
48
50
  union.push(write_template(project)(importer)(tpl.row ?? tpl));
49
51
  for (const atom of meta.atomics) union.push(write_atomic(importer)(atom));
@@ -106,10 +108,7 @@ export namespace SdkTypeProgrammer {
106
108
  )
107
109
  return factory.createIntersectionTypeNode([
108
110
  TypeFactory.keyword("string"),
109
- SdkTypeTagProgrammer.write(importer, "string", {
110
- name: "Format",
111
- value: "date-time",
112
- } as IMetadataTypeTag),
111
+ SdkTypeTagProgrammer.writePredefined(importer, "Format", "date-time"),
113
112
  ]);
114
113
  return write(project)(importer)(meta.returns, true);
115
114
  };
@@ -117,26 +116,24 @@ export namespace SdkTypeProgrammer {
117
116
  /* -----------------------------------------------------------
118
117
  ATOMICS
119
118
  ----------------------------------------------------------- */
120
- const write_constant = (value: MetadataConstantValue) => {
121
- if (typeof value.value === "boolean")
119
+ const write_constant = (
120
+ type: string,
121
+ constant: MetadataConstantValue,
122
+ ): TypeNode => {
123
+ const value: unknown = decodeMetadataValue(type, constant.value);
124
+ if (typeof value === "boolean")
122
125
  return factory.createLiteralTypeNode(
123
- value.value ? factory.createTrue() : factory.createFalse(),
124
- );
125
- else if (typeof value.value === "bigint")
126
- return factory.createLiteralTypeNode(
127
- value.value < BigInt(0)
128
- ? factory.createPrefixUnaryExpression(
129
- SyntaxKind.MinusToken,
130
- factory.createBigIntLiteral((-value.value).toString()),
131
- )
132
- : factory.createBigIntLiteral(value.value.toString()),
133
- );
134
- else if (typeof value.value === "number")
135
- return factory.createLiteralTypeNode(
136
- ExpressionFactory.number(value.value),
126
+ value ? factory.createTrue() : factory.createFalse(),
137
127
  );
128
+ else if (typeof value === "bigint")
129
+ return factory.createLiteralTypeNode(LiteralFactory.write(value) as any);
130
+ else if (typeof value === "number")
131
+ // NaN is the one number with no literal type
132
+ return Number.isNaN(value)
133
+ ? TypeFactory.keyword("number")
134
+ : factory.createLiteralTypeNode(ExpressionFactory.number(value));
138
135
  return factory.createLiteralTypeNode(
139
- factory.createStringLiteral(value.value as string),
136
+ factory.createStringLiteral(value as string),
140
137
  );
141
138
  };
142
139