Edits history of script submission #15019 for ' Get categorized profit and loss statement (codat)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Codat = {
    	encodedKey: string
    }
    /**
     * Get categorized profit and loss statement
     * The *Get categorized profit and loss statement* endpoint returns a list of categorized accounts that appear on a company’s Profit and Loss statement. It also includes a balance as of the financial statement date.
    
    Codat suggests a category for each account automatically, but you can [change it](https://docs.codat.io/lending/features/financial-statements-overview#recategorizing-accounts) 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/enhancedProfitAndLoss/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