List packages for the authenticated user's namespace

Lists packages owned by the authenticated user within the user's namespace.

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 packages for the authenticated user's namespace
6
 * Lists packages owned by the authenticated user within the user's namespace.
7
 */
8
export async function main(
9
  auth: Github,
10
  package_type:
11
    | "npm"
12
    | "maven"
13
    | "rubygems"
14
    | "docker"
15
    | "nuget"
16
    | "container"
17
    | undefined,
18
  visibility: "public" | "private" | "internal" | undefined
19
) {
20
  const url = new URL(`https://api.github.com/user/packages`);
21
  for (const [k, v] of [
22
    ["package_type", package_type],
23
    ["visibility", visibility],
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