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