1 | |
2 | type Xero = { |
3 | token: string |
4 | } |
5 | |
6 | * Retrieves report for budget summary |
7 | * |
8 | */ |
9 | export async function main( |
10 | auth: Xero, |
11 | date: string | undefined, |
12 | periods: string | undefined, |
13 | timeframe: string | undefined, |
14 | xero_tenant_id: string |
15 | ) { |
16 | const url = new URL(`https://api.xero.com/api.xro/2.0/Reports/BudgetSummary`) |
17 | for (const [k, v] of [ |
18 | ['date', date], |
19 | ['periods', periods], |
20 | ['timeframe', timeframe] |
21 | ]) { |
22 | if (v !== undefined && v !== '' && k !== undefined) { |
23 | url.searchParams.append(k, v) |
24 | } |
25 | } |
26 | const response = await fetch(url, { |
27 | method: 'GET', |
28 | headers: { |
29 | Accept: 'application/json', |
30 | 'xero-tenant-id': xero_tenant_id, |
31 | Authorization: 'Bearer ' + auth.token |
32 | }, |
33 | body: undefined |
34 | }) |
35 | if (!response.ok) { |
36 | const text = await response.text() |
37 | throw new Error(`${response.status} ${text}`) |
38 | } |
39 | return await response.json() |
40 | } |
41 |
|