1 | |
2 | type Zoho = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Search records |
7 | * |
8 | */ |
9 | export async function main( |
10 | auth: Zoho, |
11 | module_api_name: string, |
12 | criteria: string | undefined, |
13 | email: string | undefined, |
14 | phone: string | undefined, |
15 | word: string | undefined, |
16 | converted: string | undefined, |
17 | approved: string | undefined, |
18 | page: string | undefined, |
19 | per_page: string | undefined, |
20 | fields: string | undefined, |
21 | cvid: string | undefined, |
22 | _type: string | undefined, |
23 | X_EXTERNAL?: string, |
24 | ) { |
25 | const url = new URL(`https://zohoapis.com/crm/v8/${module_api_name}/search`); |
26 | for (const [k, v] of [ |
27 | ["criteria", criteria], |
28 | ["email", email], |
29 | ["phone", phone], |
30 | ["word", word], |
31 | ["converted", converted], |
32 | ["approved", approved], |
33 | ["page", page], |
34 | ["per_page", per_page], |
35 | ["fields", fields], |
36 | ["cvid", cvid], |
37 | ["type", _type], |
38 | ]) { |
39 | if (v !== undefined && v !== "" && k !== undefined) { |
40 | url.searchParams.append(k, v); |
41 | } |
42 | } |
43 | const response = await fetch(url, { |
44 | method: "GET", |
45 | headers: { |
46 | ...(X_EXTERNAL ? { "X-EXTERNAL": X_EXTERNAL } : {}), |
47 | Authorization: "Zoho-oauthtoken " + auth.token, |
48 | }, |
49 | body: undefined, |
50 | }); |
51 | if (!response.ok) { |
52 | const text = await response.text(); |
53 | throw new Error(`${response.status} ${text}`); |
54 | } |
55 | return await response.json(); |
56 | } |
57 |
|