mirror of
https://github.com/supabase/supabase.git
synced 2026-10-06 09:55:06 +03:00
chore: trycatch
This commit is contained in:
1 parent
79f9265284
commit
da92ee4475
3 files changed
+42
-24
No files matched your search
@@ -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') ?? ''
|
||||
)
|
||||
@@ -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:
|
||||
|
||||
+18
-16
@@ -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:
|
||||
|
||||
Reference in new issue
Block a user