UNPKG

@knowmax/genericlist-core

Version:

Knowmax Generic list with basic CRUD support without any user interface implementation.

95 lines (94 loc) 3.36 kB
import type { DependencyList } from "react"; import { FetchError } from "@knowmax/http-utils"; import type { IListResponse } from "../types"; /** * Configuration object for the useSimpleList hook. */ type TSimpleListConfiguration = { /** The API endpoint URL to fetch data from */ endpoint: string; /** Bearer token for authentication */ token: string; /** Optional ordering specification (e.g., "name ASC", "date DESC") */ order?: string; /** Optional filter criteria for the query */ filter?: string; /** Whether to include total count in the response */ count?: boolean; /** Current page number (1-based, defaults to 1) */ page?: number; /** Number of items per page (defaults to 10) */ pageSize?: number; }; /** * Return type for the useSimpleList hook. */ export interface ISimpleListHookResult<T> { /** The fetched list response containing data and metadata */ result?: IListResponse<T>; /** Loading state indicator */ isLoading: boolean; /** Error object if the request failed */ error?: FetchError | Error; /** Http status code */ status?: number; /** Function to manually refetch the data */ refetch: () => Promise<void>; } /** * A simplified React hook for fetching paginated list data from an API endpoint. * * This hook is designed as a lightweight alternative to the more complex `useList` hook, * providing essential list functionality without advanced features like caching, * state management, or CRUD operations. * * **Key Features:** * - Simple pagination support * - Automatic request abortion on unmount/dependency changes * - Built-in loading and error states * - TypeScript support with generic type parameter * - Manual refetch capability * * **Use Cases:** * - Read-only data lists * - Simple pagination scenarios * - When you don't need advanced caching or state management * - Lightweight components with minimal list requirements * * @template T - The type of objects in the list * @param configuration - Configuration object containing endpoint, authentication, and query parameters * @param dependencies - Optional React dependencies array to trigger refetch when values change * @returns An object containing the list result, loading state, error state, and refetch function * * @example * ```tsx * // Basic usage * const { result, isLoading, error, refetch } = useSimpleList<User>({ * endpoint: '/api/users', * token: 'your-auth-token', * pageSize: 20, * page: 1 * }); * * // With filtering and ordering * const { result, isLoading, error } = useSimpleList<Product>({ * endpoint: '/api/products', * token: authToken, * filter: 'category eq "electronics"', * order: 'name ASC', * count: true * }); * * // With dependencies * const [searchTerm, setSearchTerm] = useState(''); * const { result, isLoading } = useSimpleList<Article>({ * endpoint: '/api/articles', * token: authToken, * filter: searchTerm ? `title contains "${searchTerm}"` : undefined * }, [searchTerm]); * ``` * * @see {@link useList} for the full-featured alternative with caching and CRUD operations */ export declare const useSimpleList: <T extends Record<string, any>>(configuration: TSimpleListConfiguration, dependencies?: DependencyList) => ISimpleListHookResult<T>; export {};