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