Get a repository README

Gets the preferred README for a repository. READMEs support [custom media types](https://docs.github.com/rest/reference/repos#custom-media-types) for retrieving the raw content or rendered HTML.

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
 * Get a repository README
6
 * Gets the preferred README for a repository.
7

8
READMEs support [custom media types](https://docs.github.com/rest/reference/repos#custom-media-types) for retrieving the raw content or rendered HTML.
9
 */
10
export async function main(
11
  auth: Github,
12
  owner: string,
13
  repo: string,
14
  ref: string | undefined
15
) {
16
  const url = new URL(`https://api.github.com/repos/${owner}/${repo}/readme`);
17
  for (const [k, v] of [["ref", ref]]) {
18
    if (v !== undefined && v !== "") {
19
      url.searchParams.append(k, v);
20
    }
21
  }
22
  const response = await fetch(url, {
23
    method: "GET",
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.json();
34
}
35