Skip to content

Commit

Permalink
Add CORS browser support for search edge function
Browse files Browse the repository at this point in the history
Temporary solution for now; we should use a fat function in the future
  • Loading branch information
limdingwen committed Jul 31, 2024
1 parent 93b094c commit 0b2d5c6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
4 changes: 3 additions & 1 deletion supabase/functions/lib/utils/build-response-proxy.ts
Original file line number Diff line number Diff line change
@@ -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,
},
};
Expand Down
4 changes: 3 additions & 1 deletion supabase/functions/lib/utils/build-response.ts
Original file line number Diff line number Diff line change
@@ -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,
});
}
5 changes: 5 additions & 0 deletions supabase/functions/lib/utils/cors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const corsHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers":
"authorization, x-client-info, apikey, content-type",
};
5 changes: 5 additions & 0 deletions supabase/functions/search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]/dist/module/index.d.ts";
import { buildResponse } from "../lib/utils/build-response.ts";
import { corsHeaders } from "../lib/utils/cors.ts";

type ClientFriendlyData = {
href: string;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 0b2d5c6

Please sign in to comment.