0

Search Lists

by
Published Jun 6, 2022
Script mailchimp Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 6 days ago
1
//native
2

3
/**
4
 * @param fields *(optional)* A list of fields to return in the response.
5
 * Reference parameters of sub-objects with dot notation.
6
 *
7
 * @param exclude_fields *(optional)* A list of fields to exclude from the response.
8
 * Reference parameters of sub-objects with dot notation. If both `fields` and `exclude_fields`
9
 * are present, then only `exclude_fields` will be used.
10
 *
11
 * @param before_date_created *(optional)* Uses ISO 8601 time format: `2022-10-21T15:41:36+00:00`.
12
 * @param since_date_created *(optional)* Uses ISO 8601 time format: `2022-10-21T15:41:36+00:00`.
13
 * @param before_campaign_last_sent *(optional)* Uses ISO 8601 time format: `2022-10-21T15:41:36+00:00`.
14
 * @param since_campaign_last_sent *(optional)* Uses ISO 8601 time format: `2022-10-21T15:41:36+00:00`.
15
 */
16
type Mailchimp = {
17
  api_key: string;
18
  server: string;
19
};
20
export async function main(
21
  auth: Mailchimp,
22
  fields?: string[],
23
  exclude_fields?: string[],
24
  count?: number,
25
  offset?: number,
26
  sort_dir?: "" | "ASC" | "DESC",
27
  before_date_created?: string,
28
  since_date_created?: string,
29
  before_campaign_last_sent?: string,
30
  since_campaign_last_sent?: string,
31
  email?: string,
32
  sort_field?: "" | "date_created",
33
  has_ecommerce_store?: boolean,
34
  include_total_contacts?: boolean,
35
) {
36
  const url = new URL(`https://${auth.server}.api.mailchimp.com/3.0/lists`);
37
  const params = {
38
    fields,
39
    exclude_fields,
40
    count,
41
    offset,
42
    sort_dir,
43
    before_date_created,
44
    since_date_created,
45
    before_campaign_last_sent,
46
    since_campaign_last_sent,
47
    email,
48
    sort_field,
49
    has_ecommerce_store,
50
    include_total_contacts,
51
  };
52
  for (const key in params) {
53
    const value = params[<keyof typeof params>key];
54
    if (value) {
55
      url.searchParams.append(
56
        key,
57
        Array.isArray(value) ? value.join(",") : "" + value,
58
      );
59
    }
60
  }
61

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

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

Other submissions
  • Submitted by adam186 Deno
    Created 398 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