pr-monitor/server/github/rest.ts
2026-07-29 09:24:20 +00:00

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;
}