完成
This commit is contained in:
37
src/lib/stats.ts
Normal file
37
src/lib/stats.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
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();
|
||||
}
|
||||
Reference in New Issue
Block a user