1 | import { removeObjectEmptyFields } from "https://deno.land/x/windmill_helpers@v1.0.0/mod.ts"; |
2 |
|
3 |
|
4 | * @param static_segment *(optional but either `static_segment` or `options` is required)* |
5 | * An array of emails to be used for a static segment. |
6 | * Any emails provided that are not present on the list will be ignored. |
7 | * Passing an empty array will create a static segment without any subscribers. |
8 | * This field cannot be provided with the options field. |
9 | * |
10 | * @param options *(optional but either `static_segment` or `options` is required)* |
11 | * The conditions of the segment. Static and fuzzy segments don't have conditions. |
12 | * Find more information about conditions at |
13 | * https://mailchimp.com/developer/marketing/docs/alternative-schemas/#segment-condition-schemas |
14 | */ |
15 | type Mailchimp = { |
16 | api_key: string; |
17 | server: string; |
18 | }; |
19 | export async function main( |
20 | auth: Mailchimp, |
21 | list_id: string, |
22 | name: string, |
23 | static_segment?: string[], |
24 | options?: { |
25 | match: string; |
26 | conditions: any[]; |
27 | }, |
28 | ) { |
29 | const url = new URL( |
30 | `https://${auth.server}.api.mailchimp.com/3.0/lists/${list_id}/segments`, |
31 | ); |
32 | const body = { |
33 | name, |
34 | static_segment, |
35 | options, |
36 | }; |
37 |
|
38 | const response = await fetch(url, { |
39 | method: "POST", |
40 | headers: { |
41 | Authorization: `Bearer ${auth.api_key}`, |
42 | }, |
43 | body: JSON.stringify(removeObjectEmptyFields(body)), |
44 | }); |
45 |
|
46 | if (!response.ok) { |
47 | throw Error(await response.text()); |
48 | } |
49 | return await response.json(); |
50 | } |
51 |
|