0

List Events

by
Published Apr 8, 2025
Script buttondown Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Buttondown = {
3
  token: string;
4
};
5
/**
6
 * List Events
7
 *
8
 */
9
export async function main(
10
  auth: Buttondown,
11
  event_type:
12
    | "activation_clicked"
13
    | "activation_delivered"
14
    | "activation_opened"
15
    | "bounced"
16
    | "complained"
17
    | "clicked"
18
    | "dropped"
19
    | "delivered"
20
    | "opened"
21
    | "sent"
22
    | "rejected"
23
    | "replied"
24
    | "unsubscribed"
25
    | undefined,
26
  ordering: string | undefined,
27
  expand: string | undefined,
28
  email_id: string | undefined,
29
  automation_id: string | undefined,
30
  subscriber_id: string | undefined,
31
) {
32
  const url = new URL(`https://api.buttondown.com/v1/events`);
33
  for (const [k, v] of [
34
    ["event_type", event_type],
35
    ["ordering", ordering],
36
    ["expand", expand],
37
    ["email_id", email_id],
38
    ["automation_id", automation_id],
39
    ["subscriber_id", subscriber_id],
40
  ]) {
41
    if (v !== undefined && v !== "" && k !== undefined) {
42
      url.searchParams.append(k, v);
43
    }
44
  }
45
  const response = await fetch(url, {
46
    method: "GET",
47
    headers: {
48
      Authorization: "Token " + auth.token,
49
    },
50
    body: undefined,
51
  });
52
  if (!response.ok) {
53
    const text = await response.text();
54
    throw new Error(`${response.status} ${text}`);
55
  }
56
  return await response.json();
57
}
58