1 | type Zendesk = { |
2 | username: string; |
3 | password: string; |
4 | subdomain: string; |
5 | }; |
6 | |
7 | * Autocomplete Custom Object Record Search |
8 | * Retrieves an array of custom object records that have a field value that matches the value specified in the `name` parameter. |
9 |
|
10 | #### Pagination |
11 |
|
12 | * Cursor pagination only. |
13 | * Returns the first 10,000 records sorted by relevancy with page limits. |
14 | #### Allowed For |
15 | * Agents |
16 | */ |
17 | export async function main( |
18 | auth: Zendesk, |
19 | custom_object_key: string, |
20 | name: string | undefined, |
21 | page_before_: string | undefined, |
22 | page_after_: string | undefined, |
23 | page_size_: string | undefined, |
24 | field_id: string | undefined, |
25 | source: string | undefined |
26 | ) { |
27 | const url = new URL( |
28 | `https://${auth.subdomain}.zendesk.com/api/v2/custom_objects/${custom_object_key}/records/autocomplete` |
29 | ); |
30 | for (const [k, v] of [ |
31 | ["name", name], |
32 | ["page[before]", page_before_], |
33 | ["page[after]", page_after_], |
34 | ["page[size]", page_size_], |
35 | ["field_id", field_id], |
36 | ["source", source], |
37 | ]) { |
38 | if (v !== undefined && v !== "") { |
39 | url.searchParams.append(k, v); |
40 | } |
41 | } |
42 | const response = await fetch(url, { |
43 | method: "GET", |
44 | headers: { |
45 | Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`), |
46 | }, |
47 | body: undefined, |
48 | }); |
49 | if (!response.ok) { |
50 | const text = await response.text(); |
51 | throw new Error(`${response.status} ${text}`); |
52 | } |
53 | return await response.json(); |
54 | } |
55 |
|