-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenapi-generator.js
202 lines (165 loc) · 4.52 KB
/
openapi-generator.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
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
import { fileURLToPath } from "url";
import path from "path";
import fs from "fs";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const basedir = `${__dirname}/defs/api`;
const methods = ["GET", "POST", "PUT", "PATCH", "DELETE"];
const map_path = (dir) => {
return `/${dir.replaceAll("[", "{").replaceAll("]", "}")}`;
};
const get_path_params = (dir) => {
return [...dir.matchAll(/\{(.+?)\}/g)];
}
const securitySchemes = {
"API Key": {
type: "apiKey",
in: "header",
name: "x-access-token",
}
}
const security = [
{ "API Key": [] }
]
const paths = {};
const public_error_payload = JSON.parse(fs.readFileSync(`${__dirname}/defs/error/PublicErrorPayload.schema.json`));
const process_dir = (target) => {
const full_target = `${basedir}${target == null ? "" : `/${target}`}`;
const filenames = fs.readdirSync(full_target).sort();
for (const name of filenames) {
if (methods.includes(name)) {
const method = name;
const path = map_path(target);
const output_path = `${full_target}/${name}/Output.schema.json`;
const output = JSON.parse(fs.readFileSync(output_path));
const payload_path = `${full_target}/${name}/Payload.schema.json`;
let payload = null;
if (fs.existsSync(payload_path)) payload = JSON.parse(fs.readFileSync(payload_path));
const query_path = `${full_target}/${name}/Query.schema.json`;
const query_params = [];
if(fs.existsSync(query_path)) {
const schema = JSON.parse(fs.readFileSync(query_path));
for(const key in schema.properties) {
query_params.push({
name: key,
in: "query",
required: !!schema.required?.includes(key),
style: "deepObject",
explode: true,
allowReserved: true,
schema: schema.properties[key],
})
}
}
const path_params = get_path_params(path).map(name => {
return {
name: name[1],
in: "path",
required: true,
schema: {
type: "string",
}
}
});
paths[path] ??= {};
paths[path][method.toLowerCase()] = {
parameters: [
...path_params,
...query_params,
],
requestBody:
payload == null
? undefined
: {
required: true,
content: {
"application/json": {
schema: payload,
},
},
},
responses: {
200: {
description: "A successful response",
content: {
"application/json": {
schema: output,
},
},
},
"4XX": {
description: "A client error",
content: {
"application/json": {
schema: { $ref: "#/components/schemas/Error" },
}
}
},
"5XX": {
description: "A server error",
content: {
"application/json": {
schema: { $ref: "#/components/schemas/Error" },
}
}
},
}
}
}
for (const name of filenames) {
if (methods.includes(name)) continue;
if (fs.statSync(`${full_target}/${name}`).isDirectory()) {
process_dir(`${target == null ? "" : `${target}/`}${name}`);
}
}
}
};
process_dir(null)
// TODO: don't hardcode this
paths["/station-pictures"].post.requestBody = {
required: true,
content: {
"image/png": {
schema: {
type: "string",
format: "binary",
}
},
"image/jpeg": {
schema: {
type: "string",
format: "binary",
}
},
"image/webp": {
schema: {
type: "string",
format: "binary",
}
}
}
};
const document = {
openapi: "3.0.3",
info: {
title: "Openstream Media Server API",
// termsOfService: "https://openstream.fm/api-terms/",
// license: {
// name: "Apache 2.0",
// url: "https://www.apache.org/licenses/LICENSE-2.0.html"
// },
version: "0.1.0",
},
servers: [{
url: "https://api.openstream.fm",
description: "Openstream production servers",
}],
security,
paths,
components: {
securitySchemes,
schemas: {
Error: public_error_payload,
}
}
}
fs.writeFileSync(`${__dirname}/openapi.json`, JSON.stringify(document, null, 2));