1 | |
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 | campaign_id: string, |
18 | fields?: string[], |
19 | exclude_fields?: string[], |
20 | ) { |
21 | let url = `https://${auth.server}.api.mailchimp.com/3.0/campaigns/${campaign_id}?`; |
22 | if (fields?.length) { |
23 | url += `fields=${fields.join(",")}`; |
24 | } |
25 | if (exclude_fields?.length) { |
26 | url += `${fields?.length ? "&" : ""}exclude_fields=${exclude_fields.join( |
27 | ",", |
28 | )}`; |
29 | } |
30 |
|
31 | const response = await fetch(url, { |
32 | method: "GET", |
33 | headers: { |
34 | Authorization: `Bearer ${auth.api_key}`, |
35 | }, |
36 | }); |
37 |
|
38 | if (!response.ok) { |
39 | throw Error(await response.text()); |
40 | } |
41 | return await response.json(); |
42 | } |
43 |
|