This commit is contained in:
eric
2026-04-02 07:15:42 -05:00
parent 644a6ac3a9
commit d401a36b71
62 changed files with 13264 additions and 78 deletions

29
services/api-client.ts Normal file
View File

@@ -0,0 +1,29 @@
import { z, ZodSchema } from "zod";
interface ApiClientOptions<TSchema extends ZodSchema> {
endpoint: string;
schema: TSchema;
init?: RequestInit;
}
export async function requestWithSchema<TSchema extends ZodSchema>({
endpoint,
schema,
init,
}: ApiClientOptions<TSchema>): Promise<z.infer<TSchema>> {
const res = await fetch(endpoint, {
...init,
headers: {
"Content-Type": "application/json",
...(init?.headers ?? {}),
},
next: { revalidate: 60 },
});
if (!res.ok) {
throw new Error(`API 请求失败: ${res.status} ${res.statusText}`);
}
const json = await res.json();
return schema.parse(json);
}