1 | |
2 | type Pipedrive = { |
3 | apiToken: string; |
4 | }; |
5 | |
6 | * Search leads |
7 | * Searches all leads by title, notes and/or custom fields. This endpoint is a wrapper of /v1/itemSearch with a narrower OAuth scope. Found leads can be filtered by the person ID and the organization ID. |
8 | */ |
9 | export async function main( |
10 | auth: Pipedrive, |
11 | term: string | undefined, |
12 | fields: "custom_fields" | "notes" | "title" | undefined, |
13 | exact_match: string | undefined, |
14 | person_id: string | undefined, |
15 | organization_id: string | undefined, |
16 | include_fields: "lead.was_seen" | undefined, |
17 | limit: string | undefined, |
18 | cursor: string | undefined, |
19 | ) { |
20 | const url = new URL(`https://api.pipedrive.com/api/v2/leads/search`); |
21 | for (const [k, v] of [ |
22 | ["term", term], |
23 | ["fields", fields], |
24 | ["exact_match", exact_match], |
25 | ["person_id", person_id], |
26 | ["organization_id", organization_id], |
27 | ["include_fields", include_fields], |
28 | ["limit", limit], |
29 | ["cursor", cursor], |
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 |
|