Edits history of script submission #16256 for ' List jobs (gorgias)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Gorgias = {
      username: string;
      apiKey: string;
      domain: string;
    };
    /**
     * List jobs
     * List jobs, paginated and ordered by their creation date, with the most recent ones first.
    
     */
    export async function main(
      auth: Gorgias,
      cursor: string | undefined,
      _type:
        | "applyMacro"
        | "deleteTicket"
        | "exportTicket"
        | "importMacro"
        | "exportMacro"
        | "updateTicket"
        | undefined,
      status:
        | "cancel_requested"
        | "canceled"
        | "done"
        | "errored"
        | "fatal_errored"
        | "pending"
        | "running"
        | "scheduled"
        | undefined,
      page: string | undefined,
      per_page: string | undefined,
      limit: string | undefined,
      order_by: "created_datetime:asc" | "created_datetime:desc" | undefined,
    ) {
      const url = new URL(`https://${auth.domain}.gorgias.com/api/jobs`);
      for (const [k, v] of [
        ["cursor", cursor],
        ["type", _type],
        ["status", status],
        ["page", page],
        ["per_page", per_page],
        ["limit", limit],
        ["order_by", order_by],
      ]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "GET",
        headers: {
          Authorization: "Basic " + btoa(`${auth.username}:${auth.apiKey}`),
        },
        body: undefined,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 235 days ago