0

Search for view's items

by
Published Oct 17, 2025

List view's items, paginated, and ordered by the attribute specified in the data of view.

Script gorgias Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Gorgias = {
3
  username: string;
4
  apiKey: string;
5
  domain: string;
6
};
7
/**
8
 * Search for view's items
9
 * List view's items, paginated, and ordered by the attribute specified in the data of view.
10
 */
11
export async function main(
12
  auth: Gorgias,
13
  view_id: string,
14
  ignored_item: string | undefined,
15
  cursor: string | undefined,
16
  direction: "prev" | "next" | undefined,
17
  limit: string | undefined,
18
  body: {
19
    view?: {
20
      category?: string;
21
      fields?:
22
        | "details"
23
        | "tags"
24
        | "customer"
25
        | "last_message"
26
        | "name"
27
        | "email"
28
        | "created"
29
        | "updated"
30
        | "assignee"
31
        | "assignee_team"
32
        | "channel"
33
        | "closed"
34
        | "language"
35
        | "last_received_message"
36
        | "integrations"
37
        | "snooze"
38
        | "status"
39
        | "subject"[];
40
      filters?: string;
41
      filters_ast?: {};
42
      order_by?: string;
43
      order_dir?: "asc" | "desc";
44
      search?: string;
45
      type?: "ticket-list";
46
    };
47
  },
48
) {
49
  const url = new URL(
50
    `https://${auth.domain}.gorgias.com/api/views/${view_id}/items`,
51
  );
52
  for (const [k, v] of [
53
    ["ignored_item", ignored_item],
54
    ["cursor", cursor],
55
    ["direction", direction],
56
    ["limit", limit],
57
  ]) {
58
    if (v !== undefined && v !== "" && k !== undefined) {
59
      url.searchParams.append(k, v);
60
    }
61
  }
62
  const response = await fetch(url, {
63
    method: "PUT",
64
    headers: {
65
      "Content-Type": "application/json",
66
      Authorization: "Basic " + btoa(`${auth.username}:${auth.apiKey}`),
67
    },
68
    body: JSON.stringify(body),
69
  });
70
  if (!response.ok) {
71
    const text = await response.text();
72
    throw new Error(`${response.status} ${text}`);
73
  }
74
  return await response.json();
75
}
76