List package versions for a package owned by the authenticated user

Lists package versions for a package owned by the authenticated 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 package versions for a package owned by the authenticated user
6
 * Lists package versions for a package owned by the authenticated user.
7
 */
8
export async function main(
9
  auth: Github,
10
  package_type: "npm" | "maven" | "rubygems" | "docker" | "nuget" | "container",
11
  package_name: string,
12
  page: string | undefined,
13
  per_page: string | undefined,
14
  state: "active" | "deleted" | undefined
15
) {
16
  const url = new URL(
17
    `https://api.github.com/user/packages/${package_type}/${package_name}/versions`
18
  );
19
  for (const [k, v] of [
20
    ["page", page],
21
    ["per_page", per_page],
22
    ["state", state],
23
  ]) {
24
    if (v !== undefined && v !== "") {
25
      url.searchParams.append(k, v);
26
    }
27
  }
28
  const response = await fetch(url, {
29
    method: "GET",
30
    headers: {
31
      Authorization: "Bearer " + auth.token,
32
    },
33
    body: undefined,
34
  });
35
  if (!response.ok) {
36
    const text = await response.text();
37
    throw new Error(`${response.status} ${text}`);
38
  }
39
  return await response.json();
40
}
41