Initial commit: KOC LOOP platform
This commit is contained in:
41
lib/partner-cors.ts
Normal file
41
lib/partner-cors.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { env } from "cloudflare:workers";
|
||||
|
||||
function allowedOrigin(request: Request) {
|
||||
const origin = request.headers.get("origin");
|
||||
if (!origin) return null;
|
||||
const portalOrigin = String(
|
||||
(env as unknown as { KOC_PORTAL_URL?: string }).KOC_PORTAL_URL ?? "",
|
||||
).replace(/\/$/, "");
|
||||
return origin === portalOrigin || origin === "http://localhost:3000"
|
||||
? origin
|
||||
: null;
|
||||
}
|
||||
|
||||
export function withPartnerCors(request: Request, response: Response) {
|
||||
const origin = allowedOrigin(request);
|
||||
if (origin) {
|
||||
response.headers.set("Access-Control-Allow-Origin", origin);
|
||||
response.headers.set("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
|
||||
response.headers.set(
|
||||
"Access-Control-Allow-Headers",
|
||||
[
|
||||
"Content-Type",
|
||||
"X-KOC-Task",
|
||||
"X-KOC-Claim",
|
||||
"X-KOC-Delegation",
|
||||
"X-KOC-Distribution",
|
||||
"X-KOC-File-Name",
|
||||
"X-KOC-Upload-Kind",
|
||||
"X-KOC-OCR-Exposure",
|
||||
"X-KOC-OCR-Views",
|
||||
"X-KOC-OCR-Status",
|
||||
].join(", "),
|
||||
);
|
||||
response.headers.append("Vary", "Origin");
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
export function partnerOptions(request: Request) {
|
||||
return withPartnerCors(request, new Response(null, { status: 204 }));
|
||||
}
|
||||
Reference in New Issue
Block a user