1 | |
2 |
|
3 | |
4 | * List Expense Reports |
5 | * Query expense reports with optional filters. Returns up to 50 records per page; paginate with offset. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Coupa, |
9 | updated_after: string | undefined, |
10 | limit: number | undefined, |
11 | offset: number | undefined, |
12 | return_object: "limited" | "shallow" | undefined |
13 | ) { |
14 | const base = auth.instance_url.replace(/\/+$/, "") |
15 | const url = new URL(`${base}/api/expense_reports`) |
16 | const filters: { [key: string]: string | number | boolean | undefined } = { |
17 | "updated_at[gt]": updated_after, |
18 | limit, |
19 | offset, |
20 | return_object, |
21 | } |
22 | for (const [k, v] of Object.entries(filters)) { |
23 | if (v !== undefined && v !== "") { |
24 | url.searchParams.append(k, String(v)) |
25 | } |
26 | } |
27 |
|
28 | const response = await fetch(url, { |
29 | headers: { |
30 | Authorization: `Bearer ${auth.token}`, |
31 | Accept: "application/json", |
32 | }, |
33 | }) |
34 |
|
35 | if (!response.ok) { |
36 | throw new Error(`${response.status} ${await response.text()}`) |
37 | } |
38 |
|
39 | return await response.json() |
40 | } |
41 |
|