Initial commit: KOC LOOP platform

This commit is contained in:
巫凤萍
2026-07-30 12:06:41 +08:00
commit d2cde2c9b2
98 changed files with 44076 additions and 0 deletions

41
lib/partner-cors.ts Normal file
View 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 }));
}