List repositories starred by a user

Lists repositories a 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 a user
6
 * Lists repositories a 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
  username: string,
13
  sort: "created" | "updated" | undefined,
14
  direction: "asc" | "desc" | undefined,
15
  per_page: string | undefined,
16
  page: string | undefined
17
) {
18
  const url = new URL(`https://api.github.com/users/${username}/starred`);
19
  for (const [k, v] of [
20
    ["sort", sort],
21
    ["direction", direction],
22
    ["per_page", per_page],
23
    ["page", page],
24
  ]) {
25
    if (v !== undefined && v !== "") {
26
      url.searchParams.append(k, v);
27
    }
28
  }
29
  const response = await fetch(url, {
30
    method: "GET",
31
    headers: {
32
      Authorization: "Bearer " + auth.token,
33
    },
34
    body: undefined,
35
  });
36
  if (!response.ok) {
37
    const text = await response.text();
38
    throw new Error(`${response.status} ${text}`);
39
  }
40
  return await response.json();
41
}
42