-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpressServer.js
More file actions
265 lines (249 loc) · 8.19 KB
/
Copy pathexpressServer.js
File metadata and controls
265 lines (249 loc) · 8.19 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
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
const http = require("node:http");
const fs = require("node:fs");
const path = require("node:path");
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const OpenApiValidator = require("express-openapi-validator");
const logger = require("./logger");
const config = require("./config");
class ExpressServer {
static sanitizeOperationId(operationId) {
if (!operationId || typeof operationId !== "string") {
return null;
}
let result = operationId.trim();
if (result.length === 0) {
return null;
}
result = result.replace(/[_-]+/g, " ");
result = result.replace(/[^a-zA-Z0-9_$]+/g, " ");
result = result
.split(" ")
.filter((segment) => segment.length > 0)
.map((segment, index) => {
if (index === 0) {
return segment;
}
return segment.charAt(0).toUpperCase() + segment.slice(1);
})
.join("");
result = result.replace(/^[^a-zA-Z_$]+/, "");
if (result.length === 0) {
return null;
}
return result.charAt(0).toLowerCase() + result.slice(1);
}
static sanitizeTagName(tagName) {
if (!tagName || typeof tagName !== "string") {
return null;
}
let result = tagName.trim();
if (result.length === 0) {
return null;
}
result = result.replace(/[_-]+/g, " ");
result = result.replace(/[^a-zA-Z0-9_$]+/g, " ");
const parts = result
.split(" ")
.filter((segment) => segment.length > 0)
.map((segment) => segment.charAt(0).toUpperCase() + segment.slice(1));
if (parts.length === 0) {
return null;
}
return parts.join("");
}
static normalizeOperationIds(schema) {
if (!schema || !schema.paths) {
return;
}
const methods = ["get", "put", "post", "delete", "options", "head", "patch", "trace"];
for (const pathKey of Object.keys(schema.paths)) {
const pathItem = schema.paths[pathKey];
for (const method of methods) {
const operation = pathItem[method];
if (operation?.operationId) {
const normalizedId = ExpressServer.sanitizeOperationId(operation.operationId);
if (normalizedId && normalizedId !== operation.operationId) {
operation["x-original-operationId"] = operation.operationId;
operation.operationId = normalizedId;
}
}
}
}
}
constructor(port, openApiJsonPath) {
this.port = port;
this.app = express();
try {
this.schema = JSON.parse(fs.readFileSync(openApiJsonPath, "utf8"));
if (this.schema?.components) {
const { components } = this.schema;
const componentMirrors = [
"schemas",
"responses",
"parameters",
"examples",
"requestBodies",
"headers",
"securitySchemes",
"links",
"callbacks",
"pathItems",
];
for (const key of componentMirrors) {
if (!this.schema[key] && components[key]) {
this.schema[key] = components[key];
}
}
}
ExpressServer.normalizeOperationIds(this.schema);
} catch (e) {
logger.error("failed to start Express Server", e.message);
}
this.setupMiddleware();
}
static getStatusText(status) {
const statusTexts = {
400: "Bad Request",
401: "Unauthorized",
403: "Forbidden",
404: "Not Found",
405: "Method Not Allowed",
409: "Conflict",
422: "Unprocessable Entity",
429: "Too Many Requests",
500: "Internal Server Error",
501: "Not Implemented",
502: "Bad Gateway",
503: "Service Unavailable",
504: "Gateway Timeout",
};
return statusTexts[status] || "Unknown Error";
}
setupMiddleware() {
// this.setupAllowedMedia();
this.app.use(cors());
this.app.use(bodyParser.json({ limit: "14MB" }));
this.app.use(express.json());
this.app.use(express.urlencoded({ extended: false }));
this.app.use((_req, res, next) => {
res.set("API-Version", this.schema.info.version);
next();
});
const sendOpenApiSpec = (_req, res) => res.json(this.schema);
this.app.get("/v1/openapi.json", sendOpenApiSpec);
this.app.use(
OpenApiValidator.middleware({
apiSpec: this.schema,
validateApiSpec: false,
validateSecurity: false,
validateRequests: {
coerceTypes: false,
allowUnknownBodyProperties: false,
},
operationHandlers: {
basePath: path.join(__dirname),
},
fileUploader: { dest: config.FILE_UPLOAD_PATH },
}),
);
ExpressServer.registerRoutes(this.app, this.schema);
}
static toExpressPath(openApiPath) {
return openApiPath.replace(/{/g, ":").replace(/}/g, "");
}
static resolveOperationHandler(operation) {
let handlerRef = operation["x-eov-operation-handler"];
if (!handlerRef && operation.tags?.length > 0) {
const tagName = ExpressServer.sanitizeTagName(operation.tags[0]);
if (tagName) {
handlerRef = `controllers/${tagName}Controller`;
}
}
if (!handlerRef) {
logger.warn(`No handler reference found for operation ${operation.operationId}`);
return null;
}
const normalizedRef = handlerRef.replace(/\\/g, "/");
const modulePath = normalizedRef.endsWith(".js")
? path.join(__dirname, normalizedRef)
: path.join(__dirname, `${normalizedRef}.js`);
if (!fs.existsSync(modulePath)) {
logger.warn(`Handler module missing for ${operation.operationId}: ${modulePath}`);
return null;
}
// eslint-disable-next-line global-require, import/no-dynamic-require
const controllerModule = require(modulePath);
const handler = controllerModule[operation.operationId];
if (typeof handler !== "function") {
logger.warn(`Handler function ${operation.operationId} not found in ${modulePath}`);
return null;
}
return handler;
}
static registerRoutes(app, schema) {
if (!schema || !schema.paths) {
return;
}
const methods = ["get", "put", "post", "delete", "options", "head", "patch", "trace"];
for (const pathKey of Object.keys(schema.paths)) {
const expressPath = ExpressServer.toExpressPath(pathKey);
const pathItem = schema.paths[pathKey];
for (const method of methods) {
const operation = pathItem[method];
if (!operation) {
continue;
}
const handler = ExpressServer.resolveOperationHandler(operation);
if (!handler) {
continue;
}
app[method](expressPath, async (req, res, next) => {
logger.info(`Incoming request for ${method.toUpperCase()} ${expressPath}`);
req.openapi = req.openapi || {};
req.openapi.schema = req.openapi.schema || operation;
req.openapi.pathParams = req.openapi.pathParams || req.params;
try {
await Promise.resolve(handler(req, res, next));
} catch (error) {
next(error);
}
});
}
}
}
launch() {
// eslint-disable-next-line no-unused-vars
this.app.use((err, req, res, _next) => {
// format errors using RFC 7807 Problem Details format
const status = err.status || 500;
const problemDetails = {
type: `https://httpstatuses.com/${status}`,
title: ExpressServer.getStatusText(status),
status,
detail: err.message || err.toString(),
};
// Add instance URI if available
if (req.originalUrl) {
problemDetails.instance = req.originalUrl;
}
// Add additional error details if available
if (err.status === 400 && err.errors && Array.isArray(err.errors) && err.errors.length > 0) {
problemDetails.invalidParams = err.errors;
}
// Set the proper content type for problem+json
res.set("Content-Type", "application/problem+json");
res.status(status).json(problemDetails);
});
http.createServer(this.app).listen(this.port);
console.log(`Listening on port ${this.port}`);
}
async close() {
if (this.server !== undefined) {
await this.server.close();
console.log(`Server on port ${this.port} shut down`);
}
}
}
module.exports = ExpressServer;