1 | |
2 | type Xero = { |
3 | token: string |
4 | } |
5 | |
6 | * Retrieves a specific budget, which includes budget lines |
7 | * |
8 | */ |
9 | export async function main( |
10 | auth: Xero, |
11 | BudgetID: string, |
12 | DateTo: string | undefined, |
13 | DateFrom: string | undefined, |
14 | xero_tenant_id: string |
15 | ) { |
16 | const url = new URL(`https://api.xero.com/api.xro/2.0/Budgets/${BudgetID}`) |
17 | for (const [k, v] of [ |
18 | ['DateTo', DateTo], |
19 | ['DateFrom', DateFrom] |
20 | ]) { |
21 | if (v !== undefined && v !== '' && k !== undefined) { |
22 | url.searchParams.append(k, v) |
23 | } |
24 | } |
25 | const response = await fetch(url, { |
26 | method: 'GET', |
27 | headers: { |
28 | Accept: 'application/json', |
29 | 'xero-tenant-id': xero_tenant_id, |
30 | Authorization: 'Bearer ' + auth.token |
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 |
|