Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DgBEW6qAwgn2fcQbA2f4ZQ
30 lines
992 B
TypeScript
30 lines
992 B
TypeScript
import { getToken } from './token.js';
|
|
import { GithubError } from './client.js';
|
|
|
|
const BASE = 'https://api.github.com';
|
|
|
|
export async function restJson<T>(path: string): Promise<T> {
|
|
const res = await fetch(`${BASE}${path}`, {
|
|
headers: {
|
|
authorization: `bearer ${getToken()}`,
|
|
accept: 'application/vnd.github+json',
|
|
'user-agent': 'pr-monitor',
|
|
},
|
|
});
|
|
if (!res.ok) {
|
|
throw new GithubError(`GitHub REST ${path}: ${res.status}`);
|
|
}
|
|
return res.json() as Promise<T>;
|
|
}
|
|
|
|
/** Download an artifact zip (follows the storage redirect). */
|
|
export async function restZip(path: string, maxBytes: number): Promise<Uint8Array | null> {
|
|
const res = await fetch(`${BASE}${path}`, {
|
|
headers: { authorization: `bearer ${getToken()}`, 'user-agent': 'pr-monitor' },
|
|
});
|
|
if (!res.ok) throw new GithubError(`GitHub REST ${path}: ${res.status}`);
|
|
const buf = new Uint8Array(await res.arrayBuffer());
|
|
if (buf.byteLength > maxBytes) return null;
|
|
return buf;
|
|
}
|