Update a gist

Allows you to update a gist's description and to update, delete, or rename gist files. Files from the previous version of the gist that aren't explicitly changed during an edit are unchanged.

Script github Verified

by hugo697 ยท 10/25/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 366 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * Update a gist
6
 * Allows you to update a gist's description and to update, delete, or rename gist files. Files from the previous version of the gist that aren't explicitly changed during an edit are unchanged.
7
 */
8
export async function main(
9
  auth: Github,
10
  gist_id: string,
11
  body: { [k: string]: unknown } & {
12
    description?: string;
13
    files?: { [k: string]: { [k: string]: unknown } };
14
    [k: string]: unknown;
15
  }
16
) {
17
  const url = new URL(`https://api.github.com/gists/${gist_id}`);
18

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