1 | |
2 | type Grist = { |
3 | apiKey: string; |
4 | host: string; |
5 | }; |
6 | |
7 | * Add records to a table |
8 | * |
9 | */ |
10 | export async function main( |
11 | auth: Grist, |
12 | docId: string, |
13 | tableId: string, |
14 | noparse: string | undefined, |
15 | body: { records: { fields: {} }[] }, |
16 | ) { |
17 | const url = new URL( |
18 | `https://${auth.host}/api/docs/${docId}/tables/${tableId}/records`, |
19 | ); |
20 | for (const [k, v] of [["noparse", noparse]]) { |
21 | if (v !== undefined && v !== "" && k !== undefined) { |
22 | url.searchParams.append(k, v); |
23 | } |
24 | } |
25 | const response = await fetch(url, { |
26 | method: "POST", |
27 | headers: { |
28 | "Content-Type": "application/json", |
29 | Authorization: "Bearer " + auth.apiKey, |
30 | }, |
31 | body: JSON.stringify(body), |
32 | }); |
33 | if (!response.ok) { |
34 | const text = await response.text(); |
35 | throw new Error(`${response.status} ${text}`); |
36 | } |
37 | return await response.json(); |
38 | } |
39 |
|