Edits history of script submission #14980 for ' Download Excel report (codat)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Codat = {
    	encodedKey: string
    }
    /**
     * Download Excel report
     * The *Download Excel report* endpoint downloads the latest successfully generated Excel report of a specified report type for a given company. 
    
    The downloadable Excel file is returned in the response. You can save it to your local machine.
    
    You can [learn more](https://docs.codat.io/lending/features/excel-download-overview#feature-components) about valid Excel report types.
     */
    export async function main(
    	auth: Codat,
    	companyId: string,
    	reportType: 'audit' | 'enhancedFinancials' | 'enhancedInvoices' | 'enhancedCashFlow' | undefined
    ) {
    	const url = new URL(`https://api.codat.io/data/companies/${companyId}/assess/excel/download`)
    	for (const [k, v] of [['reportType', reportType]]) {
    		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.text()
    }
    

    Submitted by hugo697 235 days ago