@nestia/sdk 2.4.7 → 2.5.0-dev.20240129

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 (40) hide show
  1. package/lib/generates/internal/E2eFileProgrammer.js +44 -46
  2. package/lib/generates/internal/E2eFileProgrammer.js.map +1 -1
  3. package/lib/generates/internal/SdkDtoGenerator.js +2 -1
  4. package/lib/generates/internal/SdkDtoGenerator.js.map +1 -1
  5. package/lib/generates/internal/SdkFileProgrammer.js +16 -10
  6. package/lib/generates/internal/SdkFileProgrammer.js.map +1 -1
  7. package/lib/generates/internal/SdkFunctionProgrammer.d.ts +6 -1
  8. package/lib/generates/internal/SdkFunctionProgrammer.js +73 -323
  9. package/lib/generates/internal/SdkFunctionProgrammer.js.map +1 -1
  10. package/lib/generates/internal/SdkNamespaceProgrammer.d.ts +11 -0
  11. package/lib/generates/internal/SdkNamespaceProgrammer.js +175 -0
  12. package/lib/generates/internal/SdkNamespaceProgrammer.js.map +1 -0
  13. package/lib/generates/internal/SdkRouteProgrammer.d.ts +7 -0
  14. package/lib/generates/internal/SdkRouteProgrammer.js +55 -0
  15. package/lib/generates/internal/SdkRouteProgrammer.js.map +1 -0
  16. package/lib/generates/internal/SdkSimulationProgrammer.d.ts +7 -1
  17. package/lib/generates/internal/SdkSimulationProgrammer.js +98 -88
  18. package/lib/generates/internal/SdkSimulationProgrammer.js.map +1 -1
  19. package/lib/utils/FormatUtil.d.ts +6 -0
  20. package/lib/utils/FormatUtil.js +36 -0
  21. package/lib/utils/FormatUtil.js.map +1 -0
  22. package/lib/utils/ImportDictionary.d.ts +2 -0
  23. package/lib/utils/ImportDictionary.js +45 -0
  24. package/lib/utils/ImportDictionary.js.map +1 -1
  25. package/package.json +6 -3
  26. package/src/analyses/ConfigAnalyzer.ts +147 -147
  27. package/src/analyses/ControllerAnalyzer.ts +390 -390
  28. package/src/analyses/PathAnalyzer.ts +110 -110
  29. package/src/analyses/ReflectAnalyzer.ts +464 -464
  30. package/src/generates/SwaggerGenerator.ts +376 -376
  31. package/src/generates/internal/E2eFileProgrammer.ts +148 -73
  32. package/src/generates/internal/SdkDtoGenerator.ts +6 -1
  33. package/src/generates/internal/SdkFileProgrammer.ts +40 -13
  34. package/src/generates/internal/SdkFunctionProgrammer.ts +176 -475
  35. package/src/generates/internal/SdkNamespaceProgrammer.ts +495 -0
  36. package/src/generates/internal/SdkRouteProgrammer.ts +82 -0
  37. package/src/generates/internal/SdkSimulationProgrammer.ts +359 -133
  38. package/src/generates/internal/SwaggerSchemaGenerator.ts +444 -444
  39. package/src/utils/FormatUtil.ts +30 -0
  40. package/src/utils/ImportDictionary.ts +72 -0
@@ -1,7 +1,10 @@
1
1
  import fs from "fs";
2
+ import ts from "typescript";
3
+ import { IdentifierFactory } from "typia/lib/factories/IdentifierFactory";
2
4
 
3
5
  import { INestiaConfig } from "../../INestiaConfig";
4
6
  import { IRoute } from "../../structures/IRoute";
7
+ import { FormatUtil } from "../../utils/FormatUtil";
5
8
  import { ImportDictionary } from "../../utils/ImportDictionary";
6
9
  import { SdkDtoGenerator } from "./SdkDtoGenerator";
7
10
  import { SdkImportWizard } from "./SdkImportWizard";
