The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
feat: add cors example
thorwebdev94e29812f844
diff --git a/examples/edge-functions/README.md b/examples/edge-functions/README.mdindex 93f1193c6b8..99028a5ec39 100644--- a/examples/edge-functions/README.md+++ b/examples/edge-functions/README.md@@ -8,7 +8,8 @@The function examples are located in [`./supabase/functions`](./supabase/functions):-- [`select-from-table-with-auth-rls`](./supabase/functions/select-from-table-with-auth-rls/index.ts): Retrieve data from an authenticated user via RLS+- [`browser-with-cors`](./supabase/functions/browser-with-cors/index.ts): Handle CORS headers for function invocations from browser environments.+- [`select-from-table-with-auth-rls`](./supabase/functions/select-from-table-with-auth-rls/index.ts): Retrieve data from an authenticated user via RLS.## Develop locallydiff --git a/examples/edge-functions/supabase/functions/browser-with-cors/_utils/cors.ts b/examples/edge-functions/supabase/functions/browser-with-cors/_utils/cors.tsnew file mode 100644index 00000000000..4be3f02881e--- /dev/null+++ b/examples/edge-functions/supabase/functions/browser-with-cors/_utils/cors.ts@@ -0,0 +1,4 @@+export const corsHeaders = {+ 'Access-Control-Allow-Origin': '*',+ 'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey',+}diff --git a/examples/edge-functions/supabase/functions/browser-with-cors/index.ts b/examples/edge-functions/supabase/functions/browser-with-cors/index.tsnew file mode 100644index 00000000000..b4666de0d39--- /dev/null+++ b/examples/edge-functions/supabase/functions/browser-with-cors/index.ts@@ -0,0 +1,30 @@+// Follow this setup guide to integrate the Deno language server with your editor:+// https://deno.land/manual/getting_started/setup_your_environment+// This enables autocomplete, go to definition, etc.++import { serve } from 'https://deno.land/[email protected]/http/server.ts'+import { corsHeaders } from './_utils/cors.ts'++console.log('Hello from Functions!')++serve(async (req) => {+ // This is needed if you're planning to invoke your function from a browser.+ if (req.method === 'OPTIONS') {+ return new Response('ok', { headers: corsHeaders })+ }++ const { name } = await req.json()+ const data = {+ message: `Hello ${name}!`,+ }++ return new Response(JSON.stringify(data), {+ headers: { ...corsHeaders, 'Content-Type': 'application/json' },+ })+})++// To invoke:+// curl -i --location --request POST 'http://localhost:54321/functions/v1/browser-with-cors' \+// --header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24ifQ.625_WdcF3KHqz5amU0x2X5WWHP-OEs_4qj0ssLNHzTs' \+// --header 'Content-Type: application/json' \+// --data '{"name":"Functions"}'diff --git a/examples/edge-functions/supabase/functions/select-from-table-with-auth-rls/_utils/cors.ts b/examples/edge-functions/supabase/functions/select-from-table-with-auth-rls/_utils/cors.tsindex 32196801f9c..4be3f02881e 100644--- a/examples/edge-functions/supabase/functions/select-from-table-with-auth-rls/_utils/cors.ts+++ b/examples/edge-functions/supabase/functions/select-from-table-with-auth-rls/_utils/cors.ts@@ -1,7 +1,4 @@export const corsHeaders = {'Access-Control-Allow-Origin': '*',- 'Access-Control-Allow-Methods': 'POST',- 'Access-Control-Expose-Headers': 'Content-Length, X-JSON',- 'Access-Control-Allow-Headers':- 'apikey, X-Client-Info, Content-Type, Authorization, Accept, Accept-Language, X-Authorization',+ 'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey',}