1 |
|
2 | * @param fields *(optional)* A list of fields to return in the response. |
3 | * Reference parameters of sub-objects with dot notation. |
4 | * |
5 | * @param exclude_fields *(optional)* A list of fields to exclude from the response. |
6 | * Reference parameters of sub-objects with dot notation. If both `fields` and `exclude_fields` |
7 | * are present, then only `exclude_fields` will be used. |
8 | */ |
9 | type Mailchimp = { |
10 | api_key: string; |
11 | server: string; |
12 | }; |
13 | export async function main( |
14 | auth: Mailchimp, |
15 | campaign_id: string, |
16 | fields?: string[], |
17 | exclude_fields?: string[], |
18 | ) { |
19 | const url = new URL( |
20 | `https://${auth.server}.api.mailchimp.com/3.0/reports/${campaign_id}`, |
21 | ); |
22 | const params = { |
23 | fields, |
24 | exclude_fields, |
25 | }; |
26 | for (const key in params) { |
27 | const value = params[<keyof typeof params>key]; |
28 | if (value?.length) { |
29 | url.searchParams.append(key, value.join(",")); |
30 | } |
31 | } |
32 |
|
33 | const response = await fetch(url, { |
34 | method: "GET", |
35 | headers: { |
36 | Authorization: `Bearer ${auth.api_key}`, |
37 | }, |
38 | }); |
39 |
|
40 | if (!response.ok) { |
41 | throw Error(await response.text()); |
42 | } |
43 | return await response.json(); |
44 | } |
45 |
|