mirror of
https://github.com/supabase/supabase.git
synced 2026-10-16 14:49:10 +03:00
update tools
This commit is contained in:
1 parent
4fa7e022f3
commit
68c34d5892
2 files changed
+56
-50
No files matched your search
@@ -117,9 +117,10 @@ async function handlePost(req: NextApiRequest, res: NextApiResponse) {
|
||||
Please make sure that all queries are valid Postgres SQL queries
|
||||
|
||||
# You write edge functions
|
||||
Your purpose is to generate a edge function with the constraints given by the user.
|
||||
Your purpose is to generate entire edge functions with the constraints given by the user.
|
||||
- First, always use the getEdgeFunctionKnowledge tool to get knowledge about how to write edge functions for Supabase
|
||||
- When writing edge functions, always ensure that they are written in TypeScript and Deno JavaScript runtime.
|
||||
- When writing edge functions, write complete code so the user doesn't need to replace any placeholders.
|
||||
- When writing edge functions, always ensure that they are written in a way that is compatible with the database schema.
|
||||
- When suggesting edge functions, follow the guidelines in getEdgeFunctionKnowledge tool. Always create personalised edge functions based on the database schema
|
||||
- When outputting edge functions, always include a props comment in the first line of the code block:
|
||||
|
||||
@@ -420,7 +420,7 @@ export const getTools = ({
|
||||
|
||||
### Simple Hello World Function
|
||||
|
||||
\`\`\`tsx
|
||||
\`\`\`edge
|
||||
interface reqPayload {
|
||||
name: string;
|
||||
}
|
||||
@@ -442,7 +442,7 @@ export const getTools = ({
|
||||
|
||||
### Example Function using Node built-in API
|
||||
|
||||
\`\`\`tsx
|
||||
\`\`\`edge
|
||||
import { randomBytes } from "node:crypto";
|
||||
import { createServer } from "node:http";
|
||||
import process from "node:process";
|
||||
@@ -465,7 +465,7 @@ export const getTools = ({
|
||||
|
||||
### Using npm packages in Functions
|
||||
|
||||
\`\`\`tsx
|
||||
\`\`\`edge
|
||||
import express from "npm:express@4.18.2";
|
||||
|
||||
const app = express();
|
||||
@@ -479,7 +479,7 @@ export const getTools = ({
|
||||
|
||||
### Generate embeddings using built-in @Supabase.ai API
|
||||
|
||||
\`\`\`tsx
|
||||
\`\`\`edge
|
||||
const model = new Supabase.ai.Session('gte-small');
|
||||
|
||||
Deno.serve(async (req: Request) => {
|
||||
@@ -500,58 +500,63 @@ export const getTools = ({
|
||||
|
||||
## Integrating with Supabase Auth
|
||||
|
||||
\`\`\`tsx
|
||||
import { createClient } from '@supabase/supabase-js'
|
||||
import { corsHeaders } from '../_shared/cors'
|
||||
\`\`\`edge
|
||||
import { createClient } from \\'jsr:@supabase/supabase-js@2\\'
|
||||
import { corsHeaders } from \\'../_shared/cors.ts\\'
|
||||
|
||||
console.log('Function "select-from-table-with-auth-rls" up and running!')
|
||||
console.log(\`Function "select-from-table-with-auth-rls" up and running!\`)
|
||||
|
||||
Deno.serve(async (req: Request) => {
|
||||
// This is needed if you're planning to invoke your function from a browser.
|
||||
if (req.method === 'OPTIONS') {
|
||||
return new Response('ok', { headers: corsHeaders })
|
||||
}
|
||||
Deno.serve(async (req: Request) => {
|
||||
// This is needed if you\\'re planning to invoke your function from a browser.
|
||||
if (req.method === \\'OPTIONS\\') {
|
||||
return new Response(\\'ok\\', { headers: corsHeaders })
|
||||
}
|
||||
|
||||
try {
|
||||
// Create a Supabase client with the Auth context of the logged in user.
|
||||
const supabaseClient = createClient(
|
||||
// Supabase API URL - env var exported by default.
|
||||
Deno.env.get('SUPABASE_URL') ?? '',
|
||||
// Supabase API ANON KEY - env var exported by default.
|
||||
Deno.env.get('SUPABASE_ANON_KEY') ?? '',
|
||||
// Create client with Auth context of the user that called the function.
|
||||
// This way your row-level-security (RLS) policies are applied.
|
||||
{
|
||||
global: {
|
||||
headers: { Authorization: req.headers.get('Authorization')! },
|
||||
},
|
||||
}
|
||||
)
|
||||
try {
|
||||
// Create a Supabase client with the Auth context of the logged in user.
|
||||
const supabaseClient = createClient(
|
||||
// Supabase API URL - env var exported by default.
|
||||
Deno.env.get(\\'SUPABASE_URL\\') ?? \\'\\',
|
||||
// Supabase API ANON KEY - env var exported by default.
|
||||
Deno.env.get(\\'SUPABASE_ANON_KEY\\') ?? \\'\\',
|
||||
// Create client with Auth context of the user that called the function.
|
||||
// This way your row-level-security (RLS) policies are applied.
|
||||
{
|
||||
global: {
|
||||
headers: { Authorization: req.headers.get(\\'Authorization\\')! },
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
// First get the token from the Authorization header
|
||||
const token = req.headers.get('Authorization')?.replace('Bearer ', '')
|
||||
if (!token) throw new Error('No authorization token provided')
|
||||
// First get the token from the Authorization header
|
||||
const token = req.headers.get(\\'Authorization\\').replace(\\'Bearer \\', \\'\\')
|
||||
|
||||
// Now we can get the session or user object
|
||||
const {
|
||||
data: { user },
|
||||
} = await supabaseClient.auth.getUser(token)
|
||||
// Now we can get the session or user object
|
||||
const {
|
||||
data: { user },
|
||||
} = await supabaseClient.auth.getUser(token)
|
||||
|
||||
// And we can run queries in the context of our authenticated user
|
||||
const { data, error } = await supabaseClient.from('users').select('*')
|
||||
if (error) throw error
|
||||
// And we can run queries in the context of our authenticated user
|
||||
const { data, error } = await supabaseClient.from(\\'users\\').select(\\'*\\')
|
||||
if (error) throw error
|
||||
|
||||
return new Response(JSON.stringify({ user, 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,
|
||||
})
|
||||
}
|
||||
})
|
||||
return new Response(JSON.stringify({ user, 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:
|
||||
// curl -i --location --request POST \\'http://localhost:54321/functions/v1/select-from-table-with-auth-rls\\' \\
|
||||
// --header \\'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24ifQ.625_WdcF3KHqz5amU0x2X5WWHP-OEs_4qj0ssLNHzTs\\' \\
|
||||
// --header \\'Content-Type: application/json\\' \\
|
||||
// --data \\'{"name":"Functions"}\\'
|
||||
\`\`\`
|
||||
`
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user