Support redirects in fetchJson, add tests for it (#11993)

This commit is contained in:
Jacob MacDonald
2025-10-24 15:35:09 -07:00
committed by GitHub
parent d91484eb4d
commit cdff69b7b2
2 changed files with 215 additions and 1 deletions

View File

@@ -10,7 +10,10 @@ export function getGitHubToken(): string | undefined {
return process.env['GITHUB_TOKEN'];
}
export async function fetchJson<T>(url: string): Promise<T> {
export async function fetchJson<T>(
url: string,
redirectCount: number = 0,
): Promise<T> {
const headers: { 'User-Agent': string; Authorization?: string } = {
'User-Agent': 'gemini-cli',
};
@@ -21,6 +24,18 @@ export async function fetchJson<T>(url: string): Promise<T> {
return new Promise((resolve, reject) => {
https
.get(url, { headers }, (res) => {
if (res.statusCode === 302 || res.statusCode === 301) {
if (redirectCount >= 10) {
return reject(new Error('Too many redirects'));
}
if (!res.headers.location) {
return reject(new Error('No location header in redirect response'));
}
fetchJson<T>(res.headers.location!, redirectCount++)
.then(resolve)
.catch(reject);
return;
}
if (res.statusCode !== 200) {
return reject(
new Error(`Request failed with status code ${res.statusCode}`),