0

Create Gist

by
Published today

Create a gist from a map of file name to file content. OAuth connections need the gist scope.

Script github Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 1 hour ago
1
//native
2

3
/**
4
 * Create Gist
5
 * Create a gist from a map of file name to file content. OAuth connections need the gist scope.
6
 */
7
export async function main(
8
  auth: RT.Github,
9
  files: { [filename: string]: string },
10
  description: string | undefined,
11
  public_gist: boolean = false
12
) {
13
  const response = await fetch("https://api.github.com/gists", {
14
    method: "POST",
15
    headers: {
16
      Authorization: `Bearer ${auth.token}`,
17
      "Content-Type": "application/json",
18
      Accept: "application/vnd.github+json",
19
      "X-GitHub-Api-Version": "2022-11-28",
20
    },
21
    body: JSON.stringify({
22
      description,
23
      public: public_gist,
24
      files: Object.fromEntries(
25
        Object.entries(files).map(([name, content]) => [name, { content }])
26
      ),
27
    }),
28
  })
29

30
  if (!response.ok) {
31
    throw new Error(`${response.status} ${await response.text()}`)
32
  }
33

34
  return await response.json()
35
}
36