Edits history of script submission #20883 for ' Gets project's logs (supabase)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Supabase = {
    	key: string
    }
    /**
     * Gets project's logs
     * Executes a SQL query on the project's logs.
    
    Either the 'iso_timestamp_start' and 'iso_timestamp_end' parameters must be provided.
    If both are not provided, only the last 1 minute of logs will be queried.
    The timestamp range must be no more than 24 hours and is rounded to the nearest minute. If the range is more than 24 hours, a validation error will be thrown.
    
     */
    export async function main(
    	auth: Supabase,
    	ref: string,
    	sql: string | undefined,
    	iso_timestamp_start: string | undefined,
    	iso_timestamp_end: string | undefined
    ) {
    	const url = new URL(`https://api.supabase.com/v1/projects/${ref}/analytics/endpoints/logs.all`)
    	for (const [k, v] of [
    		['sql', sql],
    		['iso_timestamp_start', iso_timestamp_start],
    		['iso_timestamp_end', iso_timestamp_end]
    	]) {
    		if (v !== undefined && v !== '' && k !== undefined) {
    			url.searchParams.append(k, v)
    		}
    	}
    	const response = await fetch(url, {
    		method: 'GET',
    		headers: {
    			Authorization: 'Bearer ' + auth.key
    		},
    		body: undefined
    	})
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    	return await response.json()
    }
    

    Submitted by hugo697 207 days ago