1 | |
2 | type Zoho = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Get related records using external id |
7 | * |
8 | */ |
9 | export async function main( |
10 | auth: Zoho, |
11 | module_api_name: string, |
12 | external_value: string, |
13 | related_list_api_name: string, |
14 | page: string | undefined, |
15 | per_page: string | undefined, |
16 | fields: string | undefined, |
17 | If_Modified_Since?: string, |
18 | X_EXTERNAL?: string, |
19 | ) { |
20 | const url = new URL( |
21 | `https://zohoapis.com/crm/v8/${module_api_name}/${external_value}/${related_list_api_name}`, |
22 | ); |
23 | for (const [k, v] of [ |
24 | ["page", page], |
25 | ["per_page", per_page], |
26 | ["fields", fields], |
27 | ]) { |
28 | if (v !== undefined && v !== "" && k !== undefined) { |
29 | url.searchParams.append(k, v); |
30 | } |
31 | } |
32 | const response = await fetch(url, { |
33 | method: "GET", |
34 | headers: { |
35 | ...(If_Modified_Since ? { "If-Modified-Since": If_Modified_Since } : {}), |
36 | ...(X_EXTERNAL ? { "X-EXTERNAL": X_EXTERNAL } : {}), |
37 | Authorization: "Zoho-oauthtoken " + auth.token, |
38 | }, |
39 | body: undefined, |
40 | }); |
41 | if (!response.ok) { |
42 | const text = await response.text(); |
43 | throw new Error(`${response.status} ${text}`); |
44 | } |
45 | return await response.json(); |
46 | } |
47 |
|