1 | |
2 | type Pipedrive = { |
3 | apiToken: string; |
4 | }; |
5 | |
6 | * Search products |
7 | * Searches all products by name, code and/or custom fields. This endpoint is a wrapper of /v1/itemSearch with a narrower OAuth scope. |
8 | */ |
9 | export async function main( |
10 | auth: Pipedrive, |
11 | term: string | undefined, |
12 | fields: "code" | "custom_fields" | "name" | undefined, |
13 | exact_match: string | undefined, |
14 | include_fields: "product.price" | undefined, |
15 | limit: string | undefined, |
16 | cursor: string | undefined, |
17 | ) { |
18 | const url = new URL(`https://api.pipedrive.com/api/v2/products/search`); |
19 | for (const [k, v] of [ |
20 | ["term", term], |
21 | ["fields", fields], |
22 | ["exact_match", exact_match], |
23 | ["include_fields", include_fields], |
24 | ["limit", limit], |
25 | ["cursor", cursor], |
26 | ]) { |
27 | if (v !== undefined && v !== "" && k !== undefined) { |
28 | url.searchParams.append(k, v); |
29 | } |
30 | } |
31 | const response = await fetch(url, { |
32 | method: "GET", |
33 | headers: { |
34 | "x-api-token": auth.apiToken, |
35 | }, |
36 | body: undefined, |
37 | }); |
38 | if (!response.ok) { |
39 | const text = await response.text(); |
40 | throw new Error(`${response.status} ${text}`); |
41 | } |
42 | return await response.json(); |
43 | } |
44 |
|