@nestia/sdk 3.1.0-dev.20240429 → 3.1.0-dev.20240430

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