@nestia/sdk 2.6.2 → 2.6.3

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 (46) hide show
  1. package/lib/analyses/ControllerAnalyzer.js +3 -3
  2. package/lib/analyses/ControllerAnalyzer.js.map +1 -1
  3. package/lib/analyses/ImportAnalyzer.d.ts +1 -2
  4. package/lib/analyses/ImportAnalyzer.js +2 -2
  5. package/lib/analyses/ImportAnalyzer.js.map +1 -1
  6. package/lib/analyses/ReflectAnalyzer.js +2 -2
  7. package/lib/analyses/ReflectAnalyzer.js.map +1 -1
  8. package/lib/generates/SwaggerGenerator.js +4 -4
  9. package/lib/generates/SwaggerGenerator.js.map +1 -1
  10. package/lib/generates/internal/ImportDictionary.js +6 -8
  11. package/lib/generates/internal/ImportDictionary.js.map +1 -1
  12. package/lib/structures/TypeEntry.js +2 -2
  13. package/lib/structures/TypeEntry.js.map +1 -1
  14. package/package.json +4 -4
  15. package/src/INestiaConfig.ts +248 -248
  16. package/src/NestiaSdkApplication.ts +255 -255
  17. package/src/analyses/ControllerAnalyzer.ts +402 -402
  18. package/src/analyses/ExceptionAnalyzer.ts +148 -148
  19. package/src/analyses/ImportAnalyzer.ts +1 -2
  20. package/src/analyses/ReflectAnalyzer.ts +463 -463
  21. package/src/analyses/SecurityAnalyzer.ts +24 -24
  22. package/src/generates/CloneGenerator.ts +62 -62
  23. package/src/generates/E2eGenerator.ts +66 -66
  24. package/src/generates/SdkGenerator.ts +84 -84
  25. package/src/generates/SwaggerGenerator.ts +446 -446
  26. package/src/generates/internal/E2eFileProgrammer.ts +182 -182
  27. package/src/generates/internal/FilePrinter.ts +53 -53
  28. package/src/generates/internal/ImportDictionary.ts +147 -149
  29. package/src/generates/internal/SdkAliasCollection.ts +152 -152
  30. package/src/generates/internal/SdkCloneProgrammer.ts +155 -155
  31. package/src/generates/internal/SdkFileProgrammer.ts +115 -115
  32. package/src/generates/internal/SdkFunctionProgrammer.ts +298 -298
  33. package/src/generates/internal/SdkImportWizard.ts +55 -55
  34. package/src/generates/internal/SdkNamespaceProgrammer.ts +510 -510
  35. package/src/generates/internal/SdkRouteProgrammer.ts +83 -83
  36. package/src/generates/internal/SdkSimulationProgrammer.ts +365 -365
  37. package/src/generates/internal/SdkTypeProgrammer.ts +385 -385
  38. package/src/generates/internal/SwaggerSchemaGenerator.ts +438 -438
  39. package/src/structures/IController.ts +94 -94
  40. package/src/structures/IRoute.ts +53 -53
  41. package/src/structures/ISwagger.ts +91 -91
  42. package/src/structures/ISwaggerRoute.ts +54 -54
  43. package/src/structures/ISwaggerSecurityScheme.ts +65 -65
  44. package/src/structures/ParamCategory.ts +1 -1
  45. package/src/structures/TypeEntry.ts +1 -1
  46. package/src/utils/StringUtil.ts +6 -6
