1 | |
2 | type Zoho = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Upload file |
7 | * |
8 | */ |
9 | export async function main( |
10 | auth: Zoho, |
11 | body: { file: {} }, |
12 | feature?: string, |
13 | X_CRM_ORG?: string, |
14 | ) { |
15 | const url = new URL(`https://zohoapis.com/crm/v8/upload`); |
16 |
|
17 | const formData = new FormData(); |
18 | for (const [k, v] of Object.entries(body)) { |
19 | if (v !== undefined && v !== "") { |
20 | formData.append(k, String(v)); |
21 | } |
22 | } |
23 | const response = await fetch(url, { |
24 | method: "POST", |
25 | headers: { |
26 | ...(feature ? { feature: feature } : {}), |
27 | ...(X_CRM_ORG ? { "X-CRM-ORG": X_CRM_ORG } : {}), |
28 | Authorization: "Zoho-oauthtoken " + auth.token, |
29 | }, |
30 | body: formData, |
31 | }); |
32 | if (!response.ok) { |
33 | const text = await response.text(); |
34 | throw new Error(`${response.status} ${text}`); |
35 | } |
36 | return await response.json(); |
37 | } |
38 |
|