From 87df843b82d45abcce5221f312d00d62c2863d8a Mon Sep 17 00:00:00 2001 From: Emmanuel Raviart Date: Sun, 14 Aug 2022 09:05:04 +0200 Subject: [PATCH] Move CORS support to hooks. --- src/hooks.ts | 7 ++++++- src/routes/article/[id].ts | 3 +-- src/routes/article/index.ts | 3 +-- src/routes/dossier_legislatif/[id].ts | 3 +-- src/routes/dossier_legislatif/index.ts | 3 +-- src/routes/id/[...eli].ts | 3 +-- src/routes/id/index.ts | 3 +-- src/routes/idcc/[id].ts | 3 +-- src/routes/idcc/index.ts | 3 +-- src/routes/jo/[id].ts | 3 +-- src/routes/jo/index.ts | 3 +-- src/routes/recherche/index.ts | 5 ++--- src/routes/section_ta/[id].ts | 3 +-- src/routes/section_ta/index.ts | 3 +-- src/routes/texte_version/[id].ts | 3 +-- src/routes/texte_version/index.ts | 3 +-- src/routes/textekali/[id].ts | 3 +-- src/routes/textekali/index.ts | 3 +-- src/routes/textelr/[id].ts | 3 +-- src/routes/textelr/index.ts | 3 +-- src/routes/versions/[...eli].ts | 3 +-- src/routes/versions/index.ts | 3 +-- 22 files changed, 28 insertions(+), 44 deletions(-) diff --git a/src/hooks.ts b/src/hooks.ts index 36abd74..e6303e9 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -9,6 +9,7 @@ export const getSession: GetSession = async () => { } export const handle: Handle = async ({ event, resolve }) => { + // Add CORS support. const { request } = event if (request.method === "OPTIONS") { const headers: { [name: string]: string } = { @@ -32,5 +33,9 @@ export const handle: Handle = async ({ event, resolve }) => { return new Response(null, { status: 204, headers }) } - return await resolve(event) + const response = await resolve(event) + if (response.headers.get("Content-Type")?.startsWith("application/json")) { + response.headers.set("Access-Control-Allow-Origin", "*") + } + return response } diff --git a/src/routes/article/[id].ts b/src/routes/article/[id].ts index de3d000..50c4fc5 100644 --- a/src/routes/article/[id].ts +++ b/src/routes/article/[id].ts @@ -13,10 +13,9 @@ export const GET: RequestHandler = async ({ params }) => { ).map(({ data }) => data)[0] if (article === undefined) { - return { headers: { "Access-Control-Allow-Origin": "*" }, status: 404 } + return { status: 404 } } return { - headers: { "Access-Control-Allow-Origin": "*" }, body: { article: article as unknown as JSONObject }, } } diff --git a/src/routes/article/index.ts b/src/routes/article/index.ts index 3598515..5e5a390 100644 --- a/src/routes/article/index.ts +++ b/src/routes/article/index.ts @@ -48,7 +48,7 @@ export const GET: RequestHandler = async ({ url }) => { ) return { // status: 400, - headers: { "Access-Control-Allow-Origin": "*" }, + body: { error: { query: queryError as unknown as JSONObject, @@ -66,7 +66,6 @@ export const GET: RequestHandler = async ({ url }) => { ` ).map(({ data }) => data) return { - headers: { "Access-Control-Allow-Origin": "*" }, body: { article: articles as unknown as JSONObject[] }, } } diff --git a/src/routes/dossier_legislatif/[id].ts b/src/routes/dossier_legislatif/[id].ts index bedc5a6..d75a1e0 100644 --- a/src/routes/dossier_legislatif/[id].ts +++ b/src/routes/dossier_legislatif/[id].ts @@ -13,10 +13,9 @@ export const GET: RequestHandler = async ({ params }) => { ).map(({ data }) => data)[0] if (dossierLegislatif === undefined) { - return { headers: { "Access-Control-Allow-Origin": "*" }, status: 404 } + return { status: 404 } } return { - headers: { "Access-Control-Allow-Origin": "*" }, body: { dossier_legislatif: dossierLegislatif as unknown as JSONObject }, } } diff --git a/src/routes/dossier_legislatif/index.ts b/src/routes/dossier_legislatif/index.ts index 7f55b53..d84cdb7 100644 --- a/src/routes/dossier_legislatif/index.ts +++ b/src/routes/dossier_legislatif/index.ts @@ -48,7 +48,7 @@ export const GET: RequestHandler = async ({ url }) => { ) return { // status: 400, - headers: { "Access-Control-Allow-Origin": "*" }, + body: { error: { query: queryError as unknown as JSONObject, @@ -66,7 +66,6 @@ export const GET: RequestHandler = async ({ url }) => { ` ).map(({ data }) => data) return { - headers: { "Access-Control-Allow-Origin": "*" }, body: { dossier_legislatif: dossiersLegislatifs as unknown as JSONObject[], }, diff --git a/src/routes/id/[...eli].ts b/src/routes/id/[...eli].ts index 356955f..bd7be10 100644 --- a/src/routes/id/[...eli].ts +++ b/src/routes/id/[...eli].ts @@ -13,10 +13,9 @@ export const GET: RequestHandler = async ({ params }) => { )[0] if (idWrapper === undefined) { - return { headers: { "Access-Control-Allow-Origin": "*" }, status: 404 } + return { status: 404 } } return { - headers: { "Access-Control-Allow-Origin": "*" }, body: { id: idWrapper as unknown as JSONObject }, } } diff --git a/src/routes/id/index.ts b/src/routes/id/index.ts index c2c25f9..eef25fa 100644 --- a/src/routes/id/index.ts +++ b/src/routes/id/index.ts @@ -48,7 +48,7 @@ export const GET: RequestHandler = async ({ url }) => { ) return { // status: 400, - headers: { "Access-Control-Allow-Origin": "*" }, + body: { error: { query: queryError as unknown as JSONObject, @@ -64,7 +64,6 @@ export const GET: RequestHandler = async ({ url }) => { LIMIT ${limit} ` return { - headers: { "Access-Control-Allow-Origin": "*" }, body: { id: idsWrapper as unknown as JSONObject[] }, } } diff --git a/src/routes/idcc/[id].ts b/src/routes/idcc/[id].ts index aee376c..94a1c49 100644 --- a/src/routes/idcc/[id].ts +++ b/src/routes/idcc/[id].ts @@ -13,10 +13,9 @@ export const GET: RequestHandler = async ({ params }) => { ).map(({ data }) => data)[0] if (idcc === undefined) { - return { headers: { "Access-Control-Allow-Origin": "*" }, status: 404 } + return { status: 404 } } return { - headers: { "Access-Control-Allow-Origin": "*" }, body: { idcc: idcc as unknown as JSONObject }, } } diff --git a/src/routes/idcc/index.ts b/src/routes/idcc/index.ts index 79ce6be..1ef6c8a 100644 --- a/src/routes/idcc/index.ts +++ b/src/routes/idcc/index.ts @@ -48,7 +48,7 @@ export const GET: RequestHandler = async ({ url }) => { ) return { // status: 400, - headers: { "Access-Control-Allow-Origin": "*" }, + body: { error: { query: queryError as unknown as JSONObject, @@ -66,7 +66,6 @@ export const GET: RequestHandler = async ({ url }) => { ` ).map(({ data }) => data) return { - headers: { "Access-Control-Allow-Origin": "*" }, body: { idcc: idccArray as unknown as JSONObject[] }, } } diff --git a/src/routes/jo/[id].ts b/src/routes/jo/[id].ts index 187e1d3..e6b237e 100644 --- a/src/routes/jo/[id].ts +++ b/src/routes/jo/[id].ts @@ -13,10 +13,9 @@ export const GET: RequestHandler = async ({ params }) => { ).map(({ data }) => data)[0] if (jo === undefined) { - return { headers: { "Access-Control-Allow-Origin": "*" }, status: 404 } + return { status: 404 } } return { - headers: { "Access-Control-Allow-Origin": "*" }, body: { jo: jo as unknown as JSONObject }, } } diff --git a/src/routes/jo/index.ts b/src/routes/jo/index.ts index 93eeae7..24255e0 100644 --- a/src/routes/jo/index.ts +++ b/src/routes/jo/index.ts @@ -48,7 +48,7 @@ export const GET: RequestHandler = async ({ url }) => { ) return { // status: 400, - headers: { "Access-Control-Allow-Origin": "*" }, + body: { error: { query: queryError as unknown as JSONObject, @@ -66,7 +66,6 @@ export const GET: RequestHandler = async ({ url }) => { ` ).map(({ data }) => data) return { - headers: { "Access-Control-Allow-Origin": "*" }, body: { jo: jos as unknown as JSONObject[] }, } } diff --git a/src/routes/recherche/index.ts b/src/routes/recherche/index.ts index 371009e..675694c 100644 --- a/src/routes/recherche/index.ts +++ b/src/routes/recherche/index.ts @@ -48,7 +48,7 @@ export const GET: RequestHandler = async ({ url }) => { ) return { // status: 400, - headers: { "Access-Control-Allow-Origin": "*" }, + body: { error: { query: queryError as unknown as JSONObject, @@ -74,10 +74,9 @@ export const GET: RequestHandler = async ({ url }) => { ` ).map(({ data }) => data) return { - headers: { "Access-Control-Allow-Origin": "*" }, body: { articles: articles as unknown as JSONObject[], q }, } } } - return { headers: { "Access-Control-Allow-Origin": "*" }, body: {} } + return { body: {} } } diff --git a/src/routes/section_ta/[id].ts b/src/routes/section_ta/[id].ts index c147edb..83c190e 100644 --- a/src/routes/section_ta/[id].ts +++ b/src/routes/section_ta/[id].ts @@ -13,10 +13,9 @@ export const GET: RequestHandler = async ({ params }) => { ).map(({ data }) => data)[0] if (sectionTa === undefined) { - return { headers: { "Access-Control-Allow-Origin": "*" }, status: 404 } + return { status: 404 } } return { - headers: { "Access-Control-Allow-Origin": "*" }, body: { section_ta: sectionTa as unknown as JSONObject }, } } diff --git a/src/routes/section_ta/index.ts b/src/routes/section_ta/index.ts index a4609dc..839ddf3 100644 --- a/src/routes/section_ta/index.ts +++ b/src/routes/section_ta/index.ts @@ -48,7 +48,7 @@ export const GET: RequestHandler = async ({ url }) => { ) return { // status: 400, - headers: { "Access-Control-Allow-Origin": "*" }, + body: { error: { query: queryError as unknown as JSONObject, @@ -66,7 +66,6 @@ export const GET: RequestHandler = async ({ url }) => { ` ).map(({ data }) => data) return { - headers: { "Access-Control-Allow-Origin": "*" }, body: { section_ta: sectionTaArray as unknown as JSONObject[] }, } } diff --git a/src/routes/texte_version/[id].ts b/src/routes/texte_version/[id].ts index 41d7370..a220461 100644 --- a/src/routes/texte_version/[id].ts +++ b/src/routes/texte_version/[id].ts @@ -13,10 +13,9 @@ export const GET: RequestHandler = async ({ params }) => { ).map(({ data }) => data)[0] if (texteVersion === undefined) { - return { headers: { "Access-Control-Allow-Origin": "*" }, status: 404 } + return { status: 404 } } return { - headers: { "Access-Control-Allow-Origin": "*" }, body: { texte_version: texteVersion as unknown as JSONObject }, } } diff --git a/src/routes/texte_version/index.ts b/src/routes/texte_version/index.ts index dc690a5..b62685d 100644 --- a/src/routes/texte_version/index.ts +++ b/src/routes/texte_version/index.ts @@ -48,7 +48,7 @@ export const GET: RequestHandler = async ({ url }) => { ) return { // status: 400, - headers: { "Access-Control-Allow-Origin": "*" }, + body: { error: { query: queryError as unknown as JSONObject, @@ -66,7 +66,6 @@ export const GET: RequestHandler = async ({ url }) => { ` ).map(({ data }) => data) return { - headers: { "Access-Control-Allow-Origin": "*" }, body: { texte_version: texteVersionArray as unknown as JSONObject[] }, } } diff --git a/src/routes/textekali/[id].ts b/src/routes/textekali/[id].ts index 898e2ef..557b479 100644 --- a/src/routes/textekali/[id].ts +++ b/src/routes/textekali/[id].ts @@ -13,10 +13,9 @@ export const GET: RequestHandler = async ({ params }) => { ).map(({ data }) => data)[0] if (textekali === undefined) { - return { headers: { "Access-Control-Allow-Origin": "*" }, status: 404 } + return { status: 404 } } return { - headers: { "Access-Control-Allow-Origin": "*" }, body: { textekali: textekali as unknown as JSONObject }, } } diff --git a/src/routes/textekali/index.ts b/src/routes/textekali/index.ts index fc46f5f..1e2cb9d 100644 --- a/src/routes/textekali/index.ts +++ b/src/routes/textekali/index.ts @@ -48,7 +48,7 @@ export const GET: RequestHandler = async ({ url }) => { ) return { // status: 400, - headers: { "Access-Control-Allow-Origin": "*" }, + body: { error: { query: queryError as unknown as JSONObject, @@ -66,7 +66,6 @@ export const GET: RequestHandler = async ({ url }) => { ` ).map(({ data }) => data) return { - headers: { "Access-Control-Allow-Origin": "*" }, body: { textekali: textekaliArray as unknown as JSONObject[] }, } } diff --git a/src/routes/textelr/[id].ts b/src/routes/textelr/[id].ts index 66ca9ce..7e2fe99 100644 --- a/src/routes/textelr/[id].ts +++ b/src/routes/textelr/[id].ts @@ -13,10 +13,9 @@ export const GET: RequestHandler = async ({ params }) => { ).map(({ data }) => data)[0] if (textelr === undefined) { - return { headers: { "Access-Control-Allow-Origin": "*" }, status: 404 } + return { status: 404 } } return { - headers: { "Access-Control-Allow-Origin": "*" }, body: { textelr: textelr as unknown as JSONObject }, } } diff --git a/src/routes/textelr/index.ts b/src/routes/textelr/index.ts index 2483572..e8a2b6c 100644 --- a/src/routes/textelr/index.ts +++ b/src/routes/textelr/index.ts @@ -48,7 +48,7 @@ export const GET: RequestHandler = async ({ url }) => { ) return { // status: 400, - headers: { "Access-Control-Allow-Origin": "*" }, + body: { error: { query: queryError as unknown as JSONObject, @@ -66,7 +66,6 @@ export const GET: RequestHandler = async ({ url }) => { ` ).map(({ data }) => data) return { - headers: { "Access-Control-Allow-Origin": "*" }, body: { textelr: textelrArray as unknown as JSONObject[] }, } } diff --git a/src/routes/versions/[...eli].ts b/src/routes/versions/[...eli].ts index 32107f1..e01cbfc 100644 --- a/src/routes/versions/[...eli].ts +++ b/src/routes/versions/[...eli].ts @@ -13,10 +13,9 @@ export const GET: RequestHandler = async ({ params }) => { )[0] if (versionsWrapper === undefined) { - return { headers: { "Access-Control-Allow-Origin": "*" }, status: 404 } + return { status: 404 } } return { - headers: { "Access-Control-Allow-Origin": "*" }, body: { versions: versionsWrapper as unknown as JSONObject }, } } diff --git a/src/routes/versions/index.ts b/src/routes/versions/index.ts index dfc81ff..213fede 100644 --- a/src/routes/versions/index.ts +++ b/src/routes/versions/index.ts @@ -48,7 +48,7 @@ export const GET: RequestHandler = async ({ url }) => { ) return { // status: 400, - headers: { "Access-Control-Allow-Origin": "*" }, + body: { error: { query: queryError as unknown as JSONObject, @@ -64,7 +64,6 @@ export const GET: RequestHandler = async ({ url }) => { LIMIT ${limit} ` return { - headers: { "Access-Control-Allow-Origin": "*" }, body: { versions: versionsWrapper as unknown as JSONObject[] }, } }