0
Search Lists
One script reply has been approved by the moderators Verified
Created by saskiacimex 687 days ago Viewed 4725 times
0
Submitted by adam186 Deno
Verified 512 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
 * @param before_date_created *(optional)* Uses ISO 8601 time format: `2022-10-21T15:41:36+00:00`.
10
 * @param since_date_created *(optional)* Uses ISO 8601 time format: `2022-10-21T15:41:36+00:00`.
11
 * @param before_campaign_last_sent *(optional)* Uses ISO 8601 time format: `2022-10-21T15:41:36+00:00`.
12
 * @param since_campaign_last_sent *(optional)* Uses ISO 8601 time format: `2022-10-21T15:41:36+00:00`.
13
 */
14
type Mailchimp = {
15
  api_key: string;
16
  server: string;
17
};
18
export async function main(
19
  auth: Mailchimp,
20
  fields?: string[],
21
  exclude_fields?: string[],
22
  count?: number,
23
  offset?: number,
24
  sort_dir?: "" | "ASC" | "DESC",
25
  before_date_created?: string,
26
  since_date_created?: string,
27
  before_campaign_last_sent?: string,
28
  since_campaign_last_sent?: string,
29
  email?: string,
30
  sort_field?: "" | "date_created",
31
  has_ecommerce_store?: boolean,
32
  include_total_contacts?: boolean,
33
) {
34
  const url = new URL(`https://${auth.server}.api.mailchimp.com/3.0/lists`);
35
  const params = {
36
    fields,
37
    exclude_fields,
38
    count,
39
    offset,
40
    sort_dir,
41
    before_date_created,
42
    since_date_created,
43
    before_campaign_last_sent,
44
    since_campaign_last_sent,
45
    email,
46
    sort_field,
47
    has_ecommerce_store,
48
    include_total_contacts,
49
  };
50
  for (const key in params) {
51
    const value = params[<keyof typeof params>key];
52
    if (value) {
53
      url.searchParams.append(
54
        key,
55
        Array.isArray(value) ? value.join(",") : "" + value,
56
      );
57
    }
58
  }
59

60
  const response = await fetch(url, {
61
    method: "GET",
62
    headers: {
63
      Authorization: `Bearer ${auth.api_key}`,
64
    },
65
  });
66

67
  if (!response.ok) {
68
    throw Error(await response.text());
69
  }
70
  return await response.json();
71
}
72