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