/** * Async data and fetch composables built on bQuery signals. * * @module bquery/reactive */ import { type BqueryFetchParseAs } from '../platform/config'; import { Signal } from './core'; /** Allowed status values for async composables. */ export type AsyncDataStatus = 'idle' | 'pending' | 'success' | 'error'; /** Reactive source types that can trigger refreshes. */ export type AsyncWatchSource = (() => unknown) | { value: unknown; }; /** Options shared by async composables. */ export interface UseAsyncDataOptions { /** Run the handler immediately (default: true). */ immediate?: boolean; /** Default data value before the first successful execution. */ defaultValue?: TData; /** Optional reactive sources that trigger refreshes when they change. */ watch?: AsyncWatchSource[]; /** Transform the resolved value before storing it. */ transform?: (value: TResult) => TData; /** Called after a successful execution. */ onSuccess?: (value: TData) => void; /** Called after a failed execution. */ onError?: (error: Error) => void; } /** Return value of useAsyncData() and useFetch(). */ export interface AsyncDataState { /** Reactive data signal. */ data: Signal; /** Last error encountered by the composable. */ error: Signal; /** Current lifecycle status. */ status: Signal; /** Computed boolean that mirrors `status === 'pending'`. */ pending: { readonly value: boolean; peek(): boolean; }; /** Execute the handler manually. Returns the cached data value when called after dispose(). */ execute: () => Promise; /** Alias for execute(). */ refresh: () => Promise; /** Abort the current in-flight request (useFetch only; no-op for useAsyncData). */ abort: () => void; /** Clear data, error, and status back to the initial state. */ clear: () => void; /** Dispose reactive watchers and prevent future executions. */ dispose: () => void; } /** Configuration for automatic request retries in useFetch(). */ export interface UseFetchRetryConfig { /** Maximum number of retry attempts (default: 3). */ count: number; /** Delay in ms between retries, or a function receiving the attempt index. */ delay?: number | ((attempt: number) => number); /** Predicate deciding whether to retry. Defaults to network / 5xx errors. */ retryOn?: (error: Error, attempt: number) => boolean; } /** Options for useFetch(). */ export interface UseFetchOptions extends UseAsyncDataOptions, Omit { /** Base URL prepended to relative URLs. */ baseUrl?: string; /** Query parameters appended to the request URL. */ query?: Record; /** Request headers. */ headers?: HeadersInit; /** Request body, including plain objects for JSON requests. */ body?: BodyInit | Record | unknown[] | null; /** Override the parser used for the response body. */ parseAs?: BqueryFetchParseAs; /** Custom fetch implementation for testing or adapters. */ fetcher?: typeof fetch; /** Request timeout in milliseconds. 0 means no timeout. */ timeout?: number; /** External AbortSignal for request cancellation. */ signal?: AbortSignal; /** Retry configuration. Pass a number for simple retry count, or a config object. */ retry?: number | UseFetchRetryConfig; /** Custom status validation. Returns `true` for acceptable statuses. */ validateStatus?: (status: number) => boolean; } /** Input accepted by useFetch(). */ export type FetchInput = string | URL | Request | (() => string | URL | Request); /** * Create a reactive wrapper around an async resolver. * * @template TResult - Raw result type returned by the handler * @template TData - Stored data type after optional transformation * @param handler - Async function to execute * @param options - Execution, transform, and refresh options * @returns Reactive data state with execute(), refresh(), and clear() * * @example * ```ts * const user = useAsyncData(() => fetch('/api/user').then((res) => res.json())); * ``` */ export declare const useAsyncData: (handler: () => Promise, options?: UseAsyncDataOptions) => AsyncDataState; /** * Reactive fetch composable using the browser Fetch API. * * Supports timeout, abort, retry, and custom status validation in addition * to the core useFetch features (query params, JSON body, baseUrl, watch). * * @template TResponse - Raw parsed response type * @template TData - Stored response type after optional transformation * @param input - Request URL, Request object, or lazy input factory * @param options - Request and reactive state options * @returns Reactive fetch state with execute(), refresh(), abort(), clear(), and dispose() * * @example * ```ts * const users = useFetch<{ id: number; name: string }[]>('/api/users', { * timeout: 5000, * retry: 3, * }); * ``` */ export declare const useFetch: (input: FetchInput, options?: UseFetchOptions) => AsyncDataState; /** * Create a preconfigured useFetch() helper. * * @param defaults - Default request options merged into every useFetch() call * @returns A useFetch-compatible function with merged defaults * * @example * ```ts * const useApiFetch = createUseFetch({ baseUrl: 'https://api.example.com' }); * const profile = useApiFetch('/profile'); * ``` */ /** Overload for factories without a configured transform, preserving per-call `TResponse -> TData` inference. */ export declare function createUseFetch(defaults?: UseFetchOptions): (input: FetchInput, options?: UseFetchOptions) => AsyncDataState; /** Overload for factories with a configured transform, preserving the transformed factory data type by default. */ export declare function createUseFetch(defaults: UseFetchOptions): (input: FetchInput, options?: UseFetchOptions) => AsyncDataState; //# sourceMappingURL=async-data.d.ts.map