1 | |
2 | type Grist = { |
3 | apiKey: string; |
4 | host: string; |
5 | }; |
6 | |
7 | * Upload attachments to a doc |
8 | * |
9 | */ |
10 | export async function main( |
11 | auth: Grist, |
12 | docId: string, |
13 | body: { upload?: string[] }, |
14 | ) { |
15 | const url = new URL(`https://${auth.host}/api/docs/${docId}/attachments`); |
16 |
|
17 | const formData = new FormData(); |
18 | for (const [k, v] of Object.entries(body)) { |
19 | if (v !== undefined) { |
20 | formData.append(k, String(v)); |
21 | } |
22 | } |
23 | const response = await fetch(url, { |
24 | method: "POST", |
25 | headers: { |
26 | Authorization: "Bearer " + auth.apiKey, |
27 | }, |
28 | body: formData, |
29 | }); |
30 | if (!response.ok) { |
31 | const text = await response.text(); |
32 | throw new Error(`${response.status} ${text}`); |
33 | } |
34 | return await response.json(); |
35 | } |
36 |
|