export interface ApiKeySummary { id: number; name: string; token_prefix: string; created_at: string; updated_at: string; last_used_at: string | null; expires_at: string | null; revoked_at: string | null; } export interface CreateApiKeyResponse { token: string; apiKey: ApiKeySummary; } async function handleResponse(response: Response): Promise { if (!response.ok) { const errorBody = await response.json().catch(() => null); const message = errorBody?.error || 'Request failed'; throw new Error(message); } return (await response.json()) as T; } export async function fetchApiKeys(): Promise { const response = await fetch('/api/profile/api-keys', { credentials: 'include', }); return handleResponse(response); } export async function createApiKey(payload: { name: string; expires_at?: string | null; }): Promise { const response = await fetch('/api/profile/api-keys', { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify(payload), }); return handleResponse(response); } export async function revokeApiKey(id: number): Promise { const response = await fetch(`/api/profile/api-keys/${id}/revoke`, { method: 'POST', credentials: 'include', }); return handleResponse(response); } export async function deleteApiKey(id: number): Promise { const response = await fetch(`/api/profile/api-keys/${id}`, { method: 'DELETE', credentials: 'include', }); if (!response.ok) { const errorBody = await response.json().catch(() => null); const message = errorBody?.error || 'Failed to delete API key'; throw new Error(message); } }