0
Create a gist
One script reply has been approved by the moderators Verified

Allows you to add a new gist with one or more files.

Note: Don't name your files "gistfile" with a numerical suffix. This is the format of the automatic naming scheme that Gist uses internally.

Created by hugo697 583 days ago Viewed 22510 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 583 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * Create a gist
6
 * Allows you to add a new gist with one or more files.
7

8
**Note:** Don't name your files "gistfile" with a numerical suffix. This is the format of the automatic naming scheme that Gist uses internally.
9
 */
10
export async function main(
11
  auth: Github,
12
  body: {
13
    description?: string;
14
    files: { [k: string]: { content: string; [k: string]: unknown } };
15
    public?: boolean | ("true" | "false");
16
    [k: string]: unknown;
17
  }
18
) {
19
  const url = new URL(`https://api.github.com/gists`);
20

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