@nestia/sdk 1.0.0 → 1.0.2
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.
- package/assets/config/nestia.config.ts +70 -70
- package/lib/INestiaConfig.d.ts +28 -9
- package/lib/analyses/ControllerAnalyzer.js +21 -1
- package/lib/analyses/ControllerAnalyzer.js.map +1 -1
- package/lib/executable/internal/NestiaSdkConfig.js +168 -14
- package/lib/executable/internal/NestiaSdkConfig.js.map +1 -1
- package/lib/executable/sdk.js +16 -16
- package/lib/generates/FunctionGenerator.js +35 -15
- package/lib/generates/FunctionGenerator.js.map +1 -1
- package/lib/generates/SwaggerGenerator.d.ts +1 -1
- package/lib/generates/SwaggerGenerator.js +36 -16
- package/lib/generates/SwaggerGenerator.js.map +1 -1
- package/lib/structures/IRoute.d.ts +8 -0
- package/lib/structures/ISwaggerDocument.d.ts +95 -0
- package/lib/structures/{ISwagger.js → ISwaggerDocument.js} +1 -1
- package/lib/structures/ISwaggerDocument.js.map +1 -0
- package/package.json +3 -3
- package/src/INestiaConfig.ts +147 -120
- package/src/NestiaSdkApplication.ts +183 -183
- package/src/analyses/ControllerAnalyzer.ts +223 -203
- package/src/analyses/GenericAnalyzer.ts +53 -53
- package/src/analyses/ImportAnalyzer.ts +143 -143
- package/src/analyses/PathAnalyzer.ts +58 -58
- package/src/analyses/ReflectAnalyzer.ts +279 -279
- package/src/analyses/SourceFinder.ts +59 -59
- package/src/executable/internal/CommandParser.ts +15 -15
- package/src/executable/internal/NestiaConfigCompilerOptions.ts +18 -18
- package/src/executable/internal/NestiaSdkCommand.ts +174 -174
- package/src/executable/internal/NestiaSdkConfig.ts +35 -35
- package/src/executable/internal/nestia.config.getter.ts +12 -12
- package/src/executable/sdk.ts +74 -74
- package/src/generates/FileGenerator.ts +156 -156
- package/src/generates/FunctionGenerator.ts +322 -287
- package/src/generates/SdkGenerator.ts +50 -50
- package/src/generates/SwaggerGenerator.ts +422 -393
- package/src/index.ts +3 -3
- package/src/module.ts +2 -2
- package/src/structures/IController.ts +27 -27
- package/src/structures/IRoute.ts +33 -29
- package/src/structures/ISwaggerDocument.ts +117 -0
- package/src/structures/ITypeTuple.ts +6 -6
- package/src/structures/MethodType.ts +11 -11
- package/src/structures/ParamCategory.ts +1 -1
- package/src/structures/TypeEntry.ts +22 -22
- package/src/utils/ArrayUtil.ts +26 -26
- package/src/utils/ImportDictionary.ts +56 -56
- package/src/utils/MapUtil.ts +14 -14
- package/src/utils/StripEnums.ts +10 -10
- package/lib/structures/ISwagger.d.ts +0 -48
- package/lib/structures/ISwagger.js.map +0 -1
- package/src/structures/ISwagger.ts +0 -55
|
@@ -1,393 +1,422 @@
|
|
|
1
|
-
import fs from "fs";
|
|
2
|
-
import NodePath from "path";
|
|
3
|
-
import { Singleton } from "tstl/thread/Singleton";
|
|
4
|
-
import { VariadicSingleton } from "tstl/thread/VariadicSingleton";
|
|
5
|
-
import ts from "typescript";
|
|
6
|
-
import { IJsonApplication, IJsonSchema } from "typia";
|
|
7
|
-
import { CommentFactory } from "typia/lib/factories/CommentFactory";
|
|
8
|
-
import { MetadataCollection } from "typia/lib/factories/MetadataCollection";
|
|
9
|
-
import { MetadataFactory } from "typia/lib/factories/MetadataFactory";
|
|
10
|
-
import { Metadata } from "typia/lib/metadata/Metadata";
|
|
11
|
-
import { ApplicationProgrammer } from "typia/lib/programmers/ApplicationProgrammer";
|
|
12
|
-
|
|
13
|
-
import { INestiaConfig } from "../INestiaConfig";
|
|
14
|
-
import { IRoute } from "../structures/IRoute";
|
|
15
|
-
import {
|
|
16
|
-
import { MapUtil } from "../utils/MapUtil";
|
|
17
|
-
|
|
18
|
-
export namespace SwaggerGenerator {
|
|
19
|
-
export async function generate(
|
|
20
|
-
checker: ts.TypeChecker,
|
|
21
|
-
config: INestiaConfig.
|
|
22
|
-
routeList: IRoute[],
|
|
23
|
-
): Promise<void> {
|
|
24
|
-
// PREPARE ASSETS
|
|
25
|
-
const parsed: NodePath.ParsedPath = NodePath.parse(config.output);
|
|
26
|
-
const location: string = !!parsed.ext
|
|
27
|
-
? NodePath.resolve(config.output)
|
|
28
|
-
: NodePath.join(NodePath.resolve(config.output), "swagger.json");
|
|
29
|
-
|
|
30
|
-
const collection: MetadataCollection = new MetadataCollection({
|
|
31
|
-
replace: MetadataCollection.replace,
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
// CONSTRUCT SWAGGER DOCUMENTS
|
|
35
|
-
const tupleList: Array<ISchemaTuple> = [];
|
|
36
|
-
const swagger:
|
|
37
|
-
const pathDict: Map<string,
|
|
38
|
-
|
|
39
|
-
for (const route of routeList) {
|
|
40
|
-
const path:
|
|
41
|
-
pathDict,
|
|
42
|
-
get_path(route.path, route.parameters),
|
|
43
|
-
() => ({}),
|
|
44
|
-
);
|
|
45
|
-
path[route.method.toLowerCase()] = generate_route(
|
|
46
|
-
checker,
|
|
47
|
-
collection,
|
|
48
|
-
tupleList,
|
|
49
|
-
route,
|
|
50
|
-
);
|
|
51
|
-
}
|
|
52
|
-
swagger.paths = {};
|
|
53
|
-
for (const [path, routes] of pathDict) {
|
|
54
|
-
swagger.paths[path] = routes;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// FILL JSON-SCHEMAS
|
|
58
|
-
const application: IJsonApplication = ApplicationProgrammer.generate(
|
|
59
|
-
tupleList.map(({ metadata }) => metadata),
|
|
60
|
-
{
|
|
61
|
-
purpose: "swagger",
|
|
62
|
-
},
|
|
63
|
-
);
|
|
64
|
-
swagger.components = {
|
|
65
|
-
...(swagger.components
|
|
66
|
-
schemas: application.components.schemas,
|
|
67
|
-
};
|
|
68
|
-
tupleList.forEach(({ schema }, index) => {
|
|
69
|
-
Object.assign(schema, application.schemas[index]!);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
//
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
const
|
|
130
|
-
.
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
function
|
|
209
|
-
checker: ts.TypeChecker,
|
|
210
|
-
collection: MetadataCollection,
|
|
211
|
-
tupleList: Array<ISchemaTuple>,
|
|
212
|
-
route: IRoute,
|
|
213
|
-
parameter: IRoute.IParameter,
|
|
214
|
-
):
|
|
215
|
-
const schema: IJsonSchema | null = generate_schema(
|
|
216
|
-
checker,
|
|
217
|
-
collection,
|
|
218
|
-
tupleList,
|
|
219
|
-
parameter.type.type,
|
|
220
|
-
);
|
|
221
|
-
if (schema === null)
|
|
222
|
-
throw new Error(
|
|
223
|
-
`Error on NestiaApplication.sdk(): invalid
|
|
224
|
-
);
|
|
225
|
-
|
|
226
|
-
return {
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
)
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
.
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import NodePath from "path";
|
|
3
|
+
import { Singleton } from "tstl/thread/Singleton";
|
|
4
|
+
import { VariadicSingleton } from "tstl/thread/VariadicSingleton";
|
|
5
|
+
import ts from "typescript";
|
|
6
|
+
import { IJsonApplication, IJsonSchema } from "typia";
|
|
7
|
+
import { CommentFactory } from "typia/lib/factories/CommentFactory";
|
|
8
|
+
import { MetadataCollection } from "typia/lib/factories/MetadataCollection";
|
|
9
|
+
import { MetadataFactory } from "typia/lib/factories/MetadataFactory";
|
|
10
|
+
import { Metadata } from "typia/lib/metadata/Metadata";
|
|
11
|
+
import { ApplicationProgrammer } from "typia/lib/programmers/ApplicationProgrammer";
|
|
12
|
+
|
|
13
|
+
import { INestiaConfig } from "../INestiaConfig";
|
|
14
|
+
import { IRoute } from "../structures/IRoute";
|
|
15
|
+
import { ISwaggerDocument } from "../structures/ISwaggerDocument";
|
|
16
|
+
import { MapUtil } from "../utils/MapUtil";
|
|
17
|
+
|
|
18
|
+
export namespace SwaggerGenerator {
|
|
19
|
+
export async function generate(
|
|
20
|
+
checker: ts.TypeChecker,
|
|
21
|
+
config: INestiaConfig.ISwaggerConfig,
|
|
22
|
+
routeList: IRoute[],
|
|
23
|
+
): Promise<void> {
|
|
24
|
+
// PREPARE ASSETS
|
|
25
|
+
const parsed: NodePath.ParsedPath = NodePath.parse(config.output);
|
|
26
|
+
const location: string = !!parsed.ext
|
|
27
|
+
? NodePath.resolve(config.output)
|
|
28
|
+
: NodePath.join(NodePath.resolve(config.output), "swagger.json");
|
|
29
|
+
|
|
30
|
+
const collection: MetadataCollection = new MetadataCollection({
|
|
31
|
+
replace: MetadataCollection.replace,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// CONSTRUCT SWAGGER DOCUMENTS
|
|
35
|
+
const tupleList: Array<ISchemaTuple> = [];
|
|
36
|
+
const swagger: ISwaggerDocument = await initialize(location);
|
|
37
|
+
const pathDict: Map<string, ISwaggerDocument.IPath> = new Map();
|
|
38
|
+
|
|
39
|
+
for (const route of routeList) {
|
|
40
|
+
const path: ISwaggerDocument.IPath = MapUtil.take(
|
|
41
|
+
pathDict,
|
|
42
|
+
get_path(route.path, route.parameters),
|
|
43
|
+
() => ({}),
|
|
44
|
+
);
|
|
45
|
+
path[route.method.toLowerCase()] = generate_route(
|
|
46
|
+
checker,
|
|
47
|
+
collection,
|
|
48
|
+
tupleList,
|
|
49
|
+
route,
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
swagger.paths = {};
|
|
53
|
+
for (const [path, routes] of pathDict) {
|
|
54
|
+
swagger.paths[path] = routes;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// FILL JSON-SCHEMAS
|
|
58
|
+
const application: IJsonApplication = ApplicationProgrammer.generate(
|
|
59
|
+
tupleList.map(({ metadata }) => metadata),
|
|
60
|
+
{
|
|
61
|
+
purpose: "swagger",
|
|
62
|
+
},
|
|
63
|
+
);
|
|
64
|
+
swagger.components = {
|
|
65
|
+
...(swagger.components ?? {}),
|
|
66
|
+
schemas: application.components.schemas,
|
|
67
|
+
};
|
|
68
|
+
tupleList.forEach(({ schema }, index) => {
|
|
69
|
+
Object.assign(schema, application.schemas[index]!);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// CONFIGURE SECURITY
|
|
73
|
+
if (config.security) fill_security(config.security, swagger);
|
|
74
|
+
|
|
75
|
+
// ERASE IJsonComponents.IObject.$id
|
|
76
|
+
for (const obj of Object.values(swagger.components.schemas))
|
|
77
|
+
if (obj.$id) delete obj.$id;
|
|
78
|
+
|
|
79
|
+
// DO GENERATE
|
|
80
|
+
await fs.promises.writeFile(
|
|
81
|
+
location,
|
|
82
|
+
JSON.stringify(swagger, null, 2),
|
|
83
|
+
"utf8",
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/* ---------------------------------------------------------
|
|
88
|
+
INITIALIZERS
|
|
89
|
+
--------------------------------------------------------- */
|
|
90
|
+
async function initialize(path: string): Promise<ISwaggerDocument> {
|
|
91
|
+
// LOAD OR CREATE NEW SWAGGER DATA
|
|
92
|
+
const swagger: ISwaggerDocument = fs.existsSync(path)
|
|
93
|
+
? JSON.parse(await fs.promises.readFile(path, "utf8"))
|
|
94
|
+
: {
|
|
95
|
+
openapi: "3.0.1",
|
|
96
|
+
servers: [
|
|
97
|
+
{
|
|
98
|
+
url: "https://github.com/samchon/nestia",
|
|
99
|
+
description: "insert your server url",
|
|
100
|
+
},
|
|
101
|
+
],
|
|
102
|
+
info: {
|
|
103
|
+
version: "0.1.0",
|
|
104
|
+
title: "Generated by nestia - https://github.com/samchon/nestia",
|
|
105
|
+
},
|
|
106
|
+
paths: {},
|
|
107
|
+
components: {},
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
// RETURNS
|
|
111
|
+
return swagger;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function get_path(path: string, parameters: IRoute.IParameter[]): string {
|
|
115
|
+
const filtered: IRoute.IParameter[] = parameters.filter(
|
|
116
|
+
(param) => param.category === "param" && !!param.field,
|
|
117
|
+
);
|
|
118
|
+
for (const param of filtered)
|
|
119
|
+
path = path.replace(`:${param.field}`, `{${param.field}}`);
|
|
120
|
+
return path;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function generate_route(
|
|
124
|
+
checker: ts.TypeChecker,
|
|
125
|
+
collection: MetadataCollection,
|
|
126
|
+
tupleList: Array<ISchemaTuple>,
|
|
127
|
+
route: IRoute,
|
|
128
|
+
): ISwaggerDocument.IRoute {
|
|
129
|
+
const bodyParam = route.parameters.find(
|
|
130
|
+
(param) => param.category === "body",
|
|
131
|
+
);
|
|
132
|
+
const tags: string[] = route.tags
|
|
133
|
+
.filter(
|
|
134
|
+
(tag) =>
|
|
135
|
+
tag.name === "tag" &&
|
|
136
|
+
tag.text &&
|
|
137
|
+
tag.text.find(
|
|
138
|
+
(elem) => elem.kind === "text" && elem.text.length,
|
|
139
|
+
) !== undefined,
|
|
140
|
+
)
|
|
141
|
+
.map((tag) => tag.text!.find((elem) => elem.kind === "text")!.text);
|
|
142
|
+
|
|
143
|
+
const encrypted: boolean =
|
|
144
|
+
route.encrypted === true ||
|
|
145
|
+
!!route.parameters.find((param) => param.encrypted === true);
|
|
146
|
+
return {
|
|
147
|
+
tags,
|
|
148
|
+
summary: encrypted ? "encrypted" : undefined,
|
|
149
|
+
parameters: route.parameters
|
|
150
|
+
.filter((param) => param.category !== "body")
|
|
151
|
+
.map((param) =>
|
|
152
|
+
generate_parameter(
|
|
153
|
+
checker,
|
|
154
|
+
collection,
|
|
155
|
+
tupleList,
|
|
156
|
+
route,
|
|
157
|
+
param,
|
|
158
|
+
),
|
|
159
|
+
),
|
|
160
|
+
requestBody: bodyParam
|
|
161
|
+
? generate_request_body(
|
|
162
|
+
checker,
|
|
163
|
+
collection,
|
|
164
|
+
tupleList,
|
|
165
|
+
route,
|
|
166
|
+
bodyParam,
|
|
167
|
+
)
|
|
168
|
+
: undefined,
|
|
169
|
+
responses: generate_response_body(
|
|
170
|
+
checker,
|
|
171
|
+
collection,
|
|
172
|
+
tupleList,
|
|
173
|
+
route,
|
|
174
|
+
),
|
|
175
|
+
description: CommentFactory.generate(route.comments),
|
|
176
|
+
"x-nestia-jsDocTags": route.tags,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function fill_security(
|
|
181
|
+
security: Required<INestiaConfig.ISwaggerConfig>["security"],
|
|
182
|
+
swagger: ISwaggerDocument,
|
|
183
|
+
): void {
|
|
184
|
+
swagger.security ??= [];
|
|
185
|
+
swagger.components.securitySchemes = {};
|
|
186
|
+
|
|
187
|
+
for (const [key, value] of Object.entries(security)) {
|
|
188
|
+
swagger.security.push(key);
|
|
189
|
+
swagger.components.securitySchemes[key] = emend_security(value);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function emend_security(
|
|
194
|
+
input: INestiaConfig.ISwaggerConfig.ISecurityScheme,
|
|
195
|
+
): ISwaggerDocument.ISecurityScheme {
|
|
196
|
+
if (input.type === "apiKey")
|
|
197
|
+
return {
|
|
198
|
+
...input,
|
|
199
|
+
in: input.in ?? "header",
|
|
200
|
+
name: input.name ?? "Authorization",
|
|
201
|
+
};
|
|
202
|
+
return input;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/* ---------------------------------------------------------
|
|
206
|
+
REQUEST & RESPONSE
|
|
207
|
+
--------------------------------------------------------- */
|
|
208
|
+
function generate_parameter(
|
|
209
|
+
checker: ts.TypeChecker,
|
|
210
|
+
collection: MetadataCollection,
|
|
211
|
+
tupleList: Array<ISchemaTuple>,
|
|
212
|
+
route: IRoute,
|
|
213
|
+
parameter: IRoute.IParameter,
|
|
214
|
+
): ISwaggerDocument.IParameter {
|
|
215
|
+
const schema: IJsonSchema | null = generate_schema(
|
|
216
|
+
checker,
|
|
217
|
+
collection,
|
|
218
|
+
tupleList,
|
|
219
|
+
parameter.type.type,
|
|
220
|
+
);
|
|
221
|
+
if (schema === null)
|
|
222
|
+
throw new Error(
|
|
223
|
+
`Error on NestiaApplication.sdk(): invalid parameter type on ${route.symbol}#${parameter.name}`,
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
return {
|
|
227
|
+
name: parameter.field ?? parameter.name,
|
|
228
|
+
in: parameter.category === "param" ? "path" : parameter.category,
|
|
229
|
+
description:
|
|
230
|
+
get_parametric_description(route, "param", parameter.name) ||
|
|
231
|
+
"",
|
|
232
|
+
schema,
|
|
233
|
+
required: true,
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function generate_request_body(
|
|
238
|
+
checker: ts.TypeChecker,
|
|
239
|
+
collection: MetadataCollection,
|
|
240
|
+
tupleList: Array<ISchemaTuple>,
|
|
241
|
+
route: IRoute,
|
|
242
|
+
parameter: IRoute.IParameter,
|
|
243
|
+
): ISwaggerDocument.IRequestBody {
|
|
244
|
+
const schema: IJsonSchema | null = generate_schema(
|
|
245
|
+
checker,
|
|
246
|
+
collection,
|
|
247
|
+
tupleList,
|
|
248
|
+
parameter.type.type,
|
|
249
|
+
);
|
|
250
|
+
if (schema === null)
|
|
251
|
+
throw new Error(
|
|
252
|
+
`Error on NestiaApplication.sdk(): invalid request body type on ${route.symbol}.`,
|
|
253
|
+
);
|
|
254
|
+
|
|
255
|
+
return {
|
|
256
|
+
description:
|
|
257
|
+
warning.get(parameter.encrypted).get("request") +
|
|
258
|
+
(get_parametric_description(route, "param", parameter.name) ??
|
|
259
|
+
""),
|
|
260
|
+
content: {
|
|
261
|
+
"application/json": {
|
|
262
|
+
schema,
|
|
263
|
+
},
|
|
264
|
+
},
|
|
265
|
+
required: true,
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function generate_response_body(
|
|
270
|
+
checker: ts.TypeChecker,
|
|
271
|
+
collection: MetadataCollection,
|
|
272
|
+
tupleList: Array<ISchemaTuple>,
|
|
273
|
+
route: IRoute,
|
|
274
|
+
): ISwaggerDocument.IResponseBody {
|
|
275
|
+
// OUTPUT WITH SUCCESS STATUS
|
|
276
|
+
const status: string =
|
|
277
|
+
route.method === "GET" || route.method === "DELETE" ? "200" : "201";
|
|
278
|
+
const schema: IJsonSchema | null = generate_schema(
|
|
279
|
+
checker,
|
|
280
|
+
collection,
|
|
281
|
+
tupleList,
|
|
282
|
+
route.output.type,
|
|
283
|
+
);
|
|
284
|
+
const success: ISwaggerDocument.IResponseBody = {
|
|
285
|
+
[status]: {
|
|
286
|
+
description:
|
|
287
|
+
warning.get(route.encrypted).get("response", route.method) +
|
|
288
|
+
(get_parametric_description(route, "return") ??
|
|
289
|
+
get_parametric_description(route, "returns") ??
|
|
290
|
+
""),
|
|
291
|
+
content:
|
|
292
|
+
schema === null || route.output.name === "void"
|
|
293
|
+
? undefined
|
|
294
|
+
: {
|
|
295
|
+
"application/json": {
|
|
296
|
+
schema,
|
|
297
|
+
},
|
|
298
|
+
},
|
|
299
|
+
},
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
// EXCEPTION STATUSES
|
|
303
|
+
const exceptions: ISwaggerDocument.IResponseBody = Object.fromEntries(
|
|
304
|
+
route.tags
|
|
305
|
+
.filter(
|
|
306
|
+
(tag) =>
|
|
307
|
+
tag.name === "throw" &&
|
|
308
|
+
tag.text &&
|
|
309
|
+
tag.text.find(
|
|
310
|
+
(elem) =>
|
|
311
|
+
elem.kind === "text" &&
|
|
312
|
+
isNaN(
|
|
313
|
+
Number(
|
|
314
|
+
elem.text
|
|
315
|
+
.split(" ")
|
|
316
|
+
.map((str) => str.trim())[0],
|
|
317
|
+
),
|
|
318
|
+
) === false,
|
|
319
|
+
) !== undefined,
|
|
320
|
+
)
|
|
321
|
+
.map((tag) => {
|
|
322
|
+
const text: string = tag.text!.find(
|
|
323
|
+
(elem) => elem.kind === "text",
|
|
324
|
+
)!.text;
|
|
325
|
+
const elements: string[] = text
|
|
326
|
+
.split(" ")
|
|
327
|
+
.map((str) => str.trim());
|
|
328
|
+
|
|
329
|
+
return [
|
|
330
|
+
elements[0],
|
|
331
|
+
{
|
|
332
|
+
description: elements.slice(1).join(" "),
|
|
333
|
+
},
|
|
334
|
+
];
|
|
335
|
+
}),
|
|
336
|
+
);
|
|
337
|
+
return { ...exceptions, ...success };
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/* ---------------------------------------------------------
|
|
341
|
+
UTILS
|
|
342
|
+
--------------------------------------------------------- */
|
|
343
|
+
function generate_schema(
|
|
344
|
+
checker: ts.TypeChecker,
|
|
345
|
+
collection: MetadataCollection,
|
|
346
|
+
tupleList: Array<ISchemaTuple>,
|
|
347
|
+
type: ts.Type,
|
|
348
|
+
): IJsonSchema | null {
|
|
349
|
+
const metadata: Metadata = MetadataFactory.generate(
|
|
350
|
+
checker,
|
|
351
|
+
collection,
|
|
352
|
+
type,
|
|
353
|
+
{
|
|
354
|
+
resolve: false,
|
|
355
|
+
constant: true,
|
|
356
|
+
},
|
|
357
|
+
);
|
|
358
|
+
if (metadata.empty() && metadata.nullable === false) return null;
|
|
359
|
+
|
|
360
|
+
const schema: IJsonSchema = {} as IJsonSchema;
|
|
361
|
+
tupleList.push({ metadata, schema });
|
|
362
|
+
return schema;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
function get_parametric_description(
|
|
366
|
+
route: IRoute,
|
|
367
|
+
tagName: string,
|
|
368
|
+
parameterName?: string,
|
|
369
|
+
): string | undefined {
|
|
370
|
+
const parametric: (elem: ts.JSDocTagInfo) => boolean = parameterName
|
|
371
|
+
? (tag) =>
|
|
372
|
+
tag.text!.find(
|
|
373
|
+
(elem) =>
|
|
374
|
+
elem.kind === "parameterName" &&
|
|
375
|
+
elem.text === parameterName,
|
|
376
|
+
) !== undefined
|
|
377
|
+
: () => true;
|
|
378
|
+
|
|
379
|
+
const tag: ts.JSDocTagInfo | undefined = route.tags.find(
|
|
380
|
+
(tag) => tag.name === tagName && tag.text && parametric(tag),
|
|
381
|
+
);
|
|
382
|
+
return tag && tag.text
|
|
383
|
+
? tag.text.find((elem) => elem.kind === "text")?.text
|
|
384
|
+
: undefined;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
const warning = new VariadicSingleton((encrypted: boolean) => {
|
|
389
|
+
if (encrypted === false) return new Singleton(() => "");
|
|
390
|
+
|
|
391
|
+
return new VariadicSingleton(
|
|
392
|
+
(type: "request" | "response", method?: string) => {
|
|
393
|
+
const summary =
|
|
394
|
+
type === "request"
|
|
395
|
+
? "Request body must be encrypted."
|
|
396
|
+
: "Response data have been encrypted.";
|
|
397
|
+
|
|
398
|
+
const component =
|
|
399
|
+
type === "request"
|
|
400
|
+
? "[EncryptedBody](https://github.com/samchon/@nestia/core#encryptedbody)"
|
|
401
|
+
: `[EncryptedRoute.${method![0].toUpperCase()}.${method!
|
|
402
|
+
.substring(1)
|
|
403
|
+
.toLowerCase()}](https://github.com/samchon/@nestia/core#encryptedroute)`;
|
|
404
|
+
|
|
405
|
+
return `## Warning
|
|
406
|
+
${summary}
|
|
407
|
+
|
|
408
|
+
The ${type} body data would be encrypted as "AES-128(256) / CBC mode / PKCS#5 Padding / Base64 Encoding", through the ${component} component.
|
|
409
|
+
|
|
410
|
+
Therefore, just utilize this swagger editor only for referencing. If you need to call the real API, using [SDK](https://github.com/samchon/nestia#software-development-kit) would be much better.
|
|
411
|
+
|
|
412
|
+
-----------------
|
|
413
|
+
|
|
414
|
+
`;
|
|
415
|
+
},
|
|
416
|
+
);
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
interface ISchemaTuple {
|
|
420
|
+
metadata: Metadata;
|
|
421
|
+
schema: IJsonSchema;
|
|
422
|
+
}
|