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 | const url = new URL( |
22 | `https://${auth.server}.api.mailchimp.com/3.0/reports/${campaign_id}`, |
23 | ); |
24 | const params = { |
25 | fields, |
26 | exclude_fields, |
27 | }; |
28 | for (const key in params) { |
29 | const value = params[<keyof typeof params>key]; |
30 | if (value?.length) { |
31 | url.searchParams.append(key, value.join(",")); |
32 | } |
33 | } |
34 |
|
35 | const response = await fetch(url, { |
36 | method: "GET", |
37 | headers: { |
38 | Authorization: `Bearer ${auth.api_key}`, |
39 | }, |
40 | }); |
41 |
|
42 | if (!response.ok) { |
43 | throw Error(await response.text()); |
44 | } |
45 | return await response.json(); |
46 | } |
47 |
|