@nestia/sdk 2.4.3 → 2.4.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 (70) hide show
  1. package/lib/INestiaConfig.d.ts +13 -0
  2. package/lib/analyses/ControllerAnalyzer.js +12 -1
  3. package/lib/analyses/ControllerAnalyzer.js.map +1 -1
  4. package/lib/analyses/PathAnalyzer.d.ts +2 -2
  5. package/lib/analyses/PathAnalyzer.js +27 -11
  6. package/lib/analyses/PathAnalyzer.js.map +1 -1
  7. package/lib/analyses/ReflectAnalyzer.js +11 -2
  8. package/lib/analyses/ReflectAnalyzer.js.map +1 -1
  9. package/lib/executable/internal/NestiaConfigLoader.js +5 -1
  10. package/lib/executable/internal/NestiaConfigLoader.js.map +1 -1
  11. package/lib/executable/sdk.js +11 -11
  12. package/lib/generates/SwaggerGenerator.js +16 -22
  13. package/lib/generates/SwaggerGenerator.js.map +1 -1
  14. package/lib/generates/internal/SwaggerSchemaGenerator.js +22 -15
  15. package/lib/generates/internal/SwaggerSchemaGenerator.js.map +1 -1
  16. package/lib/structures/ISwaggerComponents.d.ts +1 -1
  17. package/lib/structures/ISwaggerRoute.d.ts +3 -3
  18. package/package.json +5 -5
  19. package/src/INestiaConfig.ts +248 -234
  20. package/src/NestiaSdkApplication.ts +253 -253
  21. package/src/analyses/AccessorAnalyzer.ts +60 -60
  22. package/src/analyses/ConfigAnalyzer.ts +147 -147
  23. package/src/analyses/ControllerAnalyzer.ts +390 -379
  24. package/src/analyses/ExceptionAnalyzer.ts +115 -115
  25. package/src/analyses/GenericAnalyzer.ts +51 -51
  26. package/src/analyses/ImportAnalyzer.ts +138 -138
  27. package/src/analyses/PathAnalyzer.ts +110 -98
  28. package/src/analyses/ReflectAnalyzer.ts +11 -6
  29. package/src/analyses/SecurityAnalyzer.ts +20 -20
  30. package/src/executable/internal/CommandParser.ts +15 -15
  31. package/src/executable/internal/NestiaConfigLoader.ts +67 -67
  32. package/src/executable/internal/NestiaSdkCommand.ts +60 -60
  33. package/src/executable/sdk.ts +73 -73
  34. package/src/generates/E2eGenerator.ts +64 -64
  35. package/src/generates/SdkGenerator.ts +96 -96
  36. package/src/generates/SwaggerGenerator.ts +376 -372
  37. package/src/generates/internal/E2eFileProgrammer.ts +123 -123
  38. package/src/generates/internal/SdkDistributionComposer.ts +91 -91
  39. package/src/generates/internal/SdkDtoGenerator.ts +424 -424
  40. package/src/generates/internal/SdkFileProgrammer.ts +106 -106
  41. package/src/generates/internal/SdkImportWizard.ts +55 -55
  42. package/src/generates/internal/SdkRouteDirectory.ts +17 -17
  43. package/src/generates/internal/SdkSimulationProgrammer.ts +133 -133
  44. package/src/generates/internal/SdkTypeDefiner.ts +119 -119
  45. package/src/generates/internal/SwaggerSchemaGenerator.ts +18 -2
  46. package/src/generates/internal/SwaggerSchemaValidator.ts +198 -198
  47. package/src/index.ts +4 -4
  48. package/src/module.ts +2 -2
  49. package/src/structures/IErrorReport.ts +6 -6
  50. package/src/structures/INestiaProject.ts +13 -13
  51. package/src/structures/INormalizedInput.ts +20 -20
  52. package/src/structures/ISwagger.ts +91 -91
  53. package/src/structures/ISwaggerComponents.ts +29 -29
  54. package/src/structures/ISwaggerError.ts +8 -8
  55. package/src/structures/ISwaggerInfo.ts +80 -80
  56. package/src/structures/ISwaggerLazyProperty.ts +7 -7
  57. package/src/structures/ISwaggerLazySchema.ts +7 -7
  58. package/src/structures/ISwaggerRoute.ts +51 -51
  59. package/src/structures/ISwaggerSecurityScheme.ts +65 -65
  60. package/src/structures/ITypeTuple.ts +6 -6
  61. package/src/structures/MethodType.ts +5 -5
  62. package/src/structures/ParamCategory.ts +1 -1
  63. package/src/structures/TypeEntry.ts +22 -22
  64. package/src/utils/ArrayUtil.ts +26 -26
  65. package/src/utils/FileRetriever.ts +22 -22
  66. package/src/utils/ImportDictionary.ts +125 -125
  67. package/src/utils/MapUtil.ts +14 -14
  68. package/src/utils/PathUtil.ts +10 -10
  69. package/src/utils/SourceFinder.ts +66 -66
  70. package/src/utils/StripEnums.ts +5 -5
