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