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