@necessarylion/adonis-autoswagger 0.0.4 → 0.0.5
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/dist/adonis_helpers.d.ts +21 -0
- package/dist/{adonishelpers.js → adonis_helpers.js} +17 -2
- package/dist/auto_swagger.d.ts +28 -0
- package/dist/{autoswagger.js → auto_swagger.js} +43 -394
- package/dist/example.d.ts +47 -0
- package/dist/example.js +48 -1
- package/dist/file.d.ts +44 -0
- package/dist/file.js +93 -0
- package/dist/helpers.d.ts +11 -2
- package/dist/helpers.js +11 -25
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -2
- package/dist/parsers/comment_parser.d.ts +10 -0
- package/dist/parsers/comment_parser.js +417 -0
- package/dist/parsers/enum_parser.d.ts +5 -0
- package/dist/parsers/enum_parser.js +51 -0
- package/dist/parsers/helpers.d.ts +13 -0
- package/dist/parsers/helpers.js +38 -0
- package/dist/parsers/index.d.ts +6 -0
- package/dist/parsers/index.js +22 -0
- package/dist/parsers/interface_parser.d.ts +13 -0
- package/dist/parsers/interface_parser.js +249 -0
- package/dist/parsers/model_parser.d.ts +11 -0
- package/dist/parsers/model_parser.js +234 -0
- package/dist/parsers/route_parser.d.ts +10 -0
- package/dist/parsers/route_parser.js +46 -0
- package/dist/parsers/validator_parser.d.ts +7 -0
- package/dist/parsers/validator_parser.js +192 -0
- package/dist/schema.d.ts +46 -0
- package/dist/schema.js +266 -0
- package/dist/types.d.ts +16 -17
- package/dist/ui.d.ts +31 -0
- package/dist/ui.js +157 -0
- package/package.json +2 -2
- package/dist/adonishelpers.d.ts +0 -6
- package/dist/autoswagger.d.ts +0 -30
- package/dist/parsers.d.ts +0 -83
- package/dist/parsers.js +0 -1142
- /package/dist/{scalarCustomCss.d.ts → scalar_custom_css.d.ts} +0 -0
- /package/dist/{scalarCustomCss.js → scalar_custom_css.js} +0 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Serialize v6 middleware
|
|
3
|
+
* @param middleware any
|
|
4
|
+
* @returns
|
|
5
|
+
*/
|
|
6
|
+
export declare function serializeV6Middleware(middleware: any): string[];
|
|
7
|
+
/**
|
|
8
|
+
* Serialize v6 handler
|
|
9
|
+
* @param handler any
|
|
10
|
+
* @returns
|
|
11
|
+
*/
|
|
12
|
+
export declare function serializeV6Handler(handler: any): Promise<any>;
|
|
13
|
+
/**
|
|
14
|
+
* Parse binding reference
|
|
15
|
+
* @param binding string | [any | any, any]
|
|
16
|
+
* @returns
|
|
17
|
+
*/
|
|
18
|
+
export declare function parseBindingReference(binding: string | [any | any, any]): Promise<{
|
|
19
|
+
moduleNameOrPath: string;
|
|
20
|
+
method: string;
|
|
21
|
+
}>;
|
|
@@ -3,8 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.serializeV6Middleware = serializeV6Middleware;
|
|
4
4
|
exports.serializeV6Handler = serializeV6Handler;
|
|
5
5
|
exports.parseBindingReference = parseBindingReference;
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Serialize v6 middleware
|
|
8
|
+
* @param middleware any
|
|
9
|
+
* @returns
|
|
10
|
+
*/
|
|
11
|
+
function serializeV6Middleware(middleware) {
|
|
12
|
+
return [...middleware.all()].reduce((result, one) => {
|
|
8
13
|
if (typeof one === "function") {
|
|
9
14
|
result.push(one.name || "closure");
|
|
10
15
|
return result;
|
|
@@ -15,6 +20,11 @@ function serializeV6Middleware(mw) {
|
|
|
15
20
|
return result;
|
|
16
21
|
}, []);
|
|
17
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Serialize v6 handler
|
|
25
|
+
* @param handler any
|
|
26
|
+
* @returns
|
|
27
|
+
*/
|
|
18
28
|
async function serializeV6Handler(handler) {
|
|
19
29
|
/**
|
|
20
30
|
* Value is a controller reference
|
|
@@ -33,6 +43,11 @@ async function serializeV6Handler(handler) {
|
|
|
33
43
|
name: handler.name || "closure",
|
|
34
44
|
};
|
|
35
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* Parse binding reference
|
|
48
|
+
* @param binding string | [any | any, any]
|
|
49
|
+
* @returns
|
|
50
|
+
*/
|
|
36
51
|
async function parseBindingReference(binding) {
|
|
37
52
|
const parseImports = (await import("parse-imports")).default;
|
|
38
53
|
/**
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { options, AdonisRoutes } from "./types";
|
|
2
|
+
declare class AutoSwagger {
|
|
3
|
+
private uiService;
|
|
4
|
+
private fileService;
|
|
5
|
+
private schemaService;
|
|
6
|
+
private options;
|
|
7
|
+
private schemas;
|
|
8
|
+
private commentParser;
|
|
9
|
+
private routeParser;
|
|
10
|
+
private customPaths;
|
|
11
|
+
constructor();
|
|
12
|
+
/**
|
|
13
|
+
* Get data based on adonis version
|
|
14
|
+
* @param route AdonisRoute
|
|
15
|
+
* @returns
|
|
16
|
+
*/
|
|
17
|
+
ui(url: string, options?: options): string;
|
|
18
|
+
rapidoc(url: string, style?: string): string;
|
|
19
|
+
scalar(url: string, proxyUrl?: string): string;
|
|
20
|
+
stoplight(url: string, theme?: "light" | "dark"): string;
|
|
21
|
+
jsonToYaml(json: any): any;
|
|
22
|
+
json(routes: AdonisRoutes, options: options): Promise<any>;
|
|
23
|
+
writeFile(routes: AdonisRoutes, options: options): Promise<void>;
|
|
24
|
+
docs(routes: AdonisRoutes, options: options): Promise<string>;
|
|
25
|
+
private generate;
|
|
26
|
+
private getDataBasedOnAdonisVersion;
|
|
27
|
+
}
|
|
28
|
+
export { AutoSwagger };
|
|
@@ -37,183 +37,59 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
39
|
exports.AutoSwagger = void 0;
|
|
40
|
-
const
|
|
41
|
-
const
|
|
40
|
+
const adonis_helpers_1 = require("./adonis_helpers");
|
|
41
|
+
const index_1 = require("./parsers/index");
|
|
42
|
+
const lodash_1 = __importStar(require("lodash"));
|
|
43
|
+
const helpers_1 = require("./helpers");
|
|
44
|
+
const example_1 = __importDefault(require("./example"));
|
|
42
45
|
const path_1 = __importDefault(require("path"));
|
|
43
|
-
const
|
|
44
|
-
const
|
|
46
|
+
const fs_1 = __importDefault(require("fs"));
|
|
47
|
+
const lodash_2 = require("lodash");
|
|
45
48
|
const http_status_code_1 = __importDefault(require("http-status-code"));
|
|
46
|
-
const
|
|
47
|
-
const
|
|
48
|
-
const
|
|
49
|
-
const scalarCustomCss_1 = require("./scalarCustomCss");
|
|
50
|
-
const adonishelpers_1 = require("./adonishelpers");
|
|
51
|
-
const parsers_1 = require("./parsers");
|
|
52
|
-
const helpers_1 = require("./helpers");
|
|
53
|
-
const example_1 = __importStar(require("./example"));
|
|
49
|
+
const ui_1 = require("./ui");
|
|
50
|
+
const file_1 = require("./file");
|
|
51
|
+
const schema_1 = require("./schema");
|
|
54
52
|
class AutoSwagger {
|
|
53
|
+
uiService;
|
|
54
|
+
fileService;
|
|
55
|
+
schemaService;
|
|
55
56
|
options;
|
|
56
57
|
schemas = {};
|
|
57
58
|
commentParser;
|
|
58
|
-
modelParser;
|
|
59
|
-
interfaceParser;
|
|
60
|
-
enumParser;
|
|
61
59
|
routeParser;
|
|
62
|
-
validatorParser;
|
|
63
60
|
customPaths = {};
|
|
61
|
+
constructor() {
|
|
62
|
+
this.uiService = new ui_1.UIService();
|
|
63
|
+
this.fileService = new file_1.FileService();
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Get data based on adonis version
|
|
67
|
+
* @param route AdonisRoute
|
|
68
|
+
* @returns
|
|
69
|
+
*/
|
|
64
70
|
ui(url, options) {
|
|
65
|
-
|
|
66
|
-
? "persistAuthorization: true,"
|
|
67
|
-
: "";
|
|
68
|
-
return `<!DOCTYPE html>
|
|
69
|
-
<html lang="en">
|
|
70
|
-
<head>
|
|
71
|
-
<meta charset="UTF-8">
|
|
72
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
73
|
-
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
|
74
|
-
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.1.3/swagger-ui-standalone-preset.js"></script>
|
|
75
|
-
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.1.3/swagger-ui-bundle.js"></script>
|
|
76
|
-
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.1.3/swagger-ui.css" />
|
|
77
|
-
<title>Documentation</title>
|
|
78
|
-
</head>
|
|
79
|
-
<body>
|
|
80
|
-
<div id="swagger-ui"></div>
|
|
81
|
-
<script>
|
|
82
|
-
window.onload = function() {
|
|
83
|
-
SwaggerUIBundle({
|
|
84
|
-
url: "${url}",
|
|
85
|
-
dom_id: '#swagger-ui',
|
|
86
|
-
presets: [
|
|
87
|
-
SwaggerUIBundle.presets.apis,
|
|
88
|
-
SwaggerUIStandalonePreset
|
|
89
|
-
],
|
|
90
|
-
layout: "BaseLayout",
|
|
91
|
-
${persistAuthString}
|
|
92
|
-
})
|
|
93
|
-
}
|
|
94
|
-
</script>
|
|
95
|
-
</body>
|
|
96
|
-
</html>`;
|
|
71
|
+
return this.uiService.ui(url, options);
|
|
97
72
|
}
|
|
98
73
|
rapidoc(url, style = "view") {
|
|
99
|
-
return (
|
|
100
|
-
<!doctype html> <!-- Important: must specify -->
|
|
101
|
-
<html>
|
|
102
|
-
<head>
|
|
103
|
-
<meta charset="utf-8"> <!-- Important: rapi-doc uses utf8 characters -->
|
|
104
|
-
<script type="module" src="https://unpkg.com/rapidoc/dist/rapidoc-min.js"></script>
|
|
105
|
-
<title>Documentation</title>
|
|
106
|
-
</head>
|
|
107
|
-
<body>
|
|
108
|
-
<rapi-doc
|
|
109
|
-
spec-url = "` +
|
|
110
|
-
url +
|
|
111
|
-
`"
|
|
112
|
-
theme = "dark"
|
|
113
|
-
bg-color = "#24283b"
|
|
114
|
-
schema-style="tree"
|
|
115
|
-
schema-expand-level = "10"
|
|
116
|
-
header-color = "#1a1b26"
|
|
117
|
-
allow-try = "true"
|
|
118
|
-
nav-hover-bg-color = "#1a1b26"
|
|
119
|
-
nav-bg-color = "#24283b"
|
|
120
|
-
text-color = "#c0caf5"
|
|
121
|
-
nav-text-color = "#c0caf5"
|
|
122
|
-
primary-color = "#9aa5ce"
|
|
123
|
-
heading-text = "Documentation"
|
|
124
|
-
sort-tags = "true"
|
|
125
|
-
render-style = "` +
|
|
126
|
-
style +
|
|
127
|
-
`"
|
|
128
|
-
default-schema-tab = "example"
|
|
129
|
-
show-components = "true"
|
|
130
|
-
allow-spec-url-load = "false"
|
|
131
|
-
allow-spec-file-load = "false"
|
|
132
|
-
sort-endpoints-by = "path"
|
|
133
|
-
|
|
134
|
-
> </rapi-doc>
|
|
135
|
-
</body>
|
|
136
|
-
</html>
|
|
137
|
-
`);
|
|
74
|
+
return this.uiService.rapidoc(url, style);
|
|
138
75
|
}
|
|
139
76
|
scalar(url, proxyUrl = "https://proxy.scalar.com") {
|
|
140
|
-
return
|
|
141
|
-
<!doctype html>
|
|
142
|
-
<html>
|
|
143
|
-
<head>
|
|
144
|
-
<title>API Reference</title>
|
|
145
|
-
<meta charset="utf-8" />
|
|
146
|
-
<meta
|
|
147
|
-
name="viewport"
|
|
148
|
-
content="width=device-width, initial-scale=1" />
|
|
149
|
-
<style>
|
|
150
|
-
${scalarCustomCss_1.scalarCustomCss}
|
|
151
|
-
</style>
|
|
152
|
-
</head>
|
|
153
|
-
<body>
|
|
154
|
-
<script
|
|
155
|
-
id="api-reference"
|
|
156
|
-
data-url="${url}"
|
|
157
|
-
data-proxy-url="${proxyUrl}"></script>
|
|
158
|
-
<script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script>
|
|
159
|
-
</body>
|
|
160
|
-
</html>
|
|
161
|
-
`;
|
|
77
|
+
return this.uiService.scalar(url, proxyUrl);
|
|
162
78
|
}
|
|
163
79
|
stoplight(url, theme = "dark") {
|
|
164
|
-
return
|
|
165
|
-
<!doctype html>
|
|
166
|
-
<html data-theme="${theme}">
|
|
167
|
-
<head>
|
|
168
|
-
<title>API Documentation - Stoplight</title>
|
|
169
|
-
<meta charset="utf-8" />
|
|
170
|
-
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
|
171
|
-
<script src="https://unpkg.com/@stoplight/elements/web-components.min.js"></script>
|
|
172
|
-
<link rel="stylesheet" href="https://unpkg.com/@stoplight/elements/styles.min.css">
|
|
173
|
-
</head>
|
|
174
|
-
<body style="min-height:100vh">
|
|
175
|
-
<elements-api
|
|
176
|
-
style="display:block;height:100vh;width:100%;"
|
|
177
|
-
apiDescriptionUrl=${url}
|
|
178
|
-
router="hash"
|
|
179
|
-
layout="sidebar"
|
|
180
|
-
/>
|
|
181
|
-
</body>
|
|
182
|
-
</html>
|
|
183
|
-
`;
|
|
80
|
+
return this.uiService.stoplight(url, theme);
|
|
184
81
|
}
|
|
185
82
|
jsonToYaml(json) {
|
|
186
|
-
return
|
|
83
|
+
return this.fileService.jsonToYaml(json);
|
|
187
84
|
}
|
|
188
85
|
async json(routes, options) {
|
|
189
|
-
|
|
190
|
-
const str = await this.readFile(options.path, "json");
|
|
191
|
-
return JSON.parse(str);
|
|
192
|
-
}
|
|
193
|
-
return await this.generate(routes, options);
|
|
86
|
+
return this.fileService.json(routes, options, this.generate.bind(this));
|
|
194
87
|
}
|
|
195
88
|
async writeFile(routes, options) {
|
|
196
|
-
|
|
197
|
-
const contents = this.jsonToYaml(json);
|
|
198
|
-
const filePath = options.path + "swagger.yml";
|
|
199
|
-
const filePathJson = options.path + "swagger.json";
|
|
200
|
-
fs_1.default.writeFileSync(filePath, contents);
|
|
201
|
-
fs_1.default.writeFileSync(filePathJson, JSON.stringify(json, null, 2));
|
|
202
|
-
}
|
|
203
|
-
async readFile(rootPath, type = "yml") {
|
|
204
|
-
const filePath = rootPath + "swagger." + type;
|
|
205
|
-
const data = fs_1.default.readFileSync(filePath, "utf-8");
|
|
206
|
-
if (!data) {
|
|
207
|
-
console.error("Error reading file");
|
|
208
|
-
return;
|
|
209
|
-
}
|
|
210
|
-
return data;
|
|
89
|
+
return this.fileService.writeFile(routes, options, this.generate.bind(this));
|
|
211
90
|
}
|
|
212
91
|
async docs(routes, options) {
|
|
213
|
-
|
|
214
|
-
return this.readFile(options.path);
|
|
215
|
-
}
|
|
216
|
-
return this.jsonToYaml(await this.generate(routes, options));
|
|
92
|
+
return this.fileService.docs(routes, options, this.generate.bind(this));
|
|
217
93
|
}
|
|
218
94
|
async generate(adonisRoutes, options) {
|
|
219
95
|
this.options = {
|
|
@@ -241,13 +117,10 @@ class AutoSwagger {
|
|
|
241
117
|
catch (e) {
|
|
242
118
|
console.error(e);
|
|
243
119
|
}
|
|
244
|
-
this.
|
|
245
|
-
this.
|
|
246
|
-
this.
|
|
247
|
-
this.
|
|
248
|
-
this.validatorParser = new parsers_1.ValidatorParser();
|
|
249
|
-
this.enumParser = new parsers_1.EnumParser();
|
|
250
|
-
this.schemas = await this.getSchemas();
|
|
120
|
+
this.schemaService = new schema_1.SchemaService(this.options);
|
|
121
|
+
this.commentParser = new index_1.CommentParser(this.options);
|
|
122
|
+
this.routeParser = new index_1.RouteParser(this.options);
|
|
123
|
+
this.schemas = await this.schemaService.getSchemas();
|
|
251
124
|
if (this.options.debug) {
|
|
252
125
|
console.log(this.options);
|
|
253
126
|
console.log("Found Schemas", Object.keys(this.schemas));
|
|
@@ -308,7 +181,7 @@ class AutoSwagger {
|
|
|
308
181
|
sscheme = this.options.defaultSecurityScheme;
|
|
309
182
|
}
|
|
310
183
|
let securities = {
|
|
311
|
-
|
|
184
|
+
auth: { [sscheme]: ["access"] },
|
|
312
185
|
"auth:api": { [sscheme]: ["access"] },
|
|
313
186
|
...this.options.authMiddlewares
|
|
314
187
|
?.map((am) => ({
|
|
@@ -342,7 +215,7 @@ class AutoSwagger {
|
|
|
342
215
|
PUT: "204",
|
|
343
216
|
};
|
|
344
217
|
if (!Array.isArray(route.middleware)) {
|
|
345
|
-
route.middleware = (0,
|
|
218
|
+
route.middleware = (0, adonis_helpers_1.serializeV6Middleware)(route.middleware);
|
|
346
219
|
}
|
|
347
220
|
route.middleware.forEach((m) => {
|
|
348
221
|
if (typeof securities[m] !== "undefined") {
|
|
@@ -371,7 +244,6 @@ class AutoSwagger {
|
|
|
371
244
|
let description = "";
|
|
372
245
|
let summary = "";
|
|
373
246
|
let tag = "";
|
|
374
|
-
let operationId;
|
|
375
247
|
if (security.length > 0) {
|
|
376
248
|
responses["401"] = {
|
|
377
249
|
description: `Returns **401** (${http_status_code_1.default.getMessage(401)})`,
|
|
@@ -398,11 +270,11 @@ class AutoSwagger {
|
|
|
398
270
|
parameters = (0, helpers_1.mergeParams)(parameters, actionParams);
|
|
399
271
|
if (tag != "") {
|
|
400
272
|
globalTags.push({
|
|
401
|
-
name: tag.toUpperCase()
|
|
273
|
+
name: tag.toUpperCase(),
|
|
402
274
|
});
|
|
403
275
|
tags = [tag.toUpperCase()];
|
|
404
276
|
}
|
|
405
|
-
if ((0,
|
|
277
|
+
if ((0, lodash_1.isEmpty)(responses)) {
|
|
406
278
|
responses[responseCodes[method]] = {
|
|
407
279
|
description: http_status_code_1.default.getMessage(responseCodes[method]),
|
|
408
280
|
content: {
|
|
@@ -453,7 +325,7 @@ class AutoSwagger {
|
|
|
453
325
|
break;
|
|
454
326
|
}
|
|
455
327
|
}
|
|
456
|
-
action = (0,
|
|
328
|
+
action = (0, lodash_2.startCase)(action);
|
|
457
329
|
const sf = sourceFile.split("/").at(-1).replace(".ts", "");
|
|
458
330
|
let m = {
|
|
459
331
|
summary: `${summary}${action !== "" ? ` ${action}` : "route"}`,
|
|
@@ -478,7 +350,7 @@ class AutoSwagger {
|
|
|
478
350
|
});
|
|
479
351
|
}
|
|
480
352
|
// filter unused tags
|
|
481
|
-
const usedTags =
|
|
353
|
+
const usedTags = lodash_1.default.uniq(Object.entries(paths)
|
|
482
354
|
.map(([p, val]) => Object.entries(val)[0][1].tags)
|
|
483
355
|
.flat());
|
|
484
356
|
docs.tags = globalTags.filter((tag) => usedTags.includes(tag.name));
|
|
@@ -497,7 +369,7 @@ class AutoSwagger {
|
|
|
497
369
|
sourceFile = route.meta.resolvedHandler.namespace;
|
|
498
370
|
action = route.meta.resolvedHandler.method;
|
|
499
371
|
// If not defined by an annotation, use the combination of "controllerNameMethodName"
|
|
500
|
-
if (action !== "" && (0,
|
|
372
|
+
if (action !== "" && (0, lodash_1.isUndefined)(operationId) && route.handler) {
|
|
501
373
|
operationId = (0, helpers_1.formatOperationId)(route.handler);
|
|
502
374
|
}
|
|
503
375
|
}
|
|
@@ -522,7 +394,7 @@ class AutoSwagger {
|
|
|
522
394
|
else {
|
|
523
395
|
// handles lazy import
|
|
524
396
|
// const TestController = () => import('#controllers/test_controller')
|
|
525
|
-
v6handler = await (0,
|
|
397
|
+
v6handler = await (0, adonis_helpers_1.serializeV6Handler)(v6handler);
|
|
526
398
|
action = v6handler.method;
|
|
527
399
|
sourceFile = v6handler.moduleNameOrPath;
|
|
528
400
|
operationId = (0, helpers_1.formatOperationId)(sourceFile + "." + action);
|
|
@@ -548,235 +420,12 @@ class AutoSwagger {
|
|
|
548
420
|
if (this.options.debug) {
|
|
549
421
|
if (sourceFile !== "") {
|
|
550
422
|
console.log(typeof customAnnotations !== "undefined" &&
|
|
551
|
-
!
|
|
423
|
+
!lodash_1.default.isEmpty(customAnnotations)
|
|
552
424
|
? `\x1b[32m✓ FOUND for ${action}\x1b[0m`
|
|
553
425
|
: `\x1b[33m✗ MISSING for ${action}\x1b[0m`, `${sourceFile} (${route.methods[0].toUpperCase()} ${route.pattern})`);
|
|
554
426
|
}
|
|
555
427
|
}
|
|
556
428
|
return { sourceFile, action, customAnnotations, operationId };
|
|
557
429
|
}
|
|
558
|
-
async getSchemas() {
|
|
559
|
-
let schemas = {
|
|
560
|
-
Any: {
|
|
561
|
-
description: "Any JSON object not defined as schema",
|
|
562
|
-
},
|
|
563
|
-
};
|
|
564
|
-
schemas = {
|
|
565
|
-
...schemas,
|
|
566
|
-
...(await this.getInterfaces()),
|
|
567
|
-
...(await this.getSerializers()),
|
|
568
|
-
...(await this.getModels()),
|
|
569
|
-
...(await this.getValidators()),
|
|
570
|
-
...(await this.getEnums()),
|
|
571
|
-
};
|
|
572
|
-
return schemas;
|
|
573
|
-
}
|
|
574
|
-
async getValidators() {
|
|
575
|
-
const validators = {};
|
|
576
|
-
let p6 = path_1.default.join(this.options.appPath, "validators");
|
|
577
|
-
if (typeof this.customPaths["#validators"] !== "undefined") {
|
|
578
|
-
// it's v6
|
|
579
|
-
p6 = p6.replaceAll("app/validators", this.customPaths["#validators"]);
|
|
580
|
-
p6 = p6.replaceAll("app\\validators", this.customPaths["#validators"]);
|
|
581
|
-
}
|
|
582
|
-
if (!(0, fs_2.existsSync)(p6)) {
|
|
583
|
-
if (this.options.debug) {
|
|
584
|
-
console.log("Validators paths don't exist", p6);
|
|
585
|
-
}
|
|
586
|
-
return validators;
|
|
587
|
-
}
|
|
588
|
-
const files = await this.getFiles(p6, []);
|
|
589
|
-
if (this.options.debug) {
|
|
590
|
-
console.log("Found validator files", files);
|
|
591
|
-
}
|
|
592
|
-
try {
|
|
593
|
-
for (let file of files) {
|
|
594
|
-
if (/^[a-zA-Z]:/.test(file)) {
|
|
595
|
-
file = "file:///" + file;
|
|
596
|
-
}
|
|
597
|
-
const val = await import(file);
|
|
598
|
-
for (const [key, value] of Object.entries(val)) {
|
|
599
|
-
if (value.constructor.name.includes("VineValidator")) {
|
|
600
|
-
validators[key] = await this.validatorParser.validatorToObject(value);
|
|
601
|
-
validators[key].description = key + " (Validator)";
|
|
602
|
-
}
|
|
603
|
-
}
|
|
604
|
-
}
|
|
605
|
-
}
|
|
606
|
-
catch (e) {
|
|
607
|
-
console.log("**You are probably using 'node ace serve --hmr', which is not supported yet. Use 'node ace serve --watch' instead.**");
|
|
608
|
-
console.error(e.message);
|
|
609
|
-
}
|
|
610
|
-
return validators;
|
|
611
|
-
}
|
|
612
|
-
async getSerializers() {
|
|
613
|
-
const serializers = {};
|
|
614
|
-
let p6 = path_1.default.join(this.options.appPath, "serializers");
|
|
615
|
-
if (typeof this.customPaths["#serializers"] !== "undefined") {
|
|
616
|
-
// it's v6
|
|
617
|
-
p6 = p6.replaceAll("app/serializers", this.customPaths["#serializers"]);
|
|
618
|
-
p6 = p6.replaceAll("app\\serializers", this.customPaths["#serializers"]);
|
|
619
|
-
}
|
|
620
|
-
if (!(0, fs_2.existsSync)(p6)) {
|
|
621
|
-
if (this.options.debug) {
|
|
622
|
-
console.log("Serializers paths don't exist", p6);
|
|
623
|
-
}
|
|
624
|
-
return serializers;
|
|
625
|
-
}
|
|
626
|
-
const files = await this.getFiles(p6, []);
|
|
627
|
-
if (this.options.debug) {
|
|
628
|
-
console.log("Found serializer files", files);
|
|
629
|
-
}
|
|
630
|
-
for (let file of files) {
|
|
631
|
-
if (/^[a-zA-Z]:/.test(file)) {
|
|
632
|
-
file = "file:///" + file;
|
|
633
|
-
}
|
|
634
|
-
const val = await import(file);
|
|
635
|
-
for (const [key, value] of Object.entries(val)) {
|
|
636
|
-
if (key.indexOf("Serializer") > -1) {
|
|
637
|
-
serializers[key] = value;
|
|
638
|
-
}
|
|
639
|
-
}
|
|
640
|
-
}
|
|
641
|
-
return serializers;
|
|
642
|
-
}
|
|
643
|
-
async getModels() {
|
|
644
|
-
const models = {};
|
|
645
|
-
let p = path_1.default.join(this.options.appPath, "Models");
|
|
646
|
-
let p6 = path_1.default.join(this.options.appPath, "models");
|
|
647
|
-
if (typeof this.customPaths["#models"] !== "undefined") {
|
|
648
|
-
// it's v6
|
|
649
|
-
p6 = p6.replaceAll("app/models", this.customPaths["#models"]);
|
|
650
|
-
p6 = p6.replaceAll("app\\models", this.customPaths["#models"]);
|
|
651
|
-
}
|
|
652
|
-
if (!(0, fs_2.existsSync)(p) && !(0, fs_2.existsSync)(p6)) {
|
|
653
|
-
if (this.options.debug) {
|
|
654
|
-
console.log("Model paths don't exist", p, p6);
|
|
655
|
-
}
|
|
656
|
-
return models;
|
|
657
|
-
}
|
|
658
|
-
if ((0, fs_2.existsSync)(p6)) {
|
|
659
|
-
p = p6;
|
|
660
|
-
}
|
|
661
|
-
const files = await this.getFiles(p, []);
|
|
662
|
-
const readFile = util_1.default.promisify(fs_1.default.readFile);
|
|
663
|
-
if (this.options.debug) {
|
|
664
|
-
console.log("Found model files", files);
|
|
665
|
-
}
|
|
666
|
-
for (let file of files) {
|
|
667
|
-
file = file.replace(".js", "");
|
|
668
|
-
const data = await readFile(file, "utf8");
|
|
669
|
-
file = file.replace(".ts", "");
|
|
670
|
-
const split = file.split("/");
|
|
671
|
-
let name = split[split.length - 1].replace(".ts", "");
|
|
672
|
-
file = file.replace("app/", "/app/");
|
|
673
|
-
const parsed = this.modelParser.parseModelProperties(data);
|
|
674
|
-
if (parsed.name !== "") {
|
|
675
|
-
name = parsed.name;
|
|
676
|
-
}
|
|
677
|
-
let schema = {
|
|
678
|
-
type: "object",
|
|
679
|
-
properties: parsed.props,
|
|
680
|
-
description: name + " (Model)",
|
|
681
|
-
};
|
|
682
|
-
if (parsed.required.length > 0) {
|
|
683
|
-
schema['required'] = parsed.required;
|
|
684
|
-
}
|
|
685
|
-
if (name.toLowerCase().includes('readme.md'))
|
|
686
|
-
continue;
|
|
687
|
-
models[name] = schema;
|
|
688
|
-
}
|
|
689
|
-
return models;
|
|
690
|
-
}
|
|
691
|
-
async getInterfaces() {
|
|
692
|
-
let interfaces = {
|
|
693
|
-
...example_1.ExampleInterfaces.paginationInterface(),
|
|
694
|
-
};
|
|
695
|
-
let p = path_1.default.join(this.options.appPath, "Interfaces");
|
|
696
|
-
let p6 = path_1.default.join(this.options.appPath, "interfaces");
|
|
697
|
-
if (typeof this.customPaths["#interfaces"] !== "undefined") {
|
|
698
|
-
// it's v6
|
|
699
|
-
p6 = p6.replaceAll("app/interfaces", this.customPaths["#interfaces"]);
|
|
700
|
-
p6 = p6.replaceAll("app\\interfaces", this.customPaths["#interfaces"]);
|
|
701
|
-
}
|
|
702
|
-
if (!(0, fs_2.existsSync)(p) && !(0, fs_2.existsSync)(p6)) {
|
|
703
|
-
if (this.options.debug) {
|
|
704
|
-
console.log("Interface paths don't exist", p, p6);
|
|
705
|
-
}
|
|
706
|
-
return interfaces;
|
|
707
|
-
}
|
|
708
|
-
if ((0, fs_2.existsSync)(p6)) {
|
|
709
|
-
p = p6;
|
|
710
|
-
}
|
|
711
|
-
const files = await this.getFiles(p, []);
|
|
712
|
-
if (this.options.debug) {
|
|
713
|
-
console.log("Found interfaces files", files);
|
|
714
|
-
}
|
|
715
|
-
const readFile = util_1.default.promisify(fs_1.default.readFile);
|
|
716
|
-
for (let file of files) {
|
|
717
|
-
file = file.replace(".js", "");
|
|
718
|
-
const data = await readFile(file, "utf8");
|
|
719
|
-
file = file.replace(".ts", "");
|
|
720
|
-
interfaces = {
|
|
721
|
-
...interfaces,
|
|
722
|
-
...this.interfaceParser.parseInterfaces(data),
|
|
723
|
-
};
|
|
724
|
-
}
|
|
725
|
-
return interfaces;
|
|
726
|
-
}
|
|
727
|
-
async getFiles(dir, files_) {
|
|
728
|
-
const fs = require("fs");
|
|
729
|
-
files_ = files_ || [];
|
|
730
|
-
var files = await fs.readdirSync(dir);
|
|
731
|
-
for (let i in files) {
|
|
732
|
-
var name = dir + "/" + files[i];
|
|
733
|
-
if (fs.statSync(name).isDirectory()) {
|
|
734
|
-
await this.getFiles(name, files_);
|
|
735
|
-
}
|
|
736
|
-
else {
|
|
737
|
-
files_.push(name);
|
|
738
|
-
}
|
|
739
|
-
}
|
|
740
|
-
return files_;
|
|
741
|
-
}
|
|
742
|
-
async getEnums() {
|
|
743
|
-
let enums = {};
|
|
744
|
-
const enumParser = new parsers_1.EnumParser();
|
|
745
|
-
let p = path_1.default.join(this.options.appPath, "Types");
|
|
746
|
-
let p6 = path_1.default.join(this.options.appPath, "types");
|
|
747
|
-
if (typeof this.customPaths["#types"] !== "undefined") {
|
|
748
|
-
// it's v6
|
|
749
|
-
p6 = p6.replaceAll("app/types", this.customPaths["#types"]);
|
|
750
|
-
p6 = p6.replaceAll("app\\types", this.customPaths["#types"]);
|
|
751
|
-
}
|
|
752
|
-
if (!(0, fs_2.existsSync)(p) && !(0, fs_2.existsSync)(p6)) {
|
|
753
|
-
if (this.options.debug) {
|
|
754
|
-
console.log("Enum paths don't exist", p, p6);
|
|
755
|
-
}
|
|
756
|
-
return enums;
|
|
757
|
-
}
|
|
758
|
-
if ((0, fs_2.existsSync)(p6)) {
|
|
759
|
-
p = p6;
|
|
760
|
-
}
|
|
761
|
-
const files = await this.getFiles(p, []);
|
|
762
|
-
if (this.options.debug) {
|
|
763
|
-
console.log("Found enum files", files);
|
|
764
|
-
}
|
|
765
|
-
const readFile = util_1.default.promisify(fs_1.default.readFile);
|
|
766
|
-
for (let file of files) {
|
|
767
|
-
file = file.replace(".js", "");
|
|
768
|
-
const data = await readFile(file, "utf8");
|
|
769
|
-
file = file.replace(".ts", "");
|
|
770
|
-
const split = file.split("/");
|
|
771
|
-
const name = split[split.length - 1].replace(".ts", "");
|
|
772
|
-
file = file.replace("app/", "/app/");
|
|
773
|
-
const parsedEnums = enumParser.parseEnums(data);
|
|
774
|
-
enums = {
|
|
775
|
-
...enums,
|
|
776
|
-
...parsedEnums,
|
|
777
|
-
};
|
|
778
|
-
}
|
|
779
|
-
return enums;
|
|
780
|
-
}
|
|
781
430
|
}
|
|
782
431
|
exports.AutoSwagger = AutoSwagger;
|