0
Search repositories
One script reply has been approved by the moderators Verified

Find repositories via various criteria.

Created by hugo697 406 days ago Viewed 9018 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 406 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * Search repositories
6
 * Find repositories via various criteria.
7
 */
8
export async function main(
9
  auth: Github,
10
  q: string | undefined,
11
  sort: "stars" | "forks" | "help-wanted-issues" | "updated" | undefined,
12
  order: "desc" | "asc" | undefined,
13
  per_page: string | undefined,
14
  page: string | undefined
15
) {
16
  const url = new URL(`https://api.github.com/search/repositories`);
17
  for (const [k, v] of [
18
    ["q", q],
19
    ["sort", sort],
20
    ["order", order],
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