UNPKG

use-fetch-smart

Version:

A smart React data-fetching hook with caching, retries, TTL, schema validation, and automatic token refresh.

101 lines (91 loc) 2.86 kB
import * as react_jsx_runtime from 'react/jsx-runtime'; import React from 'react'; import { AxiosInstance } from 'axios'; interface SchemaValidator<T = any> { parse: (data: unknown) => T; } type SchemaMode = "error" | "warn"; interface FetchOptions { cache?: boolean; persist?: boolean; token?: string; baseURL?: string; ttl?: number; } interface FetchState<T> { data: T | null; loading: boolean; error: unknown | null; } interface SmartConfig { baseURL?: string; token?: string; retryLimit?: number; refreshToken?: () => Promise<string | null> | null; } declare function useGetSmart<T = any>(url: string, opts?: { cacheTimeMs?: number; persist?: boolean; swr?: boolean; schema?: SchemaValidator<T>; schemaMode?: SchemaMode; prefetchNext?: (data: T, ctx: { url: string; }) => Array<{ url: string; schema?: SchemaValidator<any>; schemaMode?: SchemaMode; ttlMs?: number; persist?: boolean; }>; }): { data: T | null; loading: boolean; error: any; refetch: () => Promise<void>; }; declare function usePostSmart<T = any, Body = any>(url: string, opts?: { schema?: SchemaValidator<T>; schemaMode?: SchemaMode; }): { mutate: (body: Body) => Promise<any>; data: T | null; loading: boolean; error: any; }; declare function usePutSmart<T = any, Body = any>(url: string, opts?: { schema?: SchemaValidator<T>; schemaMode?: SchemaMode; }): { mutate: (body: Body) => Promise<any>; data: T | null; loading: boolean; error: any; }; declare function useDeleteSmart<T = any>(url: string, opts?: { schema?: SchemaValidator<T>; schemaMode?: SchemaMode; }): { mutate: () => Promise<any>; data: T | null; loading: boolean; error: any; }; declare const setGlobalToken: (token: string | null) => void; declare const createSmartAxios: (baseURL: string, refreshTokenFn?: () => Promise<string | null>, retryLimit?: number) => AxiosInstance; interface FetchSmartConfig { baseURL?: string; token?: string; retryLimit?: number; refreshToken?: () => Promise<string | null>; } interface FetchSmartContextValue { axiosInstance: ReturnType<typeof createSmartAxios>; } declare const FetchSmartProvider: ({ config, children, }: { config: FetchSmartConfig; children: React.ReactNode; }) => react_jsx_runtime.JSX.Element; declare const useFetchSmartContext: () => FetchSmartContextValue; declare const FetchSmartDevtools: () => react_jsx_runtime.JSX.Element | null; export { type FetchOptions, type FetchSmartConfig, FetchSmartDevtools, FetchSmartProvider, type FetchState, type SchemaMode, type SchemaValidator, type SmartConfig, createSmartAxios, setGlobalToken, useDeleteSmart, useFetchSmartContext, useGetSmart, usePostSmart, usePutSmart };