Edits history of script submission #20944 for ' Retrieve daily activity summaries for a given user ID (terra)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    /**
     * Retrieve daily activity summaries for a given user ID
     * Fetches daily summaries of activity metrics such as steps, distance, calories burned etc. for a given user ID
     */
    export async function main(
    	auth: RT.Terra,
    	user_id: string | undefined,
    	start_date: string | undefined,
    	end_date?: string | undefined,
    	to_webhook?: string | undefined,
    	with_samples?: string | undefined
    ) {
    	const url = new URL(`https://api.tryterra.co/v2/daily`)
    	for (const [k, v] of [
    		['user_id', user_id],
    		['start_date', start_date],
    		['end_date', end_date],
    		['to_webhook', to_webhook],
    		['with_samples', with_samples]
    	]) {
    		if (v !== undefined && v !== '') {
    			url.searchParams.append(k, v)
    		}
    	}
    	const response = await fetch(url, {
    		method: 'GET',
    		headers: {
    			'dev-id': auth.devId,
    			'X-api-key': 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 235 days ago