List Search Results

Use the ampersand character (&) to append the `sort_by` or `sort_order` parameters to the URL.

Script zendesk Verified

by hugo697 ยท 11/7/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 377 days ago
1
type Zendesk = {
2
  username: string;
3
  password: string;
4
  subdomain: string;
5
};
6
/**
7
 * List Search Results
8
 * Use the ampersand character (&) to append the `sort_by` or `sort_order` parameters to the URL.
9
 */
10
export async function main(
11
  auth: Zendesk,
12
  query: string | undefined,
13
  sort_by: string | undefined,
14
  sort_order: string | undefined
15
) {
16
  const url = new URL(`https://${auth.subdomain}.zendesk.com/api/v2/search`);
17
  for (const [k, v] of [
18
    ["query", query],
19
    ["sort_by", sort_by],
20
    ["sort_order", sort_order],
21
  ]) {
22
    if (v !== undefined && v !== "") {
23
      url.searchParams.append(k, v);
24
    }
25
  }
26
  const response = await fetch(url, {
27
    method: "GET",
28
    headers: {
29
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
30
    },
31
    body: undefined,
32
  });
33
  if (!response.ok) {
34
    const text = await response.text();
35
    throw new Error(`${response.status} ${text}`);
36
  }
37
  return await response.json();
38
}
39