1 | |
2 | type Supabase = { |
3 | key: string |
4 | } |
5 | |
6 | * Lists SQL snippets for the logged in user |
7 | * |
8 | */ |
9 | export async function main( |
10 | auth: Supabase, |
11 | project_ref: string | undefined, |
12 | cursor: string | undefined, |
13 | limit: string | undefined, |
14 | sort_by: 'name' | 'inserted_at' | undefined, |
15 | sort_order: 'asc' | 'desc' | undefined |
16 | ) { |
17 | const url = new URL(`https://api.supabase.com/v1/snippets`) |
18 | for (const [k, v] of [ |
19 | ['project_ref', project_ref], |
20 | ['cursor', cursor], |
21 | ['limit', limit], |
22 | ['sort_by', sort_by], |
23 | ['sort_order', sort_order] |
24 | ]) { |
25 | if (v !== undefined && v !== '' && k !== undefined) { |
26 | url.searchParams.append(k, v) |
27 | } |
28 | } |
29 | const response = await fetch(url, { |
30 | method: 'GET', |
31 | headers: { |
32 | Authorization: 'Bearer ' + auth.key |
33 | }, |
34 | body: undefined |
35 | }) |
36 | if (!response.ok) { |
37 | const text = await response.text() |
38 | throw new Error(`${response.status} ${text}`) |
39 | } |
40 | return await response.json() |
41 | } |
42 |
|