Edits history of script submission #16284 for ' Search for view's items (gorgias)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Gorgias = {
      username: string;
      apiKey: string;
      domain: string;
    };
    /**
     * Search for view's items
     * List view's items, paginated, and ordered by the attribute specified in the data of view.
     */
    export async function main(
      auth: Gorgias,
      view_id: string,
      ignored_item: string | undefined,
      cursor: string | undefined,
      direction: "prev" | "next" | undefined,
      limit: string | undefined,
      body: {
        view?: {
          category?: string;
          fields?:
            | "details"
            | "tags"
            | "customer"
            | "last_message"
            | "name"
            | "email"
            | "created"
            | "updated"
            | "assignee"
            | "assignee_team"
            | "channel"
            | "closed"
            | "language"
            | "last_received_message"
            | "integrations"
            | "snooze"
            | "status"
            | "subject"[];
          filters?: string;
          filters_ast?: {};
          order_by?: string;
          order_dir?: "asc" | "desc";
          search?: string;
          type?: "ticket-list";
        };
      },
    ) {
      const url = new URL(
        `https://${auth.domain}.gorgias.com/api/views/${view_id}/items`,
      );
      for (const [k, v] of [
        ["ignored_item", ignored_item],
        ["cursor", cursor],
        ["direction", direction],
        ["limit", limit],
      ]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "PUT",
        headers: {
          "Content-Type": "application/json",
          Authorization: "Basic " + btoa(`${auth.username}:${auth.apiKey}`),
        },
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 235 days ago