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