From 0b2d5c62b1c02b3da2e95b0071984b438e45a072 Mon Sep 17 00:00:00 2001 From: Lim Ding Wen Date: Wed, 31 Jul 2024 18:13:20 +0800 Subject: [PATCH] Add CORS browser support for search edge function Temporary solution for now; we should use a fat function in the future --- supabase/functions/lib/utils/build-response-proxy.ts | 4 +++- supabase/functions/lib/utils/build-response.ts | 4 +++- supabase/functions/lib/utils/cors.ts | 5 +++++ supabase/functions/search/index.ts | 5 +++++ 4 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 supabase/functions/lib/utils/cors.ts diff --git a/supabase/functions/lib/utils/build-response-proxy.ts b/supabase/functions/lib/utils/build-response-proxy.ts index daf070d..bc65e79 100644 --- a/supabase/functions/lib/utils/build-response-proxy.ts +++ b/supabase/functions/lib/utils/build-response-proxy.ts @@ -1,10 +1,12 @@ +import { corsHeaders } from "./cors.ts"; + export default function buildResponseProxy(data: object, status: number = 200) { const dataStr = JSON.stringify(data); console.info(`Proxy response ${status}: ${dataStr}`); return { body: dataStr, init: { - headers: { "Content-Type": "application/json" }, + headers: { ...corsHeaders, "Content-Type": "application/json" }, status, }, }; diff --git a/supabase/functions/lib/utils/build-response.ts b/supabase/functions/lib/utils/build-response.ts index a7cd0e6..36a0423 100644 --- a/supabase/functions/lib/utils/build-response.ts +++ b/supabase/functions/lib/utils/build-response.ts @@ -1,8 +1,10 @@ +import { corsHeaders } from "./cors.ts"; + export function buildResponse(data: object, status: number = 200) { const dataStr = JSON.stringify(data); console.info(`Response ${status}: ${dataStr}`); return new Response(dataStr, { - headers: { "Content-Type": "application/json" }, + headers: { ...corsHeaders, "Content-Type": "application/json" }, status, }); } diff --git a/supabase/functions/lib/utils/cors.ts b/supabase/functions/lib/utils/cors.ts new file mode 100644 index 0000000..02b087a --- /dev/null +++ b/supabase/functions/lib/utils/cors.ts @@ -0,0 +1,5 @@ +export const corsHeaders = { + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Headers": + "authorization, x-client-info, apikey, content-type", +}; diff --git a/supabase/functions/search/index.ts b/supabase/functions/search/index.ts index 51ed0f9..1e4e904 100644 --- a/supabase/functions/search/index.ts +++ b/supabase/functions/search/index.ts @@ -2,6 +2,7 @@ import "https://esm.sh/@supabase/functions-js/src/edge-runtime.d.ts"; import { createSupabase } from "../lib/utils/create-supabase.ts"; import { SupabaseClient } from "https://esm.sh/v135/@supabase/supabase-js@2.24.0/dist/module/index.d.ts"; import { buildResponse } from "../lib/utils/build-response.ts"; +import { corsHeaders } from "../lib/utils/cors.ts"; type ClientFriendlyData = { href: string; @@ -58,6 +59,10 @@ async function getBillSearchResults( } Deno.serve(async (req) => { + if (req.method === "OPTIONS") { + return new Response("ok", { headers: corsHeaders }); + } + const supabase = createSupabase(); const { query } = await req.json();