Edits history of script submission #12680 for ' Get consumption metrics for each project (neondb)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Neondb = {
    	apiKey: string
    }
    /**
     * Get consumption metrics for each project
     * Retrieves consumption metrics for Scale and Business plan projects. History begins at the time of upgrade.
    Available for Scale and Business plan users only.
    Issuing a call to this API does not wake a project's compute endpoint.
    
     */
    export async function main(
    	auth: Neondb,
    	cursor: string | undefined,
    	limit: string | undefined,
    	project_ids: string | undefined,
    	from: string | undefined,
    	to: string | undefined,
    	granularity: 'hourly' | 'daily' | 'monthly' | undefined,
    	org_id: string | undefined,
    	include_v1_metrics: string | undefined
    ) {
    	const url = new URL(`https://console.neon.tech/api/v2/consumption_history/projects`)
    	for (const [k, v] of [
    		['cursor', cursor],
    		['limit', limit],
    		['project_ids', project_ids],
    		['from', from],
    		['to', to],
    		['granularity', granularity],
    		['org_id', org_id],
    		['include_v1_metrics', include_v1_metrics]
    	]) {
    		if (v !== undefined && v !== '' && k !== undefined) {
    			url.searchParams.append(k, v)
    		}
    	}
    	const response = await fetch(url, {
    		method: 'GET',
    		headers: {
    			Authorization: 'Bearer ' + auth.apiKey
    		},
    		body: undefined
    	})
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    	return await response.json()
    }
    

    Submitted by hugo697 428 days ago