@nestia/sdk 1.0.4 → 1.0.5

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 (43) hide show
  1. package/README.md +12 -58
  2. package/assets/config/nestia.config.ts +70 -70
  3. package/lib/executable/internal/NestiaSdkConfig.js.map +1 -1
  4. package/lib/executable/sdk.js +16 -16
  5. package/lib/generates/FunctionGenerator.js +7 -7
  6. package/lib/generates/FunctionGenerator.js.map +1 -1
  7. package/lib/generates/SwaggerGenerator.js +11 -9
  8. package/lib/generates/SwaggerGenerator.js.map +1 -1
  9. package/lib/index.d.ts +1 -0
  10. package/lib/index.js +4 -0
  11. package/lib/index.js.map +1 -1
  12. package/package.json +2 -1
  13. package/src/INestiaConfig.ts +147 -147
  14. package/src/NestiaSdkApplication.ts +183 -183
  15. package/src/analyses/ControllerAnalyzer.ts +223 -223
  16. package/src/analyses/GenericAnalyzer.ts +53 -53
  17. package/src/analyses/ImportAnalyzer.ts +143 -143
  18. package/src/analyses/PathAnalyzer.ts +58 -58
  19. package/src/analyses/ReflectAnalyzer.ts +287 -287
  20. package/src/analyses/SourceFinder.ts +59 -59
  21. package/src/executable/internal/CommandParser.ts +15 -15
  22. package/src/executable/internal/NestiaConfigCompilerOptions.ts +18 -18
  23. package/src/executable/internal/NestiaSdkCommand.ts +174 -174
  24. package/src/executable/internal/NestiaSdkConfig.ts +36 -35
  25. package/src/executable/internal/nestia.config.getter.ts +12 -12
  26. package/src/executable/sdk.ts +74 -74
  27. package/src/generates/FileGenerator.ts +156 -156
  28. package/src/generates/FunctionGenerator.ts +323 -322
  29. package/src/generates/SdkGenerator.ts +50 -50
  30. package/src/generates/SwaggerGenerator.ts +433 -430
  31. package/src/index.ts +4 -3
  32. package/src/module.ts +2 -2
  33. package/src/structures/IController.ts +27 -27
  34. package/src/structures/IRoute.ts +33 -33
  35. package/src/structures/ISwaggerDocument.ts +119 -119
  36. package/src/structures/ITypeTuple.ts +6 -6
  37. package/src/structures/MethodType.ts +11 -11
  38. package/src/structures/ParamCategory.ts +1 -1
  39. package/src/structures/TypeEntry.ts +22 -22
  40. package/src/utils/ArrayUtil.ts +26 -26
  41. package/src/utils/ImportDictionary.ts +56 -56
  42. package/src/utils/MapUtil.ts +14 -14
  43. package/src/utils/StripEnums.ts +10 -10
