0
Create a GitHub Pages site
One script reply has been approved by the moderators Verified

Configures a GitHub Pages site. For more information, see "About GitHub Pages."

To use this endpoint, you must be a repository administrator, maintainer, or have the 'manage GitHub Pages settings' permission. A token with the repo scope or Pages write permission is required. GitHub Apps must have the administration:write and pages:write permissions.

Created by hugo697 200 days ago Viewed 6202 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 200 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * Create a GitHub Pages site
6
 * Configures a GitHub Pages site. For more information, see "About GitHub Pages."
7

8
To use this endpoint, you must be a repository administrator, maintainer, or have the 'manage GitHub Pages settings' permission. A token with the `repo` scope or Pages write permission is required. GitHub Apps must have the `administration:write` and `pages:write` permissions.
9
 */
10
export async function main(
11
  auth: Github,
12
  owner: string,
13
  repo: string,
14
  body: { [k: string]: unknown } & {
15
    build_type?: "legacy" | "workflow";
16
    source?: { branch: string; path?: "/" | "/docs"; [k: string]: unknown };
17
    [k: string]: unknown;
18
  }
19
) {
20
  const url = new URL(`https://api.github.com/repos/${owner}/${repo}/pages`);
21

22
  const response = await fetch(url, {
23
    method: "POST",
24
    headers: {
25
      "Content-Type": "application/json",
26
      Authorization: "Bearer " + auth.token,
27
    },
28
    body: JSON.stringify(body),
29
  });
30
  if (!response.ok) {
31
    const text = await response.text();
32
    throw new Error(`${response.status} ${text}`);
33
  }
34
  return await response.json();
35
}
36