(token: string, endpoint: string, init?: RequestInit)
| 14 | } |
| 15 | |
| 16 | async function githubFetch(token: string, endpoint: string, init?: RequestInit) { |
| 17 | const baseUrl = 'https://api.github.com'; |
| 18 | const response = await fetch(`${baseUrl}${endpoint}`, { |
| 19 | ...init, |
| 20 | headers: { |
| 21 | Accept: 'application/vnd.github+json', |
| 22 | Authorization: `Bearer ${token}`, |
| 23 | 'User-Agent': 'Claudable-Next', |
| 24 | ...init?.headers, |
| 25 | }, |
| 26 | }); |
| 27 | |
| 28 | const contentType = response.headers.get('content-type') ?? ''; |
| 29 | const isJson = contentType.includes('application/json'); |
| 30 | const body: any = response.status === 204 |
| 31 | ? null |
| 32 | : isJson |
| 33 | ? await response.json().catch(() => null) |
| 34 | : await response.text(); |
| 35 | |
| 36 | if (!response.ok) { |
| 37 | let message = 'GitHub API request failed'; |
| 38 | if (body) { |
| 39 | if (typeof body === 'string') { |
| 40 | message = body; |
| 41 | } else if (typeof body === 'object') { |
| 42 | const errorMessage = (body as Record<string, unknown>).message; |
| 43 | const errors = (body as Record<string, unknown>).errors; |
| 44 | if (typeof errorMessage === 'string' && errorMessage.trim().length > 0) { |
| 45 | message = errorMessage; |
| 46 | } else if (Array.isArray(errors) && errors.length > 0) { |
| 47 | const aggregated = errors |
| 48 | .map((err) => (err && typeof err === 'object' ? (err as Record<string, unknown>).message : null)) |
| 49 | .filter((value): value is string => typeof value === 'string' && value.trim().length > 0) |
| 50 | .join(', '); |
| 51 | if (aggregated) { |
| 52 | message = aggregated; |
| 53 | } |
| 54 | } else { |
| 55 | message = JSON.stringify(body); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | throw new GitHubError(message, response.status); |
| 60 | } |
| 61 | |
| 62 | return body; |
| 63 | } |
| 64 | |
| 65 | export async function getGithubUser(): Promise<GitHubUserInfo> { |
| 66 | const token = await getPlainServiceToken('github'); |
no outgoing calls
no test coverage detected