* Handle GitHub API requests using fixtures.
( url: string, storage: ReturnType<typeof useStorage>, )
| 415 | * Handle GitHub API requests using fixtures. |
| 416 | */ |
| 417 | async function handleGitHubApi( |
| 418 | url: string, |
| 419 | storage: ReturnType<typeof useStorage>, |
| 420 | ): Promise<MockResult | null> { |
| 421 | const urlObj = URL.parse(url) |
| 422 | if (!urlObj) return null |
| 423 | |
| 424 | const { host, pathname } = urlObj |
| 425 | |
| 426 | if (host !== 'api.github.com') return null |
| 427 | |
| 428 | // Contributors stats endpoint: /repos/{owner}/{repo}/stats/contributors |
| 429 | const contributorsStatsMatch = pathname.match(/^\/repos\/([^/]+)\/([^/]+)\/stats\/contributors$/) |
| 430 | if (contributorsStatsMatch) { |
| 431 | const contributorsStats = await storage.getItem<unknown[]>( |
| 432 | FIXTURE_PATHS.githubContributorsStats, |
| 433 | ) |
| 434 | if (contributorsStats) { |
| 435 | return { data: contributorsStats } |
| 436 | } |
| 437 | return { data: [] } |
| 438 | } |
| 439 | |
| 440 | // Contributors endpoint: /repos/{owner}/{repo}/contributors |
| 441 | const contributorsMatch = pathname.match(/^\/repos\/([^/]+)\/([^/]+)\/contributors$/) |
| 442 | if (contributorsMatch) { |
| 443 | const contributors = await storage.getItem<unknown[]>(FIXTURE_PATHS.githubContributors) |
| 444 | if (contributors) { |
| 445 | return { data: contributors } |
| 446 | } |
| 447 | // Return empty array if no fixture exists |
| 448 | return { data: [] } |
| 449 | } |
| 450 | |
| 451 | // Commits endpoint: /repos/{owner}/{repo}/commits |
| 452 | const commitsMatch = pathname.match(/^\/repos\/([^/]+)\/([^/]+)\/commits$/) |
| 453 | if (commitsMatch) { |
| 454 | // Return a single-item array; fetchPageCount will use body.length when no Link header |
| 455 | return { data: [{ sha: 'mock-commit' }] } |
| 456 | } |
| 457 | |
| 458 | // Search endpoint: /search/issues, /search/commits, etc. |
| 459 | const searchMatch = pathname.match(/^\/search\/(.+)$/) |
| 460 | if (searchMatch) { |
| 461 | return { data: { total_count: 0, incomplete_results: false, items: [] } } |
| 462 | } |
| 463 | |
| 464 | // Other GitHub API endpoints can be added here as needed |
| 465 | return null |
| 466 | } |
| 467 | |
| 468 | interface FixtureMatchWithVersion extends FixtureMatch { |
| 469 | version?: string // 'latest', a semver version, or undefined for full packument |