@jay-js/system
Version:
A powerful and flexible TypeScript library for UI, state management, lazy loading, routing and managing draggable elements in modern web applications.
51 lines (50 loc) • 1.38 kB
TypeScript
import type { TQueryFetcher, TQueryKey, TQueryOptions, TQueryStore } from "./types.js";
/**
* Creates a reactive query for async data fetching with caching and automatic revalidation
*
* @param key Unique identifier for the query, can be reactive function
* @param fetcher Async function to fetch data
* @param options Query configuration options
* @returns Query store with reactive states and control methods
*
* @example
* Basic usage:
* ```typescript
* const usersQuery = query('users', async () => {
* const res = await fetch('/api/users');
* return res.json();
* });
*
* // Use in component
* effect(() => {
* if (usersQuery.data.value) {
* console.log(usersQuery.data.value);
* }
* });
* ```
*
* @example
* With options:
* ```typescript
* const todosQuery = query('todos', fetchTodos, {
* staleTime: 5000,
* cacheTime: 300000,
* refetchOnFocus: true,
* retry: 3
* });
* ```
*
* @example
* Reactive key:
* ```typescript
* const userId = state(1);
* const userQuery = query(
* () => `user-${userId.value}`,
* async () => {
* const res = await fetch(`/api/users/${userId.value}`);
* return res.json();
* }
* );
* ```
*/
export declare function query<TData = unknown, TError = Error>(key: TQueryKey, fetcher: TQueryFetcher<TData>, options?: TQueryOptions<TData>): TQueryStore<TData, TError>;