@@ -13,7 +16,7 @@ export namespace E2eFileProgrammer {
13
16
  (props: { api: string; current: string }) =>
14
17
  async (route: IRoute): Promise<void> => {
15
18
  const importer: ImportDictionary = new ImportDictionary(
16
- `${props.current}/${name(route)}.ts`,
19
+ `${props.current}/${getFunctionName(route)}.ts`,
17
20
  );
18
21
  if (config.clone !== true)
19
22
  for (const tuple of route.imports)
@@ -30,90 +33,162 @@ export namespace E2eFileProgrammer {
30
33
  name: "api",
31
34
  });
32
35
 
33
- const body: string = arrow(config)(importer)(route);
34
- const content: string = [importer.toScript(props.current), "", body].join(
35
- "\n",
36
- );
36
+ const functor = generate_function(config)(importer)(route);
37
37
 
38
- await fs.promises.writeFile(importer.file, content, "utf8");
38
+ await fs.promises.writeFile(
39
+ importer.file,
40
+ await FormatUtil.beautify(
41
+ ts
42
+ .createPrinter()
43
+ .printFile(
44
+ ts.factory.createSourceFile(
45
+ [
46
+ ...importer.toStatements(props.current),
47
+ FormatUtil.enter(),
48
+ functor,
49
+ ],
50
+ ts.factory.createToken(ts.SyntaxKind.EndOfFileToken),
51
+ ts.NodeFlags.None,
52
+ ),
53
+ ),
54
+ ),
55
+ "utf8",
56
+ );
39
57
  };
40
58
 
41
- const arrow =
59
+ const generate_function =
42
60
  (config: INestiaConfig) =>
43
61
  (importer: ImportDictionary) =>
