1 | |
2 | type Brevo = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * Get all the contacts |
7 | * |
8 | */ |
9 | export async function main( |
10 | auth: Brevo, |
11 | limit: string | undefined, |
12 | offset: string | undefined, |
13 | modifiedSince: string | undefined, |
14 | createdSince: string | undefined, |
15 | sort: "asc" | "desc" | undefined, |
16 | segmentId: string | undefined, |
17 | listIds: string | undefined, |
18 | filter: string | undefined, |
19 | ) { |
20 | const url = new URL(`https://api.brevo.com/v3/contacts`); |
21 | for (const [k, v] of [ |
22 | ["limit", limit], |
23 | ["offset", offset], |
24 | ["modifiedSince", modifiedSince], |
25 | ["createdSince", createdSince], |
26 | ["sort", sort], |
27 | ["segmentId", segmentId], |
28 | ["listIds", listIds], |
29 | ["filter", filter], |
30 | ]) { |
31 | if (v !== undefined && v !== "" && k !== undefined) { |
32 | url.searchParams.append(k, v); |
33 | } |
34 | } |
35 | const response = await fetch(url, { |
36 | method: "GET", |
37 | headers: { |
38 | "api-key": auth.apiKey, |
39 | }, |
40 | body: undefined, |
41 | }); |
42 | if (!response.ok) { |
43 | const text = await response.text(); |
44 | throw new Error(`${response.status} ${text}`); |
45 | } |
46 | return await response.json(); |
47 | } |
48 |
|