0

Get balance definition list

by
Published Apr 8, 2025

Returns balance definition page

Script brevo Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Brevo = {
3
  apiKey: string;
4
};
5
/**
6
 * Get balance definition list
7
 * Returns balance definition page
8
 */
9
export async function main(
10
  auth: Brevo,
11
  loyaltyProgramId: string,
12
  limit: string | undefined,
13
  offset: string | undefined,
14
  sortField: "name" | "createdAt" | "updatedAt" | undefined,
15
  sort: "asc" | "desc" | undefined,
16
  version: "active" | "draft" | undefined,
17
) {
18
  const url = new URL(
19
    `https://api.brevo.com/v3/loyalty/balance/programs/${loyaltyProgramId}/balance-definitions`,
20
  );
21
  for (const [k, v] of [
22
    ["limit", limit],
23
    ["offset", offset],
24
    ["sortField", sortField],
25
    ["sort", sort],
26
    ["version", version],
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