1 | |
2 | type Xero = { |
3 | token: string |
4 | } |
5 | |
6 | * Retrieves timesheets |
7 | * |
8 | */ |
9 | export async function main( |
10 | auth: Xero, |
11 | page: string | undefined, |
12 | filter: string | undefined, |
13 | status: string | undefined, |
14 | startDate: string | undefined, |
15 | endDate: string | undefined, |
16 | sort: string | undefined, |
17 | Xero_Tenant_Id: string |
18 | ) { |
19 | const url = new URL(`https://api.xero.com/payroll.xro/2.0/Timesheets`) |
20 | for (const [k, v] of [ |
21 | ['page', page], |
22 | ['filter', filter], |
23 | ['status', status], |
24 | ['startDate', startDate], |
25 | ['endDate', endDate], |
26 | ['sort', sort] |
27 | ]) { |
28 | if (v !== undefined && v !== '' && k !== undefined) { |
29 | url.searchParams.append(k, v) |
30 | } |
31 | } |
32 | const response = await fetch(url, { |
33 | method: 'GET', |
34 | headers: { |
35 | Accept: 'application/json', |
36 | 'Xero-Tenant-Id': Xero_Tenant_Id, |
37 | Authorization: 'Bearer ' + auth.token |
38 | }, |
39 | body: undefined |
40 | }) |
41 | if (!response.ok) { |
42 | const text = await response.text() |
43 | throw new Error(`${response.status} ${text}`) |
44 | } |
45 | return await response.json() |
46 | } |
47 |
|