use-fetch-with-callbacks
Version:
A powerful React hook for HTTP requests with comprehensive callback support, request chaining, and TypeScript integration
35 lines (34 loc) • 1.26 kB
TypeScript
import type { UseFetchOptions, FetchResult } from './types';
/**
* A React hook that provides fetch functionality with callback support for success, error, and loading states
*
* @template T - The type of data expected from the API response
* @param endpoint - The API endpoint path to make requests to
* @param options - Optional configuration including base URL and headers
* @returns An object containing response data, loading state, error state, and request methods
*
* @example
* Basic usage:
* ```typescript
* const { response, loading, error, fetchData } = useFetchWithCallbacks<User>('/users/1', {
* baseUrl: 'https://api.example.com',
* headers: { 'Authorization': 'Bearer token' }
* });
*
* // Fetch data with callbacks
* fetchData(
* (data) => console.log('Success:', data),
* (error) => console.error('Error:', error),
* (loading) => console.log('Loading:', loading)
* );
* ```
*
* @example
* Chaining operations with multiple endpoints:
* ```typescript
* const { chain } = useFetchWithCallbacks<User>('/users/1', {
* baseUrl: 'https://api.example.com'
* });
*/
declare const useFetchWithCallbacks: <T>(endpoint: string, options?: UseFetchOptions) => FetchResult<T>;
export default useFetchWithCallbacks;