1 | |
2 | type Brevo = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * Return all your products |
7 | * |
8 | */ |
9 | export async function main( |
10 | auth: Brevo, |
11 | limit: string | undefined, |
12 | offset: string | undefined, |
13 | sort: "asc" | "desc" | undefined, |
14 | ids: string | undefined, |
15 | name: string | undefined, |
16 | price_lte_: string | undefined, |
17 | price_gte_: string | undefined, |
18 | price_lt_: string | undefined, |
19 | price_gt_: string | undefined, |
20 | price_eq_: string | undefined, |
21 | price_ne_: string | undefined, |
22 | categories: string | undefined, |
23 | modifiedSince: string | undefined, |
24 | createdSince: string | undefined, |
25 | ) { |
26 | const url = new URL(`https://api.brevo.com/v3/products`); |
27 | for (const [k, v] of [ |
28 | ["limit", limit], |
29 | ["offset", offset], |
30 | ["sort", sort], |
31 | ["ids", ids], |
32 | ["name", name], |
33 | ["price[lte]", price_lte_], |
34 | ["price[gte]", price_gte_], |
35 | ["price[lt]", price_lt_], |
36 | ["price[gt]", price_gt_], |
37 | ["price[eq]", price_eq_], |
38 | ["price[ne]", price_ne_], |
39 | ["categories", categories], |
40 | ["modifiedSince", modifiedSince], |
41 | ["createdSince", createdSince], |
42 | ]) { |
43 | if (v !== undefined && v !== "" && k !== undefined) { |
44 | url.searchParams.append(k, v); |
45 | } |
46 | } |
47 | const response = await fetch(url, { |
48 | method: "GET", |
49 | headers: { |
50 | "api-key": auth.apiKey, |
51 | }, |
52 | body: undefined, |
53 | }); |
54 | if (!response.ok) { |
55 | const text = await response.text(); |
56 | throw new Error(`${response.status} ${text}`); |
57 | } |
58 | return await response.json(); |
59 | } |
60 |
|