export type StatsResponse = { total: number; byTemplate: Record; updatedAt: string | null; }; const configuredStatsApiBase = (import.meta.env.VITE_STATS_API_BASE || "").replace(/\/$/, ""); function buildStatsApiUrl(path: string) { return configuredStatsApiBase ? `${configuredStatsApiBase}${path}` : path; } export async function fetchStats(): Promise { const response = await fetch(buildStatsApiUrl("/api/stats")); if (!response.ok) { throw new Error("获取统计信息失败。"); } return response.json(); } export async function incrementGenerationCount(templateId: string): Promise { const response = await fetch(buildStatsApiUrl("/api/generate-count"), { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ templateId }), }); if (!response.ok) { throw new Error("记录生成次数失败。"); } return response.json(); }