//native
type Codat = {
encodedKey: string
}
/**
* Get profit and loss
* Gets the latest profit and loss for a company.
*/
export async function main(
auth: Codat,
companyId: string,
periodLength: string | undefined,
periodsToCompare: string | undefined,
startMonth: string | undefined
) {
const url = new URL(`https://api.codat.io/companies/${companyId}/data/financials/profitAndLoss`)
for (const [k, v] of [
['periodLength', periodLength],
['periodsToCompare', periodsToCompare],
['startMonth', startMonth]
]) {
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