|
| 1 | +export const EDGE_FUNCTION_TEMPLATES = [ |
| 2 | + { |
| 3 | + value: 'hello-world', |
| 4 | + name: 'Simple Hello World', |
| 5 | + description: 'Basic function that returns a JSON response', |
| 6 | + content: `// Setup type definitions for built-in Supabase Runtime APIs |
| 7 | +import "jsr:@supabase/functions-js/edge-runtime.d.ts"; |
| 8 | +interface reqPayload { |
| 9 | + name: string; |
| 10 | +} |
| 11 | +
|
| 12 | +console.info('server started'); |
| 13 | +
|
| 14 | +Deno.serve(async (req: Request) => { |
| 15 | + const { name }: reqPayload = await req.json(); |
| 16 | + const data = { |
| 17 | + message: \`Hello \${name} from foo!\`, |
| 18 | + }; |
| 19 | +
|
| 20 | + return new Response( |
| 21 | + JSON.stringify(data), |
| 22 | + { headers: { 'Content-Type': 'application/json', 'Connection': 'keep-alive' }} |
| 23 | + ); |
| 24 | +});`, |
| 25 | + }, |
| 26 | + { |
| 27 | + value: 'database-access', |
| 28 | + name: 'Supabase Database Access', |
| 29 | + description: 'Example using Supabase client to query your database', |
| 30 | + content: `// Setup type definitions for built-in Supabase Runtime APIs |
| 31 | +import "jsr:@supabase/functions-js/edge-runtime.d.ts"; |
| 32 | +import { createClient } from 'jsr:@supabase/supabase-js@2' |
| 33 | +
|
| 34 | +Deno.serve(async (req) => { |
| 35 | + try { |
| 36 | + const supabase = createClient( |
| 37 | + Deno.env.get('SUPABASE_URL') ?? '', |
| 38 | + Deno.env.get('SUPABASE_ANON_KEY') ?? '', |
| 39 | + { global: { headers: { Authorization: req.headers.get('Authorization')! } } } |
| 40 | + ) |
| 41 | +
|
| 42 | + const { data, error } = await supabase.from('table_name').select('*') |
| 43 | +
|
| 44 | + if (error) { |
| 45 | + throw error |
| 46 | + } |
| 47 | +
|
| 48 | + return new Response(JSON.stringify({ data }), { |
| 49 | + headers: { 'Content-Type': 'application/json' }, |
| 50 | + status: 200, |
| 51 | + }) |
| 52 | + } catch (err) { |
| 53 | + return new Response(String(err?.message ?? err), { status: 500 }) |
| 54 | + } |
| 55 | +})`, |
| 56 | + }, |
| 57 | + { |
| 58 | + value: 'node-api', |
| 59 | + name: 'Node Built-in API Example', |
| 60 | + description: 'Example using Node.js built-in crypto and http modules', |
| 61 | + content: `// Setup type definitions for built-in Supabase Runtime APIs |
| 62 | +import "jsr:@supabase/functions-js/edge-runtime.d.ts"; |
| 63 | +import { randomBytes } from "node:crypto"; |
| 64 | +import { createServer } from "node:http"; |
| 65 | +import process from "node:process"; |
| 66 | +
|
| 67 | +const generateRandomString = (length) => { |
| 68 | + const buffer = randomBytes(length); |
| 69 | + return buffer.toString('hex'); |
| 70 | +}; |
| 71 | +
|
| 72 | +const randomString = generateRandomString(10); |
| 73 | +console.log(randomString); |
| 74 | +
|
| 75 | +const server = createServer((req, res) => { |
| 76 | + const message = \`Hello\`; |
| 77 | + res.end(message); |
| 78 | +}); |
| 79 | +
|
| 80 | +server.listen(9999);`, |
| 81 | + }, |
| 82 | + { |
| 83 | + value: 'express', |
| 84 | + name: 'Express Server', |
| 85 | + description: 'Example using Express.js for routing', |
| 86 | + content: `// Setup type definitions for built-in Supabase Runtime APIs |
| 87 | +import "jsr:@supabase/functions-js/edge-runtime.d.ts"; |
| 88 | +import express from "npm:express@4.18.2"; |
| 89 | +
|
| 90 | +const app = express(); |
| 91 | +
|
| 92 | +app.get(/(.*)/, (req, res) => { |
| 93 | + res.send("Welcome to Supabase"); |
| 94 | +}); |
| 95 | +
|
| 96 | +app.listen(8000);`, |
| 97 | + }, |
| 98 | +] |
0 commit comments