Skip to content

Commit 1cefb95

Browse files
SaxonFjoshenlim
andauthored
Create edge function (supabase#33726)
* page components * page component changes * settings but broken saving * rvert * use sheet for provider * styling * remove things * Some refactoring and fixing, added JSDocs to layouts * Smol refactor * Fix * Update JSDocs * updated scaffolding * update edge functions layout * remove params * Clean up * Spelling * Clean up FormFieldWrappers * One last clean up * create edge functon * use component * flag * fix merge errors * fix imports * fix merge issues * fix import * fix flag * remove unused * move templates * re-add flag * Small refactors * Smol refactors * Refactor * add rename and delete via context * create function fixes * moved function name * Smol update * Small fix for selected state in edge functions side menu when on /new --------- Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
1 parent bcaf5d4 commit 1cefb95

File tree

11 files changed

+1091
-22
lines changed

11 files changed

+1091
-22
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
]

apps/studio/components/layouts/EdgeFunctionsLayout/EdgeFunctionsLayout.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import { useRouter } from 'next/router'
2+
import { PropsWithChildren } from 'react'
3+
14
import { useParams } from 'common'
25
import { ProductMenu } from 'components/ui/ProductMenu'
36
import { withAuth } from 'hooks/misc/withAuth'
4-
import { useRouter } from 'next/router'
5-
import { PropsWithChildren } from 'react'
67
import ProjectLayout from '../ProjectLayout/ProjectLayout'
78

89
const EdgeFunctionsProductMenu = () => {
@@ -19,7 +20,7 @@ const EdgeFunctionsProductMenu = () => {
1920
{
2021
name: 'Functions',
2122
key: 'main',
22-
pages: [undefined, '[functionSlug]'],
23+
pages: [undefined, '[functionSlug]', 'new'],
2324
url: `/project/${projectRef}/functions`,
2425
items: [],
2526
},

apps/studio/components/layouts/PageLayout/PageHeader.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ interface PageHeaderProps {
2626
secondaryActions?: ReactNode
2727
className?: string
2828
isCompact?: boolean
29+
pageMeta?: ReactNode
2930
}
3031

3132
export const PageHeader = ({
@@ -37,6 +38,7 @@ export const PageHeader = ({
3738
secondaryActions,
3839
className,
3940
isCompact = false,
41+
pageMeta,
4042
}: PageHeaderProps) => {
4143
const { ref } = useParams()
4244

0 commit comments

Comments
 (0)