@@ -1,98 +1,110 @@
1
- import { RequestMethod } from "@nestjs/common";
2
- import path from "path";
3
- import { Token, parse } from "path-to-regexp";
4
-
5
- import { INormalizedInput } from "../structures/INormalizedInput";
6
-
7
- export namespace PathAnalyzer {
8
- export const combinate =
9
- (globalPrefix: INormalizedInput["globalPrefix"]) =>
10
- (versions: Array<string | null>) =>
11
- (props: { path: string; method: string }): string[] => {
12
- const out = (str: string) =>
13
- versions.map((v) => (v === null ? str : join(v, str)));
14
- if (!globalPrefix?.prefix.length) return out(props.path);
15
- else if (!globalPrefix.exclude?.length)
16
- return out(props.path).map((str) => join(globalPrefix.prefix, str));
17
- return globalPrefix.exclude.some((exclude) =>
18
- typeof exclude === "string"
19
- ? RegExp(exclude).test(props.path)
20
- : METHOD(exclude.method) === props.method &&
21
- RegExp(exclude.path).test(props.path),
22
- )
23
- ? out(props.path)
24
- : out(props.path).map((str) => join(globalPrefix.prefix, str));
25
- };
26
-
27
- export const join = (...args: string[]) =>
28
- "/" +
29
- _Trim(
30
- path
31
- .join(...args.filter((s) => !!s.length))
32
- .split("\\")
33
- .join("/"),
34
- );
35
-
36
- export const escape = (str: string, method: () => string) =>
37
- "/" +
38
- _Parse(str, method)
39
- .map((arg) => (arg.type === "param" ? `:${arg.value}` : arg.value))
40
- .join("/");
41
-
42
- export const parameters = (str: string, method: () => string) =>
43
- _Parse(str, method)
44
- .filter((arg) => arg.type === "param")
45
- .map((arg) => arg.value);
46
-
47
- function _Parse(str: string, method: () => string): IArgument[] {
48
- const tokens: Token[] = parse(path.join(str).split("\\").join("/"));
49
- const output: IArgument[] = [];
50
-
51
- for (const key of tokens) {
52
- if (typeof key === "string")
53
- output.push({
54
- type: "path",
55
- value: _Trim(key),
56
- });
57
- else if (typeof key.name === "number" || _Trim(key.name) === "")
58
- throw new Error(`Error on ${method}: ${ERROR_MESSAGE}.`);
59
- else
60
- output.push({
61
- type: "param",
62
- value: _Trim(key.name),
63
- });
64
- }
65
- return output;
66
- }
67
-
68
- function _Trim(str: string): string {
69
- if (str[0] === "/") str = str.substring(1);
70
- if (str[str.length - 1] === "/") str = str.substring(0, str.length - 1);
71
- return str;
72
- }
73
-
74
- interface IArgument {
75
- type: "param" | "path";
76
- value: string;
77
- }
78
- }
79
-
80
- const ERROR_MESSAGE = "nestia supports only string typed parameter on path";
81
- const METHOD = (value: RequestMethod) =>
82
- value === RequestMethod.ALL
83
- ? "all"
84
- : value === RequestMethod.DELETE
85
- ? "delete"
86
- : value === RequestMethod.GET
87
- ? "get"
88
- : value === RequestMethod.HEAD
89
- ? "head"
90
- : value === RequestMethod.OPTIONS
91
- ? "options"
92
- : value === RequestMethod.PATCH
93
- ? "patch"
94
- : value === RequestMethod.POST
95
- ? "post"
96
- : value === RequestMethod.PUT
97
- ? "put"
98
- : "unknown";
1
+ import { RequestMethod } from "@nestjs/common";
2
+ import path from "path";
3
+ import { Token, parse } from "path-to-regexp";
4
+
5
+ import { INormalizedInput } from "../structures/INormalizedInput";
6
+
7
+ export namespace PathAnalyzer {
8
+ export const combinate =
9
+ (globalPrefix: INormalizedInput["globalPrefix"]) =>
10
+ (versions: Array<string | null>) =>
11
+ (props: { path: string; method: string }): string[] => {
12
+ const out = (str: string) =>
13
+ versions.map((v) => (v === null ? str : join(v, str)));
14
+ if (!globalPrefix?.prefix.length) return out(props.path);
15
+ else if (!globalPrefix.exclude?.length)
16
+ return out(props.path).map((str) => join(globalPrefix.prefix, str));
17
+ return globalPrefix.exclude.some((exclude) =>
18
+ typeof exclude === "string"
19
+ ? RegExp(exclude).test(props.path)
20
+ : METHOD(exclude.method) === props.method &&
21
+ RegExp(exclude.path).test(props.path),
22
+ )
23
+ ? out(props.path)
24
+ : out(props.path).map((str) => join(globalPrefix.prefix, str));
25
+ };
26
+
27
+ export const join = (...args: string[]) =>
28
+ "/" +
29
+ _Trim(
30
+ path
31
+ .join(...args.filter((s) => !!s.length))
32
+ .split("\\")
33
+ .join("/"),
34
+ );
35
+
36
+ export const escape = (str: string): string | null => {
37
+ const args = _Parse(str);
38
+ if (args === null) return null;
39
+ return (
40
+ "/" +
41
+ args
42
+ .map((arg) => (arg.type === "param" ? `:${arg.value}` : arg.value))
43
+ .join("/")
44
+ );
45
+ };
46
+
47
+ export const parameters = (str: string): string[] | null => {
48
+ const args = _Parse(str);
49
+ if (args === null) return null;
50
+ return args.filter((arg) => arg.type === "param").map((arg) => arg.value);
51
+ };
52
+
53
+ function _Parse(str: string): IArgument[] | null {
54
+ const tokens: Token[] | null = (() => {
55
+ try {
56
+ return parse(path.join(str).split("\\").join("/"));
57
+ } catch {
58
+ return null;
59
+ }
60
+ })();
61
+ if (tokens === null) return null;
62
+
63
+ const output: IArgument[] = [];
64
+ for (const key of tokens) {
65
+ if (typeof key === "string")
66
+ output.push({
67
+ type: "path",
68
+ value: _Trim(key),
69
+ });
70
+ else if (typeof key.name === "number" || _Trim(key.name) === "")
71
+ return null;
72
+ else
73
+ output.push({
74
+ type: "param",
75
+ value: _Trim(key.name),
76
+ });
77
+ }
78
+ return output;
79
+ }
80
+
81
+ function _Trim(str: string): string {
82
+ if (str[0] === "/") str = str.substring(1);
83
+ if (str[str.length - 1] === "/") str = str.substring(0, str.length - 1);
84
+ return str;
85
+ }
86
+
87
+ interface IArgument {
88
+ type: "param" | "path";
89
+ value: string;
90
+ }
91
+ }
92
+
93
+ const METHOD = (value: RequestMethod) =>
94
+ value === RequestMethod.ALL
95
+ ? "all"
96
+ : value === RequestMethod.DELETE
97
+ ? "delete"
98
+ : value === RequestMethod.GET
99
+ ? "get"
100
+ : value === RequestMethod.HEAD
101
+ ? "head"
102
+ : value === RequestMethod.OPTIONS
103
+ ? "options"
104
+ : value === RequestMethod.PATCH
105
+ ? "patch"
106
+ : value === RequestMethod.POST
107
+ ? "post"
108
+ : value === RequestMethod.PUT
109
+ ? "put"
110
+ : "unknown";
@@ -310,18 +310,23 @@ export namespace ReflectAnalyzer {
310
310
  if (location.includes("*")) continue;
311
311
 
312
312
  // LIST UP PARAMETERS
313
- const binded: string[] = PathAnalyzer.parameters(
314
- location,
315
- () => `${controller.name}.${name}()`,
316
- ).sort();
317
-
313
+ const binded: string[] | null = PathAnalyzer.parameters(location);
314
+ if (binded === null) {
315
+ project.errors.push({
316
+ file: controller.file,
317
+ controller: controller.name,
318
+ function: name,
319
+ message: `invalid path ("${location}")`,
320
+ });
321
+ continue;
322
+ }
318
323
  const parameters: string[] = meta.parameters
319
324
  .filter((param) => param.category === "param")
320
325
  .map((param) => param.field!)
321
326
  .sort();
322
327
 
323
328
  // DO VALIDATE
324
- if (equal(binded, parameters) === false)
329
+ if (equal(binded.sort(), parameters) === false)
325
330
  errors.push({
326
331
  file: controller.file,
327
332
  controller: controller.name,
@@ -1,20 +1,20 @@
1
- import { MapUtil } from "../utils/MapUtil";
2
-
3
- export namespace SecurityAnalyzer {
4
- export const merge = (...entire: Record<string, string[]>[]) => {
5
- const dict: Map<string, Set<string>> = new Map();
6
- for (const obj of entire)
7
- for (const [key, value] of Object.entries(obj)) {
8
- const set = MapUtil.take(dict, key, () => new Set());
9
- for (const val of value) set.add(val);
10
- }
11
- const output: Record<string, string[]>[] = [];
12
- for (const [key, set] of dict) {
13
- const obj = {
14
- [key]: [...set],
15
- };
16
- output.push(obj);
17
- }
18
- return output;
19
- };
20
- }
1
+ import { MapUtil } from "../utils/MapUtil";
2
+
3
+ export namespace SecurityAnalyzer {
4
+ export const merge = (...entire: Record<string, string[]>[]) => {
5
+ const dict: Map<string, Set<string>> = new Map();
6
+ for (const obj of entire)
7
+ for (const [key, value] of Object.entries(obj)) {
8
+ const set = MapUtil.take(dict, key, () => new Set());
9
+ for (const val of value) set.add(val);
10
+ }
11
+ const output: Record<string, string[]>[] = [];
12
+ for (const [key, set] of dict) {
13
+ const obj = {
14
+ [key]: [...set],
15
+ };
16
+ output.push(obj);
17
+ }
18
+ return output;
19
+ };
20
+ }
@@ -1,15 +1,15 @@
1
- export namespace CommandParser {
2
- export function parse(argList: string[]): Record<string, string> {
3
- const output: Record<string, string> = {};
4
- argList.forEach((arg, i) => {
5
- if (arg.startsWith("--") === false) return;
6
-
7
- const key = arg.slice(2);
8
- const value: string | undefined = argList[i + 1];
9
- if (value === undefined || value.startsWith("--")) return;
10
-
11
- output[key] = value;
12
- });
13
- return output;
14
- }
15
- }
1
+ export namespace CommandParser {
2
+ export function parse(argList: string[]): Record<string, string> {
3
+ const output: Record<string, string> = {};
4
+ argList.forEach((arg, i) => {
5
+ if (arg.startsWith("--") === false) return;
6
+
7
+ const key = arg.slice(2);
8
+ const value: string | undefined = argList[i + 1];
9
+ if (value === undefined || value.startsWith("--")) return;
10
+
11
+ output[key] = value;
12
+ });
13
+ return output;
14
+ }
15
+ }
@@ -1,67 +1,67 @@
1
- import fs from "fs";
2
- import path from "path";
3
- import { register } from "ts-node";
4
- import { parseNative } from "tsconfck";
5
- import ts from "typescript";
6
- import typia from "typia";
7
-
8
- import { INestiaConfig } from "../../INestiaConfig";
9
-
10
- export namespace NestiaConfigLoader {
11
- export const compilerOptions = async (
12
- project: string,
13
- ): Promise<ts.CompilerOptions> => {
14
- const configFileName = ts.findConfigFile(
15
- process.cwd(),
16
- ts.sys.fileExists,
17
- project,
18
- );
19
- if (!configFileName) throw new Error(`unable to find "${project}" file.`);
20
-
21
- const { tsconfig } = await parseNative(configFileName);
22
- const configFileText = JSON.stringify(tsconfig);
23
- const { config } = ts.parseConfigFileTextToJson(
24
- configFileName,
25
- configFileText,
26
- );
27
- const configParseResult = ts.parseJsonConfigFileContent(
28
- config,
29
- ts.sys,
30
- path.dirname(configFileName),
31
- );
32
-
33
- const { moduleResolution, ...result } =
34
- configParseResult.raw.compilerOptions;
35
- return result;
36
- };
37
-
38
- export const config = async (
39
- file: string,
40
- options: ts.CompilerOptions,
41
- ): Promise<INestiaConfig> => {
42
- if (fs.existsSync(path.resolve(file)) === false)
43
- throw new Error(`Unable to find "${file}" file.`);
44
-
45
- register({
46
- emit: false,
47
- compilerOptions: options,
48
- require: options.baseUrl ? ["tsconfig-paths/register"] : undefined,
49
- });
50
-
51
- const loaded: INestiaConfig & { default?: INestiaConfig } = await import(
52
- path.resolve(file)
53
- );
54
- const config: INestiaConfig =
55
- typeof loaded?.default === "object" && loaded.default !== null
56
- ? loaded.default
57
- : loaded;
58
-
59
- try {
60
- return typia.assert(config);
61
- } catch (exp) {
62
- if (typia.is<typia.TypeGuardError>(exp))
63
- exp.message = `invalid "${file}" data.`;
64
- throw exp;
65
- }
66
- };
67
- }
1
+ import fs from "fs";
2
+ import path from "path";
3
+ import { register } from "ts-node";
4
+ import { parseNative } from "tsconfck";
5
+ import ts from "typescript";
6
+ import typia from "typia";
7
+
8
+ import { INestiaConfig } from "../../INestiaConfig";
9
+
10
+ export namespace NestiaConfigLoader {
11
+ export const compilerOptions = async (
12
+ project: string,
13
+ ): Promise<ts.CompilerOptions> => {
14
+ const configFileName = ts.findConfigFile(
15
+ process.cwd(),
16
+ ts.sys.fileExists,
17
+ project,
18
+ );
19
+ if (!configFileName) throw new Error(`unable to find "${project}" file.`);
20
+
21
+ const { tsconfig } = await parseNative(configFileName);
22
+ const configFileText = JSON.stringify(tsconfig);
23
+ const { config } = ts.parseConfigFileTextToJson(
24
+ configFileName,
25
+ configFileText,
26
+ );
27
+ const configParseResult = ts.parseJsonConfigFileContent(
28
+ config,
29
+ ts.sys,
30
+ path.dirname(configFileName),
31
+ );
32
+
33
+ const { moduleResolution, ...result } =
34
+ configParseResult.raw.compilerOptions;
35
+ return result;
36
+ };
37
+
38
+ export const config = async (
39
+ file: string,
40
+ options: ts.CompilerOptions,
41
+ ): Promise<INestiaConfig> => {
42
+ if (fs.existsSync(path.resolve(file)) === false)
43
+ throw new Error(`Unable to find "${file}" file.`);
44
+
45
+ register({
46
+ emit: false,
47
+ compilerOptions: options,
48
+ require: options.baseUrl ? ["tsconfig-paths/register"] : undefined,
49
+ });
50
+
51
+ const loaded: INestiaConfig & { default?: INestiaConfig } = await import(
52
+ path.resolve(file)
53
+ );
54
+ const config: INestiaConfig =
55
+ typeof loaded?.default === "object" && loaded.default !== null
56
+ ? loaded.default
57
+ : loaded;
58
+
59
+ try {
60
+ return typia.assert(config);
61
+ } catch (exp) {
62
+ if (typia.is<typia.TypeGuardError>(exp))
63
+ exp.message = `invalid "${file}" data.`;
64
+ throw exp;
65
+ }
66
+ };
67
+ }
@@ -1,60 +1,60 @@
1
- import ts from "typescript";
2
-
3
- import { INestiaConfig } from "../../INestiaConfig";
4
- import { NestiaSdkApplication } from "../../NestiaSdkApplication";
5
- import { NestiaConfigLoader } from "./NestiaConfigLoader";
6
-
7
- export namespace NestiaSdkCommand {
8
- export const sdk = () => main((app) => app.sdk());
9
- export const swagger = () => main((app) => app.swagger());
10
- export const e2e = () => main((app) => app.e2e());
11
-
12
- const main = async (task: (app: NestiaSdkApplication) => Promise<void>) => {
13
- await generate(task);
14
- };
15
-
16
- const generate = async (
17
- task: (app: NestiaSdkApplication) => Promise<void>,
18
- ) => {
19
- // LOAD CONFIG INFO
20
- const compilerOptions: ts.CompilerOptions =
21
- await NestiaConfigLoader.compilerOptions(
22
- getFileArgument({
23
- type: "project",
24
- extension: "json",
25
- }) ?? "tsconfig.json",
26
- );
27
- const config: INestiaConfig = await NestiaConfigLoader.config(
28
- getFileArgument({
29
- type: "config",
30
- extension: "ts",
31
- }) ?? "nestia.config.ts",
32
- compilerOptions,
33
- );
34
-
35
- // GENERATE
36
- const app: NestiaSdkApplication = new NestiaSdkApplication(
37
- config,
38
- compilerOptions,
39
- );
40
- await task(app);
41
- };
42
-
43
- const getFileArgument = (props: {
44
- type: string;
45
- extension: string;
46
- }): string | null => {
47
- const argv: string[] = process.argv.slice(3);
48
- if (argv.length === 0) return null;
49
-
50
- const index: number = argv.findIndex((str) => str === `--${props.type}`);
51
- if (index === -1) return null;
52
- else if (argv.length === 1)
53
- throw new Error(`${props.type} file must be provided`);
54
-
55
- const file: string = argv[index + 1];
56
- if (file.endsWith(props.extension) === false)
57
- throw new Error(`${props.type} file must be ${props.extension} file`);
58
- return file;
59
- };
60
- }
1
+ import ts from "typescript";
2
+
3
+ import { INestiaConfig } from "../../INestiaConfig";
4
+ import { NestiaSdkApplication } from "../../NestiaSdkApplication";
5
+ import { NestiaConfigLoader } from "./NestiaConfigLoader";
6
+
7
+ export namespace NestiaSdkCommand {
8
+ export const sdk = () => main((app) => app.sdk());
9
+ export const swagger = () => main((app) => app.swagger());
10
+ export const e2e = () => main((app) => app.e2e());
11
+
12
+ const main = async (task: (app: NestiaSdkApplication) => Promise<void>) => {
13
+ await generate(task);
14
+ };
15
+
16
+ const generate = async (
17
+ task: (app: NestiaSdkApplication) => Promise<void>,
18
+ ) => {
19
+ // LOAD CONFIG INFO
20
+ const compilerOptions: ts.CompilerOptions =
21
+ await NestiaConfigLoader.compilerOptions(
22
+ getFileArgument({
23
+ type: "project",
24
+ extension: "json",
25
+ }) ?? "tsconfig.json",
26
+ );
27
+ const config: INestiaConfig = await NestiaConfigLoader.config(
28
+ getFileArgument({
29
+ type: "config",
30
+ extension: "ts",
31
+ }) ?? "nestia.config.ts",
32
+ compilerOptions,
33
+ );
34
+
35
+ // GENERATE
36
+ const app: NestiaSdkApplication = new NestiaSdkApplication(
37
+ config,
38
+ compilerOptions,
39
+ );
40
+ await task(app);
41
+ };
42
+
43
+ const getFileArgument = (props: {
44
+ type: string;
45
+ extension: string;
46
+ }): string | null => {
47
+ const argv: string[] = process.argv.slice(3);
48
+ if (argv.length === 0) return null;
49
+
50
+ const index: number = argv.findIndex((str) => str === `--${props.type}`);
51
+ if (index === -1) return null;
52
+ else if (argv.length === 1)
53
+ throw new Error(`${props.type} file must be provided`);
54
+
55
+ const file: string = argv[index + 1];
56
+ if (file.endsWith(props.extension) === false)
57
+ throw new Error(`${props.type} file must be ${props.extension} file`);
58
+ return file;
59
+ };
60
+ }