0

Update Gist

by
Published today

Update a gist's description or files. Files map a file name to its new content; files left out are kept, and a null content deletes the file. 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
 * Update Gist
5
 * Update a gist's description or files. Files map a file name to its new content; files left out are kept, and a null content deletes the file. OAuth connections need the gist scope.
6
 */
7
export async function main(
8
  auth: RT.Github,
9
  gist_id: string,
10
  description: string | undefined,
11
  files: { [filename: string]: string | null } | undefined
12
) {
13
  const response = await fetch(`https://api.github.com/gists/${gist_id}`, {
14
    method: "PATCH",
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
      files:
24
        files &&
25
        Object.fromEntries(
26
          Object.entries(files).map(([name, content]) => [
27
            name,
28
            content === null ? null : { content },
29
          ])
30
        ),
31
    }),
32
  })
33

34
  if (!response.ok) {
35
    throw new Error(`${response.status} ${await response.text()}`)
36
  }
37

38
  return await response.json()
39
}
40