|
| 1 | +// Import Third-party Dependencies |
| 2 | +import { request } from "undici"; |
| 3 | +import linkHeader from "http-link-header"; |
| 4 | +import combineAsyncIterators from "combine-async-iterators"; |
| 5 | + |
| 6 | +// Import Internal Dependencies |
| 7 | +import type { |
| 8 | + Repository, |
| 9 | + UserOrg |
| 10 | +} from "./github.types.ts"; |
| 11 | + |
| 12 | +// CONSTANTS |
| 13 | +const kGithubURL = new URL("https://api.github.com/"); |
| 14 | + |
| 15 | +export interface FetchOptions { |
| 16 | + /** |
| 17 | + * @default fetch-github-repo |
| 18 | + */ |
| 19 | + agent?: string; |
| 20 | + token?: string | null; |
| 21 | + /** |
| 22 | + * @default users |
| 23 | + */ |
| 24 | + kind?: "users" | "orgs"; |
| 25 | + /** |
| 26 | + * Fetch the repositories of all orgs for a given user |
| 27 | + * @default false |
| 28 | + */ |
| 29 | + fetchUserOrgs?: boolean; |
| 30 | +} |
| 31 | + |
| 32 | +export async function* fetchLazy( |
| 33 | + namespace: string, |
| 34 | + options: FetchOptions = Object.create(null) |
| 35 | +): AsyncIterableIterator<Repository> { |
| 36 | + if (typeof namespace !== "string") { |
| 37 | + throw new TypeError("namespace argument must be typeof string"); |
| 38 | + } |
| 39 | + |
| 40 | + const { |
| 41 | + agent = "fetch-github-repo", |
| 42 | + token = null, |
| 43 | + kind = "users", |
| 44 | + fetchUserOrgs = false |
| 45 | + } = options; |
| 46 | + |
| 47 | + const headers = { |
| 48 | + "User-Agent": agent, |
| 49 | + Accept: "application/vnd.github.v3.raw", |
| 50 | + ...(typeof token === "string" ? { Authorization: `token ${token}` } : {}) |
| 51 | + }; |
| 52 | + |
| 53 | + let currURL: string | null = `${kind}/${namespace}/repos`; |
| 54 | + while (currURL) { |
| 55 | + const { body, headers: currHeaders } = await request( |
| 56 | + new URL(currURL, kGithubURL), |
| 57 | + { headers } |
| 58 | + ); |
| 59 | + yield* (await body.json() as Repository[]); |
| 60 | + |
| 61 | + currURL = getNextPageUrl(currHeaders); |
| 62 | + } |
| 63 | + |
| 64 | + if (kind === "users" && fetchUserOrgs) { |
| 65 | + const { body } = await request( |
| 66 | + new URL(`users/${namespace}/orgs`, kGithubURL), |
| 67 | + { headers } |
| 68 | + ); |
| 69 | + const data = await body.json() as UserOrg[]; |
| 70 | + |
| 71 | + const iterators = data.map( |
| 72 | + (row) => fetchLazy(row.login, { agent, token, kind: "orgs" }) |
| 73 | + ); |
| 74 | + for await (const repo of combineAsyncIterators(...iterators)) { |
| 75 | + yield repo; |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +export async function fetch( |
| 81 | + namespace: string, |
| 82 | + options: FetchOptions = Object.create(null) |
| 83 | +): Promise<Repository[]> { |
| 84 | + const repositories: Repository[] = []; |
| 85 | + for await (const repo of fetchLazy(namespace, options)) { |
| 86 | + repositories.push(repo); |
| 87 | + } |
| 88 | + |
| 89 | + return repositories; |
| 90 | +} |
| 91 | + |
| 92 | +function getNextPageUrl( |
| 93 | + headers: Record<string, string | string[] | undefined> |
| 94 | +): string | null { |
| 95 | + const linkValue = headers.link; |
| 96 | + if (linkValue === undefined) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + |
| 100 | + const { refs } = linkHeader.parse(linkValue.toString()); |
| 101 | + |
| 102 | + return refs.find( |
| 103 | + (row) => row.rel === "next" || row.rel === "last" |
| 104 | + )?.uri ?? null; |
| 105 | +} |
0 commit comments