Show Results Count

Returns the number of items matching the query rather than the items. The search string works the same as a regular search.

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
 * Show Results Count
8
 * Returns the number of items matching the query rather than the items. The search string works the same as a regular search.
9

10
 */
11
export async function main(auth: Zendesk, query: string | undefined) {
12
  const url = new URL(
13
    `https://${auth.subdomain}.zendesk.com/api/v2/search/count`
14
  );
15
  for (const [k, v] of [["query", query]]) {
16
    if (v !== undefined && v !== "") {
17
      url.searchParams.append(k, v);
18
    }
19
  }
20
  const response = await fetch(url, {
21
    method: "GET",
22
    headers: {
23
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
24
    },
25
    body: undefined,
26
  });
27
  if (!response.ok) {
28
    const text = await response.text();
29
    throw new Error(`${response.status} ${text}`);
30
  }
31
  return await response.json();
32
}
33