UNPKG

@stacksjs/stx

Version:

A performant UI Framework. Powered by Bun.

84 lines 2.36 kB
/** * Reactive fetch with loading/error states */ export declare function useFetch<T = unknown>(url: string | (() => string), options?: FetchOptions<T>): FetchRef<T>; /** * Generic async data fetcher */ export declare function useAsyncData<T>(fetcher: () => Promise<T>, options?: { immediate?: boolean transform?: (data: T) => T default?: T key?: string cacheTime?: number }): FetchRef<T>; /** * POST request helper */ export declare function usePost<T = unknown, B = unknown>(url: string, options?: Omit<FetchOptions<T>, 'method' | 'body'>): { execute: (body?: B) => Promise<T | null> state: FetchRef<T> }; /** * Clear fetch cache */ export declare function clearFetchCache(key?: string): void; /** * Prefetch data into cache */ export declare function prefetch<T>(url: string, options?: FetchOptions<T>): Promise<void>; /** * useFetch - Reactive data fetching with loading/error states * * Provides a streamlined API for fetching data with automatic state management. * * @example * ```ts * // Basic fetch * const { data, loading, error, refresh } = useFetch('/api/users') * * // With options * const { data } = useFetch('/api/users', { * method: 'POST', * body: { name: 'John' }, * immediate: false, // Don't fetch immediately * }) * * // Async data with transform * const { data } = useAsyncData( * () => fetch('/api/users').then(r => r.json()), * { transform: (data) => data.users } * ) * ``` */ export declare interface FetchOptions<T = unknown> extends Omit<RequestInit, 'body'> { body?: BodyInit | Record<string, unknown> | null baseURL?: string query?: Record<string, string | number | boolean | undefined> immediate?: boolean transform?: (data: unknown) => T parseResponse?: (response: Response) => Promise<unknown> timeout?: number retry?: number retryDelay?: number key?: string cacheTime?: number refetchOnFocus?: boolean refetchInterval?: number default?: T } export declare interface FetchState<T> { data: T | null loading: boolean error: Error | null status: number | null statusText: string | null } export declare interface FetchRef<T> { get: () => FetchState<T> subscribe: (fn: (state: FetchState<T>) => void) => () => void refresh: () => Promise<void> execute: () => Promise<void> abort: () => void isLoading: () => boolean }