@@ -1,255 +1,255 @@
1
- import fs from "fs";
2
- import path from "path";
3
- import ts from "typescript";
4
-
5
- import { INestiaConfig } from "./INestiaConfig";
6
- import { AccessorAnalyzer } from "./analyses/AccessorAnalyzer";
7
- import { ConfigAnalyzer } from "./analyses/ConfigAnalyzer";
8
- import { ControllerAnalyzer } from "./analyses/ControllerAnalyzer";
9
- import { ReflectAnalyzer } from "./analyses/ReflectAnalyzer";
10
- import { E2eGenerator } from "./generates/E2eGenerator";
11
- import { SdkGenerator } from "./generates/SdkGenerator";
12
- import { SwaggerGenerator } from "./generates/SwaggerGenerator";
13
- import { IController } from "./structures/IController";
14
- import { IErrorReport } from "./structures/IErrorReport";
15
- import { INestiaProject } from "./structures/INestiaProject";
16
- import { IRoute } from "./structures/IRoute";
17
- import { MapUtil } from "./utils/MapUtil";
18
-
19
- export class NestiaSdkApplication {
20
- public constructor(
21
- private readonly config: INestiaConfig,
22
- private readonly compilerOptions: ts.CompilerOptions,
23
- ) {}
24
-
25
- public async e2e(): Promise<void> {
26
- if (!this.config.output)
27
- throw new Error(
28
- "Error on NestiaApplication.e2e(): output path of SDK is not specified.",
29
- );
30
- else if (!this.config.e2e)
31
- throw new Error(
32
- "Error on NestiaApplication.e2e(): output path of e2e test files is not specified.",
33
- );
34
-
35
- const validate =
36
- (title: string) =>
37
- async (location: string): Promise<void> => {
38
- const parent: string = path.resolve(location + "/..");
39
- const stats: fs.Stats = await fs.promises.lstat(parent);
40
- if (stats.isDirectory() === false)
41
- throw new Error(
42
- `Error on NestiaApplication.e2e(): output directory of ${title} does not exists.`,
43
- );
44
- };
45
- await validate("sdk")(this.config.output);
46
- await validate("e2e")(this.config.e2e);
47
-
48
- print_title("Nestia E2E Generator");
49
- await this.generate(
50
- "e2e",
51
- (config) => config,
52
- (checker) => (config) => async (routes) => {
53
- await SdkGenerator.generate(checker)(config)(routes);
54
- await E2eGenerator.generate(checker)(config)(routes);
55
- },
56
- );
57
- }
58
-
59
- public async sdk(): Promise<void> {
60
- if (!this.config.output)
61
- throw new Error(
62
- "Error on NestiaApplication.sdk(): output path is not specified.",
63
- );
64
-
65
- const parent: string = path.resolve(this.config.output + "/..");
66
- const stats: fs.Stats = await fs.promises.lstat(parent);
67
- if (stats.isDirectory() === false)
68
- throw new Error(
69
- "Error on NestiaApplication.sdk(): output directory does not exists.",
70
- );
71
-
72
- print_title("Nestia SDK Generator");
73
- await this.generate("sdk", (config) => config, SdkGenerator.generate);
74
- }
75
-
76
- public async swagger(): Promise<void> {
77
- if (!this.config.swagger?.output)
78
- throw new Error(
79
- `Error on NestiaApplication.swagger(): output path of the "swagger.json" is not specified.`,
80
- );
81
-
82
- const parsed: path.ParsedPath = path.parse(this.config.swagger.output);
83
- const directory: string = !!parsed.ext
84
- ? path.resolve(parsed.dir)
85
- : this.config.swagger.output;
86
- const stats: fs.Stats = await fs.promises.lstat(directory);
87
- if (stats.isDirectory() === false)
88
- throw new Error(
89
- "Error on NestiaApplication.swagger(): output directory does not exists.",
90
- );
91
-
92
- print_title("Nestia Swagger Generator");
93
- await this.generate(
94
- "swagger",
95
- (config) => config.swagger!,
96
- SwaggerGenerator.generate,
97
- );
98
- }
99
-
100
- private async generate<Config>(
101
- method: string,
102
- config: (entire: INestiaConfig) => Config,
103
- archiver: (
104
- checker: ts.TypeChecker,
105
- ) => (config: Config) => (routes: IRoute[]) => Promise<void>,
106
- ): Promise<void> {
107
- //----
108
- // ANALYZE REFLECTS
109
- //----
110
- const unique: WeakSet<any> = new WeakSet();
111
- const controllers: IController[] = [];
112
- const project: INestiaProject = {
113
- config: this.config,
114
- input: await ConfigAnalyzer.input(this.config),
115
- checker: null!,
116
- errors: [],
117
- warnings: [],
118
- };
119
-
120
- console.log("Analyzing reflections");
121
- for (const include of (await ConfigAnalyzer.input(this.config)).include)
122
- controllers.push(
123
- ...(await ReflectAnalyzer.analyze(project)(
124
- unique,
125
- include.file,
126
- include.paths,
127
- include.controller,
128
- )),
129
- );
130
-
131
- const agg: number = (() => {
132
- const set: Set<string> = new Set();
133
- for (const c of controllers)
134
- for (const cPath of c.paths)
135
- for (const f of c.functions)
136
- for (const fPath of f.paths)
137
- set.add(`${f.method}::${cPath}/${fPath}`);
138
- return set.size;
139
- })();
140
-
141
- console.log(` - controllers: #${controllers.length}`);
142
- console.log(` - paths: #${agg}`);
143
- console.log(
144
- ` - routes: #${controllers
145
- .map(
146
- (c) =>
147
- c.paths.length *
148
- c.functions.map((f) => f.paths.length).reduce((a, b) => a + b, 0),
149
- )
150
- .reduce((a, b) => a + b, 0)}`,
151
- );
152
-
153
- //----
154
- // ANALYZE TYPESCRIPT CODE
155
- //----
156
- console.log("Analyzing source codes");
157
-
158
- const program: ts.Program = ts.createProgram(
159
- controllers.map((c) => c.file),
160
- this.compilerOptions,
161
- );
162
- project.checker = program.getTypeChecker();
163
-
164
- const routeList: IRoute[] = [];
165
- for (const c of controllers) {
166
- const file: ts.SourceFile | undefined = program.getSourceFile(c.file);
167
- if (file === undefined) continue;
168
- routeList.push(...(await ControllerAnalyzer.analyze(project)(file, c)));
169
- }
170
-
171
- // REPORT ERRORS
172
- if (project.errors.length) {
173
- report_errors("error")(project.errors);
174
- process.exit(-1);
175
- }
176
- if (project.warnings.length) report_errors("warning")(project.warnings);
177
-
178
- // FIND IMPLICIT TYPES
179
- if (this.config.clone !== true) {
180
- const implicit: IRoute[] = routeList.filter(is_implicit_return_typed);
181
- if (implicit.length > 0)
182
- throw new Error(
183
- `NestiaApplication.${method}(): implicit return type is not allowed.\n` +
184
- "\n" +
185
- "List of implicit return typed routes:\n" +
186
- implicit
187
- .map(
188
- (it) =>
189
- ` - ${it.target.class.name}.${it.target.function.name} at "${it.location}"`,
190
- )
191
- .join("\n"),
192
- );
193
- }
194
-
195
- // DO GENERATE
196
- AccessorAnalyzer.analyze(routeList);
197
- await archiver(project.checker)(config(this.config))(routeList);
198
- }
199
- }
200
-
201
- const print_title = (str: string): void => {
202
- console.log("-----------------------------------------------------------");
203
- console.log(` ${str}`);
204
- console.log("-----------------------------------------------------------");
205
- };
206
-
207
- const is_implicit_return_typed = (route: IRoute): boolean => {
208
- const name: string = route.output.typeName;
209
- if (name === "void") return false;
210
- else if (name.indexOf("readonly [") !== -1) return true;
211
-
212
- const pos: number = name.indexOf("__object");
213
- if (pos === -1) return false;
214
-
215
- const before: number = pos - 1;
216
- const after: number = pos + "__object".length;
217
- for (const i of [before, after])
218
- if (name[i] === undefined) continue;
219
- else if (VARIABLE.test(name[i])) return false;
220
- return true;
221
- };
222
-
223
- const report_errors =
224
- (type: "error" | "warning") =>
225
- (errors: IErrorReport[]): void => {
226
- // key: file
227
- // key: controller
228
- // key: function
229
- // value: message
230
- const map: Map<string, Map<string, Map<string, Set<string>>>> = new Map();
231
- for (const e of errors) {
232
- const file = MapUtil.take(map, e.file, () => new Map());
233
- const controller = MapUtil.take(file, e.controller, () => new Map());
234
- const func = MapUtil.take(controller, e.function, () => new Set());
235
- func.add(e.message);
236
- }
237
-
238
- console.log("");
239
- print_title(`Nestia ${type[0].toUpperCase()}${type.slice(1)} Report`);
240
- for (const [file, cMap] of map) {
241
- for (const [controller, fMap] of cMap)
242
- for (const [func, messages] of fMap) {
243
- const location: string = path.relative(process.cwd(), file);
244
- console.log(
245
- `${location} - ${
246
- func !== null ? `${controller}.${func}()` : controller
247
- }`,
248
- );
249
- for (const msg of messages) console.log(` - ${msg}`);
250
- console.log("");
251
- }
252
- }
253
- };
254
-
255
- const VARIABLE = /[a-zA-Z_$0-9]/;
1
+ import fs from "fs";
2
+ import path from "path";
3
+ import ts from "typescript";
4
+
5
+ import { INestiaConfig } from "./INestiaConfig";
6
+ import { AccessorAnalyzer } from "./analyses/AccessorAnalyzer";
7
+ import { ConfigAnalyzer } from "./analyses/ConfigAnalyzer";
8
+ import { ControllerAnalyzer } from "./analyses/ControllerAnalyzer";
9
+ import { ReflectAnalyzer } from "./analyses/ReflectAnalyzer";
10
+ import { E2eGenerator } from "./generates/E2eGenerator";
11
+ import { SdkGenerator } from "./generates/SdkGenerator";
12
+ import { SwaggerGenerator } from "./generates/SwaggerGenerator";
13
+ import { IController } from "./structures/IController";
14
+ import { IErrorReport } from "./structures/IErrorReport";
15
+ import { INestiaProject } from "./structures/INestiaProject";
16
+ import { IRoute } from "./structures/IRoute";
17
+ import { MapUtil } from "./utils/MapUtil";
18
+
19
+ export class NestiaSdkApplication {
20
+ public constructor(
21
+ private readonly config: INestiaConfig,
22
+ private readonly compilerOptions: ts.CompilerOptions,
23
+ ) {}
24
+
25
+ public async e2e(): Promise<void> {
26
+ if (!this.config.output)
27
+ throw new Error(
28
+ "Error on NestiaApplication.e2e(): output path of SDK is not specified.",
29
+ );
30
+ else if (!this.config.e2e)
31
+ throw new Error(
32
+ "Error on NestiaApplication.e2e(): output path of e2e test files is not specified.",
33
+ );
34
+
35
+ const validate =
36
+ (title: string) =>
37
+ async (location: string): Promise<void> => {
38
+ const parent: string = path.resolve(location + "/..");
39
+ const stats: fs.Stats = await fs.promises.lstat(parent);
40
+ if (stats.isDirectory() === false)
41
+ throw new Error(
42
+ `Error on NestiaApplication.e2e(): output directory of ${title} does not exists.`,
43
+ );
44
+ };
45
+ await validate("sdk")(this.config.output);
46
+ await validate("e2e")(this.config.e2e);
47
+
48
+ print_title("Nestia E2E Generator");
49
+ await this.generate(
50
+ "e2e",
51
+ (config) => config,
52
+ (checker) => (config) => async (routes) => {
53
+ await SdkGenerator.generate(checker)(config)(routes);
54
+ await E2eGenerator.generate(checker)(config)(routes);
55
+ },
56
+ );
57
+ }
58
+
59
+ public async sdk(): Promise<void> {
60
+ if (!this.config.output)
61
+ throw new Error(
62
+ "Error on NestiaApplication.sdk(): output path is not specified.",
63
+ );
64
+
65
+ const parent: string = path.resolve(this.config.output + "/..");
66
+ const stats: fs.Stats = await fs.promises.lstat(parent);
67
+ if (stats.isDirectory() === false)
68
+ throw new Error(
69
+ "Error on NestiaApplication.sdk(): output directory does not exists.",
70
+ );
71
+
72
+ print_title("Nestia SDK Generator");
73
+ await this.generate("sdk", (config) => config, SdkGenerator.generate);
74
+ }
75
+
76
+ public async swagger(): Promise<void> {
77
+ if (!this.config.swagger?.output)
78
+ throw new Error(
79
+ `Error on NestiaApplication.swagger(): output path of the "swagger.json" is not specified.`,
80
+ );
81
+
82
+ const parsed: path.ParsedPath = path.parse(this.config.swagger.output);
83
+ const directory: string = !!parsed.ext
84
+ ? path.resolve(parsed.dir)
85
+ : this.config.swagger.output;
86
+ const stats: fs.Stats = await fs.promises.lstat(directory);
87
+ if (stats.isDirectory() === false)
88
+ throw new Error(
89
+ "Error on NestiaApplication.swagger(): output directory does not exists.",
90
+ );
91
+
92
+ print_title("Nestia Swagger Generator");
93
+ await this.generate(
94
+ "swagger",
95
+ (config) => config.swagger!,
96
+ SwaggerGenerator.generate,
97
+ );
98
+ }
99
+
100
+ private async generate<Config>(
101
+ method: string,
102
+ config: (entire: INestiaConfig) => Config,
103
+ archiver: (
104
+ checker: ts.TypeChecker,
105
+ ) => (config: Config) => (routes: IRoute[]) => Promise<void>,
106
+ ): Promise<void> {
107
+ //----
108
+ // ANALYZE REFLECTS
109
+ //----
110
+ const unique: WeakSet<any> = new WeakSet();
111
+ const controllers: IController[] = [];
112
+ const project: INestiaProject = {
113
+ config: this.config,
114
+ input: await ConfigAnalyzer.input(this.config),
115
+ checker: null!,
116
+ errors: [],
117
+ warnings: [],
118
+ };
119
+
120
+ console.log("Analyzing reflections");
121
+ for (const include of (await ConfigAnalyzer.input(this.config)).include)
122
+ controllers.push(
123
+ ...(await ReflectAnalyzer.analyze(project)(
124
+ unique,
125
+ include.file,
126
+ include.paths,
127
+ include.controller,
128
+ )),
129
+ );
130
+
131
+ const agg: number = (() => {
132
+ const set: Set<string> = new Set();
133
+ for (const c of controllers)
134
+ for (const cPath of c.paths)
135
+ for (const f of c.functions)
136
+ for (const fPath of f.paths)
137
+ set.add(`${f.method}::${cPath}/${fPath}`);
138
+ return set.size;
139
+ })();
140
+
141
+ console.log(` - controllers: #${controllers.length}`);
142
+ console.log(` - paths: #${agg}`);
143
+ console.log(
144
+ ` - routes: #${controllers
145
+ .map(
146
+ (c) =>
147
+ c.paths.length *
148
+ c.functions.map((f) => f.paths.length).reduce((a, b) => a + b, 0),
149
+ )
150
+ .reduce((a, b) => a + b, 0)}`,
151
+ );
152
+
153
+ //----
154
+ // ANALYZE TYPESCRIPT CODE
155
+ //----
156
+ console.log("Analyzing source codes");
157
+
158
+ const program: ts.Program = ts.createProgram(
159
+ controllers.map((c) => c.file),
160
+ this.compilerOptions,
161
+ );
162
+ project.checker = program.getTypeChecker();
163
+
164
+ const routeList: IRoute[] = [];
165
+ for (const c of controllers) {
166
+ const file: ts.SourceFile | undefined = program.getSourceFile(c.file);
167
+ if (file === undefined) continue;
168
+ routeList.push(...(await ControllerAnalyzer.analyze(project)(file, c)));
169
+ }
170
+
171
+ // REPORT ERRORS
172
+ if (project.errors.length) {
173
+ report_errors("error")(project.errors);
174
+ process.exit(-1);
175
+ }
176
+ if (project.warnings.length) report_errors("warning")(project.warnings);
177
+
178
+ // FIND IMPLICIT TYPES
179
+ if (this.config.clone !== true) {
180
+ const implicit: IRoute[] = routeList.filter(is_implicit_return_typed);
181
+ if (implicit.length > 0)
182
+ throw new Error(
183
+ `NestiaApplication.${method}(): implicit return type is not allowed.\n` +
184
+ "\n" +
185
+ "List of implicit return typed routes:\n" +
186
+ implicit
187
+ .map(
188
+ (it) =>
189
+ ` - ${it.target.class.name}.${it.target.function.name} at "${it.location}"`,
190
+ )
191
+ .join("\n"),
192
+ );
193
+ }
194
+
195
+ // DO GENERATE
196
+ AccessorAnalyzer.analyze(routeList);
197
+ await archiver(project.checker)(config(this.config))(routeList);
198
+ }
199
+ }
200
+
201
+ const print_title = (str: string): void => {
202
+ console.log("-----------------------------------------------------------");
203
+ console.log(` ${str}`);
204
+ console.log("-----------------------------------------------------------");
205
+ };
206
+
207
+ const is_implicit_return_typed = (route: IRoute): boolean => {
208
+ const name: string = route.output.typeName;
209
+ if (name === "void") return false;
210
+ else if (name.indexOf("readonly [") !== -1) return true;
211
+
212
+ const pos: number = name.indexOf("__object");
213
+ if (pos === -1) return false;
214
+
215
+ const before: number = pos - 1;
216
+ const after: number = pos + "__object".length;
217
+ for (const i of [before, after])
218
+ if (name[i] === undefined) continue;
219
+ else if (VARIABLE.test(name[i])) return false;
220
+ return true;
221
+ };
222
+
223
+ const report_errors =
224
+ (type: "error" | "warning") =>
225
+ (errors: IErrorReport[]): void => {
226
+ // key: file
227
+ // key: controller
228
+ // key: function
229
+ // value: message
230
+ const map: Map<string, Map<string, Map<string, Set<string>>>> = new Map();
231
+ for (const e of errors) {
232
+ const file = MapUtil.take(map, e.file, () => new Map());
233
+ const controller = MapUtil.take(file, e.controller, () => new Map());
234
+ const func = MapUtil.take(controller, e.function, () => new Set());
235
+ func.add(e.message);
236
+ }
237
+
238
+ console.log("");
239
+ print_title(`Nestia ${type[0].toUpperCase()}${type.slice(1)} Report`);
240
+ for (const [file, cMap] of map) {
241
+ for (const [controller, fMap] of cMap)
242
+ for (const [func, messages] of fMap) {
243
+ const location: string = path.relative(process.cwd(), file);
244
+ console.log(
245
+ `${location} - ${
246
+ func !== null ? `${controller}.${func}()` : controller
247
+ }`,
248
+ );
249
+ for (const msg of messages) console.log(` - ${msg}`);
250
+ console.log("");
251
+ }
252
+ }
253
+ };
254
+
255
+ const VARIABLE = /[a-zA-Z_$0-9]/;