Get a package for a user

Gets a specific package metadata for a public package owned by a user.

Script github Verified

by hugo697 ยท 10/25/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 366 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * Get a package for a user
6
 * Gets a specific package metadata for a public package owned by a user.
7
 */
8
export async function main(
9
  auth: Github,
10
  package_type: "npm" | "maven" | "rubygems" | "docker" | "nuget" | "container",
11
  package_name: string,
12
  username: string
13
) {
14
  const url = new URL(
15
    `https://api.github.com/users/${username}/packages/${package_type}/${package_name}`
16
  );
17

18
  const response = await fetch(url, {
19
    method: "GET",
20
    headers: {
21
      Authorization: "Bearer " + auth.token,
22
    },
23
    body: undefined,
24
  });
25
  if (!response.ok) {
26
    const text = await response.text();
27
    throw new Error(`${response.status} ${text}`);
28
  }
29
  return await response.json();
30
}
31