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
thorwebdev2a620d37f901
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/_shared/cors.ts b/examples/edge-functions/supabase/functions/_shared/cors.tsnew file mode 100644index 00000000000..4be3f02881e--- /dev/null+++ b/examples/edge-functions/supabase/functions/_shared/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..5a4b5b4c279--- /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 '../_shared/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.tsdeleted file mode 100644index 32196801f9c..00000000000--- a/examples/edge-functions/supabase/functions/select-from-table-with-auth-rls/_utils/cors.ts+++ /dev/null@@ -1,7 +0,0 @@-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',-}diff --git a/examples/edge-functions/supabase/functions/select-from-table-with-auth-rls/index.ts b/examples/edge-functions/supabase/functions/select-from-table-with-auth-rls/index.tsindex a8752740cf3..f5c99b22447 100644--- a/examples/edge-functions/supabase/functions/select-from-table-with-auth-rls/index.ts+++ b/examples/edge-functions/supabase/functions/select-from-table-with-auth-rls/index.ts@@ -4,7 +4,7 @@import { serve } from 'https://deno.land/[email protected]/http/server.ts'import { createClient } from 'https://esm.sh/@supabase/supabase-js@^1.33.2'-import { corsHeaders } from './_utils/cors.ts'+import { corsHeaders } from '../_shared/cors.ts'const supabase = createClient(// Supabase API URL - env var exported by default when deployed.