@nestia/sdk 0.1.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.
- package/assets/bundle/HttpError.ts +1 -0
- package/assets/bundle/IConnection.ts +1 -0
- package/assets/bundle/Primitive.ts +1 -0
- package/assets/config/nestia.config.ts +70 -0
- package/lib/INestiaConfig.d.ts +110 -0
- package/lib/INestiaConfig.js +3 -0
- package/lib/INestiaConfig.js.map +1 -0
- package/lib/NestiaSdkApplication.d.ts +11 -0
- package/lib/NestiaSdkApplication.js +156 -0
- package/lib/NestiaSdkApplication.js.map +1 -0
- package/lib/analyses/ControllerAnalyzer.d.ts +6 -0
- package/lib/analyses/ControllerAnalyzer.js +106 -0
- package/lib/analyses/ControllerAnalyzer.js.map +1 -0
- package/lib/analyses/GenericAnalyzer.d.ts +5 -0
- package/lib/analyses/GenericAnalyzer.js +41 -0
- package/lib/analyses/GenericAnalyzer.js.map +1 -0
- package/lib/analyses/ImportAnalyzer.d.ts +13 -0
- package/lib/analyses/ImportAnalyzer.js +86 -0
- package/lib/analyses/ImportAnalyzer.js.map +1 -0
- package/lib/analyses/PathAnalyzer.d.ts +5 -0
- package/lib/analyses/PathAnalyzer.js +51 -0
- package/lib/analyses/PathAnalyzer.js.map +1 -0
- package/lib/analyses/ReflectAnalyzer.d.ts +4 -0
- package/lib/analyses/ReflectAnalyzer.js +231 -0
- package/lib/analyses/ReflectAnalyzer.js.map +1 -0
- package/lib/analyses/SourceFinder.d.ts +4 -0
- package/lib/analyses/SourceFinder.js +71 -0
- package/lib/analyses/SourceFinder.js.map +1 -0
- package/lib/executable/internal/CommandParser.d.ts +3 -0
- package/lib/executable/internal/CommandParser.js +21 -0
- package/lib/executable/internal/CommandParser.js.map +1 -0
- package/lib/executable/internal/NestiaConfigCompilerOptions.d.ts +11 -0
- package/lib/executable/internal/NestiaConfigCompilerOptions.js +18 -0
- package/lib/executable/internal/NestiaConfigCompilerOptions.js.map +1 -0
- package/lib/executable/internal/NestiaSdkCommand.d.ts +4 -0
- package/lib/executable/internal/NestiaSdkCommand.js +128 -0
- package/lib/executable/internal/NestiaSdkCommand.js.map +1 -0
- package/lib/executable/internal/NestiaSdkConfig.d.ts +4 -0
- package/lib/executable/internal/NestiaSdkConfig.js +539 -0
- package/lib/executable/internal/NestiaSdkConfig.js.map +1 -0
- package/lib/executable/internal/nestia.config.getter.d.ts +1 -0
- package/lib/executable/internal/nestia.config.getter.js +24 -0
- package/lib/executable/internal/nestia.config.getter.js.map +1 -0
- package/lib/executable/sdk.d.ts +2 -0
- package/lib/executable/sdk.js +86 -0
- package/lib/executable/sdk.js.map +1 -0
- package/lib/generates/FileGenerator.d.ts +5 -0
- package/lib/generates/FileGenerator.js +138 -0
- package/lib/generates/FileGenerator.js.map +1 -0
- package/lib/generates/FunctionGenerator.d.ts +5 -0
- package/lib/generates/FunctionGenerator.js +204 -0
- package/lib/generates/FunctionGenerator.js.map +1 -0
- package/lib/generates/SdkGenerator.d.ts +7 -0
- package/lib/generates/SdkGenerator.js +45 -0
- package/lib/generates/SdkGenerator.js.map +1 -0
- package/lib/generates/SwaggerGenerator.d.ts +6 -0
- package/lib/generates/SwaggerGenerator.js +244 -0
- package/lib/generates/SwaggerGenerator.js.map +1 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +28 -0
- package/lib/index.js.map +1 -0
- package/lib/module.d.ts +2 -0
- package/lib/module.js +19 -0
- package/lib/module.js.map +1 -0
- package/lib/structures/IController.d.ts +23 -0
- package/lib/structures/IController.js +3 -0
- package/lib/structures/IController.js.map +1 -0
- package/lib/structures/IRoute.d.ts +24 -0
- package/lib/structures/IRoute.js +3 -0
- package/lib/structures/IRoute.js.map +1 -0
- package/lib/structures/ISwagger.d.ts +48 -0
- package/lib/structures/ISwagger.js +3 -0
- package/lib/structures/ISwagger.js.map +1 -0
- package/lib/structures/ITypeTuple.d.ts +5 -0
- package/lib/structures/ITypeTuple.js +3 -0
- package/lib/structures/ITypeTuple.js.map +1 -0
- package/lib/structures/MethodType.d.ts +4 -0
- package/lib/structures/MethodType.js +14 -0
- package/lib/structures/MethodType.js.map +1 -0
- package/lib/structures/ParamCategory.d.ts +1 -0
- package/lib/structures/ParamCategory.js +3 -0
- package/lib/structures/ParamCategory.js.map +1 -0
- package/lib/structures/TypeEntry.d.ts +9 -0
- package/lib/structures/TypeEntry.js +21 -0
- package/lib/structures/TypeEntry.js.map +1 -0
- package/lib/test/TestBuilder.d.ts +4 -0
- package/lib/test/TestBuilder.js +64 -0
- package/lib/test/TestBuilder.js.map +1 -0
- package/lib/test/index.d.ts +1 -0
- package/lib/test/index.js +61 -0
- package/lib/test/index.js.map +1 -0
- package/lib/test/test.builder.executor.d.ts +1 -0
- package/lib/test/test.builder.executor.js +24 -0
- package/lib/test/test.builder.executor.js.map +1 -0
- package/lib/utils/ArrayUtil.d.ts +5 -0
- package/lib/utils/ArrayUtil.js +39 -0
- package/lib/utils/ArrayUtil.js.map +1 -0
- package/lib/utils/ImportDictionary.d.ts +6 -0
- package/lib/utils/ImportDictionary.js +50 -0
- package/lib/utils/ImportDictionary.js.map +1 -0
- package/lib/utils/MapUtil.d.ts +3 -0
- package/lib/utils/MapUtil.js +16 -0
- package/lib/utils/MapUtil.js.map +1 -0
- package/lib/utils/StripEnums.d.ts +3 -0
- package/lib/utils/StripEnums.js +3 -0
- package/lib/utils/StripEnums.js.map +1 -0
- package/package.json +74 -0
- package/src/INestiaConfig.ts +120 -0
- package/src/NestiaSdkApplication.ts +183 -0
- package/src/analyses/ControllerAnalyzer.ts +203 -0
- package/src/analyses/GenericAnalyzer.ts +53 -0
- package/src/analyses/ImportAnalyzer.ts +143 -0
- package/src/analyses/PathAnalyzer.ts +58 -0
- package/src/analyses/ReflectAnalyzer.ts +279 -0
- package/src/analyses/SourceFinder.ts +59 -0
- package/src/executable/internal/CommandParser.ts +15 -0
- package/src/executable/internal/NestiaConfigCompilerOptions.ts +18 -0
- package/src/executable/internal/NestiaSdkCommand.ts +174 -0
- package/src/executable/internal/NestiaSdkConfig.ts +35 -0
- package/src/executable/internal/nestia.config.getter.ts +12 -0
- package/src/executable/sdk.ts +51 -0
- package/src/generates/FileGenerator.ts +156 -0
- package/src/generates/FunctionGenerator.ts +287 -0
- package/src/generates/SdkGenerator.ts +50 -0
- package/src/generates/SwaggerGenerator.ts +393 -0
- package/src/index.ts +3 -0
- package/src/module.ts +2 -0
- package/src/structures/IController.ts +27 -0
- package/src/structures/IRoute.ts +29 -0
- package/src/structures/ISwagger.ts +55 -0
- package/src/structures/ITypeTuple.ts +6 -0
- package/src/structures/MethodType.ts +11 -0
- package/src/structures/ParamCategory.ts +1 -0
- package/src/structures/TypeEntry.ts +22 -0
- package/src/utils/ArrayUtil.ts +26 -0
- package/src/utils/ImportDictionary.ts +56 -0
- package/src/utils/MapUtil.ts +14 -0
- package/src/utils/StripEnums.ts +10 -0
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import { HashMap } from "tstl/container/HashMap";
|
|
3
|
+
|
|
4
|
+
import { INestiaConfig } from "../INestiaConfig";
|
|
5
|
+
import { IRoute } from "../structures/IRoute";
|
|
6
|
+
import { ImportDictionary } from "../utils/ImportDictionary";
|
|
7
|
+
import { FunctionGenerator } from "./FunctionGenerator";
|
|
8
|
+
|
|
9
|
+
export namespace FileGenerator {
|
|
10
|
+
/* ---------------------------------------------------------
|
|
11
|
+
CONSTRUCTOR
|
|
12
|
+
--------------------------------------------------------- */
|
|
13
|
+
export async function generate(
|
|
14
|
+
config: INestiaConfig,
|
|
15
|
+
routeList: IRoute[],
|
|
16
|
+
): Promise<void> {
|
|
17
|
+
// CONSTRUCT FOLDER TREE
|
|
18
|
+
const root: Directory = new Directory(null, "functional");
|
|
19
|
+
for (const route of routeList) emplace(root, route);
|
|
20
|
+
|
|
21
|
+
// RELOCATE FOR ONLY ONE CONTROLLER METHOD IN AN URL CASE
|
|
22
|
+
relocate(root);
|
|
23
|
+
|
|
24
|
+
// ITERATE FILES
|
|
25
|
+
await iterate(config, config.output + "/functional", root);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function emplace(directory: Directory, route: IRoute): void {
|
|
29
|
+
// SEPARATE IDENTIFIERS
|
|
30
|
+
const identifiers: string[] = route.path
|
|
31
|
+
.split("/")
|
|
32
|
+
.filter((str) => str[0] !== ":" && str.length !== 0)
|
|
33
|
+
.map((str) => str.split("-").join("_").split(".").join("_"));
|
|
34
|
+
|
|
35
|
+
// OPEN DIRECTORIES
|
|
36
|
+
for (const key of identifiers) {
|
|
37
|
+
directory = directory.directories.take(
|
|
38
|
+
key,
|
|
39
|
+
() => new Directory(directory, key),
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// ADD ROUTE
|
|
44
|
+
directory.routes.push(route);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function relocate(directory: Directory): void {
|
|
48
|
+
if (
|
|
49
|
+
directory.parent !== null &&
|
|
50
|
+
directory.directories.empty() &&
|
|
51
|
+
directory.routes.length === 1 &&
|
|
52
|
+
directory.name === directory.routes[0].name
|
|
53
|
+
) {
|
|
54
|
+
directory.parent.routes.push(directory.routes[0]);
|
|
55
|
+
directory.parent.directories.erase(directory.name);
|
|
56
|
+
} else if (directory.directories.empty() === false)
|
|
57
|
+
for (const it of directory.directories) relocate(it.second);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/* ---------------------------------------------------------
|
|
61
|
+
FILE ITERATOR
|
|
62
|
+
--------------------------------------------------------- */
|
|
63
|
+
async function iterate(
|
|
64
|
+
config: INestiaConfig,
|
|
65
|
+
outDir: string,
|
|
66
|
+
directory: Directory,
|
|
67
|
+
): Promise<void> {
|
|
68
|
+
// CREATE A NEW DIRECTORY
|
|
69
|
+
try {
|
|
70
|
+
await fs.promises.mkdir(outDir);
|
|
71
|
+
} catch {}
|
|
72
|
+
|
|
73
|
+
// ITERATE CHILDREN
|
|
74
|
+
const content: string[] = [];
|
|
75
|
+
for (const it of directory.directories) {
|
|
76
|
+
await iterate(config, `${outDir}/${it.first}`, it.second);
|
|
77
|
+
content.push(`export * as ${it.first} from "./${it.first}";`);
|
|
78
|
+
}
|
|
79
|
+
if (content.length && directory.routes.length) content.push("");
|
|
80
|
+
|
|
81
|
+
// ITERATE ROUTES
|
|
82
|
+
const importDict: ImportDictionary = new ImportDictionary();
|
|
83
|
+
directory.routes.forEach((route, i) => {
|
|
84
|
+
for (const tuple of route.imports)
|
|
85
|
+
for (const instance of tuple[1])
|
|
86
|
+
importDict.emplace(tuple[0], false, instance);
|
|
87
|
+
|
|
88
|
+
content.push(FunctionGenerator.generate(config, route));
|
|
89
|
+
if (i !== directory.routes.length - 1) content.push("");
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
// FINALIZE THE CONTENT
|
|
93
|
+
if (directory.routes.length !== 0) {
|
|
94
|
+
const primitived: boolean =
|
|
95
|
+
config.primitive !== false &&
|
|
96
|
+
directory.routes.some(
|
|
97
|
+
(route) =>
|
|
98
|
+
route.output.name !== "void" ||
|
|
99
|
+
route.parameters.some(
|
|
100
|
+
(param) => param.category !== "param",
|
|
101
|
+
),
|
|
102
|
+
);
|
|
103
|
+
const asserted: boolean =
|
|
104
|
+
config.assert === true &&
|
|
105
|
+
directory.routes.some((route) => route.parameters.length !== 0);
|
|
106
|
+
const json: boolean =
|
|
107
|
+
config.json === true &&
|
|
108
|
+
directory.routes.some(
|
|
109
|
+
(route) =>
|
|
110
|
+
route.method === "POST" ||
|
|
111
|
+
route.method === "PUT" ||
|
|
112
|
+
route.method === "PATCH",
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
const fetcher: string[] = ["Fetcher"];
|
|
116
|
+
if (primitived) fetcher.push("Primitive");
|
|
117
|
+
|
|
118
|
+
const head: string[] = [
|
|
119
|
+
`import { ${fetcher.join(", ")} } from "@nestia/fetcher";`,
|
|
120
|
+
`import type { IConnection } from "@nestia/fetcher";`,
|
|
121
|
+
];
|
|
122
|
+
if (asserted || json) head.push(`import typia from "typia";`);
|
|
123
|
+
if (!importDict.empty()) head.push("", importDict.toScript(outDir));
|
|
124
|
+
|
|
125
|
+
content.push(...head, "", ...content.splice(0, content.length));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const script: string =
|
|
129
|
+
"/**\n" +
|
|
130
|
+
" * @packageDocumentation\n" +
|
|
131
|
+
` * @module ${directory.module}\n` +
|
|
132
|
+
" * @nestia Generated by Nestia - https://github.com/samchon/nestia \n" +
|
|
133
|
+
" */\n" +
|
|
134
|
+
"//================================================================\n" +
|
|
135
|
+
content.join("\n");
|
|
136
|
+
await fs.promises.writeFile(`${outDir}/index.ts`, script, "utf8");
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
class Directory {
|
|
141
|
+
public readonly module: string;
|
|
142
|
+
public readonly directories: HashMap<string, Directory>;
|
|
143
|
+
public readonly routes: IRoute[];
|
|
144
|
+
|
|
145
|
+
public constructor(
|
|
146
|
+
readonly parent: Directory | null,
|
|
147
|
+
readonly name: string,
|
|
148
|
+
) {
|
|
149
|
+
this.directories = new HashMap();
|
|
150
|
+
this.routes = [];
|
|
151
|
+
this.module =
|
|
152
|
+
this.parent !== null
|
|
153
|
+
? `${this.parent.module}.${name}`
|
|
154
|
+
: `api.${name}`;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import { Vector } from "tstl/container/Vector";
|
|
2
|
+
import { Pair } from "tstl/utility/Pair";
|
|
3
|
+
import ts from "typescript";
|
|
4
|
+
import { Escaper } from "typia/lib/utils/Escaper";
|
|
5
|
+
|
|
6
|
+
import { INestiaConfig } from "../INestiaConfig";
|
|
7
|
+
import { IRoute } from "../structures/IRoute";
|
|
8
|
+
|
|
9
|
+
export namespace FunctionGenerator {
|
|
10
|
+
export function generate(config: INestiaConfig, route: IRoute): string {
|
|
11
|
+
const query: IRoute.IParameter | undefined = route.parameters.find(
|
|
12
|
+
(param) => param.category === "query" && param.field === undefined,
|
|
13
|
+
);
|
|
14
|
+
const input: IRoute.IParameter | undefined = route.parameters.find(
|
|
15
|
+
(param) => param.category === "body",
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
return [head, body, tail]
|
|
19
|
+
.map((closure) => closure(route, query, input, config))
|
|
20
|
+
.filter((str) => !!str)
|
|
21
|
+
.join("\n");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/* ---------------------------------------------------------
|
|
25
|
+
BODY
|
|
26
|
+
--------------------------------------------------------- */
|
|
27
|
+
function body(
|
|
28
|
+
route: IRoute,
|
|
29
|
+
query: IRoute.IParameter | undefined,
|
|
30
|
+
input: IRoute.IParameter | undefined,
|
|
31
|
+
config: INestiaConfig,
|
|
32
|
+
): string {
|
|
33
|
+
// FETCH ARGUMENTS WITH REQUST BODY
|
|
34
|
+
const parameters = filter_parameters(route, query);
|
|
35
|
+
const fetchArguments: string[] = [
|
|
36
|
+
"connection",
|
|
37
|
+
`${route.name}.ENCRYPTED`,
|
|
38
|
+
`${route.name}.METHOD`,
|
|
39
|
+
`${route.name}.path(${parameters.map((p) => p.name).join(", ")})`,
|
|
40
|
+
];
|
|
41
|
+
if (input !== undefined) {
|
|
42
|
+
fetchArguments.push(input.name);
|
|
43
|
+
if (config.json === true)
|
|
44
|
+
fetchArguments.push(`${route.name}.stringify`);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const assertions: string =
|
|
48
|
+
config.assert === true && route.parameters.length !== 0
|
|
49
|
+
? route.parameters
|
|
50
|
+
.map(
|
|
51
|
+
(param) =>
|
|
52
|
+
` typia.assert<typeof ${param.name}>(${param.name});`,
|
|
53
|
+
)
|
|
54
|
+
.join("\n") + "\n\n"
|
|
55
|
+
: "";
|
|
56
|
+
|
|
57
|
+
// RETURNS WITH FINALIZATION
|
|
58
|
+
return (
|
|
59
|
+
"{\n" +
|
|
60
|
+
assertions +
|
|
61
|
+
" return Fetcher.fetch\n" +
|
|
62
|
+
" (\n" +
|
|
63
|
+
fetchArguments.map((param) => ` ${param}`).join(",\n") +
|
|
64
|
+
"\n" +
|
|
65
|
+
" );\n" +
|
|
66
|
+
"}"
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function filter_parameters(
|
|
71
|
+
route: IRoute,
|
|
72
|
+
query: IRoute.IParameter | undefined,
|
|
73
|
+
): IRoute.IParameter[] {
|
|
74
|
+
const parameters: IRoute.IParameter[] = route.parameters.filter(
|
|
75
|
+
(param) =>
|
|
76
|
+
param.category === "param" ||
|
|
77
|
+
(param.category === "query" && param.field !== undefined),
|
|
78
|
+
);
|
|
79
|
+
if (query) parameters.push(query);
|
|
80
|
+
return parameters;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/* ---------------------------------------------------------
|
|
84
|
+
HEAD & TAIL
|
|
85
|
+
--------------------------------------------------------- */
|
|
86
|
+
function head(
|
|
87
|
+
route: IRoute,
|
|
88
|
+
query: IRoute.IParameter | undefined,
|
|
89
|
+
input: IRoute.IParameter | undefined,
|
|
90
|
+
config: INestiaConfig,
|
|
91
|
+
): string {
|
|
92
|
+
//----
|
|
93
|
+
// CONSTRUCT COMMENT
|
|
94
|
+
//----
|
|
95
|
+
// MAIN DESCRIPTION
|
|
96
|
+
const comments: string[] = route.comments
|
|
97
|
+
.map((part) => `${part.kind === "linkText" ? " " : ""}${part.text}`)
|
|
98
|
+
.map((str) => str.split("\r\n").join("\n"))
|
|
99
|
+
.join("")
|
|
100
|
+
.split("\n")
|
|
101
|
+
.filter((str, i, array) => str !== "" || i !== array.length - 1);
|
|
102
|
+
if (comments.length) comments.push("");
|
|
103
|
+
|
|
104
|
+
// FILTER TAGS (VULNERABLE PARAMETERS WOULD BE REMOVED)
|
|
105
|
+
const tagList: ts.JSDocTagInfo[] = route.tags.filter(
|
|
106
|
+
(tag) => tag.text !== undefined,
|
|
107
|
+
);
|
|
108
|
+
if (tagList.length !== 0) {
|
|
109
|
+
const index: number = tagList.findIndex((t) => t.name === "param");
|
|
110
|
+
if (index !== -1) {
|
|
111
|
+
const capsule: Vector<ts.JSDocTagInfo> = Vector.wrap(tagList);
|
|
112
|
+
capsule.insert(capsule.nth(index), {
|
|
113
|
+
name: "param",
|
|
114
|
+
text: [
|
|
115
|
+
{
|
|
116
|
+
kind: "parameterName",
|
|
117
|
+
text: "connection",
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
kind: "space",
|
|
121
|
+
text: " ",
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
kind: "text",
|
|
125
|
+
text: "connection Information of the remote HTTP(s) server with headers (+encryption password)",
|
|
126
|
+
},
|
|
127
|
+
],
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
comments.push(
|
|
131
|
+
...tagList.map(
|
|
132
|
+
(tag) =>
|
|
133
|
+
`@${tag.name} ${tag
|
|
134
|
+
.text!.map((elem) => elem.text)
|
|
135
|
+
.join("")}`,
|
|
136
|
+
),
|
|
137
|
+
"",
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// COMPLETE THE COMMENT
|
|
142
|
+
comments.push(
|
|
143
|
+
`@controller ${route.symbol}`,
|
|
144
|
+
`@path ${route.method} ${route.path}`,
|
|
145
|
+
`@nestia Generated by Nestia - https://github.com/samchon/nestia`,
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
//----
|
|
149
|
+
// FINALIZATION
|
|
150
|
+
//----
|
|
151
|
+
// REFORM PARAMETERS TEXT
|
|
152
|
+
const parameters: string[] = [
|
|
153
|
+
"connection: IConnection",
|
|
154
|
+
...route.parameters.map((param) => {
|
|
155
|
+
const type: string =
|
|
156
|
+
config.primitive !== false &&
|
|
157
|
+
(param === query || param === input)
|
|
158
|
+
? `Primitive<${route.name}.${
|
|
159
|
+
param === query ? "Query" : "Input"
|
|
160
|
+
}>`
|
|
161
|
+
: param.type.name;
|
|
162
|
+
return `${param.name}: ${type}`;
|
|
163
|
+
}),
|
|
164
|
+
];
|
|
165
|
+
|
|
166
|
+
// OUTPUT TYPE
|
|
167
|
+
const output: string =
|
|
168
|
+
route.output.name === "void" ? "void" : `${route.name}.Output`;
|
|
169
|
+
|
|
170
|
+
// RETURNS WITH CONSTRUCTION
|
|
171
|
+
return (
|
|
172
|
+
"" +
|
|
173
|
+
"/**\n" +
|
|
174
|
+
comments.map((str) => ` * ${str}`).join("\n") +
|
|
175
|
+
"\n" +
|
|
176
|
+
" */\n" +
|
|
177
|
+
`export function ${route.name}\n` +
|
|
178
|
+
` (\n` +
|
|
179
|
+
`${parameters.map((str) => ` ${str}`).join(",\n")}\n` +
|
|
180
|
+
` ): Promise<${output}>`
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function tail(
|
|
185
|
+
route: IRoute,
|
|
186
|
+
query: IRoute.IParameter | undefined,
|
|
187
|
+
input: IRoute.IParameter | undefined,
|
|
188
|
+
config: INestiaConfig,
|
|
189
|
+
): string | null {
|
|
190
|
+
// LIST UP TYPES
|
|
191
|
+
const types: Pair<string, string>[] = [];
|
|
192
|
+
if (query !== undefined) types.push(new Pair("Query", query.type.name));
|
|
193
|
+
if (input !== undefined) types.push(new Pair("Input", input.type.name));
|
|
194
|
+
if (route.output.name !== "void")
|
|
195
|
+
types.push(new Pair("Output", route.output.name));
|
|
196
|
+
|
|
197
|
+
// PATH WITH PARAMETERS
|
|
198
|
+
const parameters: IRoute.IParameter[] = filter_parameters(route, query);
|
|
199
|
+
const path: string = compute_path(query, parameters, route.path);
|
|
200
|
+
|
|
201
|
+
return (
|
|
202
|
+
`export namespace ${route.name}\n` +
|
|
203
|
+
"{\n" +
|
|
204
|
+
(types.length !== 0
|
|
205
|
+
? types
|
|
206
|
+
.map(
|
|
207
|
+
(tuple) =>
|
|
208
|
+
` export type ${tuple.first} = ${
|
|
209
|
+
config.primitive !== false
|
|
210
|
+
? `Primitive<${tuple.second}>`
|
|
211
|
+
: tuple.second
|
|
212
|
+
};`,
|
|
213
|
+
)
|
|
214
|
+
.join("\n") + "\n"
|
|
215
|
+
: "") +
|
|
216
|
+
"\n" +
|
|
217
|
+
` export const METHOD = "${route.method}" as const;\n` +
|
|
218
|
+
` export const PATH: string = "${route.path}";\n` +
|
|
219
|
+
` export const ENCRYPTED: Fetcher.IEncrypted = {\n` +
|
|
220
|
+
` request: ${input !== undefined && input.encrypted},\n` +
|
|
221
|
+
` response: ${route.encrypted},\n` +
|
|
222
|
+
` };\n` +
|
|
223
|
+
"\n" +
|
|
224
|
+
` export function path(${parameters
|
|
225
|
+
.map((param) => `${param.name}: ${param.type.name}`)
|
|
226
|
+
.join(", ")}): string\n` +
|
|
227
|
+
` {\n` +
|
|
228
|
+
` return ${path};\n` +
|
|
229
|
+
` }\n` +
|
|
230
|
+
(config.json === true &&
|
|
231
|
+
(route.method === "POST" ||
|
|
232
|
+
route.method === "PUT" ||
|
|
233
|
+
route.method === "PATCH")
|
|
234
|
+
? ` export const stringify = (input: Input) => typia.stringify(input);\n`
|
|
235
|
+
: "") +
|
|
236
|
+
"}"
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function compute_path(
|
|
241
|
+
query: IRoute.IParameter | undefined,
|
|
242
|
+
parameters: IRoute.IParameter[],
|
|
243
|
+
path: string,
|
|
244
|
+
): string {
|
|
245
|
+
for (const param of parameters)
|
|
246
|
+
if (param.category === "param")
|
|
247
|
+
path = path.replace(
|
|
248
|
+
`:${param.field}`,
|
|
249
|
+
`\${encodeURIComponent(${param.name})}`,
|
|
250
|
+
);
|
|
251
|
+
const queryParams: IRoute.IParameter[] = parameters.filter(
|
|
252
|
+
(param) => param.category === "query" && param.field !== undefined,
|
|
253
|
+
);
|
|
254
|
+
if (query === undefined && queryParams.length === 0)
|
|
255
|
+
return `\`${path}\``;
|
|
256
|
+
|
|
257
|
+
const wrapper = (str: string) =>
|
|
258
|
+
`\`${path}?\${new URLSearchParams(${str}).toString()}\``;
|
|
259
|
+
if (query !== undefined && queryParams.length === 0)
|
|
260
|
+
return wrapper(`${query.name} as any`);
|
|
261
|
+
else if (query === undefined)
|
|
262
|
+
return wrapper(`
|
|
263
|
+
{
|
|
264
|
+
${rest_query_parameters(queryParams)}
|
|
265
|
+
} as any`);
|
|
266
|
+
|
|
267
|
+
return wrapper(`
|
|
268
|
+
{
|
|
269
|
+
...${query.name},
|
|
270
|
+
${rest_query_parameters(queryParams)},
|
|
271
|
+
} as any`);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function rest_query_parameters(parameters: IRoute.IParameter[]): string {
|
|
275
|
+
return parameters
|
|
276
|
+
.map((param) =>
|
|
277
|
+
param.name === param.field
|
|
278
|
+
? param.name
|
|
279
|
+
: `${
|
|
280
|
+
Escaper.variable(param.field!)
|
|
281
|
+
? param.field
|
|
282
|
+
: JSON.stringify(param.field)
|
|
283
|
+
}: ${param.name}`,
|
|
284
|
+
)
|
|
285
|
+
.join(`,\n${" ".repeat(12)}`);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import NodePath from "path";
|
|
3
|
+
import ts from "typescript";
|
|
4
|
+
|
|
5
|
+
import { INestiaConfig } from "../INestiaConfig";
|
|
6
|
+
import { IRoute } from "../structures/IRoute";
|
|
7
|
+
import { FileGenerator } from "./FileGenerator";
|
|
8
|
+
|
|
9
|
+
export namespace SdkGenerator {
|
|
10
|
+
export async function generate(
|
|
11
|
+
_checker: ts.TypeChecker,
|
|
12
|
+
config: INestiaConfig,
|
|
13
|
+
routeList: IRoute[],
|
|
14
|
+
): Promise<void> {
|
|
15
|
+
// PREPARE NEW DIRECTORIES
|
|
16
|
+
try {
|
|
17
|
+
await fs.promises.mkdir(config.output!);
|
|
18
|
+
} catch {}
|
|
19
|
+
|
|
20
|
+
// BUNDLING
|
|
21
|
+
const bundle: string[] = await fs.promises.readdir(BUNDLE_PATH);
|
|
22
|
+
for (const file of bundle) {
|
|
23
|
+
const current: string = `${BUNDLE_PATH}/${file}`;
|
|
24
|
+
const stats: fs.Stats = await fs.promises.stat(current);
|
|
25
|
+
|
|
26
|
+
if (stats.isFile() === true) {
|
|
27
|
+
const content: string = await fs.promises.readFile(
|
|
28
|
+
current,
|
|
29
|
+
"utf8",
|
|
30
|
+
);
|
|
31
|
+
await fs.promises.writeFile(
|
|
32
|
+
`${config.output}/${file}`,
|
|
33
|
+
content,
|
|
34
|
+
"utf8",
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// FUNCTIONAL
|
|
40
|
+
await FileGenerator.generate(config, routeList);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export const BUNDLE_PATH = NodePath.join(
|
|
44
|
+
__dirname,
|
|
45
|
+
"..",
|
|
46
|
+
"..",
|
|
47
|
+
"assets",
|
|
48
|
+
"bundle",
|
|
49
|
+
);
|
|
50
|
+
}
|