0

List Gists

by
Published today

List a user's public gists, or the authenticated user's gists (including secret ones) when username is empty. Paginate with page.

Script github Verified

The script

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

3
/**
4
 * List Gists
5
 * List a user's public gists, or the authenticated user's gists (including secret ones) when username is empty. Paginate with page.
6
 */
7
export async function main(
8
  auth: RT.Github,
9
  username: string | undefined,
10
  since: string | undefined,
11
  per_page: number = 30,
12
  page: number = 1
13
) {
14
  const url = new URL(
15
    username
16
      ? `https://api.github.com/users/${username}/gists`
17
      : "https://api.github.com/gists"
18
  )
19
  for (const [k, v] of Object.entries({ since, per_page, page })) {
20
    if (v !== undefined && v !== "") url.searchParams.append(k, String(v))
21
  }
22

23
  const response = await fetch(url, {
24
    headers: {
25
      Authorization: `Bearer ${auth.token}`,
26
      Accept: "application/vnd.github+json",
27
      "X-GitHub-Api-Version": "2022-11-28",
28
    },
29
  })
30

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

35
  return await response.json()
36
}
37