import createFetchClient from "openapi-fetch"; import createClient from "openapi-react-query"; import type { paths } from "@/client/lib/openapi-types"; // baseUrl is empty because the generated `paths` already start with `/api/v1/` // (openapi-typescript emits paths verbatim from the OpenAPI document). export const fetchClient = createFetchClient({ baseUrl: "", credentials: "include", }); export const $api = createClient(fetchClient); export function isApiError(value: unknown): value is { error: { code: string; message: string } } { if (!value || typeof value !== "object") return false; const err = (value as { error?: unknown }).error; if (!err || typeof err !== "object") return false; const obj = err as { code?: unknown; message?: unknown }; return typeof obj.code === "string" && typeof obj.message === "string"; } export function apiErrorMessage(value: unknown, fallback = "Something went wrong."): string { if (isApiError(value)) return value.error.message; if (value instanceof Error) return value.message; return fallback; }