1 | |
2 | type Grist = { |
3 | apiKey: string; |
4 | host: string; |
5 | }; |
6 | |
7 | * List metadata of all attachments in a doc |
8 | * |
9 | */ |
10 | export async function main( |
11 | auth: Grist, |
12 | docId: string, |
13 | filter: string | undefined, |
14 | sort: string | undefined, |
15 | limit: string | undefined, |
16 | X_Sort: string, |
17 | X_Limit: string, |
18 | ) { |
19 | const url = new URL(`https://${auth.host}/api/docs/${docId}/attachments`); |
20 | for (const [k, v] of [ |
21 | ["filter", filter], |
22 | ["sort", sort], |
23 | ["limit", limit], |
24 | ]) { |
25 | if (v !== undefined && v !== "" && k !== undefined) { |
26 | url.searchParams.append(k, v); |
27 | } |
28 | } |
29 | const response = await fetch(url, { |
30 | method: "GET", |
31 | headers: { |
32 | "X-Sort": X_Sort, |
33 | "X-Limit": X_Limit, |
34 | Authorization: "Bearer " + auth.apiKey, |
35 | }, |
36 | body: undefined, |
37 | }); |
38 | if (!response.ok) { |
39 | const text = await response.text(); |
40 | throw new Error(`${response.status} ${text}`); |
41 | } |
42 | return await response.json(); |
43 | } |
44 |
|