0
List forks
One script reply has been approved by the moderators Verified
Created by hugo697 173 days ago Viewed 3733 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 173 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * List forks
6
 *
7
 */
8
export async function main(
9
  auth: Github,
10
  owner: string,
11
  repo: string,
12
  sort: "newest" | "oldest" | "stargazers" | "watchers" | undefined,
13
  per_page: string | undefined,
14
  page: string | undefined
15
) {
16
  const url = new URL(`https://api.github.com/repos/${owner}/${repo}/forks`);
17
  for (const [k, v] of [
18
    ["sort", sort],
19
    ["per_page", per_page],
20
    ["page", page],
21
  ]) {
22
    if (v !== undefined && v !== "") {
23
      url.searchParams.append(k, v);
24
    }
25
  }
26
  const response = await fetch(url, {
27
    method: "GET",
28
    headers: {
29
      Authorization: "Bearer " + auth.token,
30
    },
31
    body: undefined,
32
  });
33
  if (!response.ok) {
34
    const text = await response.text();
35
    throw new Error(`${response.status} ${text}`);
36
  }
37
  return await response.json();
38
}
39