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