import { StandardSchemaV1 } from "@zap-studio/validation"; import { Logger } from "@zap-studio/logger"; //#region src/types.d.ts /** * Accepted `fetch` input type (`string`, `URL`, or `Request`). * * @example * const input: FetchInput = "/users/1"; * const withUrl: FetchInput = new URL("https://api.example.com/users/1"); */ type FetchInput = Parameters[0]; type URLSearchParamsInput = ConstructorParameters[0]; type RequestBodyInit = RequestInit & { json?: never; }; type JsonBodyInit = Omit & { /** * JSON body convenience. When provided, this is JSON-stringified into `body`. * @default undefined */ json: unknown; body?: never; }; interface CustomRequestInit { /** * Per-request query/search params * @default undefined */ searchParams?: URLSearchParamsInput; /** * Whether to throw a FetchError on HTTP errors (non-2xx responses) * @default true */ throwOnFetchError?: boolean; /** * Whether to throw a ValidationError on validation errors * @default true */ throwOnValidationError?: boolean; } /** * Extended RequestInit type to include custom fetch options * * @example * const options: ExtendedRequestInit = { * method: "POST", * json: { name: "Ada" }, * throwOnFetchError: true, * }; */ type ExtendedRequestInit = (RequestBodyInit | JsonBodyInit) & CustomRequestInit; /** * Internal defaults used by fetchInternal * * @example * const defaults: FetchDefaults = { * baseURL: "https://api.example.com", * throwOnFetchError: true, * throwOnValidationError: true, * }; */ interface FetchDefaults { /** * Base URL to prepend to all requests * @default "" */ baseURL: string; /** * Default headers to include in all requests (can be overridden per request) * @default undefined */ headers?: HeadersInit; /** * Default query/search params applied to every request (can be overridden per request) * @default undefined */ searchParams?: URLSearchParamsInput; /** * Whether to throw a `FetchError` on HTTP errors (non-2xx responses) * @default true */ throwOnFetchError: boolean; /** * Whether to throw a `ValidationError` on validation errors * @default true */ throwOnValidationError: boolean; /** * Optional logger for request/response internals. When omitted, nothing * is logged. * * Logs outgoing requests at `debug`, response status at `debug` (2xx) or * `warn` (non-2xx), and validation failures at `error`. */ logger?: Logger; } /** * Type-safe fetch function with Standard Schema validation support * * @example * import { z } from "zod"; * * const UserSchema = z.object({ id: z.number(), name: z.string() }); * const fetchUser: $Fetch = $fetch; * const user = await fetchUser("/users/1", UserSchema); */ interface $Fetch { /** * Fetch with schema validation and throwOnValidationError: false * @param input - URL or path to fetch * @param schema - Standard Schema for response validation * @param options - Extended request options with throwOnValidationError: false * @returns Standard Schema Result object with value or issues * @throws {FetchError} When `throwOnFetchError` is `true` and the response is not ok. * @throws {TypeError} When request construction, JSON request serialization, headers, * search params, native `fetch`, or `response.json()` body reading fail with a * `TypeError`. * @throws {DOMException} When native `fetch` or `response.json()` rejects an aborted * request/body read as an `AbortError` DOMException. * @throws {SyntaxError} When `response.json()` cannot parse the response body. * @throws {unknown} Any error thrown or rejected by the provided Standard Schema validator. */ (input: FetchInput, schema: TSchema, options: ExtendedRequestInit & { throwOnValidationError: false; }): Promise>>; /** * Fetch with schema validation and throwOnValidationError: true or undefined (default) * @param input - URL or path to fetch * @param schema - Standard Schema for response validation * @param options - Extended request options * @returns Validated data of type TSchema * @throws {FetchError} When `throwOnFetchError` is `true` and the response is not ok. * @throws {ValidationError} When validation returns issues. * @throws {TypeError} When request construction, JSON request serialization, headers, * search params, native `fetch`, or `response.json()` body reading fail with a * `TypeError`. * @throws {DOMException} When native `fetch` or `response.json()` rejects an aborted * request/body read as an `AbortError` DOMException. * @throws {SyntaxError} When `response.json()` cannot parse the response body. * @throws {unknown} Any error thrown or rejected by the provided Standard Schema validator. */ (input: FetchInput, schema: TSchema, options?: ExtendedRequestInit & { throwOnValidationError?: true; }): Promise>; /** * Fetch without schema validation * @param input - URL or path to fetch * @param options - Extended request options * @returns Raw Response object * @throws {FetchError} When `throwOnFetchError` is `true` and the response is not ok. * @throws {TypeError} When request construction, JSON request serialization, headers, * search params, or native `fetch` fail with a `TypeError`. * @throws {DOMException} When native `fetch` rejects an aborted request as an * `AbortError` DOMException. */ (input: FetchInput, options?: ExtendedRequestInit): Promise; } /** * Normalized representation used by internal request execution. * * @example * const normalized: NormalizedRequest = { * url: "https://api.example.com/users", * options: {}, * }; */ interface NormalizedRequest { /** Resolved string URL from the input (string or `URL`; `Request` uses `request.url`). */ url: string; /** Original `Request` clone, present when the input was a `Request`. */ request?: Request; /** Normalized request options merged with `Request` headers. */ options: ExtendedRequestInit; } /** * API HTTP method-specific fetch functions * * @example * const user = await api.get("/users/1", UserSchema); */ interface ApiMethods { /** * DELETE method fetch function */ delete: $Fetch; /** * GET method fetch function */ get: $Fetch; /** * PATCH method fetch function */ patch: $Fetch; /** * POST method fetch function */ post: $Fetch; /** * PUT method fetch function */ put: $Fetch; } /** * Configured fetch instance returned by `createFetch(...)`. * * @example * const { $fetch, api } = createFetch({ baseURL: "https://api.example.com" }); */ interface FetchInstance { /** * Configured `$fetch` function. */ $fetch: $Fetch; /** * Configured HTTP method-specific fetch functions. */ api: ApiMethods; } //#endregion export { $Fetch, ApiMethods, ExtendedRequestInit, FetchDefaults, FetchInput, FetchInstance, NormalizedRequest }; //# sourceMappingURL=types.d.ts.map