forked from grinat/moleculer-auto-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.js
147 lines (138 loc) · 4.01 KB
/
actions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const swaggerUiAssetPath = require("swagger-ui-dist").getAbsoluteFSPath();
const fs = require("node:fs");
module.exports = {
generateDocs: {
openapi: {
// you can declare custom Path Item Object
// which override autogenerated object from params
// https://github.com/OAI/OpenAPI-Specification/blob/b748a884fa4571ffb6dd6ed9a4d20e38e41a878c/versions/3.0.3.md#path-item-object-example
summary: "OpenAPI schema url",
// you custom response
// https://github.com/OAI/OpenAPI-Specification/blob/b748a884fa4571ffb6dd6ed9a4d20e38e41a878c/versions/3.0.3.md#response-object-examples
responses: {
200: {
description: "",
content: {
"application/json": {
schema: {
$ref: "#/components/schemas/OpenAPIModel",
},
},
},
},
},
// you custom tag
// https://github.com/OAI/OpenAPI-Specification/blob/b748a884fa4571ffb6dd6ed9a4d20e38e41a878c/versions/3.0.3.md#fixed-fields-8
tags: ["openapi"],
// components which attached to root of docx
// https://github.com/OAI/OpenAPI-Specification/blob/b748a884fa4571ffb6dd6ed9a4d20e38e41a878c/versions/3.0.3.md#components-object
components: {
schemas: {
// you custom schema
// https://github.com/OAI/OpenAPI-Specification/blob/b748a884fa4571ffb6dd6ed9a4d20e38e41a878c/versions/3.0.3.md#models-with-polymorphism-support
OpenAPIModel: {
type: "object",
properties: {
openapi: {
example: "3.0.3",
type: "string",
description: "OpenAPI version",
},
info: {
type: "object",
properties: {
description: {
type: "string",
},
},
},
tags: {
type: "array",
items: {
type: "string",
},
},
},
required: ["openapi"],
},
},
},
},
handler() {
return this.generateSchema();
},
},
assets: {
openapi: {
summary: "OpenAPI assets",
description: "Return files from swagger-ui-dist folder",
},
params: {
file: {
type: "enum",
values: [
"swagger-ui.css",
"swagger-ui.css.map",
"swagger-ui-bundle.js",
"swagger-ui-bundle.js.map",
"swagger-ui-standalone-preset.js",
"swagger-ui-standalone-preset.js.map",
],
},
},
handler(ctx) {
if (ctx.params.file.indexOf(".css") > -1) {
ctx.meta.$responseType = "text/css";
} else if (ctx.params.file.indexOf(".js") > -1) {
ctx.meta.$responseType = "text/javascript";
} else {
ctx.meta.$responseType = "application/octet-stream";
}
return fs.createReadStream(`${swaggerUiAssetPath}/${ctx.params.file}`);
},
},
ui: {
openapi: {
summary: "OpenAPI ui",
description: "You can provide any schema file in query param",
},
params: {
url: { $$t: "Schema url", type: "string", optional: true },
},
handler(ctx) {
ctx.meta.$responseType = "text/html; charset=utf-8";
return `
<html>
<head>
<title>OpenAPI UI</title>
<link rel="stylesheet" href="${this.settings.assetsPath}/swagger-ui.css"/>
</head>
<body>
<div id="swagger-ui">
<p>Loading...</p>
<noscript>If you see json, you need to update your moleculer-web to 0.8.0 and moleculer to 0.12</noscript>
</div>
<script src="${this.settings.assetsPath}/swagger-ui-bundle.js"></script>
<script src="${this.settings.assetsPath}/swagger-ui-standalone-preset.js"></script>
<script>
window.onload = function() {
SwaggerUIBundle({
url: "${ctx.params.url || this.settings.schemaPath}",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset,
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout",
});
}
</script>
</body>
</html>`;
},
},
};