Edits history of script submission #20917 for ' Update a function (supabase)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Supabase = {
    	key: string
    }
    /**
     * Update a function
     * Updates a function with the specified slug and project.
     */
    export async function main(
    	auth: Supabase,
    	ref: string,
    	function_slug: string,
    	slug: string | undefined,
    	name: string | undefined,
    	verify_jwt: string | undefined,
    	import_map: string | undefined,
    	entrypoint_path: string | undefined,
    	import_map_path: string | undefined,
    	body: { name?: string; body?: string; verify_jwt?: false | true }
    ) {
    	const url = new URL(`https://api.supabase.com/v1/projects/${ref}/functions/${function_slug}`)
    	for (const [k, v] of [
    		['slug', slug],
    		['name', name],
    		['verify_jwt', verify_jwt],
    		['import_map', import_map],
    		['entrypoint_path', entrypoint_path],
    		['import_map_path', import_map_path]
    	]) {
    		if (v !== undefined && v !== '' && k !== undefined) {
    			url.searchParams.append(k, v)
    		}
    	}
    	const response = await fetch(url, {
    		method: 'PATCH',
    		headers: {
    			'Content-Type': 'application/json',
    			Authorization: 'Bearer ' + auth.key
    		},
    		body: JSON.stringify(body)
    	})
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    	return await response.json()
    }
    

    Submitted by hugo697 207 days ago