List repositories for a user

Lists public repositories for the specified user. Note: For GitHub AE, this endpoint will list internal repositories for the specified user.

Script github Verified

by hugo697 ยท 10/25/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 367 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * List repositories for a user
6
 * Lists public repositories for the specified user. Note: For GitHub AE, this endpoint will list internal repositories for the specified user.
7
 */
8
export async function main(
9
  auth: Github,
10
  username: string,
11
  type: "all" | "owner" | "member" | undefined,
12
  sort: "created" | "updated" | "pushed" | "full_name" | undefined,
13
  direction: "asc" | "desc" | undefined,
14
  per_page: string | undefined,
15
  page: string | undefined
16
) {
17
  const url = new URL(`https://api.github.com/users/${username}/repos`);
18
  for (const [k, v] of [
19
    ["type", type],
20
    ["sort", sort],
21
    ["direction", direction],
22
    ["per_page", per_page],
23
    ["page", page],
24
  ]) {
25
    if (v !== undefined && v !== "") {
26
      url.searchParams.append(k, v);
27
    }
28
  }
29
  const response = await fetch(url, {
30
    method: "GET",
31
    headers: {
32
      Authorization: "Bearer " + auth.token,
33
    },
34
    body: undefined,
35
  });
36
  if (!response.ok) {
37
    const text = await response.text();
38
    throw new Error(`${response.status} ${text}`);
39
  }
40
  return await response.json();
41
}
42