Deploy a function
One script reply has been approved by the moderators Verified

A new endpoint to deploy functions. It will create if function does not exist.

Created by hugo697 151 days ago
Submitted by hugo697 Bun
Verified 151 days ago
1
//native
2
type Supabase = {
3
	key: string
4
}
5
/**
6
 * Deploy a function
7
 * A new endpoint to deploy functions. It will create if function does not exist.
8
 */
9
export async function main(
10
	auth: Supabase,
11
	ref: string,
12
	slug: string | undefined,
13
	bundleOnly: string | undefined,
14
	body: {
15
		file?: string[]
16
		metadata: {
17
			entrypoint_path: string
18
			import_map_path?: string
19
			static_patterns?: string[]
20
			verify_jwt?: false | true
21
			name?: string
22
		}
23
	}
24
) {
25
	const url = new URL(`https://api.supabase.com/v1/projects/${ref}/functions/deploy`)
26
	for (const [k, v] of [
27
		['slug', slug],
28
		['bundleOnly', bundleOnly]
29
	]) {
30
		if (v !== undefined && v !== '' && k !== undefined) {
31
			url.searchParams.append(k, v)
32
		}
33
	}
34
	const formData = new FormData()
35
	for (const [k, v] of Object.entries(body)) {
36
		if (v !== undefined) {
37
			formData.append(k, String(v))
38
		}
39
	}
40
	const response = await fetch(url, {
41
		method: 'POST',
42
		headers: {
43
			Authorization: 'Bearer ' + auth.key
44
		},
45
		body: formData
46
	})
47
	if (!response.ok) {
48
		const text = await response.text()
49
		throw new Error(`${response.status} ${text}`)
50
	}
51
	return await response.json()
52
}
53