import { z, ZodSchema } from "zod"; interface ApiClientOptions { endpoint: string; schema: TSchema; init?: RequestInit; } export async function requestWithSchema({ endpoint, schema, init, }: ApiClientOptions): Promise> { 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); }