1 | |
2 | type Codat = { |
3 | encodedKey: string |
4 | } |
5 | |
6 | * Get profit and loss |
7 | * Gets the latest profit and loss for a company. |
8 | */ |
9 | export async function main( |
10 | auth: Codat, |
11 | companyId: string, |
12 | periodLength: string | undefined, |
13 | periodsToCompare: string | undefined, |
14 | startMonth: string | undefined |
15 | ) { |
16 | const url = new URL(`https://api.codat.io/companies/${companyId}/data/financials/profitAndLoss`) |
17 | for (const [k, v] of [ |
18 | ['periodLength', periodLength], |
19 | ['periodsToCompare', periodsToCompare], |
20 | ['startMonth', startMonth] |
21 | ]) { |
22 | if (v !== undefined && v !== '' && k !== undefined) { |
23 | url.searchParams.append(k, v) |
24 | } |
25 | } |
26 |
|
27 | const response = await fetch(url, { |
28 | method: 'GET', |
29 | headers: { |
30 | Authorization: `Basic ${auth.encodedKey}` |
31 | }, |
32 | body: undefined |
33 | }) |
34 | if (!response.ok) { |
35 | const text = await response.text() |
36 | throw new Error(`${response.status} ${text}`) |
37 | } |
38 | return await response.json() |
39 | } |
40 |
|