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