Get a gitignore template

The API also allows fetching the source of a single template. Use the raw [media type](https://docs.github.com/rest/overview/media-types/) to get the raw contents.

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 gitignore template
6
 * The API also allows fetching the source of a single template.
7
Use the raw [media type](https://docs.github.com/rest/overview/media-types/) to get the raw contents.
8
 */
9
export async function main(auth: Github, name: string) {
10
  const url = new URL(`https://api.github.com/gitignore/templates/${name}`);
11

12
  const response = await fetch(url, {
13
    method: "GET",
14
    headers: {
15
      Authorization: "Bearer " + auth.token,
16
    },
17
    body: undefined,
18
  });
19
  if (!response.ok) {
20
    const text = await response.text();
21
    throw new Error(`${response.status} ${text}`);
22
  }
23
  return await response.json();
24
}
25