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 | * @param before_date_created *(optional)* Uses ISO 8601 time format: `2022-10-21T15:41:36+00:00`. |
12 | * @param since_date_created *(optional)* Uses ISO 8601 time format: `2022-10-21T15:41:36+00:00`. |
13 | * @param before_campaign_last_sent *(optional)* Uses ISO 8601 time format: `2022-10-21T15:41:36+00:00`. |
14 | * @param since_campaign_last_sent *(optional)* Uses ISO 8601 time format: `2022-10-21T15:41:36+00:00`. |
15 | */ |
16 | type Mailchimp = { |
17 | api_key: string; |
18 | server: string; |
19 | }; |
20 | export async function main( |
21 | auth: Mailchimp, |
22 | fields?: string[], |
23 | exclude_fields?: string[], |
24 | count?: number, |
25 | offset?: number, |
26 | sort_dir?: "" | "ASC" | "DESC", |
27 | before_date_created?: string, |
28 | since_date_created?: string, |
29 | before_campaign_last_sent?: string, |
30 | since_campaign_last_sent?: string, |
31 | email?: string, |
32 | sort_field?: "" | "date_created", |
33 | has_ecommerce_store?: boolean, |
34 | include_total_contacts?: boolean, |
35 | ) { |
36 | const url = new URL(`https://${auth.server}.api.mailchimp.com/3.0/lists`); |
37 | const params = { |
38 | fields, |
39 | exclude_fields, |
40 | count, |
41 | offset, |
42 | sort_dir, |
43 | before_date_created, |
44 | since_date_created, |
45 | before_campaign_last_sent, |
46 | since_campaign_last_sent, |
47 | email, |
48 | sort_field, |
49 | has_ecommerce_store, |
50 | include_total_contacts, |
51 | }; |
52 | for (const key in params) { |
53 | const value = params[<keyof typeof params>key]; |
54 | if (value) { |
55 | url.searchParams.append( |
56 | key, |
57 | Array.isArray(value) ? value.join(",") : "" + value, |
58 | ); |
59 | } |
60 | } |
61 |
|
62 | const response = await fetch(url, { |
63 | method: "GET", |
64 | headers: { |
65 | Authorization: `Bearer ${auth.api_key}`, |
66 | }, |
67 | }); |
68 |
|
69 | if (!response.ok) { |
70 | throw Error(await response.text()); |
71 | } |
72 | return await response.json(); |
73 | } |
74 |
|