Update a function

Updates a function with the specified slug and project.

Script supabase Verified

by hugo697 ยท 10/17/2025

The script

Submitted by hugo697 Bun
Verified 207 days ago
1
//native
2
type Supabase = {
3
	key: string
4
}
5
/**
6
 * Update a function
7
 * Updates a function with the specified slug and project.
8
 */
9
export async function main(
10
	auth: Supabase,
11
	ref: string,
12
	function_slug: string,
13
	slug: string | undefined,
14
	name: string | undefined,
15
	verify_jwt: string | undefined,
16
	import_map: string | undefined,
17
	entrypoint_path: string | undefined,
18
	import_map_path: string | undefined,
19
	body: { name?: string; body?: string; verify_jwt?: false | true }
20
) {
21
	const url = new URL(`https://api.supabase.com/v1/projects/${ref}/functions/${function_slug}`)
22
	for (const [k, v] of [
23
		['slug', slug],
24
		['name', name],
25
		['verify_jwt', verify_jwt],
26
		['import_map', import_map],
27
		['entrypoint_path', entrypoint_path],
28
		['import_map_path', import_map_path]
29
	]) {
30
		if (v !== undefined && v !== '' && k !== undefined) {
31
			url.searchParams.append(k, v)
32
		}
33
	}
34
	const response = await fetch(url, {
35
		method: 'PATCH',
36
		headers: {
37
			'Content-Type': 'application/json',
38
			Authorization: 'Bearer ' + auth.key
39
		},
40
		body: JSON.stringify(body)
41
	})
42
	if (!response.ok) {
43
		const text = await response.text()
44
		throw new Error(`${response.status} ${text}`)
45
	}
46
	return await response.json()
47
}
48