1 | |
2 | type Netlify = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Create hook by site id |
7 | * |
8 | */ |
9 | export async function main( |
10 | auth: Netlify, |
11 | site_id: string | undefined, |
12 | body: { |
13 | id?: string; |
14 | site_id?: string; |
15 | type?: string; |
16 | event?: string; |
17 | data?: {}; |
18 | created_at?: string; |
19 | updated_at?: string; |
20 | disabled?: false | true; |
21 | }, |
22 | ) { |
23 | const url = new URL(`https://api.netlify.com/api/v1/hooks`); |
24 | for (const [k, v] of [["site_id", site_id]]) { |
25 | if (v !== undefined && v !== "" && k !== undefined) { |
26 | url.searchParams.append(k, v); |
27 | } |
28 | } |
29 | const response = await fetch(url, { |
30 | method: "POST", |
31 | headers: { |
32 | "Content-Type": "application/json", |
33 | Authorization: "Bearer " + auth.token, |
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 |
|