0

Get all files

by
Published Apr 8, 2025
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 all files
7
 *
8
 */
9
export async function main(
10
  auth: Brevo,
11
  entity: "companies" | "deals" | "contacts" | undefined,
12
  entityIds: string | undefined,
13
  dateFrom: string | undefined,
14
  dateTo: string | undefined,
15
  offset: string | undefined,
16
  limit: string | undefined,
17
  sort: "asc" | "desc" | undefined,
18
) {
19
  const url = new URL(`https://api.brevo.com/v3/crm/files`);
20
  for (const [k, v] of [
21
    ["entity", entity],
22
    ["entityIds", entityIds],
23
    ["dateFrom", dateFrom],
24
    ["dateTo", dateTo],
25
    ["offset", offset],
26
    ["limit", limit],
27
    ["sort", sort],
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