0

Add Or Remove Members Tags

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 members_to_add *(optional)* An array of emails to be used for a static segment.
5
 * Any emails provided that are not present on the list will be ignored.
6
 * A maximum of 500 members can be sent.
7
 *
8
 * @param members_to_remove *(optional)* An array of emails to be used for a static segment.
9
 * Any emails provided that are not present on the list will be ignored.
10
 * A maximum of 500 members can be sent.
11
 */
12
type Mailchimp = {
13
  api_key: string;
14
  server: string;
15
};
16
export async function main(
17
  auth: Mailchimp,
18
  list_id: string,
19
  segment_id: string,
20
  members_to_add?: string[],
21
  members_to_remove?: string[],
22
) {
23
  const url = new URL(
24
    `https://${auth.server}.api.mailchimp.com/3.0/lists/${list_id}/segments/${segment_id}`,
25
  );
26
  const body = {
27
    members_to_add,
28
    members_to_remove,
29
  };
30
  removeObjectEmptyFields(body, true, false);
31
  if (!Object.keys(body).length) {
32
    return "No members were declared to be added or removed.";
33
  }
34

35
  const response = await fetch(url, {
36
    method: "POST",
37
    headers: {
38
      Authorization: `Bearer ${auth.api_key}`,
39
    },
40
    body: JSON.stringify(body),
41
  });
42

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

49
function removeObjectEmptyFields(
50
  object?: Record<string, any>,
51
  removeEmptyArraysAndObjects = true,
52
  createNewObject = true,
53
) {
54
  if (!object || typeof object !== "object") return {}
55
  const obj = createNewObject ? { ...object } : object
56
  const emptyValues = [undefined, null, ""]
57
  for (const key in obj) {
58
    const value = obj[key]
59
    if (emptyValues.includes(value)) {
60
      delete obj[key]
61
    } else if (typeof value === "object") {
62
      if (Object.keys(value).length) {
63
        obj[key] = removeObjectEmptyFields(value, removeEmptyArraysAndObjects, false)
64
      }
65
      if (!Object.keys(value).length && removeEmptyArraysAndObjects) {
66
        delete obj[key]
67
      }
68
    }
69
  }
70
  return obj
71
}
72

Other submissions
  • Submitted by adam186 Deno
    Created 398 days ago
    1
    import { removeObjectEmptyFields } from "https://deno.land/x/[email protected]/mod.ts";
    2
    
    
    3
    /**
    4
     * @param members_to_add *(optional)* An array of emails to be used for a static segment.
    5
     * Any emails provided that are not present on the list will be ignored.
    6
     * A maximum of 500 members can be sent.
    7
     *
    8
     * @param members_to_remove *(optional)* An array of emails to be used for a static segment.
    9
     * Any emails provided that are not present on the list will be ignored.
    10
     * A maximum of 500 members can be sent.
    11
     */
    12
    type Mailchimp = {
    13
      api_key: string;
    14
      server: string;
    15
    };
    16
    export async function main(
    17
      auth: Mailchimp,
    18
      list_id: string,
    19
      segment_id: string,
    20
      members_to_add?: string[],
    21
      members_to_remove?: string[],
    22
    ) {
    23
      const url = new URL(
    24
        `https://${auth.server}.api.mailchimp.com/3.0/lists/${list_id}/segments/${segment_id}`,
    25
      );
    26
      const body = {
    27
        members_to_add,
    28
        members_to_remove,
    29
      };
    30
      removeObjectEmptyFields(body, true, false);
    31
      if (!Object.keys(body).length) {
    32
        return "No members were declared to be added or removed.";
    33
      }
    34
    
    
    35
      const response = await fetch(url, {
    36
        method: "POST",
    37
        headers: {
    38
          Authorization: `Bearer ${auth.api_key}`,
    39
        },
    40
        body: JSON.stringify(body),
    41
      });
    42
    
    
    43
      if (!response.ok) {
    44
        throw Error(await response.text());
    45
      }
    46
      return await response.json();
    47
    }
    48