1 | import { removeObjectEmptyFields } from "https://deno.land/x/windmill_helpers@v1.0.0/mod.ts"; |
2 |
|
3 |
|
4 | * Find more information about the parameters at |
5 | * https://mailchimp.com/developer/marketing/api/campaigns/add-campaign/ |
6 | */ |
7 | type Mailchimp = { |
8 | api_key: string; |
9 | server: string; |
10 | }; |
11 | export async function main( |
12 | auth: Mailchimp, |
13 | type: "regular" | "plaintext" | "rss" | "variate", |
14 | content_type: "template" | "multichannel" = "template", |
15 | rss_opts?: Record<string, any>, |
16 | recipients?: Record<string, any>, |
17 | variate_settings?: Record<string, any>, |
18 | settings?: Record<string, any>, |
19 | tracking?: Record<string, any>, |
20 | social_card?: Record<string, any>, |
21 | ) { |
22 | const url = `https://${auth.server}.api.mailchimp.com/3.0/campaigns`; |
23 | const body = { |
24 | type, |
25 | content_type, |
26 | rss_opts, |
27 | recipients, |
28 | variate_settings, |
29 | settings, |
30 | tracking, |
31 | social_card, |
32 | }; |
33 |
|
34 | const response = await fetch(url, { |
35 | method: "POST", |
36 | headers: { |
37 | Authorization: `Bearer ${auth.api_key}`, |
38 | }, |
39 | body: JSON.stringify(removeObjectEmptyFields(body)), |
40 | }); |
41 |
|
42 | if (!response.ok) { |
43 | throw Error(await response.text()); |
44 | } |
45 | return await response.json(); |
46 | } |
47 |
|