Download a repository archive (tar)

Gets a redirect URL to download a tar archive for a repository. If you omit `:ref`, the repository’s default branch (usually `main`) will be used. Please make sure your HTTP framework is configured to follow redirects or you will need to use the `Location` header to make a second `GET` request. **Note**: For private repositories, these links are temporary and expire after five minutes.

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
 * Download a repository archive (tar)
6
 * Gets a redirect URL to download a tar archive for a repository. If you omit `:ref`, the repository’s default branch (usually
7
`main`) will be used. Please make sure your HTTP framework is configured to follow redirects or you will need to use
8
the `Location` header to make a second `GET` request.
9
**Note**: For private repositories, these links are temporary and expire after five minutes.
10
 */
11
export async function main(
12
  auth: Github,
13
  owner: string,
14
  repo: string,
15
  ref: string
16
) {
17
  const url = new URL(
18
    `https://api.github.com/repos/${owner}/${repo}/tarball/${ref}`
19
  );
20

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