This commit is contained in:
Soph :3 2025-12-13 13:30:08 +02:00
parent 9229838f6a
commit ef0e135e61

View file

@ -168,11 +168,30 @@ async function runComparison() {
}
}
export class ClientResponse extends Response {
constructor(body?: BodyInit, init?: ResponseInit) {
super(body, init);
this.headers.set("Access-Control-Allow-Origin", process.env.ORIGIN!);
this.headers.set("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT, DELETE");
}
static override json(body: unknown, init?: ResponseInit): Response {
return new Response(JSON.stringify(body), {
...init,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": process.env.ORIGIN!,
"Access-Control-Allow-Methods": "OPTIONS, GET, POST, PUT, DELETE",
...init?.headers,
},
});
}
}
Bun.serve({
routes: {
"/": () => new Response("Sheets v2"),
"/artists.ndjson": async () => new Response(await fs.readFile("artists.ndjson")),
"/th_artists.ndjson": async () => new Response(await fs.readFile("th_artists.ndjson")),
"/": () => new ClientResponse("Sheets v2"),
"/artists.ndjson": async () => new ClientResponse(await fs.readFile("artists.ndjson")),
"/th_artists.ndjson": async () => new ClientResponse(await fs.readFile("th_artists.ndjson")),
"/ignore-th/:id": async (req) => {
const authFail = requireBasicAuth(req);
if (authFail) return authFail;
@ -181,7 +200,7 @@ Bun.serve({
const changes = changelist_ids[id];
if (!changes) {
return new Response("Id is invalid.", { status: 404 });
return new ClientResponse("Id is invalid.", { status: 404 });
}
const embed = changes.webhook.embeds[0];
@ -199,7 +218,7 @@ Bun.serve({
delete changelist_ids[id];
await updateChangelistIds();
return new Response("Ignored successfully.");
return new ClientResponse("Ignored successfully.");
},
"/merge-th/:id": async (req) => {
const authFail = requireBasicAuth(req);
@ -209,7 +228,7 @@ Bun.serve({
const changes = changelist_ids[id];
if (!changes) {
return new Response("Id is invalid.", { status: 404 });
return new ClientResponse("Id is invalid.", { status: 404 });
}
const artistsRaw = await fs.readFile("./artists.ndjson", "utf8");
@ -262,11 +281,11 @@ Bun.serve({
delete changelist_ids[id];
await updateChangelistIds();
return new Response("Merged successfully.");
return new ClientResponse("Merged successfully.");
}
},
fetch() {
return new Response("Unmatched route");
return new ClientResponse("Unmatched route");
},
hostname: process.env.HOST || "127.0.0.1",