@nestia/sdk 1.3.4 → 1.3.6
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.
- package/assets/config/nestia.config.ts +84 -70
- package/lib/generates/internal/SdkFileProgrammer.js +6 -2
- package/lib/generates/internal/SdkFileProgrammer.js.map +1 -1
- package/lib/generates/internal/SdkFunctionProgrammer.js +16 -12
- package/lib/generates/internal/SdkFunctionProgrammer.js.map +1 -1
- package/lib/generates/internal/SdkSimulationProgrammer.d.ts +4 -0
- package/lib/generates/internal/SdkSimulationProgrammer.js +81 -0
- package/lib/generates/internal/SdkSimulationProgrammer.js.map +1 -0
- package/package.json +1 -1
- package/src/INestiaConfig.ts +204 -204
- package/src/generates/internal/DistributionComposer.ts +98 -98
- package/src/generates/internal/SdkFileProgrammer.ts +156 -150
- package/src/generates/internal/SdkFunctionProgrammer.ts +24 -15
- package/src/generates/internal/SdkSimulationProgrammer.ts +92 -0
|
@@ -1,150 +1,156 @@
|
|
|
1
|
-
import fs from "fs";
|
|
2
|
-
|
|
3
|
-
import { INestiaConfig } from "../../INestiaConfig";
|
|
4
|
-
import { IRoute } from "../../structures/IRoute";
|
|
5
|
-
import { ImportDictionary } from "../../utils/ImportDictionary";
|
|
6
|
-
import { SdkFunctionProgrammer } from "./SdkFunctionProgrammer";
|
|
7
|
-
import { SdkRouteDirectory } from "./SdkRouteDirectory";
|
|
8
|
-
|
|
9
|
-
export namespace SdkFileProgrammer {
|
|
10
|
-
/* ---------------------------------------------------------
|
|
11
|
-
CONSTRUCTOR
|
|
12
|
-
--------------------------------------------------------- */
|
|
13
|
-
export const generate =
|
|
14
|
-
(config: INestiaConfig) =>
|
|
15
|
-
async (routeList: IRoute[]): Promise<void> => {
|
|
16
|
-
// CONSTRUCT FOLDER TREE
|
|
17
|
-
const root: SdkRouteDirectory = new SdkRouteDirectory(
|
|
18
|
-
null,
|
|
19
|
-
"functional",
|
|
20
|
-
);
|
|
21
|
-
for (const route of routeList) emplace(root)(route);
|
|
22
|
-
|
|
23
|
-
// RELOCATE FOR ONLY ONE CONTROLLER METHOD IN AN URL CASE
|
|
24
|
-
relocate(root);
|
|
25
|
-
|
|
26
|
-
// ITERATE FILES
|
|
27
|
-
await iterate(config)(root)(config.output + "/functional");
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
const emplace =
|
|
31
|
-
(directory: SdkRouteDirectory) =>
|
|
32
|
-
(route: IRoute): void => {
|
|
33
|
-
// SEPARATE IDENTIFIERS
|
|
34
|
-
const identifiers: string[] = route.path
|
|
35
|
-
.split("/")
|
|
36
|
-
.filter((str) => str.length && str[0] !== ":")
|
|
37
|
-
.map((str) => str.split("-").join("_").split(".").join("_"));
|
|
38
|
-
|
|
39
|
-
// OPEN DIRECTORIES
|
|
40
|
-
for (const key of identifiers) {
|
|
41
|
-
directory = directory.directories.take(
|
|
42
|
-
key,
|
|
43
|
-
() => new SdkRouteDirectory(directory, key),
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// ADD ROUTE
|
|
48
|
-
directory.routes.push(route);
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
const relocate = (directory: SdkRouteDirectory): void => {
|
|
52
|
-
if (
|
|
53
|
-
directory.parent !== null &&
|
|
54
|
-
directory.directories.empty() &&
|
|
55
|
-
directory.routes.length === 1 &&
|
|
56
|
-
directory.name === directory.routes[0].name
|
|
57
|
-
) {
|
|
58
|
-
directory.parent.routes.push(directory.routes[0]);
|
|
59
|
-
directory.parent.directories.erase(directory.name);
|
|
60
|
-
} else if (directory.directories.empty() === false)
|
|
61
|
-
for (const it of directory.directories) relocate(it.second);
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
/* ---------------------------------------------------------
|
|
65
|
-
FILE ITERATOR
|
|
66
|
-
--------------------------------------------------------- */
|
|
67
|
-
const iterate =
|
|
68
|
-
(config: INestiaConfig) =>
|
|
69
|
-
(directory: SdkRouteDirectory) =>
|
|
70
|
-
async (outDir: string): Promise<void> => {
|
|
71
|
-
// CREATE A NEW DIRECTORY
|
|
72
|
-
try {
|
|
73
|
-
await fs.promises.mkdir(outDir);
|
|
74
|
-
} catch {}
|
|
75
|
-
|
|
76
|
-
// ITERATE CHILDREN
|
|
77
|
-
const content: string[] = [];
|
|
78
|
-
for (const it of directory.directories) {
|
|
79
|
-
await iterate(config)(it.second)(`${outDir}/${it.first}`);
|
|
80
|
-
content.push(`export * as ${it.first} from "./${it.first}";`);
|
|
81
|
-
}
|
|
82
|
-
if (content.length && directory.routes.length) content.push("");
|
|
83
|
-
|
|
84
|
-
// ITERATE ROUTES
|
|
85
|
-
const importDict: ImportDictionary = new ImportDictionary();
|
|
86
|
-
directory.routes.forEach((route, i) => {
|
|
87
|
-
for (const tuple of route.imports)
|
|
88
|
-
for (const instance of tuple[1])
|
|
89
|
-
importDict.emplace(tuple[0], false, instance);
|
|
90
|
-
|
|
91
|
-
content.push(SdkFunctionProgrammer.generate(config)(route));
|
|
92
|
-
if (i !== directory.routes.length - 1) content.push("");
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
// FINALIZE THE CONTENT
|
|
96
|
-
if (directory.routes.length !== 0) {
|
|
97
|
-
const primitived: boolean =
|
|
98
|
-
config.primitive !== false &&
|
|
99
|
-
directory.routes.some(
|
|
100
|
-
(route) =>
|
|
101
|
-
route.output.name !== "void" ||
|
|
102
|
-
route.parameters.some(
|
|
103
|
-
(param) => param.category !== "param",
|
|
104
|
-
),
|
|
105
|
-
);
|
|
106
|
-
const asserted: boolean =
|
|
107
|
-
config.assert === true &&
|
|
108
|
-
directory.routes.some(
|
|
109
|
-
(route) => route.parameters.length !== 0,
|
|
110
|
-
);
|
|
111
|
-
const json: boolean =
|
|
112
|
-
config.json === true &&
|
|
113
|
-
directory.routes.some(
|
|
114
|
-
(route) =>
|
|
115
|
-
route.method === "POST" ||
|
|
116
|
-
route.method === "PUT" ||
|
|
117
|
-
route.method === "PATCH",
|
|
118
|
-
);
|
|
119
|
-
const random: boolean =
|
|
120
|
-
config.random === true &&
|
|
121
|
-
directory.routes.some((s) => s.output.name !== "void");
|
|
122
|
-
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
|
|
3
|
+
import { INestiaConfig } from "../../INestiaConfig";
|
|
4
|
+
import { IRoute } from "../../structures/IRoute";
|
|
5
|
+
import { ImportDictionary } from "../../utils/ImportDictionary";
|
|
6
|
+
import { SdkFunctionProgrammer } from "./SdkFunctionProgrammer";
|
|
7
|
+
import { SdkRouteDirectory } from "./SdkRouteDirectory";
|
|
8
|
+
|
|
9
|
+
export namespace SdkFileProgrammer {
|
|
10
|
+
/* ---------------------------------------------------------
|
|
11
|
+
CONSTRUCTOR
|
|
12
|
+
--------------------------------------------------------- */
|
|
13
|
+
export const generate =
|
|
14
|
+
(config: INestiaConfig) =>
|
|
15
|
+
async (routeList: IRoute[]): Promise<void> => {
|
|
16
|
+
// CONSTRUCT FOLDER TREE
|
|
17
|
+
const root: SdkRouteDirectory = new SdkRouteDirectory(
|
|
18
|
+
null,
|
|
19
|
+
"functional",
|
|
20
|
+
);
|
|
21
|
+
for (const route of routeList) emplace(root)(route);
|
|
22
|
+
|
|
23
|
+
// RELOCATE FOR ONLY ONE CONTROLLER METHOD IN AN URL CASE
|
|
24
|
+
relocate(root);
|
|
25
|
+
|
|
26
|
+
// ITERATE FILES
|
|
27
|
+
await iterate(config)(root)(config.output + "/functional");
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const emplace =
|
|
31
|
+
(directory: SdkRouteDirectory) =>
|
|
32
|
+
(route: IRoute): void => {
|
|
33
|
+
// SEPARATE IDENTIFIERS
|
|
34
|
+
const identifiers: string[] = route.path
|
|
35
|
+
.split("/")
|
|
36
|
+
.filter((str) => str.length && str[0] !== ":")
|
|
37
|
+
.map((str) => str.split("-").join("_").split(".").join("_"));
|
|
38
|
+
|
|
39
|
+
// OPEN DIRECTORIES
|
|
40
|
+
for (const key of identifiers) {
|
|
41
|
+
directory = directory.directories.take(
|
|
42
|
+
key,
|
|
43
|
+
() => new SdkRouteDirectory(directory, key),
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// ADD ROUTE
|
|
48
|
+
directory.routes.push(route);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const relocate = (directory: SdkRouteDirectory): void => {
|
|
52
|
+
if (
|
|
53
|
+
directory.parent !== null &&
|
|
54
|
+
directory.directories.empty() &&
|
|
55
|
+
directory.routes.length === 1 &&
|
|
56
|
+
directory.name === directory.routes[0].name
|
|
57
|
+
) {
|
|
58
|
+
directory.parent.routes.push(directory.routes[0]);
|
|
59
|
+
directory.parent.directories.erase(directory.name);
|
|
60
|
+
} else if (directory.directories.empty() === false)
|
|
61
|
+
for (const it of directory.directories) relocate(it.second);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/* ---------------------------------------------------------
|
|
65
|
+
FILE ITERATOR
|
|
66
|
+
--------------------------------------------------------- */
|
|
67
|
+
const iterate =
|
|
68
|
+
(config: INestiaConfig) =>
|
|
69
|
+
(directory: SdkRouteDirectory) =>
|
|
70
|
+
async (outDir: string): Promise<void> => {
|
|
71
|
+
// CREATE A NEW DIRECTORY
|
|
72
|
+
try {
|
|
73
|
+
await fs.promises.mkdir(outDir);
|
|
74
|
+
} catch {}
|
|
75
|
+
|
|
76
|
+
// ITERATE CHILDREN
|
|
77
|
+
const content: string[] = [];
|
|
78
|
+
for (const it of directory.directories) {
|
|
79
|
+
await iterate(config)(it.second)(`${outDir}/${it.first}`);
|
|
80
|
+
content.push(`export * as ${it.first} from "./${it.first}";`);
|
|
81
|
+
}
|
|
82
|
+
if (content.length && directory.routes.length) content.push("");
|
|
83
|
+
|
|
84
|
+
// ITERATE ROUTES
|
|
85
|
+
const importDict: ImportDictionary = new ImportDictionary();
|
|
86
|
+
directory.routes.forEach((route, i) => {
|
|
87
|
+
for (const tuple of route.imports)
|
|
88
|
+
for (const instance of tuple[1])
|
|
89
|
+
importDict.emplace(tuple[0], false, instance);
|
|
90
|
+
|
|
91
|
+
content.push(SdkFunctionProgrammer.generate(config)(route));
|
|
92
|
+
if (i !== directory.routes.length - 1) content.push("");
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// FINALIZE THE CONTENT
|
|
96
|
+
if (directory.routes.length !== 0) {
|
|
97
|
+
const primitived: boolean =
|
|
98
|
+
config.primitive !== false &&
|
|
99
|
+
directory.routes.some(
|
|
100
|
+
(route) =>
|
|
101
|
+
route.output.name !== "void" ||
|
|
102
|
+
route.parameters.some(
|
|
103
|
+
(param) => param.category !== "param",
|
|
104
|
+
),
|
|
105
|
+
);
|
|
106
|
+
const asserted: boolean =
|
|
107
|
+
config.assert === true &&
|
|
108
|
+
directory.routes.some(
|
|
109
|
+
(route) => route.parameters.length !== 0,
|
|
110
|
+
);
|
|
111
|
+
const json: boolean =
|
|
112
|
+
config.json === true &&
|
|
113
|
+
directory.routes.some(
|
|
114
|
+
(route) =>
|
|
115
|
+
route.method === "POST" ||
|
|
116
|
+
route.method === "PUT" ||
|
|
117
|
+
route.method === "PATCH",
|
|
118
|
+
);
|
|
119
|
+
const random: boolean =
|
|
120
|
+
config.random === true &&
|
|
121
|
+
directory.routes.some((s) => s.output.name !== "void");
|
|
122
|
+
|
|
123
|
+
const classes: string[] = ["Fetcher"];
|
|
124
|
+
const typings: string[] = ["IConnection"];
|
|
125
|
+
if (primitived) typings.push("Primitive");
|
|
126
|
+
if (
|
|
127
|
+
config.random &&
|
|
128
|
+
directory.routes.some((r) => !!r.parameters.length)
|
|
129
|
+
)
|
|
130
|
+
classes.push("HttpError");
|
|
131
|
+
|
|
132
|
+
const head: string[] = [
|
|
133
|
+
`import { ${classes.join(", ")} } from "@nestia/fetcher";`,
|
|
134
|
+
`import type { ${typings.join(
|
|
135
|
+
", ",
|
|
136
|
+
)} } from "@nestia/fetcher";`,
|
|
137
|
+
];
|
|
138
|
+
if (asserted || json || random || classes.length === 2)
|
|
139
|
+
head.push(`import typia from "typia";`);
|
|
140
|
+
if (!importDict.empty())
|
|
141
|
+
head.push("", importDict.toScript(outDir));
|
|
142
|
+
|
|
143
|
+
content.push(...head, "", ...content.splice(0, content.length));
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const script: string =
|
|
147
|
+
"/**\n" +
|
|
148
|
+
" * @packageDocumentation\n" +
|
|
149
|
+
` * @module ${directory.module}\n` +
|
|
150
|
+
" * @nestia Generated by Nestia - https://github.com/samchon/nestia \n" +
|
|
151
|
+
" */\n" +
|
|
152
|
+
"//================================================================\n" +
|
|
153
|
+
content.join("\n");
|
|
154
|
+
await fs.promises.writeFile(`${outDir}/index.ts`, script, "utf8");
|
|
155
|
+
};
|
|
156
|
+
}
|
|
@@ -4,6 +4,7 @@ import { Escaper } from "typia/lib/utils/Escaper";
|
|
|
4
4
|
|
|
5
5
|
import { INestiaConfig } from "../../INestiaConfig";
|
|
6
6
|
import { IRoute } from "../../structures/IRoute";
|
|
7
|
+
import { SdkSimulationProgrammer } from "./SdkSimulationProgrammer";
|
|
7
8
|
|
|
8
9
|
export namespace SdkFunctionProgrammer {
|
|
9
10
|
export const generate =
|
|
@@ -62,18 +63,16 @@ export namespace SdkFunctionProgrammer {
|
|
|
62
63
|
// FUNCTION CALL STATEMENT
|
|
63
64
|
const caller = (awa: boolean) => {
|
|
64
65
|
const random = () =>
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
`${space(10)})`,
|
|
76
|
-
].join("\n");
|
|
66
|
+
[
|
|
67
|
+
`${route.name}.simulate(`,
|
|
68
|
+
` connection,`,
|
|
69
|
+
...route.parameters.map((p) => ` ${p.name},`),
|
|
70
|
+
`)`,
|
|
71
|
+
]
|
|
72
|
+
.map((line, i) =>
|
|
73
|
+
i === 0 ? line : `${space(10)}${line}`,
|
|
74
|
+
)
|
|
75
|
+
.join("\n");
|
|
77
76
|
const fetch = (tab: string) =>
|
|
78
77
|
[
|
|
79
78
|
`${awa ? "await " : ""}Fetcher.fetch(`,
|
|
@@ -175,9 +174,9 @@ export namespace SdkFunctionProgrammer {
|
|
|
175
174
|
const type: string =
|
|
176
175
|
config.primitive !== false &&
|
|
177
176
|
(param === props.query || param === props.input)
|
|
178
|
-
?
|
|
177
|
+
? `${route.name}.${
|
|
179
178
|
param === props.query ? "Query" : "Input"
|
|
180
|
-
}
|
|
179
|
+
}`
|
|
181
180
|
: param.type.name;
|
|
182
181
|
return `${param.name}${param.optional ? "?" : ""}: ${type}`;
|
|
183
182
|
}),
|
|
@@ -254,7 +253,14 @@ export namespace SdkFunctionProgrammer {
|
|
|
254
253
|
` };\n` +
|
|
255
254
|
"\n" +
|
|
256
255
|
` export const path = (${parameters
|
|
257
|
-
.map(
|
|
256
|
+
.map(
|
|
257
|
+
(param) =>
|
|
258
|
+
`${param.name}: ${
|
|
259
|
+
param.category === "query"
|
|
260
|
+
? `${route.name}.Query`
|
|
261
|
+
: param.type.name
|
|
262
|
+
}`,
|
|
263
|
+
)
|
|
258
264
|
.join(", ")}): string => {\n` +
|
|
259
265
|
`${path};\n` +
|
|
260
266
|
` }\n` +
|
|
@@ -262,6 +268,9 @@ export namespace SdkFunctionProgrammer {
|
|
|
262
268
|
? ` export const random = (g?: Partial<typia.IRandomGenerator>): Output =>\n` +
|
|
263
269
|
` typia.random<Output>(g);\n`
|
|
264
270
|
: "") +
|
|
271
|
+
(config.random
|
|
272
|
+
? SdkSimulationProgrammer.generate(route) + "\n"
|
|
273
|
+
: "") +
|
|
265
274
|
(config.json === true &&
|
|
266
275
|
route.parameters.find((param) => param.category === "body") !==
|
|
267
276
|
undefined
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { IRoute } from "../../structures/IRoute";
|
|
2
|
+
|
|
3
|
+
export namespace SdkSimulationProgrammer {
|
|
4
|
+
export const generate = (route: IRoute): string => {
|
|
5
|
+
const output: boolean = route.output.name !== "void";
|
|
6
|
+
const returns = () => [
|
|
7
|
+
`return typia.random<Output>(`,
|
|
8
|
+
` typeof connection.random === 'object'`,
|
|
9
|
+
` && connection.random !== null`,
|
|
10
|
+
` ? connection.random`,
|
|
11
|
+
` : undefined`,
|
|
12
|
+
`);`,
|
|
13
|
+
];
|
|
14
|
+
const body: string[] = [
|
|
15
|
+
...(route.parameters.length !== 0 ? assert(route.parameters) : []),
|
|
16
|
+
...route.parameters
|
|
17
|
+
.map((p) => [
|
|
18
|
+
`assert(`,
|
|
19
|
+
` ${message(p)}`,
|
|
20
|
+
`)(() => typia.assert(${p.name}));`,
|
|
21
|
+
])
|
|
22
|
+
.flat(),
|
|
23
|
+
...(output ? returns() : []),
|
|
24
|
+
];
|
|
25
|
+
|
|
26
|
+
return [
|
|
27
|
+
`export const simulate = async (`,
|
|
28
|
+
` ${
|
|
29
|
+
route.parameters.length === 0 && route.output.name === "void"
|
|
30
|
+
? "_connection"
|
|
31
|
+
: "connection"
|
|
32
|
+
}: IConnection,`,
|
|
33
|
+
...route.parameters.map(
|
|
34
|
+
(p) =>
|
|
35
|
+
` ${p.name}: ${
|
|
36
|
+
p.category === "query" || p.category === "body"
|
|
37
|
+
? `${route.name}.${
|
|
38
|
+
p.category === "query" ? "Query" : "Input"
|
|
39
|
+
}`
|
|
40
|
+
: p.type.name
|
|
41
|
+
},`,
|
|
42
|
+
),
|
|
43
|
+
`): Promise<${output ? "Output" : "void"}> => {`,
|
|
44
|
+
...body.map((l) => ` ${l}`),
|
|
45
|
+
`}`,
|
|
46
|
+
]
|
|
47
|
+
.map((line) => ` ${line}`)
|
|
48
|
+
.join("\n");
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const message = (p: IRoute.IParameter): string => {
|
|
52
|
+
if (p.category === "param")
|
|
53
|
+
return `() => "URL parameter \\"${p.name}\\" is not \${exp.expected} type."`;
|
|
54
|
+
else
|
|
55
|
+
return `() => "Request ${
|
|
56
|
+
p.category === "query" ? "query parameters are" : "body is"
|
|
57
|
+
} not following the promised type."`;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const assert = (parameters: IRoute.IParameter[]): string[] => {
|
|
61
|
+
return [
|
|
62
|
+
`const assert =`,
|
|
63
|
+
` (message: (exp: typia.TypeGuardError) => string) =>`,
|
|
64
|
+
` <T>(task: () => T): void => {`,
|
|
65
|
+
` try {`,
|
|
66
|
+
` task()`,
|
|
67
|
+
` }`,
|
|
68
|
+
` catch (exp) {`,
|
|
69
|
+
` if (typia.is<typia.TypeGuardError>(exp))`,
|
|
70
|
+
` throw new HttpError(`,
|
|
71
|
+
` METHOD,`,
|
|
72
|
+
" `${connection.host}${path(" +
|
|
73
|
+
parameters
|
|
74
|
+
.filter((p) => p.category !== "body")
|
|
75
|
+
.map((p) => p.name)
|
|
76
|
+
.join(", ") +
|
|
77
|
+
")}`,",
|
|
78
|
+
` 400,`,
|
|
79
|
+
` JSON.stringify({`,
|
|
80
|
+
` method: exp.method,`,
|
|
81
|
+
` path: exp.path,`,
|
|
82
|
+
` expected: exp.expected,`,
|
|
83
|
+
` value: exp.value,`,
|
|
84
|
+
` message: message(exp),`,
|
|
85
|
+
` })`,
|
|
86
|
+
` );`,
|
|
87
|
+
` throw exp`,
|
|
88
|
+
` }`,
|
|
89
|
+
` };`,
|
|
90
|
+
];
|
|
91
|
+
};
|
|
92
|
+
}
|