diff --git a/examples/edge-functions/supabase/functions/_shared/supabaseClient.ts b/examples/edge-functions/supabase/functions/_shared/supabaseClient.ts new file mode 100644 index 00000000000..2205294d529 --- /dev/null +++ b/examples/edge-functions/supabase/functions/_shared/supabaseClient.ts @@ -0,0 +1,8 @@ +import { createClient } from 'https://esm.sh/@supabase/supabase-js@^1.33.2' + +export const supabaseClient = createClient( + // Supabase API URL - env var exported by default when deployed. + Deno.env.get('SUPABASE_URL') ?? '', + // Supabase API ANON KEY - env var exported by default when deployed. + Deno.env.get('SUPABASE_ANON_KEY') ?? '' +) diff --git a/examples/edge-functions/supabase/functions/browser-with-cors/index.ts b/examples/edge-functions/supabase/functions/browser-with-cors/index.ts index 5a4b5b4c279..1ab04de4e1f 100644 --- a/examples/edge-functions/supabase/functions/browser-with-cors/index.ts +++ b/examples/edge-functions/supabase/functions/browser-with-cors/index.ts @@ -5,7 +5,7 @@ import { serve } from 'https://deno.land/std@0.131.0/http/server.ts' import { corsHeaders } from '../_shared/cors.ts' -console.log('Hello from Functions!') +console.log(`Function "browser-with-cors" up and running!`) serve(async (req) => { // This is needed if you're planning to invoke your function from a browser. @@ -13,14 +13,22 @@ serve(async (req) => { return new Response('ok', { headers: corsHeaders }) } - const { name } = await req.json() - const data = { - message: `Hello ${name}!`, - } + try { + const { name } = await req.json() + const data = { + message: `Hello ${name}!`, + } - return new Response(JSON.stringify(data), { - headers: { ...corsHeaders, 'Content-Type': 'application/json' }, - }) + return new Response(JSON.stringify(data), { + headers: { ...corsHeaders, 'Content-Type': 'application/json' }, + status: 200, + }) + } catch (error) { + return new Response(JSON.stringify({ error: error.message }), { + headers: { ...corsHeaders, 'Content-Type': 'application/json' }, + status: 400, + }) + } }) // To invoke: 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.ts index f5c99b22447..a1103cc5489 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 @@ -3,16 +3,9 @@ // This enables autocomplete, go to definition, etc. import { serve } from 'https://deno.land/std@0.131.0/http/server.ts' -import { createClient } from 'https://esm.sh/@supabase/supabase-js@^1.33.2' +import { supabaseClient } from '../_shared/supabaseClient.ts' import { corsHeaders } from '../_shared/cors.ts' -const supabase = createClient( - // Supabase API URL - env var exported by default when deployed. - Deno.env.get('SUPABASE_URL') ?? '', - // Supabase API ANON KEY - env var exported by default when deployed. - Deno.env.get('SUPABASE_ANON_KEY') ?? '' -) - console.log(`Function "select-from-table-with-auth-rls" up and running!`) serve(async (req: Request) => { @@ -20,16 +13,25 @@ serve(async (req: Request) => { if (req.method === 'OPTIONS') { return new Response('ok', { headers: corsHeaders }) } - // Set the Auth context of the user that called the function. - // This way your row-level-security (RLS) policies are applied. - supabase.auth.setAuth(req.headers.get('Authorization')!.replace('Bearer ', '')) - const { data, error } = await supabase.from('users').select('*') - console.log({ data, error }) + try { + // Set the Auth context of the user that called the function. + // This way your row-level-security (RLS) policies are applied. + supabaseClient.auth.setAuth(req.headers.get('Authorization')!.replace('Bearer ', '')) - return new Response(JSON.stringify({ data, error }), { - headers: { ...corsHeaders, 'Content-Type': 'application/json' }, - }) + const { data, error } = await supabaseClient.from('users').select('*') + console.log({ data, error }) + + return new Response(JSON.stringify({ data, error }), { + headers: { ...corsHeaders, 'Content-Type': 'application/json' }, + status: 200, + }) + } catch (error) { + return new Response(JSON.stringify({ error: error.message }), { + headers: { ...corsHeaders, 'Content-Type': 'application/json' }, + status: 400, + }) + } }) // To invoke: