fix: explicit OPTIONS handler for CORS preflight
This commit is contained in:
parent
7a9417297f
commit
fa8ff8b361
|
|
@ -8,14 +8,31 @@ import { migrate } from "./db";
|
||||||
|
|
||||||
await migrate();
|
await migrate();
|
||||||
|
|
||||||
|
const CORS_ORIGIN = process.env.CORS_ORIGINS?.split(",") ?? ["https://pingql.com"];
|
||||||
|
|
||||||
|
const CORS_HEADERS = {
|
||||||
|
"access-control-allow-credentials": "true",
|
||||||
|
"access-control-allow-methods": "GET, POST, PUT, PATCH, DELETE, OPTIONS",
|
||||||
|
"access-control-allow-headers": "Content-Type, Authorization",
|
||||||
|
};
|
||||||
|
|
||||||
const app = new Elysia()
|
const app = new Elysia()
|
||||||
.use(cors({
|
.use(cors({
|
||||||
origin: process.env.CORS_ORIGINS?.split(",") ?? ["https://pingql.com"],
|
origin: CORS_ORIGIN,
|
||||||
credentials: true,
|
credentials: true,
|
||||||
allowedHeaders: ["Content-Type", "Authorization"],
|
allowedHeaders: ["Content-Type", "Authorization"],
|
||||||
methods: ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"],
|
methods: ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"],
|
||||||
preflight: true,
|
preflight: true,
|
||||||
}))
|
}))
|
||||||
|
// Explicit OPTIONS handler for cross-origin preflight
|
||||||
|
.options("/*", ({ request }) => {
|
||||||
|
const origin = request.headers.get("origin") ?? "";
|
||||||
|
const allowed = CORS_ORIGIN.includes(origin) ? origin : CORS_ORIGIN[0];
|
||||||
|
return new Response(null, {
|
||||||
|
status: 204,
|
||||||
|
headers: { ...CORS_HEADERS, "access-control-allow-origin": allowed },
|
||||||
|
});
|
||||||
|
})
|
||||||
.get("/", () => ({
|
.get("/", () => ({
|
||||||
name: "PingQL API",
|
name: "PingQL API",
|
||||||
version: "1",
|
version: "1",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue