0

The schema of a table

by
Published Apr 8, 2025

The schema follows [frictionlessdata's table-schema standard](https://specs.frictionlessdata.io/table-schema/).

Script grist Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Grist = {
3
  apiKey: string;
4
  host: string;
5
};
6
/**
7
 * The schema of a table
8
 * The schema follows [frictionlessdata's table-schema standard](https://specs.frictionlessdata.io/table-schema/).
9
 */
10
export async function main(
11
  auth: Grist,
12
  docId: string,
13
  tableId: string | undefined,
14
  header: "colId" | "label" | undefined,
15
) {
16
  const url = new URL(
17
    `https://${auth.host}/api/docs/${docId}/download/table-schema`,
18
  );
19
  for (const [k, v] of [
20
    ["tableId", tableId],
21
    ["header", header],
22
  ]) {
23
    if (v !== undefined && v !== "" && k !== undefined) {
24
      url.searchParams.append(k, v);
25
    }
26
  }
27
  const response = await fetch(url, {
28
    method: "GET",
29
    headers: {
30
      Authorization: "Bearer " + auth.apiKey,
31
    },
32
    body: undefined,
33
  });
34
  if (!response.ok) {
35
    const text = await response.text();
36
    throw new Error(`${response.status} ${text}`);
37
  }
38
  return await response.text();
39
}
40