Search Campaigns

Script mailchimp Verified

by rossumblament ยท 6/6/2022

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 5 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
type Mailchimp = {
12
  api_key: string;
13
  server: string;
14
};
15
export async function main(
16
  auth: Mailchimp,
17
  fields?: string[],
18
  exclude_fields?: string[],
19
  count?: number,
20
  offset?: number,
21
  sort_dir?: "" | "ASC" | "DESC",
22
  type?: "" | "regular" | "plaintext" | "absplit" | "rss" | "variate",
23
  status?: "" | "save" | "paused" | "schedule" | "sending" | "sent",
24
  before_send_time?: string,
25
  since_send_time?: string,
26
  before_create_time?: string,
27
  since_create_time?: string,
28
  list_id?: string,
29
  folder_id?: string,
30
  member_id?: string,
31
  sort_field?: "" | "create_time" | "send_time",
32
) {
33
  const url = new URL(`https://${auth.server}.api.mailchimp.com/3.0/campaigns`);
34
  const params = {
35
    fields,
36
    exclude_fields,
37
    count,
38
    offset,
39
    sort_dir,
40
    type,
41
    status,
42
    before_send_time,
43
    since_send_time,
44
    before_create_time,
45
    since_create_time,
46
    list_id,
47
    folder_id,
48
    member_id,
49
    sort_field,
50
  };
51
  for (const key in params) {
52
    const value = params[<keyof typeof params>key];
53
    if (value) {
54
      url.searchParams.append(
55
        key,
56
        Array.isArray(value) ? value.join(",") : "" + value,
57
      );
58
    }
59
  }
60

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

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

Other submissions
  • Submitted by adam186 Deno
    Created 397 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
      fields?: string[],
    16
      exclude_fields?: string[],
    17
      count?: number,
    18
      offset?: number,
    19
      sort_dir?: "" | "ASC" | "DESC",
    20
      type?: "" | "regular" | "plaintext" | "absplit" | "rss" | "variate",
    21
      status?: "" | "save" | "paused" | "schedule" | "sending" | "sent",
    22
      before_send_time?: string,
    23
      since_send_time?: string,
    24
      before_create_time?: string,
    25
      since_create_time?: string,
    26
      list_id?: string,
    27
      folder_id?: string,
    28
      member_id?: string,
    29
      sort_field?: "" | "create_time" | "send_time",
    30
    ) {
    31
      const url = new URL(`https://${auth.server}.api.mailchimp.com/3.0/campaigns`);
    32
      const params = {
    33
        fields,
    34
        exclude_fields,
    35
        count,
    36
        offset,
    37
        sort_dir,
    38
        type,
    39
        status,
    40
        before_send_time,
    41
        since_send_time,
    42
        before_create_time,
    43
        since_create_time,
    44
        list_id,
    45
        folder_id,
    46
        member_id,
    47
        sort_field,
    48
      };
    49
      for (const key in params) {
    50
        const value = params[<keyof typeof params>key];
    51
        if (value) {
    52
          url.searchParams.append(
    53
            key,
    54
            Array.isArray(value) ? value.join(",") : "" + value,
    55
          );
    56
        }
    57
      }
    58
    
    
    59
      const response = await fetch(url, {
    60
        method: "GET",
    61
        headers: {
    62
          Authorization: `Bearer ${auth.api_key}`,
    63
        },
    64
      });
    65
    
    
    66
      if (!response.ok) {
    67
        throw Error(await response.text());
    68
      }
    69
      return await response.json();
    70
    }
    71