Listing Comments

#### Pagination - Cursor pagination (recommended) - Offset pagination See Pagination.

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
 * Listing Comments
8
 * #### Pagination
9

10
- Cursor pagination (recommended)
11
- Offset pagination
12

13
See Pagination.
14
 */
15
export async function main(
16
  auth: Zendesk,
17
  request_id: string,
18
  since: string | undefined,
19
  role: string | undefined
20
) {
21
  const url = new URL(
22
    `https://${auth.subdomain}.zendesk.com/api/v2/requests/${request_id}/comments`
23
  );
24
  for (const [k, v] of [
25
    ["since", since],
26
    ["role", role],
27
  ]) {
28
    if (v !== undefined && v !== "") {
29
      url.searchParams.append(k, v);
30
    }
31
  }
32
  const response = await fetch(url, {
33
    method: "GET",
34
    headers: {
35
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
36
    },
37
    body: undefined,
38
  });
39
  if (!response.ok) {
40
    const text = await response.text();
41
    throw new Error(`${response.status} ${text}`);
42
  }
43
  return await response.json();
44
}
45