1 | |
2 | type Xero = { |
3 | token: string |
4 | } |
5 | |
6 | * Retrieves journals |
7 | * |
8 | */ |
9 | export async function main( |
10 | auth: Xero, |
11 | offset: string | undefined, |
12 | paymentsOnly: string | undefined, |
13 | xero_tenant_id: string, |
14 | If_Modified_Since: string |
15 | ) { |
16 | const url = new URL(`https://api.xero.com/api.xro/2.0/Journals`) |
17 | for (const [k, v] of [ |
18 | ['offset', offset], |
19 | ['paymentsOnly', paymentsOnly] |
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 | 'If-Modified-Since': If_Modified_Since, |
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 |
|