@nestia/sdk 2.5.0-dev.20240129 → 2.5.0-dev.20240130

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 (69) hide show
  1. package/lib/INestiaConfig.d.ts +6 -0
  2. package/lib/NestiaSdkApplication.js +1 -1
  3. package/lib/NestiaSdkApplication.js.map +1 -1
  4. package/lib/executable/internal/NestiaConfigLoader.js +6 -2
  5. package/lib/executable/internal/NestiaConfigLoader.js.map +1 -1
  6. package/lib/generates/CloneGenerator.d.ts +6 -0
  7. package/lib/generates/CloneGenerator.js +60 -0
  8. package/lib/generates/CloneGenerator.js.map +1 -0
  9. package/lib/generates/E2eGenerator.d.ts +2 -1
  10. package/lib/generates/E2eGenerator.js +2 -2
  11. package/lib/generates/E2eGenerator.js.map +1 -1
  12. package/lib/generates/SdkGenerator.js +3 -3
  13. package/lib/generates/SdkGenerator.js.map +1 -1
  14. package/lib/generates/SwaggerGenerator.js +11 -8
  15. package/lib/generates/SwaggerGenerator.js.map +1 -1
  16. package/lib/generates/internal/E2eFileProgrammer.d.ts +2 -1
  17. package/lib/generates/internal/E2eFileProgrammer.js +12 -16
  18. package/lib/generates/internal/E2eFileProgrammer.js.map +1 -1
  19. package/lib/generates/internal/SdkAliasCollection.d.ts +12 -0
  20. package/lib/generates/internal/SdkAliasCollection.js +89 -0
  21. package/lib/generates/internal/SdkAliasCollection.js.map +1 -0
  22. package/lib/generates/internal/SdkFileProgrammer.d.ts +2 -1
  23. package/lib/generates/internal/SdkFileProgrammer.js +5 -5
  24. package/lib/generates/internal/SdkFileProgrammer.js.map +1 -1
  25. package/lib/generates/internal/SdkFunctionProgrammer.js +6 -6
  26. package/lib/generates/internal/SdkFunctionProgrammer.js.map +1 -1
  27. package/lib/generates/internal/SdkInterfaceProgrammer.d.ts +12 -0
  28. package/lib/generates/internal/SdkInterfaceProgrammer.js +112 -0
  29. package/lib/generates/internal/SdkInterfaceProgrammer.js.map +1 -0
  30. package/lib/generates/internal/SdkNamespaceProgrammer.d.ts +1 -1
  31. package/lib/generates/internal/SdkNamespaceProgrammer.js +19 -18
  32. package/lib/generates/internal/SdkNamespaceProgrammer.js.map +1 -1
  33. package/lib/generates/internal/SdkRouteProgrammer.d.ts +1 -1
  34. package/lib/generates/internal/SdkRouteProgrammer.js +2 -2
  35. package/lib/generates/internal/SdkRouteProgrammer.js.map +1 -1
  36. package/lib/generates/internal/SdkSimulationProgrammer.d.ts +1 -1
  37. package/lib/generates/internal/SdkSimulationProgrammer.js +11 -12
  38. package/lib/generates/internal/SdkSimulationProgrammer.js.map +1 -1
  39. package/lib/generates/internal/{SdkDtoGenerator.d.ts → SdkTypeProgrammer.d.ts} +2 -4
  40. package/lib/generates/internal/SdkTypeProgrammer.js +160 -0
  41. package/lib/generates/internal/SdkTypeProgrammer.js.map +1 -0
  42. package/lib/structures/ISwagger.d.ts +1 -1
  43. package/lib/utils/FormatUtil.js +3 -1
  44. package/lib/utils/FormatUtil.js.map +1 -1
  45. package/package.json +3 -3
  46. package/src/INestiaConfig.ts +7 -0
  47. package/src/NestiaSdkApplication.ts +1 -1
  48. package/src/generates/CloneGenerator.ts +75 -0
  49. package/src/generates/E2eGenerator.ts +3 -1
  50. package/src/generates/SdkGenerator.ts +3 -3
  51. package/src/generates/SwaggerGenerator.ts +4 -2
  52. package/src/generates/internal/E2eFileProgrammer.ts +12 -19
  53. package/src/generates/internal/SdkAliasCollection.ts +145 -0
  54. package/src/generates/internal/SdkFileProgrammer.ts +5 -3
  55. package/src/generates/internal/SdkFunctionProgrammer.ts +9 -9
  56. package/src/generates/internal/SdkInterfaceProgrammer.ts +211 -0
  57. package/src/generates/internal/SdkNamespaceProgrammer.ts +31 -19
  58. package/src/generates/internal/SdkRouteProgrammer.ts +5 -1
  59. package/src/generates/internal/SdkSimulationProgrammer.ts +14 -16
  60. package/src/generates/internal/SdkTypeProgrammer.ts +252 -0
  61. package/src/structures/ISwagger.ts +1 -1
  62. package/src/utils/FormatUtil.ts +2 -1
  63. package/lib/generates/internal/SdkDtoGenerator.js +0 -295
  64. package/lib/generates/internal/SdkDtoGenerator.js.map +0 -1
  65. package/lib/generates/internal/SdkTypeDefiner.d.ts +0 -11
  66. package/lib/generates/internal/SdkTypeDefiner.js +0 -82
  67. package/lib/generates/internal/SdkTypeDefiner.js.map +0 -1
  68. package/src/generates/internal/SdkDtoGenerator.ts +0 -429
  69. package/src/generates/internal/SdkTypeDefiner.ts +0 -119
