1 | |
2 | type Pipedrive = { |
3 | apiToken: string; |
4 | }; |
5 | |
6 | * Perform a search from multiple item types |
7 | * Performs a search from your choice of item types and fields. |
8 | */ |
9 | export async function main( |
10 | auth: Pipedrive, |
11 | term: string | undefined, |
12 | item_types: |
13 | | "deal" |
14 | | "person" |
15 | | "organization" |
16 | | "product" |
17 | | "lead" |
18 | | "file" |
19 | | "mail_attachment" |
20 | | "project" |
21 | | undefined, |
22 | fields: |
23 | | "address" |
24 | | "code" |
25 | | "custom_fields" |
26 | | "email" |
27 | | "name" |
28 | | "notes" |
29 | | "organization_name" |
30 | | "person_name" |
31 | | "phone" |
32 | | "title" |
33 | | "description" |
34 | | undefined, |
35 | search_for_related_items: string | undefined, |
36 | exact_match: string | undefined, |
37 | include_fields: |
38 | | "deal.cc_email" |
39 | | "person.picture" |
40 | | "product.price" |
41 | | undefined, |
42 | limit: string | undefined, |
43 | cursor: string | undefined, |
44 | ) { |
45 | const url = new URL(`https://api.pipedrive.com/api/v2/itemSearch`); |
46 | for (const [k, v] of [ |
47 | ["term", term], |
48 | ["item_types", item_types], |
49 | ["fields", fields], |
50 | ["search_for_related_items", search_for_related_items], |
51 | ["exact_match", exact_match], |
52 | ["include_fields", include_fields], |
53 | ["limit", limit], |
54 | ["cursor", cursor], |
55 | ]) { |
56 | if (v !== undefined && v !== "" && k !== undefined) { |
57 | url.searchParams.append(k, v); |
58 | } |
59 | } |
60 | const response = await fetch(url, { |
61 | method: "GET", |
62 | headers: { |
63 | "x-api-token": auth.apiToken, |
64 | }, |
65 | body: undefined, |
66 | }); |
67 | if (!response.ok) { |
68 | const text = await response.text(); |
69 | throw new Error(`${response.status} ${text}`); |
70 | } |
71 | return await response.json(); |
72 | } |
73 |
|