//native
type Grist = {
apiKey: string;
host: string;
};
/**
* Fetch data from a table
* Deprecated in favor of `records` endpoints. We have no immediate plans to remove these endpoints, but consider `records` a better starting point for new projects.
*/
export async function main(
auth: Grist,
docId: string,
tableId: string,
filter: string | undefined,
sort: string | undefined,
limit: string | undefined,
X_Sort: string,
X_Limit: string,
) {
const url = new URL(
`https://${auth.host}/api/docs/${docId}/tables/${tableId}/data`,
);
for (const [k, v] of [
["filter", filter],
["sort", sort],
["limit", limit],
]) {
if (v !== undefined && v !== "" && k !== undefined) {
url.searchParams.append(k, v);
}
}
const response = await fetch(url, {
method: "GET",
headers: {
"X-Sort": X_Sort,
"X-Limit": X_Limit,
Authorization: "Bearer " + auth.apiKey,
},
body: undefined,
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
Submitted by hugo697 92 days ago