Edits history of script submission #11905 for ' List Events (buttondown)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Buttondown = {
      token: string;
    };
    /**
     * List Events
     *
     */
    export async function main(
      auth: Buttondown,
      event_type:
        | "activation_clicked"
        | "activation_delivered"
        | "activation_opened"
        | "bounced"
        | "complained"
        | "clicked"
        | "dropped"
        | "delivered"
        | "opened"
        | "sent"
        | "rejected"
        | "replied"
        | "unsubscribed"
        | undefined,
      ordering: string | undefined,
      expand: string | undefined,
      email_id: string | undefined,
      automation_id: string | undefined,
      subscriber_id: string | undefined,
    ) {
      const url = new URL(`https://api.buttondown.com/v1/events`);
      for (const [k, v] of [
        ["event_type", event_type],
        ["ordering", ordering],
        ["expand", expand],
        ["email_id", email_id],
        ["automation_id", automation_id],
        ["subscriber_id", subscriber_id],
      ]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "GET",
        headers: {
          Authorization: "Token " + auth.token,
        },
        body: undefined,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 428 days ago