0
List packages for a user
One script reply has been approved by the moderators Verified

Lists all packages in a user's namespace for which the requesting user has access.

Created by hugo697 448 days ago Viewed 11400 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 448 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * List packages for a user
6
 * Lists all packages in a user's namespace for which the requesting user has access.
7
 */
8
export async function main(
9
  auth: Github,
10
  username: string,
11
  package_type:
12
    | "npm"
13
    | "maven"
14
    | "rubygems"
15
    | "docker"
16
    | "nuget"
17
    | "container"
18
    | undefined,
19
  visibility: "public" | "private" | "internal" | undefined
20
) {
21
  const url = new URL(`https://api.github.com/users/${username}/packages`);
22
  for (const [k, v] of [
23
    ["package_type", package_type],
24
    ["visibility", visibility],
25
  ]) {
26
    if (v !== undefined && v !== "") {
27
      url.searchParams.append(k, v);
28
    }
29
  }
30
  const response = await fetch(url, {
31
    method: "GET",
32
    headers: {
33
      Authorization: "Bearer " + auth.token,
34
    },
35
    body: undefined,
36
  });
37
  if (!response.ok) {
38
    const text = await response.text();
39
    throw new Error(`${response.status} ${text}`);
40
  }
41
  return await response.json();
42
}
43