//native
type Grist = {
apiKey: string;
host: string;
};
/**
* Add or update records of a table
*
*/
export async function main(
auth: Grist,
docId: string,
tableId: string,
noparse: string | undefined,
onmany: "first" | "none" | "all" | undefined,
noadd: string | undefined,
noupdate: string | undefined,
allow_empty_require: string | undefined,
body: { records: { require: {}; fields?: {} }[] },
) {
const url = new URL(
`https://${auth.host}/api/docs/${docId}/tables/${tableId}/records`,
);
for (const [k, v] of [
["noparse", noparse],
["onmany", onmany],
["noadd", noadd],
["noupdate", noupdate],
["allow_empty_require", allow_empty_require],
]) {
if (v !== undefined && v !== "" && k !== undefined) {
url.searchParams.append(k, v);
}
}
const response = await fetch(url, {
method: "PUT",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + auth.apiKey,
},
body: JSON.stringify(body),
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.text();
}
Submitted by hugo697 329 days ago