1 | |
2 | type Netlify = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Create site deploy |
7 | * |
8 | */ |
9 | export async function main( |
10 | auth: Netlify, |
11 | site_id: string, |
12 | deploy_previews: string | undefined, |
13 | production: string | undefined, |
14 | state: |
15 | | "new" |
16 | | "pending_review" |
17 | | "accepted" |
18 | | "rejected" |
19 | | "enqueued" |
20 | | "building" |
21 | | "uploading" |
22 | | "uploaded" |
23 | | "preparing" |
24 | | "prepared" |
25 | | "processing" |
26 | | "processed" |
27 | | "ready" |
28 | | "error" |
29 | | "retrying" |
30 | | undefined, |
31 | branch: string | undefined, |
32 | latest_published: string | undefined, |
33 | title: string | undefined, |
34 | body: { |
35 | files?: {}; |
36 | draft?: false | true; |
37 | async?: false | true; |
38 | functions?: {}; |
39 | function_schedules?: { name?: string; cron?: string }[]; |
40 | functions_config?: {}; |
41 | branch?: string; |
42 | framework?: string; |
43 | framework_version?: string; |
44 | }, |
45 | ) { |
46 | const url = new URL( |
47 | `https://api.netlify.com/api/v1/sites/${site_id}/deploys`, |
48 | ); |
49 | for (const [k, v] of [ |
50 | ["deploy-previews", deploy_previews], |
51 | ["production", production], |
52 | ["state", state], |
53 | ["branch", branch], |
54 | ["latest-published", latest_published], |
55 | ["title", title], |
56 | ]) { |
57 | if (v !== undefined && v !== "" && k !== undefined) { |
58 | url.searchParams.append(k, v); |
59 | } |
60 | } |
61 | const response = await fetch(url, { |
62 | method: "POST", |
63 | headers: { |
64 | "Content-Type": "application/json", |
65 | Authorization: "Bearer " + auth.token, |
66 | }, |
67 | body: JSON.stringify(body), |
68 | }); |
69 | if (!response.ok) { |
70 | const text = await response.text(); |
71 | throw new Error(`${response.status} ${text}`); |
72 | } |
73 | return await response.json(); |
74 | } |
75 |
|