ai-code-battle/cmd/acb-indexer/src/api.ts
jedarden 4bbc3f0515 Complete Phase 4: Add index builder container
- Add /api/data/export endpoint to worker-api for data export
- Create cmd/acb-indexer/ TypeScript container:
  - API client for fetching data from Worker API
  - Index generator for leaderboard, bot profiles, match index
  - File writer for outputting JSON files
  - Optional Cloudflare Pages deploy support
  - Unit tests (6 tests)
- Update PROGRESS.md to mark Phase 4 complete

Phase 4 is now complete. All exit criteria met:
- Matchmaker cron creates jobs in D1
- Workers claim and execute matches
- Replays land in R2
- Results flow into D1
- Ratings update via Glicko-2
- Leaderboard.json rebuilds automatically
- Stale job reaper recovers from worker disappearance

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-24 08:39:47 -04:00

42 lines
1 KiB
TypeScript

// API Client for fetching data from Worker API
import type { ApiClientConfig, ExportData } from './types.js';
export class ApiClient {
private apiUrl: string;
private apiKey: string;
constructor(config: ApiClientConfig) {
this.apiUrl = config.apiUrl.replace(/\/$/, '');
this.apiKey = config.apiKey;
}
/**
* Fetch all data needed for index building
*/
async fetchExportData(): Promise<ExportData> {
const response = await fetch(`${this.apiUrl}/api/data/export`, {
headers: {
'X-API-Key': this.apiKey,
'Accept': 'application/json',
},
});
if (!response.ok) {
const text = await response.text();
throw new Error(`API request failed: ${response.status} - ${text}`);
}
const result = await response.json() as { success: boolean; data?: ExportData; error?: string };
if (!result.success) {
throw new Error(`API returned error: ${result.error}`);
}
if (!result.data) {
throw new Error('API returned no data');
}
return result.data;
}
}