0
Get List
One script reply has been approved by the moderators Verified
Created by armancedh2b 662 days ago Viewed 2444 times
0
Submitted by adam186 Deno
Verified 487 days ago
1
/**
2
 * @param fields *(optional)* A list of fields to return in the response.
3
 * Reference parameters of sub-objects with dot notation.
4
 *
5
 * @param exclude_fields *(optional)* A list of fields to exclude from the response.
6
 * Reference parameters of sub-objects with dot notation. If both `fields` and `exclude_fields`
7
 * are present, then only `exclude_fields` will be used.
8
 */
9
type Mailchimp = {
10
  api_key: string;
11
  server: string;
12
};
13
export async function main(
14
  auth: Mailchimp,
15
  list_id: string,
16
  fields?: string[],
17
  exclude_fields?: string[],
18
  include_total_contacts?: boolean,
19
) {
20
  const url = new URL(
21
    `https://${auth.server}.api.mailchimp.com/3.0/lists/${list_id}`,
22
  );
23
  const params = {
24
    fields,
25
    exclude_fields,
26
    include_total_contacts,
27
  };
28
  for (const key in params) {
29
    const value = params[<keyof typeof params>key];
30
    if (value) {
31
      url.searchParams.append(
32
        key,
33
        Array.isArray(value) ? value.join(",") : "" + value,
34
      );
35
    }
36
  }
37

38
  const response = await fetch(url, {
39
    method: "GET",
40
    headers: {
41
      Authorization: `Bearer ${auth.api_key}`,
42
    },
43
  });
44

45
  if (!response.ok) {
46
    throw Error(await response.text());
47
  }
48
  return await response.json();
49
}
50