@@ -1,82 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SdkTypeDefiner = void 0;
4
- const SdkDtoGenerator_1 = require("./SdkDtoGenerator");
5
- var SdkTypeDefiner;
6
- (function (SdkTypeDefiner) {
7
- SdkTypeDefiner.name = (config) => (importer) => (p) => p.metadata
8
- ? SdkDtoGenerator_1.SdkDtoGenerator.decode(config)(importer)(p.metadata)
9
- : p.typeName;
10
- SdkTypeDefiner.headers = (config) => (importer) => (param) => {
11
- const type = SdkTypeDefiner.name(config)(importer)(param);
12
- if (config.primitive === false)
13
- return type;
14
- const resolved = importer.external({
15
- type: true,
16
- library: "@nestia/fetcher",
17
- instance: "Resolved",
18
- });
19
- return `${resolved}<${type}>`;
20
- };
21
- SdkTypeDefiner.query = (config) => (importer) => (param) => {
22
- const type = SdkTypeDefiner.name(config)(importer)(param);
23
- if (config.primitive === false)
24
- return type;
25
- const resolved = importer.external({
26
- type: true,
27
- library: "@nestia/fetcher",
28
- instance: "Resolved",
29
- });
30
- return `${resolved}<${type}>`;
31
- };
32
- SdkTypeDefiner.input = (config) => (importer) => (param) => {
33
- const type = SdkTypeDefiner.name(config)(importer)(param);
34
- if (config.primitive === false)
35
- return type;
36
- const primitive = importer.external({
37
- type: true,
38
- library: "@nestia/fetcher",
39
- instance: "Primitive",
40
- });
41
- return `${primitive}<${type}>`;
42
- };
43
- SdkTypeDefiner.output = (config) => (importer) => (route) => {
44
- var _a;
45
- if (config.propagate !== true) {
46
- const type = SdkTypeDefiner.name(config)(importer)(route.output);
47
- if (type === "void" || config.primitive === false)
48
- return type;
49
- const wrapper = importer.external({
50
- type: true,
51
- library: "@nestia/fetcher",
52
- instance: route.output.contentType === "application/x-www-form-urlencoded"
53
- ? "Resolved"
54
- : "Primitive",
55
- });
56
- return `${wrapper}<${type}>`;
57
- }
58
- const propagation = importer.external({
59
- type: true,
60
- library: "@nestia/fetcher",
61
- instance: "IPropagation",
62
- });
63
- const branches = [
64
- {
65
- status: String((_a = route.status) !== null && _a !== void 0 ? _a : (route.method === "POST" ? 201 : 200)),
66
- type: SdkTypeDefiner.name(config)(importer)(route.output),
67
- },
68
- ...Object.entries(route.exceptions).map(([status, value]) => ({
69
- status,
70
- type: SdkTypeDefiner.name(config)(importer)(value),
71
- })),
72
- ];
73
- return (`${propagation}<{\n` +
74
- branches
75
- .map((b) => ` ${b.status.endsWith("XX") ? `"${b.status}"` : b.status}: ${b.type};`)
76
- .join("\n") +
77
- "\n" +
78
- ` }${route.status ? `, ${route.status}` : ""}>`);
79
- };
80
- SdkTypeDefiner.responseBody = (config) => (importer) => (route) => SdkTypeDefiner.output(Object.assign(Object.assign({}, config), { propagate: false }))(importer)(route);
81
- })(SdkTypeDefiner || (exports.SdkTypeDefiner = SdkTypeDefiner = {}));
82
- //# sourceMappingURL=SdkTypeDefiner.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"SdkTypeDefiner.js","sourceRoot":"","sources":["../../../src/generates/internal/SdkTypeDefiner.ts"],"names":[],"mappings":";;;AAGA,uDAAoD;AAEpD,IAAiB,cAAc,CA4G9B;AA5GD,WAAiB,cAAc;IAChB,mBAAI,GACf,CAAC,MAAqB,EAAE,EAAE,CAC1B,CAAC,QAA0B,EAAE,EAAE,CAC/B,CAAC,CAAqC,EAAU,EAAE,CAChD,CAAC,CAAC,QAAQ;QACR,CAAC,CAAC,iCAAe,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,QAAQ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G,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;gBACrE,IAAI,EAAE,eAAA,IAAI,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC;aAC3C;YACD,GAAG,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC;gBAC5D,MAAM;gBACN,IAAI,EAAE,eAAA,IAAI,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC;aACpC,CAAC,CAAC;SACJ,CAAC;QACF,OAAO,CACL,GAAG,WAAW,MAAM;YACpB,QAAQ;iBACL,GAAG,CACF,CAAC,CAAC,EAAE,EAAE,CACJ,WACE,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,MAChD,KAAK,CAAC,CAAC,IAAI,GAAG,CACjB;iBACA,IAAI,CAAC,IAAI,CAAC;YACb,IAAI;YACJ,QAAQ,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,GAAG,CACnD,CAAC;IACJ,CAAC,CAAC;IAES,2BAAY,GACvB,CAAC,MAAqB,EAAE,EAAE,CAC1B,CAAC,QAA0B,EAAE,EAAE,CAC/B,CAAC,KAAa,EAAU,EAAE,CACxB,eAAA,MAAM,iCAAM,MAAM,KAAE,SAAS,EAAE,KAAK,IAAG,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,CAAC;AAC/D,CAAC,EA5GgB,cAAc,8BAAd,cAAc,QA4G9B"}
@@ -1,429 +0,0 @@
1
- import fs from "fs";
2
- import ts from "typescript";
3
- import { MetadataCollection } from "typia/lib/factories/MetadataCollection";
4
- import { MetadataFactory } from "typia/lib/factories/MetadataFactory";
5
- import { IJsDocTagInfo } from "typia/lib/schemas/metadata/IJsDocTagInfo";
6
- import { IMetadataTypeTag } from "typia/lib/schemas/metadata/IMetadataTypeTag";
7
- import { Metadata } from "typia/lib/schemas/metadata/Metadata";
8
- import { MetadataAlias } from "typia/lib/schemas/metadata/MetadataAlias";
9
- import { MetadataArray } from "typia/lib/schemas/metadata/MetadataArray";
10
- import { MetadataAtomic } from "typia/lib/schemas/metadata/MetadataAtomic";
11
- import { MetadataConstant } from "typia/lib/schemas/metadata/MetadataConstant";
12
- import { MetadataEscaped } from "typia/lib/schemas/metadata/MetadataEscaped";
13
- import { MetadataObject } from "typia/lib/schemas/metadata/MetadataObject";
14
- import { MetadataProperty } from "typia/lib/schemas/metadata/MetadataProperty";
15
- import { MetadataTuple } from "typia/lib/schemas/metadata/MetadataTuple";
16
- import { Escaper } from "typia/lib/utils/Escaper";
17
-
18
- import { INestiaConfig } from "../../INestiaConfig";
19
- import { IRoute } from "../../structures/IRoute";
20
- import { FormatUtil } from "../../utils/FormatUtil";
21
- import { ImportDictionary } from "../../utils/ImportDictionary";
22
- import { MapUtil } from "../../utils/MapUtil";
23
-
24
- export namespace SdkDtoGenerator {
25
- export const generate =
26
- (checker: ts.TypeChecker) =>
27
- (config: INestiaConfig) =>
28
- async (routes: IRoute[]): Promise<void> => {
29
- try {
30
- await fs.promises.mkdir(`${config.output}/structures`);
31
- } catch {}
32
-
33
- const collection = new MetadataCollection({
34
- replace: MetadataCollection.replace,
35
- });
36
- for (const r of routes) {
37
- for (const p of r.parameters) {
38
- const res = MetadataFactory.analyze(checker)({
39
- escape: false,
40
- constant: true,
41
- absorb: false,
42
- })(collection)(p.type);
43
- if (res.success) p.metadata = res.data;
44
- }
45
- for (const e of Object.values(r.exceptions)) {
46
- const res = MetadataFactory.analyze(checker)({
47
- escape: true,
48
- constant: true,
49
- absorb: false,
50
- })(collection)(e.type);
51
- if (res.success) e.metadata = res.data;
52
- }
53
- const res = MetadataFactory.analyze(checker)({
54
- escape: true,
55
- constant: true,
56
- absorb: false,
57
- })(collection)(r.output.type);
58
- if (res.success) r.output.metadata = res.data;
59
- }
60
-
61
- const modules: Map<string, IModule> = new Map();
62
- for (const alias of collection.aliases())
63
- prepare(modules)(alias.name)((importer) =>
64
- defineAlias(config)(importer)(alias),
65
- );
66
- for (const object of collection.objects())
67
- prepare(modules)(object.name)((importer) =>
68
- defineObject(config)(importer)(object),
69
- );
70
-
71
- for (const module of modules.values()) await generateFile(config)(module);
72
- };
73
-
74
- const prepare =
75
- (dict: Map<string, IModule>) =>
76
- (name: string) =>
77
- (programmer: (importer: ImportDictionary) => string) => {
78
- const accessors: string[] = name.split(".");
79
- let module: IModule;
80
-
81
- accessors.forEach((acc, i) => {
82
- module = MapUtil.take(dict, acc, () => ({
83
- name: accessors.slice(0, i + 1).join("."),
84
- children: new Map(),
85
- }));
86
- if (i === accessors.length - 1) module.programmer = programmer;
87
- dict = module.children;
88
- });
89
- return module!;
90
- };
91
-
92
- const generateFile =
93
- (config: INestiaConfig) =>
94
- async (module: IModule): Promise<void> => {
95
- const importer: ImportDictionary = new ImportDictionary(
96
- `${config.output}/structures/${module.name}.ts`,
97
- );
98
-
99
- const body: string = writeModule(importer)(module);
100
- const content: string[] = [];
101
- if (!importer.empty())
102
- content.push(importer.toScript(`${config.output}/structures`), "");
103
- content.push(body);
104
-
105
- await fs.promises.writeFile(
106
- importer.file,
107
- await FormatUtil.beautify(content.join("\n")),
108
- "utf8",
109
- );
110
- };
111
-
112
- const writeModule =
113
- (importer: ImportDictionary) =>
114
- (module: IModule): string => {
115
- const content: string[] = [];
116
- if (module.programmer) content.push(module.programmer(importer));
117
- if (module.children.size) {
118
- content.push(`export namespace ${module.name.split(".").at(-1)} {`);
119
- for (const child of module.children.values())
120
- content.push(
121
- writeModule(importer)(child)
122
- .split("\n")
123
- .map((l) => ` ${l}`)
124
- .join("\n"),
125
- );
126
- content.push("}");
127
- }
128
- return content.join("\n");
129
- };
130
-
131
- const defineAlias =
132
- (config: INestiaConfig) =>
133
- (importer: ImportDictionary) =>
134
- (alias: MetadataAlias) =>
135
- [
136
- ...writeComment(alias.value.atomics)(
137
- alias.description,
138
- alias.jsDocTags,
139
- ),
140
- `export type ${alias.name.split(".").at(-1)} = ${decode(config)(
141
- importer,
142
- )(alias.value)};`,
143
- ].join("\n");
144
-
145
- const defineObject =
146
- (config: INestiaConfig) =>
147
- (importer: ImportDictionary) =>
148
- (object: MetadataObject) => {
149
- const top: string = [
150
- ...writeComment([])(object.description ?? null, object.jsDocTags),
151
- `export type ${object.name.split(".").at(-1)} = `,
152
- ].join("\n");
153
- if (object.properties.length === 0) return top + "{};";
154
-
155
- const regular: MetadataProperty[] = object.properties.filter((p) =>
156
- p.key.isSoleLiteral(),
157
- );
158
- const dynamic: MetadataProperty[] = object.properties.filter(
159
- (p) => !p.key.isSoleLiteral(),
160
- );
161
-
162
- const brackets: string[][] = [];
163
- if (regular.length) {
164
- const row: string[] = ["{"];
165
- for (const p of regular) {
166
- const key: string = p.key.constants[0].values[0] as string;
167
- const identifier: string = Escaper.variable(key)
168
- ? key
169
- : JSON.stringify(key);
170
- row.push(
171
- ...writeComment(p.value.atomics)(p.description, p.jsDocTags).map(
172
- (l) => ` ${l}`,
173
- ),
174
- ` ${identifier}${
175
- p.value.isRequired() === false ? "?" : ""
176
- }: ${decode(config)(importer)(p.value)};`,
177
- );
178
- }
179
- row.push("}");
180
- brackets.push(row);
181
- }
182
- for (const p of dynamic) {
183
- const row: string[] = ["{"];
184
- row.push(
185
- ...writeComment(p.value.atomics)(p.description, p.jsDocTags).map(
186
- (l) => ` ${l}`,
187
- ),
188
- ` [key: ${decode(config)(importer)(p.key)}]: ${decode(config)(
189
- importer,
190
- )(p.value)};`,
191
- );
192
- row.push("}");
193
- brackets.push(row);
194
- }
195
- return top + brackets.map((row) => row.join("\n")).join(" & ");
196
- };
197
-
198
- const writeComment =
199
- (atomics: MetadataAtomic[]) =>
200
- (description: string | null, jsDocTags: IJsDocTagInfo[]): string[] => {
201
- const lines: string[] = [];
202
- if (description?.length)
203
- lines.push(...description.split("\n").map((s) => `${s}`));
204
-
205
- const filtered: IJsDocTagInfo[] =
206
- !!atomics.length && !!jsDocTags?.length
207
- ? jsDocTags.filter(
208
- (tag) =>
209
- !atomics.some((a) =>
210
- a.tags.some((r) => r.some((t) => t.kind === tag.name)),
211
- ),
212
- )
213
- : jsDocTags ?? [];
214
-
215
- if (description?.length && filtered.length) lines.push("");
216
- if (filtered.length)
217
- lines.push(
218
- ...filtered.map((t) =>
219
- t.text?.length
220
- ? `@${t.name} ${t.text.map((e) => e.text).join("")}`
221
- : `@${t.name}`,
222
- ),
223
- );
224
- if (lines.length === 0) return [];
225
- return ["/**", ...lines.map((s) => ` * ${s}`), " */"];
226
- };
227
-
228
- export const decode =
229
- (config: INestiaConfig) =>
230
- (importer: ImportDictionary) =>
231
- (meta: Metadata, parentEscaped: boolean = false): string => {
232
- const union: string[] = [];
233
-
234
- // COALESCES
235
- if (meta.any) union.push("any");
236
- if (meta.nullable) union.push("null");
237
- if (meta.isRequired() === false) union.push("undefined");
238
- if (parentEscaped === false && meta.escaped)
239
- union.push(decodeEscaped(config)(importer)(meta.escaped));
240
-
241
- // ATOMICS
242
- for (const atomic of meta.atomics)
243
- union.push(decodeAtomic(importer)(atomic));
244
- for (const constant of meta.constants)
245
- union.push(decodeConstant(constant));
246
- for (const tpl of meta.templates)
247
- union.push(decodeTemplate(config)(importer)(tpl));
248
-
249
- // ARRAYS
250
- for (const array of meta.arrays)
251
- union.push(decodeArray(config)(importer)(array));
252
- for (const tuple of meta.tuples)
253
- union.push(decodeTuple(config)(importer)(tuple));
254
-
255
- // OBJECTS
256
- for (const obj of meta.objects)
257
- union.push(decodeObject(config)(importer)(obj));
258
- for (const alias of meta.aliases)
259
- union.push(decodeAlias(config)(importer)(alias));
260
-
261
- // NATIVES
262
- for (const native of meta.natives) union.push(native);
263
- for (const set of meta.sets)
264
- union.push(`Set<${decode(config)(importer)(set)}>`);
265
- for (const map of meta.maps)
266
- union.push(
267
- `Map<${decode(config)(importer)(map.key)}, ${decode(config)(importer)(
268
- map.value,
269
- )}>`,
270
- );
271
- return union.join(" | ");
272
- };
273
-
274
- const decodeEscaped =
275
- (config: INestiaConfig) =>
276
- (importer: ImportDictionary) =>
277
- (escaped: MetadataEscaped) => {
278
- if (
279
- escaped.original.size() === 1 &&
280
- escaped.original.natives.length === 1 &&
281
- escaped.original.natives[0] === "Date"
282
- )
283
- return `(string & ${importer.external({
284
- type: true,
285
- library: `typia/lib/tags/Format`,
286
- instance: "Format",
287
- })}<"date-time">)`;
288
- return `(${decode(config)(importer)(escaped.returns, true)})`;
289
- };
290
-
291
- const decodeTypeTag =
292
- (importer: ImportDictionary) =>
293
- (tag: IMetadataTypeTag): string => {
294
- const front: string = tag.name.split("<")[0];
295
- if (NATIVE_TYPE_TAGS.has(front)) {
296
- importer.external({
297
- type: true,
298
- library: `typia/lib/tags/${front}`,
299
- instance: front,
300
- });
301
- return tag.name;
302
- }
303
- importer.external({
304
- type: true,
305
- library: `typia/lib/tags/TagBase`,
306
- instance: "TagBase",
307
- });
308
- return `TagBase<${JSON.stringify(tag)}>`;
309
- };
310
-
311
- const decodeTypeTagMatrix =
312
- (importer: ImportDictionary) =>
313
- (base: string, tags: IMetadataTypeTag[][]): string => {
314
- if (tags.length === 0) return base;
315
- else if (tags.length === 1)
316
- return `(${base} & ${tags[0]
317
- .map((t) => decodeTypeTag(importer)(t))
318
- .join(" & ")})`;
319
- return (
320
- "(" +
321
- [
322
- base,
323
- ...tags.map(
324
- (row) =>
325
- `(${row.map((t) => decodeTypeTag(importer)(t)).join(" & ")})`,
326
- ),
327
- ] +
328
- ")"
329
- );
330
- };
331
-
332
- const decodeAtomic =
333
- (importer: ImportDictionary) =>
334
- (atomic: MetadataAtomic): string =>
335
- decodeTypeTagMatrix(importer)(atomic.type, atomic.tags);
336
-
337
- const decodeTemplate =
338
- (config: INestiaConfig) =>
339
- (importer: ImportDictionary) =>
340
- (template: Metadata[]): string =>
341
- "`" +
342
- template
343
- .map((meta) =>
344
- meta.size() === 1 &&
345
- meta.isRequired() &&
346
- meta.nullable === false &&
347
- meta.constants.length === 1
348
- ? String(meta.constants[0].values[0]).split("`").join("\\`")
349
- : `\${${decode(config)(importer)(meta)}}`,
350
- )
351
- .join("") +
352
- "`";
353
-
354
- const decodeConstant = (constant: MetadataConstant): string => {
355
- if (constant.values.length === 0) return JSON.stringify(constant.values[0]);
356
- return `(${constant.values.map((val) => JSON.stringify(val)).join(" | ")})`;
357
- };
358
-
359
- const decodeArray =
360
- (config: INestiaConfig) =>
361
- (importer: ImportDictionary) =>
362
- (array: MetadataArray): string =>
363
- decodeTypeTagMatrix(importer)(
364
- `Array<${decode(config)(importer)(array.type.value)}>`,
365
- array.tags,
366
- );
367
-
368
- const decodeTuple =
369
- (config: INestiaConfig) =>
370
- (importer: ImportDictionary) =>
371
- (tuple: MetadataTuple): string =>
372
- "[" +
373
- tuple.type.elements.map((e) =>
374
- e.rest
375
- ? `...${decode(config)(importer)(e.rest)}[]`
376
- : decode(config)(importer)(e),
377
- ) +
378
- "]";
379
-
380
- const decodeAlias =
381
- (config: INestiaConfig) =>
382
- (importer: ImportDictionary) =>
383
- (alias: MetadataAlias) => {
384
- importInternalFile(config)(importer)(alias.name);
385
- return alias.name;
386
- };
387
-
388
- const decodeObject =
389
- (config: INestiaConfig) =>
390
- (importer: ImportDictionary) =>
391
- (object: MetadataObject) => {
392
- importInternalFile(config)(importer)(object.name);
393
- return object.name;
394
- };
395
-
396
- const importInternalFile =
397
- (config: INestiaConfig) =>
398
- (importer: ImportDictionary) =>
399
- (name: string) => {
400
- const top = name.split(".")[0];
401
- if (importer.file === `${config.output}/structures/${top}.ts`) return;
402
- importer.internal({
403
- type: true,
404
- file: `${config.output}/structures/${name.split(".")[0]}`,
405
- instance: top,
406
- });
407
- };
408
- }
409
-
410
- const NATIVE_TYPE_TAGS = new Set([
411
- "ExclusiveMinimum",
412
- "ExclusiveMaximum",
413
- "Format",
414
- "Maximum",
415
- "MaxItems",
416
- "MaxLength",
417
- "Minimum",
418
- "MinItems",
419
- "MinLength",
420
- "MultipleOf",
421
- "Pattern",
422
- "Type",
423
- ]);
424
-
425
- interface IModule {
426
- name: string;
427
- children: Map<string, IModule>;
428
- programmer?: (importer: ImportDictionary) => string;
429
- }
@@ -1,119 +0,0 @@
1
- import { INestiaConfig } from "../../INestiaConfig";
2
- import { IRoute } from "../../structures/IRoute";
3
- import { ImportDictionary } from "../../utils/ImportDictionary";
4
- import { SdkDtoGenerator } from "./SdkDtoGenerator";
5
-
6
- export namespace SdkTypeDefiner {
7
- export const name =
8
- (config: INestiaConfig) =>
9
- (importer: ImportDictionary) =>
10
- (p: IRoute.IParameter | IRoute.IOutput): string =>
11
- p.metadata
12
- ? SdkDtoGenerator.decode(config)(importer)(p.metadata)
13
- : p.typeName;
14
-
15
- export const headers =
16
- (config: INestiaConfig) =>
17
- (importer: ImportDictionary) =>
18
- (param: IRoute.IParameter): string => {
19
- const type: string = name(config)(importer)(param);
20
- if (config.primitive === false) return type;
21
-
22
- const resolved: string = importer.external({
23
- type: true,
24
- library: "@nestia/fetcher",
25
- instance: "Resolved",
26
- });
27
- return `${resolved}<${type}>`;
28
- };
29
-
30
- export const query =
31
- (config: INestiaConfig) =>
32
- (importer: ImportDictionary) =>
33
- (param: IRoute.IParameter): string => {
34
- const type: string = name(config)(importer)(param);
35
- if (config.primitive === false) return type;
36
-
37
- const resolved: string = importer.external({
38
- type: true,
39
- library: "@nestia/fetcher",
40
- instance: "Resolved",
41
- });
42
- return `${resolved}<${type}>`;
43
- };
44
-
45
- export const input =
46
- (config: INestiaConfig) =>
47
- (importer: ImportDictionary) =>
48
- (param: IRoute.IParameter): string => {
49
- const type: string = name(config)(importer)(param);
50
- if (config.primitive === false) return type;
51
-
52
- const primitive: string = importer.external({
53
- type: true,
54
- library: "@nestia/fetcher",
55
- instance: "Primitive",
56
- });
57
- return `${primitive}<${type}>`;
58
- };
59
-
60
- export const output =
61
- (config: INestiaConfig) =>
62
- (importer: ImportDictionary) =>
63
- (route: IRoute): string => {
64
- if (config.propagate !== true) {
65
- const type: string = name(config)(importer)(route.output);
66
- if (type === "void" || config.primitive === false) return type;
67
-
68
- const wrapper: string = importer.external({
69
- type: true,
70
- library: "@nestia/fetcher",
71
- instance:
72
- route.output.contentType === "application/x-www-form-urlencoded"
73
- ? "Resolved"
74
- : "Primitive",
75
- });
76
- return `${wrapper}<${type}>`;
77
- }
78
-
79
- const propagation: string = importer.external({
80
- type: true,
81
- library: "@nestia/fetcher",
82
- instance: "IPropagation",
83
- });
84
- const branches: IBranch[] = [
85
- {
86
- status: String(route.status ?? (route.method === "POST" ? 201 : 200)),
87
- type: name(config)(importer)(route.output),
88
- },
89
- ...Object.entries(route.exceptions).map(([status, value]) => ({
90
- status,
91
- type: name(config)(importer)(value),
92
- })),
93
- ];
94
- return (
95
- `${propagation}<{\n` +
96
- branches
97
- .map(
98
- (b) =>
99
- ` ${
100
- b.status.endsWith("XX") ? `"${b.status}"` : b.status
101
- }: ${b.type};`,
102
- )
103
- .join("\n") +
104
- "\n" +
105
- ` }${route.status ? `, ${route.status}` : ""}>`
106
- );
107
- };
108
-
109
- export const responseBody =
110
- (config: INestiaConfig) =>
111
- (importer: ImportDictionary) =>
112
- (route: IRoute): string =>
113
- output({ ...config, propagate: false })(importer)(route);
114
- }
115
-
116
- interface IBranch {
117
- status: string;
118
- type: string;
119
- }