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