44
- (route: IRoute): string => {
45
- const tab: number = 2;
46
- const headers = route.parameters.find(
47
- (p) => p.category === "headers" && p.field === undefined,
62
+ (route: IRoute): ts.Statement =>
63
+ ts.factory.createVariableStatement(
64
+ [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
65
+ ts.factory.createVariableDeclarationList(
66
+ [
67
+ ts.factory.createVariableDeclaration(
68
+ ts.factory.createIdentifier(getFunctionName(route)),
69
+ undefined,
70
+ undefined,
71
+ generate_arrow(config)(importer)(route),
72
+ ),
73
+ ],
74
+ ts.NodeFlags.Const,
75
+ ),
48
76
  );
49
- const output = [
50
- `await ${accessor(route)}(`,
51
- headers !== undefined
52
- ? [
53
- "{",
54
- " ...connection,",
55
- " headers: {",
56
- " ...(connection.headers ?? {}),",
57
- ` ...${SdkImportWizard.typia(
58
- importer,
59
- )}.random<${getTypeName(config)(importer)(headers)}>(),`,
60
- " },",
61
- "},",
62
- ]
63
- .map((line) => `${" ".repeat(tab * 4)}${line}`)
64
- .join("\n")
65
- : `${" ".repeat(tab * 4)}connection,`,
66
- ...route.parameters
67
- .filter((param) => param.category !== "headers")
68
- .map(parameter(config)(importer)(tab)),
69
- `${" ".repeat((tab - 1) * 4)});`,
70
- ].join("\n");
71
- return [
72
- `export const ${name(route)} = async (`,
73
- ` connection: api.IConnection`,
74
- `): Promise<void> => {`,
75
- ...(route.output.typeName === "void"
76
- ? [` ${output}`]
77
- : [
78
- ` const output: ${SdkTypeDefiner.output(config)(importer)(
79
- route,
80
- )} = ${output}`,
81
- ` ${SdkImportWizard.typia(importer)}.assert(output);`,
82
- ]),
83
- `};`,
84
- ].join("\n");
85
- };
86
77
 
87
- const parameter =
78
+ const generate_arrow =
88
79
  (config: INestiaConfig) =>
89
80
  (importer: ImportDictionary) =>
90
- (tab: number) =>
91
- (param: IRoute.IParameter): string => {
92
- const middle: string = `${SdkImportWizard.typia(importer)}.random<${wrap(
93
- config,
94
- )(importer)(
95
- getTypeName(config)(importer)(param),
96
- param.category === "body",
97
- )}>()`;
98
- return `${" ".repeat(4 * tab)}${middle},`;
99
- };
81
+ (route: IRoute) => {
82
+ const headers = route.parameters.find(
83
+ (p) => p.category === "headers" && p.field === undefined,
84
+ );
85
+ const connection = headers
86
+ ? ts.factory.createObjectLiteralExpression(
87
+ [
88
+ ts.factory.createSpreadAssignment(
89
+ ts.factory.createIdentifier("connection"),
90
+ ),
91
+ ts.factory.createPropertyAssignment(
92
+ "headers",
93
+ ts.factory.createObjectLiteralExpression(
94
+ [
95
+ ts.factory.createSpreadAssignment(
96
+ IdentifierFactory.access(
97
+ ts.factory.createIdentifier("connection"),
98
+ )("headers"),
99
+ ),
100
+ ts.factory.createSpreadAssignment(
101
+ ts.factory.createCallExpression(
102
+ IdentifierFactory.access(
103
+ ts.factory.createIdentifier(
104
+ SdkImportWizard.typia(importer),
105
+ ),
106
+ )("random"),
107
+ [
108
+ ts.factory.createTypeReferenceNode(
109
+ getTypeName(config)(importer)(headers),
110
+ ),
111
+ ],
112
+ undefined,
113
+ ),
114
+ ),
115
+ ],
116
+ true,
117
+ ),
118
+ ),
119
+ ],
120
+ true,
121
+ )
122
+ : ts.factory.createIdentifier("connection");
123
+ const caller = ts.factory.createCallExpression(
124
+ ts.factory.createIdentifier(
125
+ ["api", "functional", ...route.accessors].join("."),
126
+ ),
127
+ undefined,
128
+ [
129
+ connection,
130
+ ...route.parameters
131
+ .filter((p) => p.category !== "headers")
132
+ .map((p) =>
133
+ ts.factory.createCallExpression(
134
+ IdentifierFactory.access(
135
+ ts.factory.createIdentifier(SdkImportWizard.typia(importer)),
136
+ )("random"),
137
+ [
138
+ ts.factory.createTypeReferenceNode(
139
+ getTypeName(config)(importer)(p),
140
+ ),
141
+ ],
142
+ undefined,
143
+ ),
144
+ ),
145
+ ],
146
+ );
147
+ const assert = ts.factory.createCallExpression(
148
+ IdentifierFactory.access(
149
+ ts.factory.createIdentifier(SdkImportWizard.typia(importer)),
150
+ )("assert"),
151
+ undefined,
152
+ [ts.factory.createIdentifier("output")],
153
+ );
100
154
 
101
- const name = (route: IRoute): string =>
102
- ["test", "api", ...route.accessors].join("_");
155
+ return ts.factory.createArrowFunction(
156
+ [ts.factory.createModifier(ts.SyntaxKind.AsyncKeyword)],
157
+ undefined,
158
+ [
159
+ IdentifierFactory.parameter(
160
+ "connection",
161
+ ts.factory.createTypeReferenceNode("api.IConnection"),
162
+ ),
163
+ ],
164
+ undefined,
165
+ undefined,
166
+ ts.factory.createBlock([
167
+ ts.factory.createVariableStatement(
168
+ [],
169
+ ts.factory.createVariableDeclarationList(
170
+ [
171
+ ts.factory.createVariableDeclaration(
172
+ "output",
173
+ undefined,
174
+ ts.factory.createTypeReferenceNode(
175
+ SdkTypeDefiner.output(config)(importer)(route),
176
+ ),
177
+ ts.factory.createAwaitExpression(caller),
178
+ ),
179
+ ],
180
+ ts.NodeFlags.Const,
181
+ ),
182
+ ),
183
+ ts.factory.createExpressionStatement(assert),
184
+ ]),
185
+ );
186
+ };
187
+ }
103
188
 
104
- const accessor = (route: IRoute): string =>
105
- ["api", "functional", ...route.accessors].join(".");
189
+ const getFunctionName = (route: IRoute): string =>
190
+ ["test", "api", ...route.accessors].join("_");
106
191
 
107
- const wrap =
108
- (config: INestiaConfig) =>
109
- (importer: ImportDictionary) =>
110
- (name: string, body: boolean): string =>
111
- config.primitive === false
112
- ? name
113
- : `${(body ? SdkImportWizard.Primitive : SdkImportWizard.Resolved)(
114
- importer,
115
- )}<${name}>`;
116
- }
117
192
  const getTypeName =
118
193
  (config: INestiaConfig) =>
119
194
  (importer: ImportDictionary) =>
@@ -17,6 +17,7 @@ import { Escaper } from "typia/lib/utils/Escaper";
17
17
 
18
18
  import { INestiaConfig } from "../../INestiaConfig";
19
19
  import { IRoute } from "../../structures/IRoute";
20
+ import { FormatUtil } from "../../utils/FormatUtil";
20
21
  import { ImportDictionary } from "../../utils/ImportDictionary";
21
22
  import { MapUtil } from "../../utils/MapUtil";
