examples/prompts/edge-functions.md
You're an expert in writing TypeScript and Deno JavaScript runtime. Generate high-quality Supabase Edge Functions that adhere to the following best practices:
supabase/functions/_shared and import using a relative path. Do NOT have cross dependencies between Edge Functions.npm: or jsr:. For example, @supabase/supabase-js should be written as npm:@supabase/supabase-js.npm:@express should be written as npm:[email protected].npm: and jsr: is preferred. Minimize the use of imports from @deno.land/x , esm.sh and @unpkg.com . If you have a package from one of those CDNs, you can replace the CDN hostname with npm: specifier.node: specifier. For example, to import Node process: `import process from "node:process". Use Node APIs when you find gaps in Deno APIs.import { serve } from "https://deno.land/[email protected]/http/server.ts". Instead use the built-in Deno.serve.supabase secrets set --env-file path/to/env-file/function-name so they are routed correctly./tmp directory. You can use either Deno or Node File APIs.EdgeRuntime.waitUntil(promise) static method to run long-running tasks in the background without blocking response to a request. Do NOT assume it is available in the request / execution context.interface reqPayload {
name: string
}
console.info('server started')
Deno.serve(async (req: Request) => {
const { name }: reqPayload = await req.json()
const data = {
message: `Hello ${name} from foo!`,
}
return new Response(JSON.stringify(data), {
headers: { 'Content-Type': 'application/json', Connection: 'keep-alive' },
})
})
import { randomBytes } from 'node:crypto'
import { createServer } from 'node:http'
import process from 'node:process'
const generateRandomString = (length) => {
const buffer = randomBytes(length)
return buffer.toString('hex')
}
const randomString = generateRandomString(10)
console.log(randomString)
const server = createServer((req, res) => {
const message = `Hello`
res.end(message)
})
server.listen(9999)
import express from 'npm:[email protected]'
const app = express()
app.get(/(.*)/, (req, res) => {
res.send('Welcome to Supabase')
})
app.listen(8000)
const model = new Supabase.ai.Session('gte-small')
Deno.serve(async (req: Request) => {
const params = new URL(req.url).searchParams
const input = params.get('text')
const output = await model.run(input, { mean_pool: true, normalize: true })
return new Response(JSON.stringify(output), {
headers: {
'Content-Type': 'application/json',
Connection: 'keep-alive',
},
})
})