@nestia/sdk 7.0.0-dev.20250607 → 7.0.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 (111) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +92 -92
  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/executable/internal/NestiaConfigLoader.js +4 -4
  15. package/lib/executable/sdk.js +12 -12
  16. package/package.json +5 -5
  17. package/src/INestiaConfig.ts +269 -269
  18. package/src/NestiaSdkApplication.ts +307 -307
  19. package/src/NestiaSwaggerComposer.ts +138 -138
  20. package/src/analyses/AccessorAnalyzer.ts +67 -67
  21. package/src/analyses/ConfigAnalyzer.ts +155 -155
  22. package/src/analyses/ExceptionAnalyzer.ts +154 -154
  23. package/src/analyses/GenericAnalyzer.ts +49 -49
  24. package/src/analyses/ImportAnalyzer.ts +171 -171
  25. package/src/analyses/PathAnalyzer.ts +69 -69
  26. package/src/analyses/ReflectControllerAnalyzer.ts +105 -105
  27. package/src/analyses/ReflectHttpOperationAnalyzer.ts +183 -183
  28. package/src/analyses/ReflectHttpOperationExceptionAnalyzer.ts +71 -71
  29. package/src/analyses/ReflectHttpOperationParameterAnalyzer.ts +348 -348
  30. package/src/analyses/ReflectHttpOperationResponseAnalyzer.ts +127 -127
  31. package/src/analyses/ReflectMetadataAnalyzer.ts +44 -44
  32. package/src/analyses/ReflectWebSocketOperationAnalyzer.ts +172 -172
  33. package/src/analyses/SecurityAnalyzer.ts +25 -25
  34. package/src/analyses/TypedHttpRouteAnalyzer.ts +204 -204
  35. package/src/analyses/TypedWebSocketRouteAnalyzer.ts +33 -33
  36. package/src/decorators/OperationMetadata.ts +15 -15
  37. package/src/executable/internal/CommandParser.ts +15 -15
  38. package/src/executable/internal/NestiaConfigLoader.ts +78 -78
  39. package/src/executable/internal/NestiaSdkCommand.ts +103 -103
  40. package/src/executable/sdk.ts +75 -75
  41. package/src/generates/CloneGenerator.ts +66 -66
  42. package/src/generates/E2eGenerator.ts +32 -32
  43. package/src/generates/SdkGenerator.ts +160 -160
  44. package/src/generates/SwaggerGenerator.ts +284 -284
  45. package/src/generates/internal/E2eFileProgrammer.ts +205 -205
  46. package/src/generates/internal/FilePrinter.ts +53 -53
  47. package/src/generates/internal/ImportDictionary.ts +163 -163
  48. package/src/generates/internal/SdkAliasCollection.ts +255 -255
  49. package/src/generates/internal/SdkDistributionComposer.ts +103 -103
  50. package/src/generates/internal/SdkFileProgrammer.ts +116 -116
  51. package/src/generates/internal/SdkHttpCloneProgrammer.ts +124 -124
  52. package/src/generates/internal/SdkHttpCloneReferencer.ts +75 -75
  53. package/src/generates/internal/SdkHttpFunctionProgrammer.ts +276 -276
  54. package/src/generates/internal/SdkHttpNamespaceProgrammer.ts +500 -500
  55. package/src/generates/internal/SdkHttpParameterProgrammer.ts +178 -178
  56. package/src/generates/internal/SdkHttpRouteProgrammer.ts +107 -107
  57. package/src/generates/internal/SdkHttpSimulationProgrammer.ts +340 -340
  58. package/src/generates/internal/SdkImportWizard.ts +55 -55
  59. package/src/generates/internal/SdkRouteDirectory.ts +18 -18
  60. package/src/generates/internal/SdkTypeProgrammer.ts +384 -384
  61. package/src/generates/internal/SdkTypeTagProgrammer.ts +102 -102
  62. package/src/generates/internal/SdkWebSocketNamespaceProgrammer.ts +366 -366
  63. package/src/generates/internal/SdkWebSocketParameterProgrammer.ts +87 -87
  64. package/src/generates/internal/SdkWebSocketRouteProgrammer.ts +279 -279
  65. package/src/generates/internal/SwaggerDescriptionComposer.ts +64 -64
  66. package/src/generates/internal/SwaggerOperationComposer.ts +119 -119
  67. package/src/generates/internal/SwaggerOperationParameterComposer.ts +177 -177
  68. package/src/generates/internal/SwaggerOperationResponseComposer.ts +110 -110
  69. package/src/index.ts +4 -4
  70. package/src/module.ts +3 -3
  71. package/src/structures/INestiaProject.ts +13 -13
  72. package/src/structures/INestiaSdkInput.ts +20 -20
  73. package/src/structures/IReflectApplication.ts +8 -8
  74. package/src/structures/IReflectController.ts +15 -15
  75. package/src/structures/IReflectHttpOperation.ts +26 -26
  76. package/src/structures/IReflectHttpOperationException.ts +19 -19
  77. package/src/structures/IReflectHttpOperationParameter.ts +81 -81
  78. package/src/structures/IReflectHttpOperationSuccess.ts +22 -22
  79. package/src/structures/IReflectOperationError.ts +26 -26
  80. package/src/structures/IReflectType.ts +4 -4
  81. package/src/structures/IReflectTypeImport.ts +4 -4
  82. package/src/structures/IReflectWebSocketOperation.ts +17 -17
  83. package/src/structures/IReflectWebSocketOperationParameter.ts +38 -38
  84. package/src/structures/ITypedApplication.ts +11 -11
  85. package/src/structures/ITypedHttpRoute.ts +41 -41
  86. package/src/structures/ITypedHttpRouteException.ts +15 -15
  87. package/src/structures/ITypedHttpRouteParameter.ts +41 -41
  88. package/src/structures/ITypedHttpRouteSuccess.ts +22 -22
  89. package/src/structures/ITypedWebSocketRoute.ts +24 -24
  90. package/src/structures/ITypedWebSocketRouteParameter.ts +3 -3
  91. package/src/structures/MethodType.ts +5 -5
  92. package/src/structures/ParamCategory.ts +1 -1
  93. package/src/structures/TypeEntry.ts +22 -22
  94. package/src/transform.ts +9 -9
  95. package/src/transformers/IOperationMetadata.ts +44 -44
  96. package/src/transformers/ISdkOperationTransformerContext.ts +8 -8
  97. package/src/transformers/SdkOperationProgrammer.ts +209 -209
  98. package/src/transformers/SdkOperationTransformer.ts +253 -253
  99. package/src/transformers/TextPlainValidator.ts +17 -17
  100. package/src/typings/get-function-location.d.ts +7 -7
  101. package/src/utils/ArrayUtil.ts +26 -26
  102. package/src/utils/FileRetriever.ts +22 -22
  103. package/src/utils/MapUtil.ts +14 -14
  104. package/src/utils/MetadataUtil.ts +26 -26
  105. package/src/utils/PathUtil.ts +10 -10
  106. package/src/utils/SourceFinder.ts +66 -66
  107. package/src/utils/StringUtil.ts +17 -17
  108. package/src/utils/StripEnums.ts +5 -5
  109. package/src/utils/VersioningStrategy.ts +28 -28
  110. package/src/validators/HttpHeadersValidator.ts +34 -34
  111. package/src/validators/HttpQueryValidator.ts +34 -34
