import { getTokenAsync } from "../github/auth.ts"; import type { UnifiedRepo } from "../types/index.ts"; export async function fetchReadme( repo: UnifiedRepo, ): Promise<{ content: string | null; error: string | null }> { if (repo.localPath) { try { const file = Bun.file(`${repo.localPath}/README.md`); if (await file.exists()) { const content = await file.text(); return { content, error: null }; } } catch { // Expected: README may not exist locally } } if (repo.github) { try { const token = await getTokenAsync(); const headers: Record = { Accept: "application/vnd.github.raw", }; if (token) { headers["Authorization"] = `Bearer ${token}`; } const response = await fetch( `https://api.github.com/repos/${repo.github.fullName}/readme`, { headers }, ); if (response.ok) { const content = await response.text(); return { content, error: null }; } } catch { // Expected: repo may not have a README on GitHub } } return { content: null, error: "README not found" }; }