@necessarylion/adonis-autoswagger 0.0.3 → 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} +42 -391
- package/dist/example.d.ts +47 -0
- package/dist/example.js +51 -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/{scalarCustomCss.d.ts → scalar_custom_css.d.ts} +1 -1
- package/dist/{scalarCustomCss.js → scalar_custom_css.js} +14 -1
- package/dist/schema.d.ts +46 -0
- package/dist/schema.js +266 -0
- package/dist/types.d.ts +16 -17
- package/dist/types.js +1 -0
- 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 -1137
|
@@ -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,182 +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 http_status_code_1 = __importDefault(require("http-status-code"));
|
|
45
|
-
const lodash_1 = __importDefault(require("lodash"));
|
|
46
|
+
const fs_1 = __importDefault(require("fs"));
|
|
46
47
|
const lodash_2 = require("lodash");
|
|
47
|
-
const
|
|
48
|
-
const
|
|
49
|
-
const
|
|
50
|
-
const
|
|
51
|
-
const helpers_1 = require("./helpers");
|
|
52
|
-
const example_1 = __importStar(require("./example"));
|
|
48
|
+
const http_status_code_1 = __importDefault(require("http-status-code"));
|
|
49
|
+
const ui_1 = require("./ui");
|
|
50
|
+
const file_1 = require("./file");
|
|
51
|
+
const schema_1 = require("./schema");
|
|
53
52
|
class AutoSwagger {
|
|
53
|
+
uiService;
|
|
54
|
+
fileService;
|
|
55
|
+
schemaService;
|
|
54
56
|
options;
|
|
55
57
|
schemas = {};
|
|
56
58
|
commentParser;
|
|
57
|
-
modelParser;
|
|
58
|
-
interfaceParser;
|
|
59
|
-
enumParser;
|
|
60
59
|
routeParser;
|
|
61
|
-
validatorParser;
|
|
62
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
|
+
*/
|
|
63
70
|
ui(url, options) {
|
|
64
|
-
|
|
65
|
-
? "persistAuthorization: true,"
|
|
66
|
-
: "";
|
|
67
|
-
return `<!DOCTYPE html>
|
|
68
|
-
<html lang="en">
|
|
69
|
-
<head>
|
|
70
|
-
<meta charset="UTF-8">
|
|
71
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
72
|
-
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
|
73
|
-
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.1.3/swagger-ui-standalone-preset.js"></script>
|
|
74
|
-
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.1.3/swagger-ui-bundle.js"></script>
|
|
75
|
-
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.1.3/swagger-ui.css" />
|
|
76
|
-
<title>Documentation</title>
|
|
77
|
-
</head>
|
|
78
|
-
<body>
|
|
79
|
-
<div id="swagger-ui"></div>
|
|
80
|
-
<script>
|
|
81
|
-
window.onload = function() {
|
|
82
|
-
SwaggerUIBundle({
|
|
83
|
-
url: "${url}",
|
|
84
|
-
dom_id: '#swagger-ui',
|
|
85
|
-
presets: [
|
|
86
|
-
SwaggerUIBundle.presets.apis,
|
|
87
|
-
SwaggerUIStandalonePreset
|
|
88
|
-
],
|
|
89
|
-
layout: "BaseLayout",
|
|
90
|
-
${persistAuthString}
|
|
91
|
-
})
|
|
92
|
-
}
|
|
93
|
-
</script>
|
|
94
|
-
</body>
|
|
95
|
-
</html>`;
|
|
71
|
+
return this.uiService.ui(url, options);
|
|
96
72
|
}
|
|
97
73
|
rapidoc(url, style = "view") {
|
|
98
|
-
return (
|
|
99
|
-
<!doctype html> <!-- Important: must specify -->
|
|
100
|
-
<html>
|
|
101
|
-
<head>
|
|
102
|
-
<meta charset="utf-8"> <!-- Important: rapi-doc uses utf8 characters -->
|
|
103
|
-
<script type="module" src="https://unpkg.com/rapidoc/dist/rapidoc-min.js"></script>
|
|
104
|
-
<title>Documentation</title>
|
|
105
|
-
</head>
|
|
106
|
-
<body>
|
|
107
|
-
<rapi-doc
|
|
108
|
-
spec-url = "` +
|
|
109
|
-
url +
|
|
110
|
-
`"
|
|
111
|
-
theme = "dark"
|
|
112
|
-
bg-color = "#24283b"
|
|
113
|
-
schema-style="tree"
|
|
114
|
-
schema-expand-level = "10"
|
|
115
|
-
header-color = "#1a1b26"
|
|
116
|
-
allow-try = "true"
|
|
117
|
-
nav-hover-bg-color = "#1a1b26"
|
|
118
|
-
nav-bg-color = "#24283b"
|
|
119
|
-
text-color = "#c0caf5"
|
|
120
|
-
nav-text-color = "#c0caf5"
|
|
121
|
-
primary-color = "#9aa5ce"
|
|
122
|
-
heading-text = "Documentation"
|
|
123
|
-
sort-tags = "true"
|
|
124
|
-
render-style = "` +
|
|
125
|
-
style +
|
|
126
|
-
`"
|
|
127
|
-
default-schema-tab = "example"
|
|
128
|
-
show-components = "true"
|
|
129
|
-
allow-spec-url-load = "false"
|
|
130
|
-
allow-spec-file-load = "false"
|
|
131
|
-
sort-endpoints-by = "path"
|
|
132
|
-
|
|
133
|
-
> </rapi-doc>
|
|
134
|
-
</body>
|
|
135
|
-
</html>
|
|
136
|
-
`);
|
|
74
|
+
return this.uiService.rapidoc(url, style);
|
|
137
75
|
}
|
|
138
76
|
scalar(url, proxyUrl = "https://proxy.scalar.com") {
|
|
139
|
-
return
|
|
140
|
-
<!doctype html>
|
|
141
|
-
<html>
|
|
142
|
-
<head>
|
|
143
|
-
<title>API Reference</title>
|
|
144
|
-
<meta charset="utf-8" />
|
|
145
|
-
<meta
|
|
146
|
-
name="viewport"
|
|
147
|
-
content="width=device-width, initial-scale=1" />
|
|
148
|
-
<style>
|
|
149
|
-
${scalarCustomCss_1.scalarCustomCss}
|
|
150
|
-
</style>
|
|
151
|
-
</head>
|
|
152
|
-
<body>
|
|
153
|
-
<script
|
|
154
|
-
id="api-reference"
|
|
155
|
-
data-url="${url}"
|
|
156
|
-
data-proxy-url="${proxyUrl}"></script>
|
|
157
|
-
<script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script>
|
|
158
|
-
</body>
|
|
159
|
-
</html>
|
|
160
|
-
`;
|
|
77
|
+
return this.uiService.scalar(url, proxyUrl);
|
|
161
78
|
}
|
|
162
79
|
stoplight(url, theme = "dark") {
|
|
163
|
-
return
|
|
164
|
-
<!doctype html>
|
|
165
|
-
<html data-theme="${theme}">
|
|
166
|
-
<head>
|
|
167
|
-
<title>API Documentation - Stoplight</title>
|
|
168
|
-
<meta charset="utf-8" />
|
|
169
|
-
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
|
170
|
-
<script src="https://unpkg.com/@stoplight/elements/web-components.min.js"></script>
|
|
171
|
-
<link rel="stylesheet" href="https://unpkg.com/@stoplight/elements/styles.min.css">
|
|
172
|
-
</head>
|
|
173
|
-
<body style="min-height:100vh">
|
|
174
|
-
<elements-api
|
|
175
|
-
style="display:block;height:100vh;width:100%;"
|
|
176
|
-
apiDescriptionUrl=${url}
|
|
177
|
-
router="hash"
|
|
178
|
-
layout="sidebar"
|
|
179
|
-
/>
|
|
180
|
-
</body>
|
|
181
|
-
</html>
|
|
182
|
-
`;
|
|
80
|
+
return this.uiService.stoplight(url, theme);
|
|
183
81
|
}
|
|
184
82
|
jsonToYaml(json) {
|
|
185
|
-
return
|
|
83
|
+
return this.fileService.jsonToYaml(json);
|
|
186
84
|
}
|
|
187
85
|
async json(routes, options) {
|
|
188
|
-
|
|
189
|
-
const str = await this.readFile(options.path, "json");
|
|
190
|
-
return JSON.parse(str);
|
|
191
|
-
}
|
|
192
|
-
return await this.generate(routes, options);
|
|
86
|
+
return this.fileService.json(routes, options, this.generate.bind(this));
|
|
193
87
|
}
|
|
194
88
|
async writeFile(routes, options) {
|
|
195
|
-
|
|
196
|
-
const contents = this.jsonToYaml(json);
|
|
197
|
-
const filePath = options.path + "swagger.yml";
|
|
198
|
-
const filePathJson = options.path + "swagger.json";
|
|
199
|
-
fs_1.default.writeFileSync(filePath, contents);
|
|
200
|
-
fs_1.default.writeFileSync(filePathJson, JSON.stringify(json, null, 2));
|
|
201
|
-
}
|
|
202
|
-
async readFile(rootPath, type = "yml") {
|
|
203
|
-
const filePath = rootPath + "swagger." + type;
|
|
204
|
-
const data = fs_1.default.readFileSync(filePath, "utf-8");
|
|
205
|
-
if (!data) {
|
|
206
|
-
console.error("Error reading file");
|
|
207
|
-
return;
|
|
208
|
-
}
|
|
209
|
-
return data;
|
|
89
|
+
return this.fileService.writeFile(routes, options, this.generate.bind(this));
|
|
210
90
|
}
|
|
211
91
|
async docs(routes, options) {
|
|
212
|
-
|
|
213
|
-
return this.readFile(options.path);
|
|
214
|
-
}
|
|
215
|
-
return this.jsonToYaml(await this.generate(routes, options));
|
|
92
|
+
return this.fileService.docs(routes, options, this.generate.bind(this));
|
|
216
93
|
}
|
|
217
94
|
async generate(adonisRoutes, options) {
|
|
218
95
|
this.options = {
|
|
@@ -240,13 +117,10 @@ class AutoSwagger {
|
|
|
240
117
|
catch (e) {
|
|
241
118
|
console.error(e);
|
|
242
119
|
}
|
|
243
|
-
this.
|
|
244
|
-
this.
|
|
245
|
-
this.
|
|
246
|
-
this.
|
|
247
|
-
this.validatorParser = new parsers_1.ValidatorParser();
|
|
248
|
-
this.enumParser = new parsers_1.EnumParser();
|
|
249
|
-
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();
|
|
250
124
|
if (this.options.debug) {
|
|
251
125
|
console.log(this.options);
|
|
252
126
|
console.log("Found Schemas", Object.keys(this.schemas));
|
|
@@ -307,7 +181,7 @@ class AutoSwagger {
|
|
|
307
181
|
sscheme = this.options.defaultSecurityScheme;
|
|
308
182
|
}
|
|
309
183
|
let securities = {
|
|
310
|
-
|
|
184
|
+
auth: { [sscheme]: ["access"] },
|
|
311
185
|
"auth:api": { [sscheme]: ["access"] },
|
|
312
186
|
...this.options.authMiddlewares
|
|
313
187
|
?.map((am) => ({
|
|
@@ -341,7 +215,7 @@ class AutoSwagger {
|
|
|
341
215
|
PUT: "204",
|
|
342
216
|
};
|
|
343
217
|
if (!Array.isArray(route.middleware)) {
|
|
344
|
-
route.middleware = (0,
|
|
218
|
+
route.middleware = (0, adonis_helpers_1.serializeV6Middleware)(route.middleware);
|
|
345
219
|
}
|
|
346
220
|
route.middleware.forEach((m) => {
|
|
347
221
|
if (typeof securities[m] !== "undefined") {
|
|
@@ -356,10 +230,9 @@ class AutoSwagger {
|
|
|
356
230
|
return;
|
|
357
231
|
globalTags.push({
|
|
358
232
|
name: tag,
|
|
359
|
-
description: "Everything related to " + tag,
|
|
360
233
|
});
|
|
361
234
|
});
|
|
362
|
-
|
|
235
|
+
let { sourceFile, action, customAnnotations, operationId } = await this.getDataBasedOnAdonisVersion(route);
|
|
363
236
|
route.methods.forEach((method) => {
|
|
364
237
|
let responses = {};
|
|
365
238
|
if (method === "HEAD")
|
|
@@ -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)})`,
|
|
@@ -399,11 +271,10 @@ class AutoSwagger {
|
|
|
399
271
|
if (tag != "") {
|
|
400
272
|
globalTags.push({
|
|
401
273
|
name: tag.toUpperCase(),
|
|
402
|
-
description: "Everything related to " + tag.toUpperCase(),
|
|
403
274
|
});
|
|
404
275
|
tags = [tag.toUpperCase()];
|
|
405
276
|
}
|
|
406
|
-
if ((0,
|
|
277
|
+
if ((0, lodash_1.isEmpty)(responses)) {
|
|
407
278
|
responses[responseCodes[method]] = {
|
|
408
279
|
description: http_status_code_1.default.getMessage(responseCodes[method]),
|
|
409
280
|
content: {
|
|
@@ -454,9 +325,10 @@ class AutoSwagger {
|
|
|
454
325
|
break;
|
|
455
326
|
}
|
|
456
327
|
}
|
|
328
|
+
action = (0, lodash_2.startCase)(action);
|
|
457
329
|
const sf = sourceFile.split("/").at(-1).replace(".ts", "");
|
|
458
330
|
let m = {
|
|
459
|
-
summary: `${summary}${action !== "" ? `
|
|
331
|
+
summary: `${summary}${action !== "" ? ` ${action}` : "route"}`,
|
|
460
332
|
description: description + "\n\n _" + sourceFile + "_ - **" + action + "**",
|
|
461
333
|
operationId: operationId,
|
|
462
334
|
parameters: parameters,
|
|
@@ -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);
|
|
@@ -555,226 +427,5 @@ class AutoSwagger {
|
|
|
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
|
-
models[name] = schema;
|
|
686
|
-
}
|
|
687
|
-
return models;
|
|
688
|
-
}
|
|
689
|
-
async getInterfaces() {
|
|
690
|
-
let interfaces = {
|
|
691
|
-
...example_1.ExampleInterfaces.paginationInterface(),
|
|
692
|
-
};
|
|
693
|
-
let p = path_1.default.join(this.options.appPath, "Interfaces");
|
|
694
|
-
let p6 = path_1.default.join(this.options.appPath, "interfaces");
|
|
695
|
-
if (typeof this.customPaths["#interfaces"] !== "undefined") {
|
|
696
|
-
// it's v6
|
|
697
|
-
p6 = p6.replaceAll("app/interfaces", this.customPaths["#interfaces"]);
|
|
698
|
-
p6 = p6.replaceAll("app\\interfaces", this.customPaths["#interfaces"]);
|
|
699
|
-
}
|
|
700
|
-
if (!(0, fs_2.existsSync)(p) && !(0, fs_2.existsSync)(p6)) {
|
|
701
|
-
if (this.options.debug) {
|
|
702
|
-
console.log("Interface paths don't exist", p, p6);
|
|
703
|
-
}
|
|
704
|
-
return interfaces;
|
|
705
|
-
}
|
|
706
|
-
if ((0, fs_2.existsSync)(p6)) {
|
|
707
|
-
p = p6;
|
|
708
|
-
}
|
|
709
|
-
const files = await this.getFiles(p, []);
|
|
710
|
-
if (this.options.debug) {
|
|
711
|
-
console.log("Found interfaces files", files);
|
|
712
|
-
}
|
|
713
|
-
const readFile = util_1.default.promisify(fs_1.default.readFile);
|
|
714
|
-
for (let file of files) {
|
|
715
|
-
file = file.replace(".js", "");
|
|
716
|
-
const data = await readFile(file, "utf8");
|
|
717
|
-
file = file.replace(".ts", "");
|
|
718
|
-
interfaces = {
|
|
719
|
-
...interfaces,
|
|
720
|
-
...this.interfaceParser.parseInterfaces(data),
|
|
721
|
-
};
|
|
722
|
-
}
|
|
723
|
-
return interfaces;
|
|
724
|
-
}
|
|
725
|
-
async getFiles(dir, files_) {
|
|
726
|
-
const fs = require("fs");
|
|
727
|
-
files_ = files_ || [];
|
|
728
|
-
var files = await fs.readdirSync(dir);
|
|
729
|
-
for (let i in files) {
|
|
730
|
-
var name = dir + "/" + files[i];
|
|
731
|
-
if (fs.statSync(name).isDirectory()) {
|
|
732
|
-
await this.getFiles(name, files_);
|
|
733
|
-
}
|
|
734
|
-
else {
|
|
735
|
-
files_.push(name);
|
|
736
|
-
}
|
|
737
|
-
}
|
|
738
|
-
return files_;
|
|
739
|
-
}
|
|
740
|
-
async getEnums() {
|
|
741
|
-
let enums = {};
|
|
742
|
-
const enumParser = new parsers_1.EnumParser();
|
|
743
|
-
let p = path_1.default.join(this.options.appPath, "Types");
|
|
744
|
-
let p6 = path_1.default.join(this.options.appPath, "types");
|
|
745
|
-
if (typeof this.customPaths["#types"] !== "undefined") {
|
|
746
|
-
// it's v6
|
|
747
|
-
p6 = p6.replaceAll("app/types", this.customPaths["#types"]);
|
|
748
|
-
p6 = p6.replaceAll("app\\types", this.customPaths["#types"]);
|
|
749
|
-
}
|
|
750
|
-
if (!(0, fs_2.existsSync)(p) && !(0, fs_2.existsSync)(p6)) {
|
|
751
|
-
if (this.options.debug) {
|
|
752
|
-
console.log("Enum paths don't exist", p, p6);
|
|
753
|
-
}
|
|
754
|
-
return enums;
|
|
755
|
-
}
|
|
756
|
-
if ((0, fs_2.existsSync)(p6)) {
|
|
757
|
-
p = p6;
|
|
758
|
-
}
|
|
759
|
-
const files = await this.getFiles(p, []);
|
|
760
|
-
if (this.options.debug) {
|
|
761
|
-
console.log("Found enum files", files);
|
|
762
|
-
}
|
|
763
|
-
const readFile = util_1.default.promisify(fs_1.default.readFile);
|
|
764
|
-
for (let file of files) {
|
|
765
|
-
file = file.replace(".js", "");
|
|
766
|
-
const data = await readFile(file, "utf8");
|
|
767
|
-
file = file.replace(".ts", "");
|
|
768
|
-
const split = file.split("/");
|
|
769
|
-
const name = split[split.length - 1].replace(".ts", "");
|
|
770
|
-
file = file.replace("app/", "/app/");
|
|
771
|
-
const parsedEnums = enumParser.parseEnums(data);
|
|
772
|
-
enums = {
|
|
773
|
-
...enums,
|
|
774
|
-
...parsedEnums,
|
|
775
|
-
};
|
|
776
|
-
}
|
|
777
|
-
return enums;
|
|
778
|
-
}
|
|
779
430
|
}
|
|
780
431
|
exports.AutoSwagger = AutoSwagger;
|