Files
clawborn/src/lib/stats.ts
2026-03-17 17:53:14 +08:00

38 lines
976 B
TypeScript

export type StatsResponse = {
total: number;
byTemplate: Record<string, number>;
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<StatsResponse> {
const response = await fetch(buildStatsApiUrl("/api/stats"));
if (!response.ok) {
throw new Error("获取统计信息失败。");
}
return response.json();
}
export async function incrementGenerationCount(templateId: string): Promise<StatsResponse> {
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();
}