@nestia/sdk 1.3.2 → 1.3.4

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 (51) hide show
  1. package/assets/config/nestia.config.ts +70 -70
  2. package/lib/INestiaConfig.d.ts +13 -0
  3. package/lib/executable/internal/NestiaSdkConfig.js +6 -2
  4. package/lib/executable/internal/NestiaSdkConfig.js.map +1 -1
  5. package/lib/executable/sdk.js +11 -11
  6. package/lib/generates/SwaggerGenerator.js +9 -9
  7. package/lib/generates/internal/DistributionComposer.js +1 -1
  8. package/lib/generates/internal/DistributionComposer.js.map +1 -1
  9. package/lib/generates/internal/E2eFileProgrammer.js +12 -12
  10. package/lib/generates/internal/SdkFileProgrammer.js +7 -5
  11. package/lib/generates/internal/SdkFileProgrammer.js.map +1 -1
  12. package/lib/generates/internal/SdkFunctionProgrammer.js +42 -49
  13. package/lib/generates/internal/SdkFunctionProgrammer.js.map +1 -1
  14. package/package.json +6 -4
  15. package/src/INestiaConfig.ts +204 -190
  16. package/src/NestiaSdkApplication.ts +262 -262
  17. package/src/analyses/ControllerAnalyzer.ts +261 -261
  18. package/src/analyses/GenericAnalyzer.ts +53 -53
  19. package/src/analyses/ImportAnalyzer.ts +164 -164
  20. package/src/analyses/PathAnalyzer.ts +58 -58
  21. package/src/analyses/ReflectAnalyzer.ts +321 -321
  22. package/src/executable/internal/CommandParser.ts +15 -15
  23. package/src/executable/internal/NestiaConfigCompilerOptions.ts +18 -18
  24. package/src/executable/internal/NestiaSdkCommand.ts +156 -156
  25. package/src/executable/internal/NestiaSdkConfig.ts +36 -36
  26. package/src/executable/internal/nestia.config.getter.ts +12 -12
  27. package/src/executable/sdk.ts +70 -70
  28. package/src/generates/E2eGenerator.ts +67 -67
  29. package/src/generates/SdkGenerator.ts +56 -56
  30. package/src/generates/SwaggerGenerator.ts +504 -504
  31. package/src/generates/internal/DistributionComposer.ts +98 -97
  32. package/src/generates/internal/E2eFileProgrammer.ts +135 -135
  33. package/src/generates/internal/SdkFileProgrammer.ts +150 -144
  34. package/src/generates/internal/SdkFunctionProgrammer.ts +51 -58
  35. package/src/generates/internal/SdkRouteDirectory.ts +21 -21
  36. package/src/index.ts +4 -4
  37. package/src/module.ts +2 -2
  38. package/src/structures/IController.ts +31 -31
  39. package/src/structures/IRoute.ts +39 -39
  40. package/src/structures/ISwaggerDocument.ts +120 -120
  41. package/src/structures/ITypeTuple.ts +6 -6
  42. package/src/structures/MethodType.ts +11 -11
  43. package/src/structures/ParamCategory.ts +1 -1
  44. package/src/structures/TypeEntry.ts +22 -22
  45. package/src/utils/ArrayUtil.ts +26 -26
  46. package/src/utils/FileRetriever.ts +22 -22
  47. package/src/utils/ImportDictionary.ts +56 -56
  48. package/src/utils/MapUtil.ts +14 -14
  49. package/src/utils/NestiaConfigUtil.ts +21 -21
  50. package/src/utils/SourceFinder.ts +60 -60
  51. package/src/utils/StripEnums.ts +10 -10
