1 | |
2 | type Gorgias = { |
3 | username: string; |
4 | apiKey: string; |
5 | domain: string; |
6 | }; |
7 | |
8 | * List rules |
9 | * List rules, paginated. |
10 | */ |
11 | export async function main( |
12 | auth: Gorgias, |
13 | cursor: string | undefined, |
14 | page: string | undefined, |
15 | per_page: string | undefined, |
16 | limit: string | undefined, |
17 | order_by: "created_datetime:asc" | "created_datetime:desc" | undefined, |
18 | ) { |
19 | const url = new URL(`https://${auth.domain}.gorgias.com/api/rules`); |
20 | for (const [k, v] of [ |
21 | ["cursor", cursor], |
22 | ["page", page], |
23 | ["per_page", per_page], |
24 | ["limit", limit], |
25 | ["order_by", order_by], |
26 | ]) { |
27 | if (v !== undefined && v !== "" && k !== undefined) { |
28 | url.searchParams.append(k, v); |
29 | } |
30 | } |
31 | const response = await fetch(url, { |
32 | method: "GET", |
33 | headers: { |
34 | Authorization: "Basic " + btoa(`${auth.username}:${auth.apiKey}`), |
35 | }, |
36 | body: undefined, |
37 | }); |
38 | if (!response.ok) { |
39 | const text = await response.text(); |
40 | throw new Error(`${response.status} ${text}`); |
41 | } |
42 | return await response.json(); |
43 | } |
44 |
|