0

List Repositories

by
Published 20 days ago

List repositories the authenticated user can access (owned, collaborator, and organization member). Paginate with page.

Script github Verified

The script

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

3
/**
4
 * List Repositories
5
 * List repositories the authenticated user can access (owned, collaborator, and organization member). Paginate with page.
6
 */
7
export async function main(
8
  auth: RT.Github,
9
  visibility: "all" | "public" | "private" | undefined,
10
  affiliation: string | undefined,
11
  sort: "created" | "updated" | "pushed" | "full_name" | undefined,
12
  per_page: number = 30,
13
  page: number = 1
14
) {
15
  const url = new URL("https://api.github.com/user/repos")
16
  for (const [k, v] of Object.entries({
17
    visibility,
18
    affiliation,
19
    sort,
20
    per_page,
21
    page,
22
  })) {
23
    if (v != null && v !== "") url.searchParams.append(k, String(v))
24
  }
25

26
  const response = await fetch(url, {
27
    headers: {
28
      Authorization: `Bearer ${auth.token}`,
29
      Accept: "application/vnd.github+json",
30
      "X-GitHub-Api-Version": "2022-11-28",
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