With openapi-fetch
openapi-fetch returns { data, error, response } — not a status-discriminated union. Use the adapter to normalize it.
import { createExpectStatus, adapters } from "expect-status";
export const expectStatus = createExpectStatus({ adapter: adapters.openapiClient, fallbackMessage: "Request failed.", defaults: { 401: "Please sign in.", "5xx": "Service unavailable.", },});Basic usage
Section titled “Basic usage”import createClient from "openapi-fetch";import type { paths } from "./api/schema";import { expectStatus } from "@/lib/expect-status";
const client = createClient<paths>({ baseUrl: "https://api.example.com" });
const user = await expectStatus( 200, client.GET("/users/{id}", { params: { path: { id: "1" } } }),);POST with dispatch
Section titled “POST with dispatch”const org = await expectStatus( 201, client.POST("/orgs", { body: { name: "Acme" } }), { 409: "Organisation already exists.", 422: "Please check your input.", },);With TanStack Query
Section titled “With TanStack Query”function useUser(id: string) { return useQuery({ queryKey: ["user", id], queryFn: () => expectStatus( 200, client.GET("/users/{id}", { params: { path: { id } } }), ), });}
function useCreateOrg() { return useMutation({ mutationFn: (data: { name: string }) => expectStatus(201, client.POST("/orgs", { body: data }), { 409: ({ message }) => ({ error: message }), 422: "Invalid input.", }), });}SafeResult
Section titled “SafeResult”const result = await expectStatus( 200, client.GET("/users/{id}", { params: { path: { id } } }), { throws: false },);
if (result.ok) { renderUser(result.data);} else { showError(result.error.message);}Alternative: manual wrapping
Section titled “Alternative: manual wrapping”If you don’t want an adapter instance, wrap per-call:
import { expectStatus } from "expect-status";
const { data, error, response } = await client.GET("/users/{id}", { params: { path: { id: "1" } },});
const user = await expectStatus(200, { status: response.status, body: data ?? error,});See also
Section titled “See also”- openapi-fetch documentation
- With TanStack Query — query and mutation patterns
- Custom Envelope — adapter details