1 | type Zendesk = { |
2 | username: string; |
3 | password: string; |
4 | subdomain: string; |
5 | }; |
6 | |
7 | * Search Triggers |
8 | * #### Pagination |
9 |
|
10 | * Offset pagination only |
11 |
|
12 | See Using Offset Pagination. |
13 | */ |
14 | export async function main( |
15 | auth: Zendesk, |
16 | query: string | undefined, |
17 | json: string | undefined, |
18 | active: string | undefined, |
19 | sort: string | undefined, |
20 | sort_by: string | undefined, |
21 | sort_order: string | undefined, |
22 | include: string | undefined |
23 | ) { |
24 | const url = new URL( |
25 | `https://${auth.subdomain}.zendesk.com/api/v2/triggers/search` |
26 | ); |
27 | for (const [k, v] of [ |
28 | ["query", query], |
29 | ["json", json], |
30 | ["active", active], |
31 | ["sort", sort], |
32 | ["sort_by", sort_by], |
33 | ["sort_order", sort_order], |
34 | ["include", include], |
35 | ]) { |
36 | if (v !== undefined && v !== "") { |
37 | url.searchParams.append(k, v); |
38 | } |
39 | } |
40 | const response = await fetch(url, { |
41 | method: "GET", |
42 | headers: { |
43 | Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`), |
44 | }, |
45 | body: undefined, |
46 | }); |
47 | if (!response.ok) { |
48 | const text = await response.text(); |
49 | throw new Error(`${response.status} ${text}`); |
50 | } |
51 | return await response.json(); |
52 | } |
53 |
|