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