import createClient, { type Client, type ClientOptions } from "openapi-fetch";
import type { paths } from "./schema";
/** The typed API client. `paths` is generated from the backend's spec (`make web/sync`). */
export type ApiClient = Client<paths>;
/** `/api` is the backend: proxied to `:8080` by Vite in development. */
export function createApiClient(options: ClientOptions = {}): ApiClient {
return createClient<paths>({ baseUrl: "/api", ...options });
}
/**
* The machine-readable code of a failed call: the `ErrorResponse.name` the API answered, or
* `http.<status>` for a body that is not one (a proxy error, poem's own `400`).
*/
export function apiErrorCode(error: unknown, response: Response): string {
if (typeof error === "object" && error !== null && "name" in error) {
const { name } = error;
if (typeof name === "string") return name;
}
return `http.${response.status}`;
}