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 | fields?: string[], |
18 | exclude_fields?: string[], |
19 | count?: number, |
20 | offset?: number, |
21 | sort_dir?: "" | "ASC" | "DESC", |
22 | type?: "" | "regular" | "plaintext" | "absplit" | "rss" | "variate", |
23 | status?: "" | "save" | "paused" | "schedule" | "sending" | "sent", |
24 | before_send_time?: string, |
25 | since_send_time?: string, |
26 | before_create_time?: string, |
27 | since_create_time?: string, |
28 | list_id?: string, |
29 | folder_id?: string, |
30 | member_id?: string, |
31 | sort_field?: "" | "create_time" | "send_time", |
32 | ) { |
33 | const url = new URL(`https://${auth.server}.api.mailchimp.com/3.0/campaigns`); |
34 | const params = { |
35 | fields, |
36 | exclude_fields, |
37 | count, |
38 | offset, |
39 | sort_dir, |
40 | type, |
41 | status, |
42 | before_send_time, |
43 | since_send_time, |
44 | before_create_time, |
45 | since_create_time, |
46 | list_id, |
47 | folder_id, |
48 | member_id, |
49 | sort_field, |
50 | }; |
51 | for (const key in params) { |
52 | const value = params[<keyof typeof params>key]; |
53 | if (value) { |
54 | url.searchParams.append( |
55 | key, |
56 | Array.isArray(value) ? value.join(",") : "" + value, |
57 | ); |
58 | } |
59 | } |
60 |
|
61 | const response = await fetch(url, { |
62 | method: "GET", |
63 | headers: { |
64 | Authorization: `Bearer ${auth.api_key}`, |
65 | }, |
66 | }); |
67 |
|
68 | if (!response.ok) { |
69 | throw Error(await response.text()); |
70 | } |
71 | return await response.json(); |
72 | } |
73 |
|