@@ -1,97 +1,98 @@
1
- import cp from "child_process";
2
- import fs from "fs";
3
- import path from "path";
4
-
5
- import { INestiaConfig } from "../../INestiaConfig";
6
-
7
- export namespace DistributionComposer {
8
- export const compose = async (config: INestiaConfig): Promise<void> => {
9
- if (!fs.existsSync(config.distribute!))
10
- await fs.promises.mkdir(config.distribute!);
11
-
12
- const root: string = process.cwd();
13
- const output: string = path.resolve(config.output!);
14
- process.chdir(config.distribute!);
15
-
16
- const exit = () => {
17
- process.chdir(root);
18
- };
19
-
20
- const typia: boolean = !!config.assert || !!config.json;
21
- const done: boolean = await configured({
22
- typia,
23
- distribute: config.distribute!,
24
- });
25
- if (done) return exit();
26
-
27
- // COPY FILES
28
- console.log("Composing SDK distribution environments...");
29
- for (const file of await fs.promises.readdir(BUNDLE))
30
- await fs.promises.copyFile(`${BUNDLE}/${file}`, file);
31
-
32
- // CONFIGURE PATHS
33
- for (const file of ["package.json", "tsconfig.json"])
34
- await replace({ root, output })(file);
35
-
36
- // INSTALL PACKAGES
37
- execute("npm install --save-dev rimraf");
38
- execute("npm install --save @nestia/fetcher@latest");
39
-
40
- if (typia) {
41
- execute("npm install --save typia@latest");
42
- execute("npx typia setup --manager npm");
43
- } else execute("npm install --save-dev typescript");
44
-
45
- exit();
46
- };
47
-
48
- const configured = async (config: {
49
- typia: boolean;
50
- distribute: string;
51
- }): Promise<boolean> =>
52
- ["package.json", "tsconfig.json"].every(fs.existsSync) &&
53
- (await (async () => {
54
- const content = JSON.parse(
55
- await fs.promises.readFile("package.json", "utf8"),
56
- );
57
- return (
58
- !!content.dependencies?.["@nestia/fetcher"] &&
59
- (config.typia === false || !!content.dependencies?.["typia"])
60
- );
61
- })()) &&
62
- (config.typia === false ||
63
- (await (async () => {
64
- const content = await fs.promises.readFile("tsconfig.json");
65
- return content.includes("typia/lib/transform");
66
- })()));
67
-
68
- const execute = (command: string) => {
69
- console.log(` - ${command}`);
70
- cp.execSync(command, { stdio: "ignore" });
71
- };
72
-
73
- const replace =
74
- (props: { root: string; output: string }) =>
75
- async (file: string): Promise<void> => {
76
- const relative = (from: string) => (to: string) =>
77
- path.relative(from, to).split("\\").join("/");
78
- const root: string = relative(process.cwd())(props.root);
79
- const output: string = relative(process.cwd())(props.output);
80
- const current: string = relative(props.root)(process.cwd());
81
-
82
- const content: string = await fs.promises.readFile(file, "utf8");
83
- await fs.promises.writeFile(
84
- file,
85
- content
86
- .split("${root}")
87
- .join(root)
88
- .split("${output}")
89
- .join(output)
90
- .split("${current}")
91
- .join(current),
92
- "utf8",
93
- );
94
- };
95
- }
96
-
97
- const BUNDLE = __dirname + "/../../../assets/bundle/distribute";
1
+ import cp from "child_process";
2
+ import fs from "fs";
3
+ import path from "path";
4
+
5
+ import { INestiaConfig } from "../../INestiaConfig";
6
+
7
+ export namespace DistributionComposer {
8
+ export const compose = async (config: INestiaConfig): Promise<void> => {
9
+ if (!fs.existsSync(config.distribute!))
10
+ await fs.promises.mkdir(config.distribute!);
11
+
12
+ const root: string = process.cwd();
13
+ const output: string = path.resolve(config.output!);
14
+ process.chdir(config.distribute!);
15
+
16
+ const exit = () => {
17
+ process.chdir(root);
18
+ };
19
+
20
+ const typia: boolean =
21
+ !!config.assert || !!config.json || !!config.random;
22
+ const done: boolean = await configured({
23
+ typia,
24
+ distribute: config.distribute!,
25
+ });
26
+ if (done) return exit();
27
+
28
+ // COPY FILES
29
+ console.log("Composing SDK distribution environments...");
30
+ for (const file of await fs.promises.readdir(BUNDLE))
31
+ await fs.promises.copyFile(`${BUNDLE}/${file}`, file);
32
+
33
+ // CONFIGURE PATHS
34
+ for (const file of ["package.json", "tsconfig.json"])
35
+ await replace({ root, output })(file);
36
+
37
+ // INSTALL PACKAGES
38
+ execute("npm install --save-dev rimraf");
39
+ execute("npm install --save @nestia/fetcher@latest");
40
+
41
+ if (typia) {
42
+ execute("npm install --save typia@latest");
43
+ execute("npx typia setup --manager npm");
44
+ } else execute("npm install --save-dev typescript");
45
+
46
+ exit();
47
+ };
48
+
49
+ const configured = async (config: {
50
+ typia: boolean;
51
+ distribute: string;
52
+ }): Promise<boolean> =>
53
+ ["package.json", "tsconfig.json"].every(fs.existsSync) &&
54
+ (await (async () => {
55
+ const content = JSON.parse(
56
+ await fs.promises.readFile("package.json", "utf8"),
57
+ );
58
+ return (
59
+ !!content.dependencies?.["@nestia/fetcher"] &&
60
+ (config.typia === false || !!content.dependencies?.["typia"])
61
+ );
62
+ })()) &&
63
+ (config.typia === false ||
64
+ (await (async () => {
65
+ const content = await fs.promises.readFile("tsconfig.json");
66
+ return content.includes("typia/lib/transform");
67
+ })()));
68
+
69
+ const execute = (command: string) => {
70
+ console.log(` - ${command}`);
71
+ cp.execSync(command, { stdio: "ignore" });
72
+ };
73
+
74
+ const replace =
75
+ (props: { root: string; output: string }) =>
76
+ async (file: string): Promise<void> => {
77
+ const relative = (from: string) => (to: string) =>
78
+ path.relative(from, to).split("\\").join("/");
79
+ const root: string = relative(process.cwd())(props.root);
80
+ const output: string = relative(process.cwd())(props.output);
81
+ const current: string = relative(props.root)(process.cwd());
82
+
83
+ const content: string = await fs.promises.readFile(file, "utf8");
84
+ await fs.promises.writeFile(
85
+ file,
86
+ content
87
+ .split("${root}")
88
+ .join(root)
89
+ .split("${output}")
90
+ .join(output)
91
+ .split("${current}")
92
+ .join(current),
93
+ "utf8",
94
+ );
95
+ };
96
+ }
97
+
98
+ const BUNDLE = __dirname + "/../../../assets/bundle/distribute";
@@ -1,135 +1,135 @@
1
- import fs from "fs";
2
- import path from "path";
3
-
4
- import { INestiaConfig } from "../../INestiaConfig";
5
- import { IRoute } from "../../structures/IRoute";
6
- import { ImportDictionary } from "../../utils/ImportDictionary";
7
-
8
- export namespace E2eFileProgrammer {
9
- export const generate =
10
- (config: INestiaConfig) =>
11
- (props: { api: string; current: string }) =>
12
- async (route: IRoute): Promise<void> => {
13
- const importDict: ImportDictionary = new ImportDictionary();
14
- for (const tuple of route.imports)
15
- for (const instance of tuple[1])
16
- importDict.emplace(tuple[0], false, instance);
17
-
18
- const additional: string[] = [];
19
- for (const param of route.parameters)
20
- if (param.category === "param")
21
- if (param.meta?.type === "uuid") additional.push(UUID);
22
- else if (param.meta?.type === "date") additional.push(DATE);
23
- const content: string = [
24
- ...(route.parameters.length || route.output.name !== "void"
25
- ? [
26
- config.primitive === false
27
- ? `import typia from "typia";`
28
- : `import typia, { Primitive } from "typia";`,
29
- "",
30
- ]
31
- : []),
32
- `import api from "./${path
33
- .relative(props.current, props.api)
34
- .split("\\")
35
- .join("/")}";`,
36
- ...(importDict.empty()
37
- ? []
38
- : [importDict.toScript(props.current)]),
39
- "",
40
- arrow(config)(route),
41
- ...(additional.length ? ["", ...additional] : []),
42
- ].join("\n");
43
-
44
- await fs.promises.writeFile(
45
- `${props.current}/${name(route)}.ts`,
46
- content,
47
- "utf8",
48
- );
49
- };
50
-
51
- const arrow =
52
- (config: INestiaConfig) =>
53
- (route: IRoute): string => {
54
- const tab: number = route.output.name === "void" ? 2 : 3;
55
- const output = [
56
- `await ${accessor(route)}(`,
57
- `${" ".repeat(tab * 4)}connection,`,
58
- ...route.parameters.map(parameter(config)(tab)),
59
- `${" ".repeat((tab - 1) * 4)});`,
60
- ].join("\n");
61
- return [
62
- `export const ${name(route)} = async (`,
63
- ` connection: api.IConnection`,
64
- `): Promise<void> => {`,
65
- ...(route.output.name === "void"
66
- ? [` ${output}`]
67
- : [
68
- ` const output: ${primitive(config)(
69
- route.output.name,
70
- )} = `,
71
- ` ${output}`,
72
- ` typia.assert(output);`,
73
- ]),
74
- `};`,
75
- ].join("\n");
76
- };
77
-
78
- const parameter =
79
- (config: INestiaConfig) =>
80
- (tab: number) =>
81
- (param: IRoute.IParameter): string => {
82
- const middle: string =
83
- param.category === "param" &&
84
- (param.meta?.type === "uuid" || param.meta?.type === "date")
85
- ? param.meta.nullable
86
- ? `Math.random() < .2 ? null : ${param.meta.type}()`
87
- : `${param.meta.type}()`
88
- : `typia.random<${primitive(config)(param.type.name)}>()`;
89
- return `${" ".repeat(4 * tab)}${middle},`;
90
- };
91
-
92
- const name = (route: IRoute): string =>
93
- postfix([
94
- "test_api",
95
- ...route.path
96
- .split("/")
97
- .filter((str) => str.length && str[0] !== ":")
98
- .map(normalize),
99
- ])(route.name).join("_");
100
-
101
- const accessor = (route: IRoute): string =>
102
- postfix([
103
- "api.functional",
104
- ...route.path
105
- .split("/")
106
- .filter((str) => str.length && str[0] !== ":")
107
- .map(normalize),
108
- ])(route.name).join(".");
109
-
110
- const normalize = (str: string) =>
111
- str.split("-").join("_").split(".").join("_");
112
-
113
- const postfix = (array: string[]) => (name: string) =>
114
- array.at(-1) === name ? array : [...array, name];
115
-
116
- const primitive =
117
- (config: INestiaConfig) =>
118
- (name: string): string =>
119
- config.primitive !== false ? `Primitive<${name}>` : name;
120
- }
121
-
122
- const UUID = `const uuid = (): string =>
123
- "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
124
- const r = (Math.random() * 16) | 0;
125
- const v = c === "x" ? r : (r & 0x3) | 0x8;
126
- return v.toString(16);
127
- });`;
128
- const DATE = `const date = (): string => {
129
- const date: Date = new Date(Math.floor(Math.random() * Date.now() * 2));
130
- return [
131
- date.getFullYear(),
132
- (date.getMonth() + 1).toString().padStart(2, "0"),
133
- date.getDate().toString().padStart(2, "0"),
134
- ].join("-");
135
- }`;
1
+ import fs from "fs";
2
+ import path from "path";
3
+
4
+ import { INestiaConfig } from "../../INestiaConfig";
5
+ import { IRoute } from "../../structures/IRoute";
6
+ import { ImportDictionary } from "../../utils/ImportDictionary";
7
+
8
+ export namespace E2eFileProgrammer {
9
+ export const generate =
10
+ (config: INestiaConfig) =>
11
+ (props: { api: string; current: string }) =>
12
+ async (route: IRoute): Promise<void> => {
13
+ const importDict: ImportDictionary = new ImportDictionary();
14
+ for (const tuple of route.imports)
15
+ for (const instance of tuple[1])
16
+ importDict.emplace(tuple[0], false, instance);
17
+
18
+ const additional: string[] = [];
19
+ for (const param of route.parameters)
20
+ if (param.category === "param")
21
+ if (param.meta?.type === "uuid") additional.push(UUID);
22
+ else if (param.meta?.type === "date") additional.push(DATE);
23
+ const content: string = [
24
+ ...(route.parameters.length || route.output.name !== "void"
25
+ ? [
26
+ config.primitive === false
27
+ ? `import typia from "typia";`
28
+ : `import typia, { Primitive } from "typia";`,
29
+ "",
30
+ ]
31
+ : []),
32
+ `import api from "./${path
33
+ .relative(props.current, props.api)
34
+ .split("\\")
35
+ .join("/")}";`,
36
+ ...(importDict.empty()
37
+ ? []
38
+ : [importDict.toScript(props.current)]),
39
+ "",
40
+ arrow(config)(route),
41
+ ...(additional.length ? ["", ...additional] : []),
42
+ ].join("\n");
43
+
44
+ await fs.promises.writeFile(
45
+ `${props.current}/${name(route)}.ts`,
46
+ content,
47
+ "utf8",
48
+ );
49
+ };
50
+
51
+ const arrow =
52
+ (config: INestiaConfig) =>
53
+ (route: IRoute): string => {
54
+ const tab: number = route.output.name === "void" ? 2 : 3;
55
+ const output = [
56
+ `await ${accessor(route)}(`,
57
+ `${" ".repeat(tab * 4)}connection,`,
58
+ ...route.parameters.map(parameter(config)(tab)),
59
+ `${" ".repeat((tab - 1) * 4)});`,
60
+ ].join("\n");
61
+ return [
62
+ `export const ${name(route)} = async (`,
63
+ ` connection: api.IConnection`,
64
+ `): Promise<void> => {`,
65
+ ...(route.output.name === "void"
66
+ ? [` ${output}`]
67
+ : [
68
+ ` const output: ${primitive(config)(
69
+ route.output.name,
70
+ )} = `,
71
+ ` ${output}`,
72
+ ` typia.assert(output);`,
73
+ ]),
74
+ `};`,
75
+ ].join("\n");
76
+ };
77
+
78
+ const parameter =
79
+ (config: INestiaConfig) =>
80
+ (tab: number) =>
81
+ (param: IRoute.IParameter): string => {
82
+ const middle: string =
83
+ param.category === "param" &&
84
+ (param.meta?.type === "uuid" || param.meta?.type === "date")
85
+ ? param.meta.nullable
86
+ ? `Math.random() < .2 ? null : ${param.meta.type}()`
87
+ : `${param.meta.type}()`
88
+ : `typia.random<${primitive(config)(param.type.name)}>()`;
89
+ return `${" ".repeat(4 * tab)}${middle},`;
90
+ };
91
+
92
+ const name = (route: IRoute): string =>
93
+ postfix([
94
+ "test_api",
95
+ ...route.path
96
+ .split("/")
97
+ .filter((str) => str.length && str[0] !== ":")
98
+ .map(normalize),
99
+ ])(route.name).join("_");
100
+
101
+ const accessor = (route: IRoute): string =>
102
+ postfix([
103
+ "api.functional",
104
+ ...route.path
105
+ .split("/")
106
+ .filter((str) => str.length && str[0] !== ":")
107
+ .map(normalize),
108
+ ])(route.name).join(".");
109
+
110
+ const normalize = (str: string) =>
111
+ str.split("-").join("_").split(".").join("_");
112
+
113
+ const postfix = (array: string[]) => (name: string) =>
114
+ array.at(-1) === name ? array : [...array, name];
115
+
116
+ const primitive =
117
+ (config: INestiaConfig) =>
118
+ (name: string): string =>
119
+ config.primitive !== false ? `Primitive<${name}>` : name;
120
+ }
121
+
122
+ const UUID = `const uuid = (): string =>
123
+ "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
124
+ const r = (Math.random() * 16) | 0;
125
+ const v = c === "x" ? r : (r & 0x3) | 0x8;
126
+ return v.toString(16);
127
+ });`;
128
+ const DATE = `const date = (): string => {
129
+ const date: Date = new Date(Math.floor(Math.random() * Date.now() * 2));
130
+ return [
131
+ date.getFullYear(),
132
+ (date.getMonth() + 1).toString().padStart(2, "0"),
133
+ date.getDate().toString().padStart(2, "0"),
134
+ ].join("-");
135
+ }`;