( endpoint: "code" | "repositories", query: string, page: number, perPage: number, label: string, )
| 124 | } |
| 125 | |
| 126 | async function githubSearch<T>( |
| 127 | endpoint: "code" | "repositories", |
| 128 | query: string, |
| 129 | page: number, |
| 130 | perPage: number, |
| 131 | label: string, |
| 132 | ): Promise<{ total_count: number; items: T[] } | null> { |
| 133 | while (true) { |
| 134 | const url = `https://api.github.com/search/${endpoint}?q=${encodeURIComponent(query)}&per_page=${perPage}&page=${page}`; |
| 135 | const res = await fetch(url, { |
| 136 | cache: "no-store", |
| 137 | headers: { |
| 138 | Accept: "application/vnd.github.v3+json", |
| 139 | ...authHeaders(), |
| 140 | }, |
| 141 | }); |
| 142 | |
| 143 | if (res.status === 403 || res.status === 429) { |
| 144 | const retry = Number(res.headers.get("retry-after") ?? "30"); |
| 145 | console.warn(` ${label}: 429 rate-limited, sleeping ${retry}s`); |
| 146 | await sleep(retry * 1000); |
| 147 | continue; |
| 148 | } |
| 149 | if (res.status === 422) { |
| 150 | // Query syntax error or hit the 1000-result cap mid-pagination. |
| 151 | return null; |
| 152 | } |
| 153 | if (!res.ok) { |
| 154 | const body = await res.text(); |
| 155 | throw new Error( |
| 156 | `${endpoint} search failed for "${query}": ${res.status} ${res.statusText} ${body.slice(0, 200)}`, |
| 157 | ); |
| 158 | } |
| 159 | |
| 160 | const data = (await res.json()) as { |
| 161 | total_count?: number; |
| 162 | items?: T[]; |
| 163 | }; |
| 164 | |
| 165 | await respectRateLimit(res, label); |
| 166 | |
| 167 | return { |
| 168 | total_count: data.total_count ?? 0, |
| 169 | items: data.items ?? [], |
| 170 | }; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | async function githubCodeSearch( |
| 175 | query: string, |
nothing calls this directly
no test coverage detected