tricoteuses-legifrance/src/hooks.ts

42 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-08-11 16:26:55 +02:00
import type { GetSession, Handle } from "@sveltejs/kit"
2022-08-10 17:50:13 +02:00
import config from "$lib/server/config"
2022-08-11 16:26:55 +02:00
export const getSession: GetSession = async () => {
2022-08-10 17:50:13 +02:00
return {
title: config.title,
}
}
2022-08-11 16:26:55 +02:00
export const handle: Handle = async ({ event, resolve }) => {
2022-08-14 09:05:04 +02:00
// Add CORS support.
2022-08-11 16:26:55 +02:00
const { request } = event
if (request.method === "OPTIONS") {
const headers: { [name: string]: string } = {
"Access-Control-Allow-Methods": "GET, OPTIONS",
"Access-Control-Allow-Origin": "*",
}
const vary: string[] = []
const allowedHeaders =
request.headers
.get("Access-Control-Request-Headers")
?.split(",")
.map((name) => name.trim()) ?? []
if (allowedHeaders.length > 0) {
vary.push("Access-Control-Request-Headers")
headers["Access-Control-Allow-Headers"] = allowedHeaders.join(", ")
}
if (vary.length > 0) {
headers["Vary"] = vary.join(", ")
}
return new Response(null, { status: 204, headers })
}
2022-08-14 09:05:04 +02:00
const response = await resolve(event)
if (response.headers.get("Content-Type")?.startsWith("application/json")) {
response.headers.set("Access-Control-Allow-Origin", "*")
}
return response
2022-08-11 16:26:55 +02:00
}