@nestia/sdk 0.2.0 → 1.0.1

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 (38) hide show
  1. package/assets/config/nestia.config.ts +79 -70
  2. package/lib/INestiaConfig.d.ts +18 -14
  3. package/lib/executable/sdk.js +16 -16
  4. package/lib/generates/FunctionGenerator.js +9 -9
  5. package/lib/generates/FunctionGenerator.js.map +1 -1
  6. package/lib/generates/SwaggerGenerator.js +9 -9
  7. package/package.json +3 -3
  8. package/src/INestiaConfig.ts +124 -120
  9. package/src/NestiaSdkApplication.ts +183 -183
  10. package/src/analyses/ControllerAnalyzer.ts +203 -203
  11. package/src/analyses/GenericAnalyzer.ts +53 -53
  12. package/src/analyses/ImportAnalyzer.ts +143 -143
  13. package/src/analyses/PathAnalyzer.ts +58 -58
  14. package/src/analyses/ReflectAnalyzer.ts +279 -279
  15. package/src/analyses/SourceFinder.ts +59 -59
  16. package/src/executable/internal/CommandParser.ts +15 -15
  17. package/src/executable/internal/NestiaConfigCompilerOptions.ts +18 -18
  18. package/src/executable/internal/NestiaSdkCommand.ts +174 -174
  19. package/src/executable/internal/NestiaSdkConfig.ts +35 -35
  20. package/src/executable/internal/nestia.config.getter.ts +12 -12
  21. package/src/executable/sdk.ts +74 -74
  22. package/src/generates/FileGenerator.ts +156 -156
  23. package/src/generates/FunctionGenerator.ts +284 -287
  24. package/src/generates/SdkGenerator.ts +50 -50
  25. package/src/generates/SwaggerGenerator.ts +393 -393
  26. package/src/index.ts +3 -3
  27. package/src/module.ts +2 -2
  28. package/src/structures/IController.ts +27 -27
  29. package/src/structures/IRoute.ts +29 -29
  30. package/src/structures/ISwagger.ts +55 -55
  31. package/src/structures/ITypeTuple.ts +6 -6
  32. package/src/structures/MethodType.ts +11 -11
  33. package/src/structures/ParamCategory.ts +1 -1
  34. package/src/structures/TypeEntry.ts +22 -22
  35. package/src/utils/ArrayUtil.ts +26 -26
  36. package/src/utils/ImportDictionary.ts +56 -56
  37. package/src/utils/MapUtil.ts +14 -14
  38. package/src/utils/StripEnums.ts +10 -10
@@ -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[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
- }
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
+ }