import { FetchError } from "./errors.js"; import { $Fetch, ApiMethods, ExtendedRequestInit, FetchDefaults, FetchInput, FetchInstance, NormalizedRequest } from "./types.js"; import { StandardSchemaV1 } from "@zap-studio/validation"; //#region src/index.d.ts /** * Default options for the global $fetch * * These defaults are used by the top-level `$fetch` export. * Use `createFetch(...)` when you need per-client defaults. * * @example * import { GLOBAL_DEFAULTS } from "@zap-studio/fetch"; * * console.log(GLOBAL_DEFAULTS.throwOnFetchError); // true */ declare const GLOBAL_DEFAULTS: FetchDefaults; /** * Type-safe fetch wrapper with Standard Schema validation. * * - When `throwOnValidationError: true`: validated data of type `TSchema` * - When `throwOnValidationError: false`: Standard Schema Result object `{ value?, issues? }` * - When `throwOnFetchError: true`: throws `FetchError` on non-ok responses * * If no schema is provided, returns the raw `Response` object. * * @throws {FetchError} When `throwOnFetchError` is `true` and the response is not ok. * @throws {ValidationError} When a schema is provided, validation returns issues, and * `throwOnValidationError` is `true`. * @throws {TypeError} When both `body` and `json` are provided, when JSON request * serialization fails, when request construction fails, when headers/search params are * invalid, when `response.json()` cannot read the body, or when the runtime `fetch` * implementation rejects network-level failures as `TypeError`. * @throws {DOMException} When the runtime rejects an aborted request or response body read * as an `AbortError` DOMException. * @throws {SyntaxError} When a schema is provided and `response.json()` cannot parse the * response body. * @throws {unknown} Any error thrown or rejected by the provided Standard Schema validator. * * @example * import { z } from "zod"; * import { $fetch } from "@zap-studio/fetch"; * * const UserSchema = z.object({ id: z.number(), name: z.string() }); * * // Basic usage (schema validation) * const user = await $fetch("/api/users/1", UserSchema, { headers: { "Authorization": "Bearer token" } }); * console.log("Validated user:", user); * * // Raw usage (no schema validation and typed Response object) * const result = await $fetch("/api/data", { method: "POST", body: JSON.stringify({ key: "value" }) }); * const json = await result.json() as ResultType; * console.log("Raw response data:", json); * * // Usage with validation errors returned instead of thrown * const result = await $fetch("/api/users/1", UserSchema, { throwOnValidationError: false }); * * if (result.issues) { * console.error("Validation errors:", result.issues); * } else { * console.log("Validated user:", result.value); * } */ declare function $fetch(input: FetchInput, schema: TSchema, options: ExtendedRequestInit & { throwOnValidationError: false; }): Promise>>; declare function $fetch(input: FetchInput, schema: TSchema, options?: ExtendedRequestInit & { throwOnValidationError?: true; }): Promise>; declare function $fetch(input: FetchInput, options?: ExtendedRequestInit): Promise; /** * Convenience methods for common HTTP verbs. * * These methods always require a schema for validation. * For raw responses without validation, use `$fetch` directly. * * Each method has the same throw behavior as {@link $fetch}. * * @example * import { z } from "zod"; * import { api } from "@zap-studio/fetch"; * * const PostSchema = z.object({ * id: z.number(), * title: z.string(), * content: z.string(), * }); * * async function fetchPost(postId: number) { * const post = await api.get(`https://api.example.com/posts/${postId}`, PostSchema); * return post; // post is typed as { id: number; title: string; content: string; } * } */ declare const 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. * * The returned `$fetch` and `api` methods have the same throw behavior as the * top-level {@link $fetch} export. * * @example * 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", json: { name: "John" } }); */ declare const createFetch: (factoryOptions?: Partial) => FetchInstance; //#endregion export { type $Fetch, $fetch, type ApiMethods, type ExtendedRequestInit, type FetchDefaults, FetchError, type FetchInput, type FetchInstance, GLOBAL_DEFAULTS, type NormalizedRequest, api, createFetch }; //# sourceMappingURL=index.d.ts.map