createFetch(factoryOptions?: CreateFetchOptions): { $fetch: $Fetch; api: ApiMethods; }
Creates a custom fetch instance with pre-configured defaults.
Use this factory to create API clients with a base URL, default headers, and other shared configuration. Each instance is independent.
import { z } from "zod";
import { createFetch } from "@zap-studio/fetch";
import { z } from "zod"; import { createFetch } from "@zap-studio/fetch";
// Create a configured instance const { $fetch, api } = createFetch({ baseURL: "https://api.example.com", headers: { "Authorization": "Bearer token" }, });
const UserSchema = z.object({ id: z.number(), name: z.string() });
// Now use relative paths - baseURL is prepended automatically const user = await api.get("/users/1", UserSchema);
// Or use $fetch directly const response = await $fetch("/users", UserSchema, { method: "POST", body: { name: "John" } });
factoryOptions: CreateFetchOptions
{ $fetch: $Fetch; api: ApiMethods; }