import type { ApiErrorCode } from "@umt/contracts"; import type { z } from "zod"; import { ApiRequestError, apiFetch, type FetcherArg } from "~/lib/api.ts"; export { ApiRequestError }; /** 请求并用 contracts 的 zod schema 校验 data;失败抛 ApiRequestError(给 react-query 的 error 态) */ export async function apiParsed(schema: S, path: string, arg?: FetcherArg): Promise> { const r = await apiFetch(path, arg); if (!r.ok) throw new ApiRequestError(r.error.code, r.error.message); const parsed = schema.safeParse(r.data); if (!parsed.success) { const issue = parsed.error.issues[0]; throw new ApiRequestError( "internal" satisfies ApiErrorCode, `响应格式不符合接口定义:${issue ? `${issue.path.join(".")} ${issue.message}` : "未知字段"}`, ); } return parsed.data; } /** 变更请求:返回 data,失败抛错(给 useMutation,由 onError 统一 toast) */ export async function apiMutate(path: string, arg: FetcherArg = {}): Promise { const r = await apiFetch(path, { method: "POST", ...arg }); if (!r.ok) throw new ApiRequestError(r.error.code, r.error.message); return r.data; } export const errorCode = (err: unknown): string | null => (err instanceof ApiRequestError ? err.code : null); /** 端点是否不存在(后端尚未提供) */ export const isMissingEndpoint = (err: unknown) => errorCode(err) === "not_found"; /** 查询字符串:去掉空值 */ export function cleanParams(p: Record) { const out: Record = {}; for (const [k, v] of Object.entries(p)) if (v !== undefined && v !== null && v !== "") out[k] = v; return out; }