List repositories starred by the authenticated user

Lists repositories the authenticated user has starred. You can also find out _when_ stars were created by passing the following custom [media type](https://docs.github.com/rest/overview/media-types/) via the `Accept` header: `application/vnd.github.star+json`.

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
 * List repositories starred by the authenticated user
6
 * Lists repositories the authenticated user has starred.
7

8
You can also find out _when_ stars were created by passing the following custom [media type](https://docs.github.com/rest/overview/media-types/) via the `Accept` header: `application/vnd.github.star+json`.
9
 */
10
export async function main(
11
  auth: Github,
12
  sort: "created" | "updated" | undefined,
13
  direction: "asc" | "desc" | undefined,
14
  per_page: string | undefined,
15
  page: string | undefined
16
) {
17
  const url = new URL(`https://api.github.com/user/starred`);
18
  for (const [k, v] of [
19
    ["sort", sort],
20
    ["direction", direction],
21
    ["per_page", per_page],
22
    ["page", page],
23
  ]) {
24
    if (v !== undefined && v !== "") {
25
      url.searchParams.append(k, v);
26
    }
27
  }
28
  const response = await fetch(url, {
29
    method: "GET",
30
    headers: {
31
      Authorization: "Bearer " + auth.token,
32
    },
33
    body: undefined,
34
  });
35
  if (!response.ok) {
36
    const text = await response.text();
37
    throw new Error(`${response.status} ${text}`);
38
  }
39
  return await response.json();
40
}
41