Edits history of script submission #15095 for ' Get refunds report (codat)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Codat = {
    	encodedKey: string
    }
    /**
     * Get refunds report
     * The *Get refunds report* endpoint returns the number and total value of refunds and the refund rate for a specific company's commerce connection over one or more periods of time.
     */
    export async function main(
    	auth: Codat,
    	companyId: string,
    	connectionId: string,
    	reportDate: string | undefined,
    	periodLength: string | undefined,
    	numberOfPeriods: string | undefined,
    	periodUnit: 'Day' | 'Week' | 'Month' | 'Year' | undefined,
    	includeDisplayNames: string | undefined
    ) {
    	const url = new URL(
    		`https://api.codat.io/data/companies/${companyId}/connections/${connectionId}/assess/commerceMetrics/refunds`
    	)
    	for (const [k, v] of [
    		['reportDate', reportDate],
    		['periodLength', periodLength],
    		['numberOfPeriods', numberOfPeriods],
    		['periodUnit', periodUnit],
    		['includeDisplayNames', includeDisplayNames]
    	]) {
    		if (v !== undefined && v !== '' && k !== undefined) {
    			url.searchParams.append(k, v)
    		}
    	}
    
    	const response = await fetch(url, {
    		method: 'GET',
    		headers: {
    			Authorization: `Basic ${auth.encodedKey}`
    		},
    		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