Monorepo for Aesthetic.Computer
aesthetic.computer
1// HTTP, 23.05.01.21.14
2// Helpers for making backend functions more nicely
3// written.
4
5function respond(statusCode, body, headers = {}) {
6 if (!headers["Content-Type"]) headers["Content-Type"] = "application/json"; // Default return a JSON reply.
7
8 if (!headers["Access-Control-Allow-Origin"]) {
9 headers["Access-Control-Allow-Origin"] = "*";
10 }
11
12 if (!headers["Access-Control-Allow-Methods"]) {
13 headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS";
14 }
15
16 if (!headers["Access-Control-Allow-Headers"]) {
17 headers["Access-Control-Allow-Headers"] = "Content-Type, Authorization";
18 }
19
20 const res = { statusCode, headers };
21 res.body = typeof body === "object" ? JSON.stringify(body) : body;
22
23 return res;
24}
25
26function pathParams(path) {
27 if (path.startsWith("/")) path = path.slice(1);
28 return path.split("/");
29}
30
31export { respond, pathParams };