Edits history of script submission #16266 for ' List widgets (gorgias)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Gorgias = {
      username: string;
      apiKey: string;
      domain: string;
    };
    /**
     * List widgets
     * List all widgets for the account, ordered.
     */
    export async function main(
      auth: Gorgias,
      cursor: string | undefined,
      limit: string | undefined,
      order_by:
        | "created_datetime:asc"
        | "created_datetime:desc"
        | "order:asc"
        | "order:desc"
        | undefined,
    ) {
      const url = new URL(`https://${auth.domain}.gorgias.com/api/widgets`);
      for (const [k, v] of [
        ["cursor", cursor],
        ["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