@@ -1,384 +1,384 @@
1
- import ts from "typescript";
2
- import { IJsDocTagInfo } from "typia";
3
- import { ExpressionFactory } from "typia/lib/factories/ExpressionFactory";
4
- import { TypeFactory } from "typia/lib/factories/TypeFactory";
5
- import { IMetadataTypeTag } from "typia/lib/schemas/metadata/IMetadataTypeTag";
6
- import { Metadata } from "typia/lib/schemas/metadata/Metadata";
7
- import { MetadataAliasType } from "typia/lib/schemas/metadata/MetadataAliasType";
8
- import { MetadataArray } from "typia/lib/schemas/metadata/MetadataArray";
9
- import { MetadataAtomic } from "typia/lib/schemas/metadata/MetadataAtomic";
10
- import { MetadataConstantValue } from "typia/lib/schemas/metadata/MetadataConstantValue";
11
- import { MetadataEscaped } from "typia/lib/schemas/metadata/MetadataEscaped";
12
- import { MetadataObjectType } from "typia/lib/schemas/metadata/MetadataObjectType";
13
- import { MetadataProperty } from "typia/lib/schemas/metadata/MetadataProperty";
14
- import { MetadataTuple } from "typia/lib/schemas/metadata/MetadataTuple";
15
- import { Escaper } from "typia/lib/utils/Escaper";
16
-
17
- import { INestiaProject } from "../../structures/INestiaProject";
18
- import { FilePrinter } from "./FilePrinter";
19
- import { ImportDictionary } from "./ImportDictionary";
20
- import { SdkTypeTagProgrammer } from "./SdkTypeTagProgrammer";
21
-
22
- export namespace SdkTypeProgrammer {
23
- /* -----------------------------------------------------------
24
- FACADE
25
- ----------------------------------------------------------- */
26
- export const write =
27
- (project: INestiaProject) =>
28
- (importer: ImportDictionary) =>
29
- (meta: Metadata, parentEscaped: boolean = false): ts.TypeNode => {
30
- const union: ts.TypeNode[] = [];
31
-
32
- // COALESCES
33
- if (meta.any) union.push(TypeFactory.keyword("any"));
34
- if (meta.nullable) union.push(writeNode("null"));
35
- if (meta.isRequired() === false) union.push(writeNode("undefined"));
36
- if (parentEscaped === false && meta.escaped)
37
- union.push(write_escaped(project)(importer)(meta.escaped));
38
-
39
- // ATOMIC TYPES
40
- for (const c of meta.constants)
41
- for (const value of c.values) union.push(write_constant(value));
42
- for (const tpl of meta.templates)
43
- union.push(write_template(project)(importer)(tpl.row ?? tpl));
44
- for (const atom of meta.atomics) union.push(write_atomic(importer)(atom));
45
-
46
- // OBJECT TYPES
47
- for (const tuple of meta.tuples)
48
- union.push(write_tuple(project)(importer)(tuple));
49
- for (const array of meta.arrays)
50
- union.push(write_array(project)(importer)(array));
51
- for (const object of meta.objects)
52
- if (
53
- object.type.name === "object" ||
54
- object.type.name === "__type" ||
55
- object.type.name.startsWith("__type.") ||
56
- object.type.name === "__object" ||
57
- object.type.name.startsWith("__object.")
58
- )
59
- union.push(write_object(project)(importer)(object.type));
60
- else union.push(writeAlias(project)(importer)(object.type));
61
- for (const alias of meta.aliases)
62
- union.push(writeAlias(project)(importer)(alias.type));
63
- for (const native of meta.natives)
64
- if (native.name === "Blob" || native.name === "File")
65
- union.push(write_native(native.name));
66
-
67
- return union.length === 1
68
- ? union[0]
69
- : ts.factory.createUnionTypeNode(union);
70
- };
71
-
72
- export const write_object =
73
- (project: INestiaProject) =>
74
- (importer: ImportDictionary) =>
75
- (object: MetadataObjectType): ts.TypeNode => {
76
- const regular = object.properties.filter((p) => p.key.isSoleLiteral());
77
- const dynamic = object.properties.filter((p) => !p.key.isSoleLiteral());
78
- return regular.length && dynamic.length
79
- ? ts.factory.createIntersectionTypeNode([
80
- write_regular_property(project)(importer)(regular),
81
- ...dynamic.map(write_dynamic_property(project)(importer)),
82
- ])
83
- : dynamic.length
84
- ? ts.factory.createIntersectionTypeNode(
85
- dynamic.map(write_dynamic_property(project)(importer)),
86
- )
87
- : write_regular_property(project)(importer)(regular);
88
- };
89
-
90
- const write_escaped =
91
- (project: INestiaProject) =>
92
- (importer: ImportDictionary) =>
93
- (meta: MetadataEscaped): ts.TypeNode => {
94
- if (
95
- meta.original.size() === 1 &&
96
- meta.original.natives.length === 1 &&
97
- meta.original.natives[0].name === "Date"
98
- )
99
- return ts.factory.createIntersectionTypeNode([
100
- TypeFactory.keyword("string"),
101
- SdkTypeTagProgrammer.write(importer, "string", {
102
- name: "Format",
103
- value: "date-time",
104
- } as IMetadataTypeTag),
105
- ]);
106
- return write(project)(importer)(meta.returns, true);
107
- };
108
-
109
- /* -----------------------------------------------------------
110
- ATOMICS
111
- ----------------------------------------------------------- */
112
- const write_constant = (value: MetadataConstantValue) => {
113
- if (typeof value.value === "boolean")
114
- return ts.factory.createLiteralTypeNode(
115
- value ? ts.factory.createTrue() : ts.factory.createFalse(),
116
- );
117
- else if (typeof value.value === "bigint")
118
- return ts.factory.createLiteralTypeNode(
119
- value.value < BigInt(0)
120
- ? ts.factory.createPrefixUnaryExpression(
121
- ts.SyntaxKind.MinusToken,
122
- ts.factory.createBigIntLiteral((-value).toString()),
123
- )
124
- : ts.factory.createBigIntLiteral(value.toString()),
125
- );
126
- else if (typeof value.value === "number")
127
- return ts.factory.createLiteralTypeNode(
128
- ExpressionFactory.number(value.value),
129
- );
130
- return ts.factory.createLiteralTypeNode(
131
- ts.factory.createStringLiteral(value.value as string),
132
- );
133
- };
134
-
135
- const write_template =
136
- (project: INestiaProject) =>
137
- (importer: ImportDictionary) =>
138
- (meta: Metadata[]): ts.TypeNode => {
139
- const head: boolean = meta[0].isSoleLiteral();
140
- const spans: [ts.TypeNode | null, string | null][] = [];
141
- for (const elem of meta.slice(head ? 1 : 0)) {
142
- const last =
143
- spans.at(-1) ??
144
- (() => {
145
- const tuple = [null!, null!] as [ts.TypeNode | null, string | null];
146
- spans.push(tuple);
147
- return tuple;
148
- })();
149
- if (elem.isSoleLiteral())
150
- if (last[1] === null)
151
- last[1] = String(elem.constants[0].values[0].value);
152
- else
153
- spans.push([
154
- ts.factory.createLiteralTypeNode(
155
- ts.factory.createStringLiteral(
156
- String(elem.constants[0].values[0].value),
157
- ),
158
- ),
159
- null,
160
- ]);
161
- else if (last[0] === null) last[0] = write(project)(importer)(elem);
162
- else spans.push([write(project)(importer)(elem), null]);
163
- }
164
- return ts.factory.createTemplateLiteralType(
165
- ts.factory.createTemplateHead(
166
- head ? (meta[0].constants[0].values[0].value as string) : "",
167
- ),
168
- spans
169
- .filter(([node]) => node !== null)
170
- .map(([node, str], i, array) =>
171
- ts.factory.createTemplateLiteralTypeSpan(
172
- node!,
173
- (i !== array.length - 1
174
- ? ts.factory.createTemplateMiddle
175
- : ts.factory.createTemplateTail)(str ?? ""),
176
- ),
177
- ),
178
- );
179
- };
180
-
181
- const write_atomic =
182
- (importer: ImportDictionary) =>
183
- (meta: MetadataAtomic): ts.TypeNode =>
184
- write_type_tag_matrix(importer)(
185
- meta.type as "boolean" | "bigint" | "number" | "string",
186
- ts.factory.createKeywordTypeNode(
187
- meta.type === "boolean"
188
- ? ts.SyntaxKind.BooleanKeyword
189
- : meta.type === "bigint"
190
- ? ts.SyntaxKind.BigIntKeyword
191
- : meta.type === "number"
192
- ? ts.SyntaxKind.NumberKeyword
193
- : ts.SyntaxKind.StringKeyword,
194
- ),
195
- meta.tags,
196
- );
197
-
198
- /* -----------------------------------------------------------
199
- INSTANCES
200
- ----------------------------------------------------------- */
201
- const write_array =
202
- (project: INestiaProject) =>
203
- (importer: ImportDictionary) =>
204
- (meta: MetadataArray): ts.TypeNode =>
205
- write_type_tag_matrix(importer)(
206
- "array",
207
- ts.factory.createArrayTypeNode(
208
- write(project)(importer)(meta.type.value),
209
- ),
210
- meta.tags,
211
- );
212
-
213
- const write_tuple =
214
- (project: INestiaProject) =>
215
- (importer: ImportDictionary) =>
216
- (meta: MetadataTuple): ts.TypeNode =>
217
- ts.factory.createTupleTypeNode(
218
- meta.type.elements.map((elem) =>
219
- elem.rest
220
- ? ts.factory.createRestTypeNode(
221
- ts.factory.createArrayTypeNode(
222
- write(project)(importer)(elem.rest),
223
- ),
224
- )
225
- : elem.optional
226
- ? ts.factory.createOptionalTypeNode(
227
- write(project)(importer)(elem),
228
- )
229
- : write(project)(importer)(elem),
230
- ),
231
- );
232
-
233
- const write_regular_property =
234
- (project: INestiaProject) =>
235
- (importer: ImportDictionary) =>
236
- (properties: MetadataProperty[]): ts.TypeLiteralNode =>
237
- ts.factory.createTypeLiteralNode(
238
- properties
239
- .map((p) => {
240
- const description: string = writeComment(p.value.atomics)(
241
- p.description,
242
- p.jsDocTags,
243
- );
244
- const signature: ts.PropertySignature =
245
- ts.factory.createPropertySignature(
246
- undefined,
247
- Escaper.variable(String(p.key.constants[0].values[0].value))
248
- ? ts.factory.createIdentifier(
249
- String(p.key.constants[0].values[0].value),
250
- )
251
- : ts.factory.createStringLiteral(
252
- String(p.key.constants[0].values[0].value),
253
- ),
254
- p.value.isRequired() === false
255
- ? ts.factory.createToken(ts.SyntaxKind.QuestionToken)
256
- : undefined,
257
- SdkTypeProgrammer.write(project)(importer)(p.value),
258
- );
259
- return !!description.length
260
- ? [
261
- ts.factory.createIdentifier("\n") as any,
262
- FilePrinter.description(signature, description),
263
- ]
264
- : signature;
265
- })
266
- .flat(),
267
- );
268
-
269
- const write_dynamic_property =
270
- (project: INestiaProject) =>
271
- (importer: ImportDictionary) =>
272
- (property: MetadataProperty): ts.TypeLiteralNode =>
273
- ts.factory.createTypeLiteralNode([
274
- FilePrinter.description(
275
- ts.factory.createIndexSignature(
276
- undefined,
277
- [
278
- ts.factory.createParameterDeclaration(
279
- undefined,
280
- undefined,
281
- ts.factory.createIdentifier("key"),
282
- undefined,
283
- SdkTypeProgrammer.write(project)(importer)(property.key),
284
- ),
285
- ],
286
- SdkTypeProgrammer.write(project)(importer)(property.value),
287
- ),
288
- writeComment(property.value.atomics)(
289
- property.description,
290
- property.jsDocTags,
291
- ),
292
- ),
293
- ]);
294
-
295
- const writeAlias =
296
- (project: INestiaProject) =>
297
- (importer: ImportDictionary) =>
298
- (meta: MetadataAliasType | MetadataObjectType): ts.TypeNode => {
299
- importInternalFile(project)(importer)(meta.name);
300
- return ts.factory.createTypeReferenceNode(meta.name);
301
- };
302
-
303
- const write_native = (name: string): ts.TypeNode =>
304
- ts.factory.createTypeReferenceNode(name);
305
-
306
- /* -----------------------------------------------------------
307
- MISCELLANEOUS
308
- ----------------------------------------------------------- */
309
- const write_type_tag_matrix =
310
- (importer: ImportDictionary) =>
311
- (
312
- from: "array" | "boolean" | "number" | "bigint" | "string" | "object",
313
- base: ts.TypeNode,
314
- matrix: IMetadataTypeTag[][],
315
- ): ts.TypeNode => {
316
- matrix = matrix.filter((row) => row.length !== 0);
317
- if (matrix.length === 0) return base;
318
- else if (matrix.length === 1)
319
- return ts.factory.createIntersectionTypeNode([
320
- base,
321
- ...matrix[0].map((tag) =>
322
- SdkTypeTagProgrammer.write(importer, from, tag),
323
- ),
324
- ]);
325
- return ts.factory.createIntersectionTypeNode([
326
- base,
327
- ts.factory.createUnionTypeNode(
328
- matrix.map((row) =>
329
- row.length === 1
330
- ? SdkTypeTagProgrammer.write(importer, from, row[0])
331
- : ts.factory.createIntersectionTypeNode(
332
- row.map((tag) =>
333
- SdkTypeTagProgrammer.write(importer, from, tag),
334
- ),
335
- ),
336
- ),
337
- ),
338
- ]);
339
- };
340
- }
341
-
342
- const writeNode = (text: string) => ts.factory.createTypeReferenceNode(text);
343
- const writeComment =
344
- (atomics: MetadataAtomic[]) =>
345
- (description: string | null, jsDocTags: IJsDocTagInfo[]): string => {
346
- const lines: string[] = [];
347
- if (description?.length)
348
- lines.push(...description.split("\n").map((s) => `${s}`));
349
-
350
- const filtered: IJsDocTagInfo[] =
351
- !!atomics.length && !!jsDocTags?.length
352
- ? jsDocTags.filter(
353
- (tag) =>
354
- !atomics.some((a) =>
355
- a.tags.some((r) => r.some((t) => t.kind === tag.name)),
356
- ),
357
- )
358
- : (jsDocTags ?? []);
359
-
360
- if (description?.length && filtered.length) lines.push("");
361
- if (filtered.length)
362
- lines.push(
363
- ...filtered.map((t) =>
364
- t.text?.length
365
- ? `@${t.name} ${t.text.map((e) => e.text).join("")}`
366
- : `@${t.name}`,
367
- ),
368
- );
369
- return lines.join("\n");
370
- };
371
-
372
- const importInternalFile =
373
- (project: INestiaProject) =>
374
- (importer: ImportDictionary) =>
375
- (name: string) => {
376
- const top = name.split(".")[0];
377
- if (importer.file === `${project.config.output}/structures/${top}.ts`)
378
- return;
379
- importer.internal({
380
- type: true,
381
- file: `${project.config.output}/structures/${name.split(".")[0]}`,
382
- instance: top,
383
- });
384
- };
1
+ import ts from "typescript";
2
+ import { IJsDocTagInfo } from "typia";
3
+ import { ExpressionFactory } from "typia/lib/factories/ExpressionFactory";
4
+ import { TypeFactory } from "typia/lib/factories/TypeFactory";
5
+ import { IMetadataTypeTag } from "typia/lib/schemas/metadata/IMetadataTypeTag";
6
+ import { Metadata } from "typia/lib/schemas/metadata/Metadata";
7
+ import { MetadataAliasType } from "typia/lib/schemas/metadata/MetadataAliasType";
8
+ import { MetadataArray } from "typia/lib/schemas/metadata/MetadataArray";
9
+ import { MetadataAtomic } from "typia/lib/schemas/metadata/MetadataAtomic";
10
+ import { MetadataConstantValue } from "typia/lib/schemas/metadata/MetadataConstantValue";
11
+ import { MetadataEscaped } from "typia/lib/schemas/metadata/MetadataEscaped";
12
+ import { MetadataObjectType } from "typia/lib/schemas/metadata/MetadataObjectType";
13
+ import { MetadataProperty } from "typia/lib/schemas/metadata/MetadataProperty";
14
+ import { MetadataTuple } from "typia/lib/schemas/metadata/MetadataTuple";
15
+ import { Escaper } from "typia/lib/utils/Escaper";
16
+
17
+ import { INestiaProject } from "../../structures/INestiaProject";
18
+ import { FilePrinter } from "./FilePrinter";
19
+ import { ImportDictionary } from "./ImportDictionary";
20
+ import { SdkTypeTagProgrammer } from "./SdkTypeTagProgrammer";
21
+
22
+ export namespace SdkTypeProgrammer {
23
+ /* -----------------------------------------------------------
24
+ FACADE
25
+ ----------------------------------------------------------- */
26
+ export const write =
27
+ (project: INestiaProject) =>
28
+ (importer: ImportDictionary) =>
29
+ (meta: Metadata, parentEscaped: boolean = false): ts.TypeNode => {
30
+ const union: ts.TypeNode[] = [];
31
+
32
+ // COALESCES
33
+ if (meta.any) union.push(TypeFactory.keyword("any"));
34
+ if (meta.nullable) union.push(writeNode("null"));
35
+ if (meta.isRequired() === false) union.push(writeNode("undefined"));
36
+ if (parentEscaped === false && meta.escaped)
37
+ union.push(write_escaped(project)(importer)(meta.escaped));
38
+
39
+ // ATOMIC TYPES
40
+ for (const c of meta.constants)
41
+ for (const value of c.values) union.push(write_constant(value));
42
+ for (const tpl of meta.templates)
43
+ union.push(write_template(project)(importer)(tpl.row ?? tpl));
44
+ for (const atom of meta.atomics) union.push(write_atomic(importer)(atom));
45
+
46
+ // OBJECT TYPES
47
+ for (const tuple of meta.tuples)
48
+ union.push(write_tuple(project)(importer)(tuple));
49
+ for (const array of meta.arrays)
50
+ union.push(write_array(project)(importer)(array));
51
+ for (const object of meta.objects)
52
+ if (
53
+ object.type.name === "object" ||
54
+ object.type.name === "__type" ||
55
+ object.type.name.startsWith("__type.") ||
56
+ object.type.name === "__object" ||
57
+ object.type.name.startsWith("__object.")
58
+ )
59
+ union.push(write_object(project)(importer)(object.type));
60
+ else union.push(writeAlias(project)(importer)(object.type));
61
+ for (const alias of meta.aliases)
62
+ union.push(writeAlias(project)(importer)(alias.type));
63
+ for (const native of meta.natives)
64
+ if (native.name === "Blob" || native.name === "File")
65
+ union.push(write_native(native.name));
66
+
67
+ return union.length === 1
68
+ ? union[0]
69
+ : ts.factory.createUnionTypeNode(union);
70
+ };
71
+
72
+ export const write_object =
73
+ (project: INestiaProject) =>
74
+ (importer: ImportDictionary) =>
75
+ (object: MetadataObjectType): ts.TypeNode => {
76
+ const regular = object.properties.filter((p) => p.key.isSoleLiteral());
77
+ const dynamic = object.properties.filter((p) => !p.key.isSoleLiteral());
78
+ return regular.length && dynamic.length
79
+ ? ts.factory.createIntersectionTypeNode([
80
+ write_regular_property(project)(importer)(regular),
81
+ ...dynamic.map(write_dynamic_property(project)(importer)),
82
+ ])
83
+ : dynamic.length
84
+ ? ts.factory.createIntersectionTypeNode(
85
+ dynamic.map(write_dynamic_property(project)(importer)),
86
+ )
87
+ : write_regular_property(project)(importer)(regular);
88
+ };
89
+
90
+ const write_escaped =
91
+ (project: INestiaProject) =>
92
+ (importer: ImportDictionary) =>
93
+ (meta: MetadataEscaped): ts.TypeNode => {
94
+ if (
95
+ meta.original.size() === 1 &&
96
+ meta.original.natives.length === 1 &&
97
+ meta.original.natives[0].name === "Date"
98
+ )
99
+ return ts.factory.createIntersectionTypeNode([
100
+ TypeFactory.keyword("string"),
101
+ SdkTypeTagProgrammer.write(importer, "string", {
102
+ name: "Format",
103
+ value: "date-time",
104
+ } as IMetadataTypeTag),
105
+ ]);
106
+ return write(project)(importer)(meta.returns, true);
107
+ };
108
+
109
+ /* -----------------------------------------------------------
110
+ ATOMICS
111
+ ----------------------------------------------------------- */
112
+ const write_constant = (value: MetadataConstantValue) => {
113
+ if (typeof value.value === "boolean")
114
+ return ts.factory.createLiteralTypeNode(
115
+ value ? ts.factory.createTrue() : ts.factory.createFalse(),
116
+ );
117
+ else if (typeof value.value === "bigint")
118
+ return ts.factory.createLiteralTypeNode(
119
+ value.value < BigInt(0)
120
+ ? ts.factory.createPrefixUnaryExpression(
121
+ ts.SyntaxKind.MinusToken,
122
+ ts.factory.createBigIntLiteral((-value).toString()),
123
+ )
124
+ : ts.factory.createBigIntLiteral(value.toString()),
125
+ );
126
+ else if (typeof value.value === "number")
127
+ return ts.factory.createLiteralTypeNode(
128
+ ExpressionFactory.number(value.value),
129
+ );
130
+ return ts.factory.createLiteralTypeNode(
131
+ ts.factory.createStringLiteral(value.value as string),
132
+ );
133
+ };
134
+
135
+ const write_template =
136
+ (project: INestiaProject) =>
137
+ (importer: ImportDictionary) =>
138
+ (meta: Metadata[]): ts.TypeNode => {
139
+ const head: boolean = meta[0].isSoleLiteral();
140
+ const spans: [ts.TypeNode | null, string | null][] = [];
141
+ for (const elem of meta.slice(head ? 1 : 0)) {
142
+ const last =
143
+ spans.at(-1) ??
144
+ (() => {
145
+ const tuple = [null!, null!] as [ts.TypeNode | null, string | null];
146
+ spans.push(tuple);
147
+ return tuple;
148
+ })();
149
+ if (elem.isSoleLiteral())
150
+ if (last[1] === null)
151
+ last[1] = String(elem.constants[0].values[0].value);
152
+ else
153
+ spans.push([
154
+ ts.factory.createLiteralTypeNode(
155
+ ts.factory.createStringLiteral(
156
+ String(elem.constants[0].values[0].value),
157
+ ),
158
+ ),
159
+ null,
160
+ ]);
161
+ else if (last[0] === null) last[0] = write(project)(importer)(elem);
162
+ else spans.push([write(project)(importer)(elem), null]);
163
+ }
164
+ return ts.factory.createTemplateLiteralType(
165
+ ts.factory.createTemplateHead(
166
+ head ? (meta[0].constants[0].values[0].value as string) : "",
167
+ ),
168
+ spans
169
+ .filter(([node]) => node !== null)
170
+ .map(([node, str], i, array) =>
171
+ ts.factory.createTemplateLiteralTypeSpan(
172
+ node!,
173
+ (i !== array.length - 1
174
+ ? ts.factory.createTemplateMiddle
175
+ : ts.factory.createTemplateTail)(str ?? ""),
176
+ ),
177
+ ),
178
+ );
179
+ };
180
+
181
+ const write_atomic =
182
+ (importer: ImportDictionary) =>
183
+ (meta: MetadataAtomic): ts.TypeNode =>
184
+ write_type_tag_matrix(importer)(
185
+ meta.type as "boolean" | "bigint" | "number" | "string",
186
+ ts.factory.createKeywordTypeNode(
187
+ meta.type === "boolean"
188
+ ? ts.SyntaxKind.BooleanKeyword
189
+ : meta.type === "bigint"
190
+ ? ts.SyntaxKind.BigIntKeyword
191
+ : meta.type === "number"
192
+ ? ts.SyntaxKind.NumberKeyword
193
+ : ts.SyntaxKind.StringKeyword,
194
+ ),
195
+ meta.tags,
196
+ );
197
+
198
+ /* -----------------------------------------------------------
199
+ INSTANCES
200
+ ----------------------------------------------------------- */
201
+ const write_array =
202
+ (project: INestiaProject) =>
203
+ (importer: ImportDictionary) =>
204
+ (meta: MetadataArray): ts.TypeNode =>
205
+ write_type_tag_matrix(importer)(
206
+ "array",
207
+ ts.factory.createArrayTypeNode(
208
+ write(project)(importer)(meta.type.value),
209
+ ),
210
+ meta.tags,
211
+ );
212
+
213
+ const write_tuple =
214
+ (project: INestiaProject) =>
215
+ (importer: ImportDictionary) =>
216
+ (meta: MetadataTuple): ts.TypeNode =>
217
+ ts.factory.createTupleTypeNode(
218
+ meta.type.elements.map((elem) =>
219
+ elem.rest
220
+ ? ts.factory.createRestTypeNode(
221
+ ts.factory.createArrayTypeNode(
222
+ write(project)(importer)(elem.rest),
223
+ ),
224
+ )
225
+ : elem.optional
226
+ ? ts.factory.createOptionalTypeNode(
227
+ write(project)(importer)(elem),
228
+ )
229
+ : write(project)(importer)(elem),
230
+ ),
231
+ );
232
+
233
+ const write_regular_property =
234
+ (project: INestiaProject) =>
235
+ (importer: ImportDictionary) =>
236
+ (properties: MetadataProperty[]): ts.TypeLiteralNode =>
237
+ ts.factory.createTypeLiteralNode(
238
+ properties
239
+ .map((p) => {
240
+ const description: string = writeComment(p.value.atomics)(
241
+ p.description,
242
+ p.jsDocTags,
243
+ );
244
+ const signature: ts.PropertySignature =
245
+ ts.factory.createPropertySignature(
246
+ undefined,
247
+ Escaper.variable(String(p.key.constants[0].values[0].value))
248
+ ? ts.factory.createIdentifier(
249
+ String(p.key.constants[0].values[0].value),
250
+ )
251
+ : ts.factory.createStringLiteral(
252
+ String(p.key.constants[0].values[0].value),
253
+ ),
254
+ p.value.isRequired() === false
255
+ ? ts.factory.createToken(ts.SyntaxKind.QuestionToken)
256
+ : undefined,
257
+ SdkTypeProgrammer.write(project)(importer)(p.value),
258
+ );
259
+ return !!description.length
260
+ ? [
261
+ ts.factory.createIdentifier("\n") as any,
262
+ FilePrinter.description(signature, description),
263
+ ]
264
+ : signature;
265
+ })
266
+ .flat(),
267
+ );
268
+
269
+ const write_dynamic_property =
270
+ (project: INestiaProject) =>
271
+ (importer: ImportDictionary) =>
272
+ (property: MetadataProperty): ts.TypeLiteralNode =>
273
+ ts.factory.createTypeLiteralNode([
274
+ FilePrinter.description(
275
+ ts.factory.createIndexSignature(
276
+ undefined,
277
+ [
278
+ ts.factory.createParameterDeclaration(
279
+ undefined,
280
+ undefined,
281
+ ts.factory.createIdentifier("key"),
282
+ undefined,
283
+ SdkTypeProgrammer.write(project)(importer)(property.key),
284
+ ),
285
+ ],
286
+ SdkTypeProgrammer.write(project)(importer)(property.value),
287
+ ),
288
+ writeComment(property.value.atomics)(
289
+ property.description,
290
+ property.jsDocTags,
291
+ ),
292
+ ),
293
+ ]);
294
+
295
+ const writeAlias =
296
+ (project: INestiaProject) =>
297
+ (importer: ImportDictionary) =>
298
+ (meta: MetadataAliasType | MetadataObjectType): ts.TypeNode => {
299
+ importInternalFile(project)(importer)(meta.name);
300
+ return ts.factory.createTypeReferenceNode(meta.name);
301
+ };
302
+
303
+ const write_native = (name: string): ts.TypeNode =>
304
+ ts.factory.createTypeReferenceNode(name);
305
+
306
+ /* -----------------------------------------------------------
307
+ MISCELLANEOUS
308
+ ----------------------------------------------------------- */
309
+ const write_type_tag_matrix =
310
+ (importer: ImportDictionary) =>
311
+ (
312
+ from: "array" | "boolean" | "number" | "bigint" | "string" | "object",
313
+ base: ts.TypeNode,
314
+ matrix: IMetadataTypeTag[][],
315
+ ): ts.TypeNode => {
316
+ matrix = matrix.filter((row) => row.length !== 0);
317
+ if (matrix.length === 0) return base;
318
+ else if (matrix.length === 1)
319
+ return ts.factory.createIntersectionTypeNode([
320
+ base,
321
+ ...matrix[0].map((tag) =>
322
+ SdkTypeTagProgrammer.write(importer, from, tag),
323
+ ),
324
+ ]);
325
+ return ts.factory.createIntersectionTypeNode([
326
+ base,
327
+ ts.factory.createUnionTypeNode(
328
+ matrix.map((row) =>
329
+ row.length === 1
330
+ ? SdkTypeTagProgrammer.write(importer, from, row[0])
331
+ : ts.factory.createIntersectionTypeNode(
332
+ row.map((tag) =>
333
+ SdkTypeTagProgrammer.write(importer, from, tag),
334
+ ),
335
+ ),
336
+ ),
337
+ ),
338
+ ]);
339
+ };
340
+ }
341
+
342
+ const writeNode = (text: string) => ts.factory.createTypeReferenceNode(text);
343
+ const writeComment =
344
+ (atomics: MetadataAtomic[]) =>
345
+ (description: string | null, jsDocTags: IJsDocTagInfo[]): string => {
346
+ const lines: string[] = [];
347
+ if (description?.length)
348
+ lines.push(...description.split("\n").map((s) => `${s}`));
349
+
350
+ const filtered: IJsDocTagInfo[] =
351
+ !!atomics.length && !!jsDocTags?.length
352
+ ? jsDocTags.filter(
353
+ (tag) =>
354
+ !atomics.some((a) =>
355
+ a.tags.some((r) => r.some((t) => t.kind === tag.name)),
356
+ ),
357
+ )
358
+ : (jsDocTags ?? []);
359
+
360
+ if (description?.length && filtered.length) lines.push("");
361
+ if (filtered.length)
362
+ lines.push(
363
+ ...filtered.map((t) =>
364
+ t.text?.length
365
+ ? `@${t.name} ${t.text.map((e) => e.text).join("")}`
366
+ : `@${t.name}`,
367
+ ),
368
+ );
369
+ return lines.join("\n");
370
+ };
371
+
372
+ const importInternalFile =
373
+ (project: INestiaProject) =>
374
+ (importer: ImportDictionary) =>
375
+ (name: string) => {
376
+ const top = name.split(".")[0];
377
+ if (importer.file === `${project.config.output}/structures/${top}.ts`)
378
+ return;
379
+ importer.internal({
380
+ type: true,
381
+ file: `${project.config.output}/structures/${name.split(".")[0]}`,
382
+ instance: top,
383
+ });
384
+ };