Edits history of script submission #14999 for ' Generate spend analysis report (codat)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Codat = {
    	encodedKey: string
    }
    /**
     * Generate spend analysis report
     * Generate new spend analysis report (no body required in POST).
     */
    export async function main(
    	auth: Codat,
    	companyId: string,
    	from: string | undefined,
    	to: string | undefined,
    	excludeSuppliersWithAnnualisedSpendBelow: string | undefined
    ) {
    	const url = new URL(`https://banking-api.codat.io/companies/${companyId}/reports/spendAnalysis`)
    	for (const [k, v] of [
    		['from', from],
    		['to', to],
    		['excludeSuppliersWithAnnualisedSpendBelow', excludeSuppliersWithAnnualisedSpendBelow]
    	]) {
    		if (v !== undefined && v !== '' && k !== undefined) {
    			url.searchParams.append(k, v)
    		}
    	}
    
    	const response = await fetch(url, {
    		method: 'POST',
    		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