Restore a package for the authenticated user

Restores a package owned by the authenticated 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
 * Restore a package for the authenticated user
6
 * Restores 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
  token: string | undefined
13
) {
14
  const url = new URL(
15
    `https://api.github.com/user/packages/${package_type}/${package_name}/restore`
16
  );
17
  for (const [k, v] of [["token", token]]) {
18
    if (v !== undefined && v !== "") {
19
      url.searchParams.append(k, v);
20
    }
21
  }
22
  const response = await fetch(url, {
23
    method: "POST",
24
    headers: {
25
      Authorization: "Bearer " + auth.token,
26
    },
27
    body: undefined,
28
  });
29
  if (!response.ok) {
30
    const text = await response.text();
31
    throw new Error(`${response.status} ${text}`);
32
  }
33
  return await response.text();
34
}
35