@@ -1,74 +1,74 @@
1
- #!/usr/bin/env node
2
- import cp from "child_process";
3
- import fs from "fs";
4
- import process from "process";
5
-
6
- import { CommandParser } from "./internal/CommandParser";
7
- import type { NestiaSdkCommand } from "./internal/NestiaSdkCommand";
8
-
9
- const USAGE = `Wrong command has been detected. Use like below:
10
-
11
- npx @nestia/sdk [command] [options?]
12
-
13
- 1. npx @nestia/sdk dependencies --manager (npm|pnpm|yarn)
14
- - npx @nestia/sdk dependencies
15
- - npx @nestia/sdk dependencies --manager pnpm
16
- 2. npx @nestia/sdk init
17
- 3. npx @nestia/sdk sdk <input> --out <output>
18
- - npx @nestia/sdk sdk # when "nestia.config.ts" be configured
19
- - npx @nestia/sdk sdk src/controllers --out src/api
20
- - npx @nestia/sdk sdk src/**/*.controller.ts --out src/api
21
- 4. npx @nestia/sdk swagger <input> --out <output>
22
- - npx @nestia/sdk swagger # when "nestia.config.ts" be configured
23
- - npx @nestia/sdk swagger src/controllers --out src/api
24
- - npx @nestia/sdk swagger src/**/*.controller.ts --out src/api
25
- `;
26
-
27
- function halt(desc: string): never {
28
- console.error(desc);
29
- process.exit(-1);
30
- }
31
-
32
- function dependencies(argv: string[]): void {
33
- // INSTALL DEPENDENCIES
34
- const module = CommandParser.parse(argv).module ?? "npm";
35
- const prefix: string = module === "yarn" ? "yarn add" : `${module} install`;
36
-
37
- for (const lib of ["@nestia/fetcher", "typia"]) {
38
- const command: string = `${prefix} ${lib}`;
39
- cp.execSync(command, { stdio: "inherit" });
40
- }
41
- }
42
-
43
- async function initialize(): Promise<void> {
44
- if (fs.existsSync("nestia.config.ts") === true)
45
- halt(
46
- `Error on nestia.sdk.initialize(): "nestia.config.ts" file already has been configured.`,
47
- );
48
- await fs.promises.copyFile(
49
- `${__dirname}/../../assets/config/nestia.config.ts`,
50
- "nestia.config.ts",
51
- );
52
- }
53
-
54
- async function execute(
55
- closure: (commander: typeof NestiaSdkCommand) => Promise<void>,
56
- ): Promise<void> {
57
- const module = await import("./internal/NestiaSdkCommand");
58
- await closure(module.NestiaSdkCommand);
59
- }
60
-
61
- async function main() {
62
- const type: string | undefined = process.argv[2];
63
- const argv: string[] = process.argv.slice(3);
64
-
65
- if (type === "dependencies") dependencies(argv);
66
- else if (type === "init") await initialize();
67
- else if (type === "sdk") await execute((c) => c.sdk(argv));
68
- else if (type === "swagger") await execute((c) => c.swagger(argv));
69
- else halt(USAGE);
70
- }
71
- main().catch((exp) => {
72
- console.log(exp.message);
73
- process.exit(-1);
74
- });
1
+ #!/usr/bin/env node
2
+ import cp from "child_process";
3
+ import fs from "fs";
4
+ import process from "process";
5
+
6
+ import { CommandParser } from "./internal/CommandParser";
7
+ import type { NestiaSdkCommand } from "./internal/NestiaSdkCommand";
8
+
9
+ const USAGE = `Wrong command has been detected. Use like below:
10
+
11
+ npx @nestia/sdk [command] [options?]
12
+
13
+ 1. npx @nestia/sdk dependencies --manager (npm|pnpm|yarn)
14
+ - npx @nestia/sdk dependencies
15
+ - npx @nestia/sdk dependencies --manager pnpm
16
+ 2. npx @nestia/sdk init
17
+ 3. npx @nestia/sdk sdk <input> --out <output>
18
+ - npx @nestia/sdk sdk # when "nestia.config.ts" be configured
19
+ - npx @nestia/sdk sdk src/controllers --out src/api
20
+ - npx @nestia/sdk sdk src/**/*.controller.ts --out src/api
21
+ 4. npx @nestia/sdk swagger <input> --out <output>
22
+ - npx @nestia/sdk swagger # when "nestia.config.ts" be configured
23
+ - npx @nestia/sdk swagger src/controllers --out src/api
24
+ - npx @nestia/sdk swagger src/**/*.controller.ts --out src/api
25
+ `;
26
+
27
+ function halt(desc: string): never {
28
+ console.error(desc);
29
+ process.exit(-1);
30
+ }
31
+
32
+ function dependencies(argv: string[]): void {
33
+ // INSTALL DEPENDENCIES
34
+ const module = CommandParser.parse(argv).module ?? "npm";
35
+ const prefix: string = module === "yarn" ? "yarn add" : `${module} install`;
36
+
37
+ for (const lib of ["@nestia/fetcher", "typia"]) {
38
+ const command: string = `${prefix} ${lib}`;
39
+ cp.execSync(command, { stdio: "inherit" });
40
+ }
41
+ }
42
+
43
+ async function initialize(): Promise<void> {
44
+ if (fs.existsSync("nestia.config.ts") === true)
45
+ halt(
46
+ `Error on nestia.sdk.initialize(): "nestia.config.ts" file already has been configured.`,
47
+ );
48
+ await fs.promises.copyFile(
49
+ `${__dirname}/../../assets/config/nestia.config.ts`,
50
+ "nestia.config.ts",
51
+ );
52
+ }
53
+
54
+ async function execute(
55
+ closure: (commander: typeof NestiaSdkCommand) => Promise<void>,
56
+ ): Promise<void> {
57
+ const module = await import("./internal/NestiaSdkCommand");
58
+ await closure(module.NestiaSdkCommand);
59
+ }
60
+
61
+ async function main() {
62
+ const type: string | undefined = process.argv[2];
63
+ const argv: string[] = process.argv.slice(3);
64
+
65
+ if (type === "dependencies") dependencies(argv);
66
+ else if (type === "init") await initialize();
67
+ else if (type === "sdk") await execute((c) => c.sdk(argv));
68
+ else if (type === "swagger") await execute((c) => c.swagger(argv));
69
+ else halt(USAGE);
70
+ }
71
+ main().catch((exp) => {
72
+ console.log(exp.message);
73
+ process.exit(-1);
74
+ });
@@ -1,156 +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.length && str[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
- }
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.length && str[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
+ }