1 | |
2 | type Pipedrive = { |
3 | apiToken: string; |
4 | }; |
5 | |
6 | * Get all products |
7 | * Returns data about all products. |
8 | */ |
9 | export async function main( |
10 | auth: Pipedrive, |
11 | owner_id: string | undefined, |
12 | ids: string | undefined, |
13 | filter_id: string | undefined, |
14 | cursor: string | undefined, |
15 | limit: string | undefined, |
16 | sort_by: "id" | "name" | "add_time" | "update_time" | undefined, |
17 | sort_direction: "asc" | "desc" | undefined, |
18 | custom_fields: string | undefined, |
19 | ) { |
20 | const url = new URL(`https://api.pipedrive.com/api/v2/products`); |
21 | for (const [k, v] of [ |
22 | ["owner_id", owner_id], |
23 | ["ids", ids], |
24 | ["filter_id", filter_id], |
25 | ["cursor", cursor], |
26 | ["limit", limit], |
27 | ["sort_by", sort_by], |
28 | ["sort_direction", sort_direction], |
29 | ["custom_fields", custom_fields], |
30 | ]) { |
31 | if (v !== undefined && v !== "" && k !== undefined) { |
32 | url.searchParams.append(k, v); |
33 | } |
34 | } |
35 | const response = await fetch(url, { |
36 | method: "GET", |
37 | headers: { |
38 | "x-api-token": auth.apiToken, |
39 | }, |
40 | body: undefined, |
41 | }); |
42 | if (!response.ok) { |
43 | const text = await response.text(); |
44 | throw new Error(`${response.status} ${text}`); |
45 | } |
46 | return await response.json(); |
47 | } |
48 |
|