22
23
 
@@ -101,7 +102,11 @@ export namespace SdkDtoGenerator {
101
102
  content.push(importer.toScript(`${config.output}/structures`), "");
102
103
  content.push(body);
103
104
 
104
- await fs.promises.writeFile(importer.file, content.join("\n"), "utf8");
105
+ await fs.promises.writeFile(
106
+ importer.file,
107
+ await FormatUtil.beautify(content.join("\n")),
108
+ "utf8",
109
+ );
105
110
  };
106
111
 
107
112
  const writeModule =
@@ -1,11 +1,13 @@
1
1
  import fs from "fs";
2
+ import ts from "typescript";
2
3
 
3
4
  import { INestiaConfig } from "../../INestiaConfig";
4
5
  import { IRoute } from "../../structures/IRoute";
6
+ import { FormatUtil } from "../../utils/FormatUtil";
5
7
  import { ImportDictionary } from "../../utils/ImportDictionary";
6
8
  import { MapUtil } from "../../utils/MapUtil";
7
- import { SdkFunctionProgrammer } from "./SdkFunctionProgrammer";
8
9
  import { SdkRouteDirectory } from "./SdkRouteDirectory";
10
+ import { SdkRouteProgrammer } from "./SdkRouteProgrammer";
9
11
 
10
12
  export namespace SdkFileProgrammer {
11
13
  /* ---------------------------------------------------------
@@ -51,12 +53,21 @@ export namespace SdkFileProgrammer {
51
53
  } catch {}
52
54
 
53
55
  // ITERATE CHILDREN
54
- const content: string[] = [];
56
+ const statements: ts.Statement[] = [];
55
57
  for (const [key, value] of directory.children) {
56
58
  await iterate(config)(value)(`${outDir}/${key}`);
57
- content.push(`export * as ${key} from "./${key}";`);
59
+ statements.push(
60
+ ts.factory.createExportDeclaration(
61
+ undefined,
62
+ false,
63
+ ts.factory.createNamespaceExport(ts.factory.createIdentifier(key)),
64
+ ts.factory.createStringLiteral(`./${key}`),
65
+ undefined,
66
+ ),
67
+ );
58
68
  }
59
- if (content.length && directory.routes.length) content.push("");
69
+ if (statements.length && directory.routes.length)
70
+ statements.push(FormatUtil.enter());
60
71
 
61
72
  // ITERATE ROUTES
62
73
  const importer: ImportDictionary = new ImportDictionary(
@@ -80,17 +91,21 @@ export namespace SdkFileProgrammer {
80
91
  instance,
81
92
  type: true,
82
93
  });
83
-
84
- content.push(SdkFunctionProgrammer.generate(config)(importer)(route));
85
- if (i !== directory.routes.length - 1) content.push("");
94
+ statements.push(
95
+ ...SdkRouteProgrammer.generate(config)(importer)(route),
96
+ );
97
+ if (i !== directory.routes.length - 1)
98
+ statements.push(FormatUtil.enter());
86
99
  });
87
100
 
88
101
  // FINALIZE THE CONTENT
89
102
  if (directory.routes.length !== 0)
90
- content.push(
91
- importer.toScript(outDir),
92
- "",
93
- ...content.splice(0, content.length),
103
+ statements.push(
104
+ ...importer.toStatements(outDir),
105
+ ...(!importer.empty() && statements.length
106
+ ? [FormatUtil.enter()]
107
+ : []),
108
+ ...statements.splice(0, statements.length),
94
109
  );
95
110
 
96
111
  const script: string =
@@ -100,7 +115,19 @@ export namespace SdkFileProgrammer {
100
115
  " * @nestia Generated by Nestia - https://github.com/samchon/nestia \n" +
101
116
  " */\n" +
102
117
  "//================================================================\n" +
103
- content.join("\n");
104
- await fs.promises.writeFile(importer.file, script, "utf8");
118
+ ts
119
+ .createPrinter()
120
+ .printFile(
121
+ ts.factory.createSourceFile(
122
+ statements,
123
+ ts.factory.createToken(ts.SyntaxKind.EndOfFileToken),
124
+ ts.NodeFlags.None,
125
+ ),
126
+ );
127
+ await fs.promises.writeFile(
128
+ importer.file,
129
+ await FormatUtil.beautify(script),
130
+ "utf8",
131
+ );
105
132
  };
106
133
  }