Edits history of script submission #15063 for ' Get enhanced balance sheet accounts (codat)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Codat = {
    	encodedKey: string
    }
    /**
     * Get enhanced balance sheet accounts
     * The Enhanced Balance Sheet Accounts endpoint returns a list of categorized accounts that appear on a company’s Balance Sheet along with a balance per financial statement date.
    
    Codat suggests a category for each account automatically, but you can change it to a more suitable one.
     */
    export async function main(
    	auth: Codat,
    	companyId: string,
    	reportDate: string | undefined,
    	numberOfPeriods: string | undefined
    ) {
    	const url = new URL(
    		`https://api.codat.io/companies/${companyId}/reports/enhancedBalanceSheet/accounts`
    	)
    	for (const [k, v] of [
    		['reportDate', reportDate],
    		['numberOfPeriods', numberOfPeriods]
    	]) {
    		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