From ef0e135e61ffa13fb12aeeefbc285e5e819d4c76 Mon Sep 17 00:00:00 2001 From: yourfriendoss Date: Sat, 13 Dec 2025 13:30:08 +0200 Subject: [PATCH] set cors --- index.ts | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/index.ts b/index.ts index aecc73e..29fb892 100644 --- a/index.ts +++ b/index.ts @@ -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",