import type { ConversationSummary, ConversationDetail, UsageInfo, } from "./types"; export class ApiError extends Error { public constructor( public readonly status: number, message: string, ) { super(message); } } function getConfig(): { apiUrl: string; apiKey: string } { return window.flexibleChatHistory; } function getApiUrl(endpoint: string): string { const config = getConfig(); return `${config.apiUrl}${endpoint}`; } function getHeaders(): HeadersInit { const config = getConfig(); return { "Content-Type": "application/json", Authorization: `Bearer ${config.apiKey}`, }; } export async function fetchConversations(): Promise { const response = await fetch(getApiUrl("/conversations"), { headers: getHeaders(), }); if (!response.ok) { throw new ApiError(response.status, "Failed to fetch conversations"); } return response.json(); } export async function fetchConversation( id: string, ): Promise { const response = await fetch(getApiUrl(`/conversations/${id}`), { headers: getHeaders(), }); if (!response.ok) { throw new ApiError(response.status, "Failed to fetch conversation"); } return response.json(); } export async function fetchUsage(): Promise { const response = await fetch(getApiUrl("/usage"), { headers: getHeaders(), }); if (!response.ok) { throw new ApiError(response.status, "Failed to fetch usage"); } return response.json(); }