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 |
|