0

List Emails

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 Emails
7
 *
8
 */
9
export async function main(
10
  auth: Buttondown,
11
  status: string | undefined,
12
  automations: string | undefined,
13
  ids: string | undefined,
14
  ordering: string | undefined,
15
  creation_date__start: string | undefined,
16
  creation_date__end: string | undefined,
17
  publish_date__start: string | undefined,
18
  publish_date__end: string | undefined,
19
  excluded_fields: string | undefined,
20
  source: string | undefined,
21
  email_type: string | undefined,
22
) {
23
  const url = new URL(`https://api.buttondown.com/v1/emails`);
24
  for (const [k, v] of [
25
    ["status", status],
26
    ["automations", automations],
27
    ["ids", ids],
28
    ["ordering", ordering],
29
    ["creation_date__start", creation_date__start],
30
    ["creation_date__end", creation_date__end],
31
    ["publish_date__start", publish_date__start],
32
    ["publish_date__end", publish_date__end],
33
    ["excluded_fields", excluded_fields],
34
    ["source", source],
35
    ["email_type", email_type],
36
  ]) {
37
    if (v !== undefined && v !== "" && k !== undefined) {
38
      url.searchParams.append(k, v);
39
    }
40
  }
41
  const response = await fetch(url, {
42
    method: "GET",
43
    headers: {
44
      Authorization: "Token " + auth.token,
45
    },
46
    body: undefined,
47
  });
48
  if (!response.ok) {
49
    const text = await response.text();
50
    throw new Error(`${response.status} ${text}`);
51
  }
52
  return await response.json();
53
}
54