@nestia/sdk 2.5.0-dev.20240128 → 2.5.0-dev.20240129-2

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