Edits history of script submission #9894 for ' Get Inverter Statistics (enode)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Enode = {
    	token: string
    }
    
    export async function main(
    	auth: Enode,
    	inverterId: string,
    	year: string | undefined,
    	month: string | undefined,
    	day: string | undefined
    ) {
    	const url = new URL(`https://enode-api.production.enode.io/inverters/${inverterId}/statistics`)
    
    	for (const [k, v] of [
    		['year', year],
    		['month', month],
    		['day', day]
    	]) {
    		if (v !== undefined && v !== '' && k !== undefined) {
    			url.searchParams.append(k, v)
    		}
    	}
    
    	const response = await fetch(url, {
    		method: 'GET',
    		headers: {
    			Authorization: 'Bearer ' + auth.token
    		},
    		body: undefined
    	})
    
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    
    	return await response.json()
    }
    

    Submitted by hugo697 581 days ago