1 | |
2 | type Grist = { |
3 | apiKey: string; |
4 | host: string; |
5 | }; |
6 | |
7 | * Create new webhooks for a document |
8 | * |
9 | */ |
10 | export async function main( |
11 | auth: Grist, |
12 | docId: string, |
13 | body: { |
14 | webhooks: { |
15 | fields: { |
16 | name?: string; |
17 | memo?: string; |
18 | url?: string; |
19 | enabled?: false | true; |
20 | eventTypes?: string[]; |
21 | isReadyColumn?: string; |
22 | tableId?: string; |
23 | }; |
24 | }[]; |
25 | }, |
26 | ) { |
27 | const url = new URL(`https://${auth.host}/api/docs/${docId}/webhooks`); |
28 |
|
29 | const response = await fetch(url, { |
30 | method: "POST", |
31 | headers: { |
32 | "Content-Type": "application/json", |
33 | Authorization: "Bearer " + auth.apiKey, |
34 | }, |
35 | body: JSON.stringify(body), |
36 | }); |
37 | if (!response.ok) { |
38 | const text = await response.text(); |
39 | throw new Error(`${response.status} ${text}`); |
40 | } |
41 | return await response.json(); |
42 | } |
43 |
|