es-fetcher
Version:
Enhance your frontend application's API implementation and calls with es-fetcher. Simplify and accelerate the process like never before.
75 lines (74 loc) • 3 kB
TypeScript
import { clearMemoryCache, deleteMemoryCache, deleteMemoryCaches } from './fetch-memory-cache';
/**
* Options for API requests made with es-fetch.
*
* @interface FetchOptions
*/
interface FetchOptions {
body?: BodyInit | null;
cache?: 'default' | 'force-cache' | 'no-cache' | 'no-store' | 'only-if-cached' | 'reload' | 'memory-cache';
credentials?: 'include' | 'omit' | 'same-origin';
headers?: [string, string][] | Record<string, string> | Headers;
integrity?: string;
keepalive?: boolean;
method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS' | 'CONNECT';
mode?: 'cors' | 'navigate' | 'no-cors' | 'same-origin';
redirect?: 'error' | 'follow' | 'manual';
referrer?: string;
referrerPolicy?: ReferrerPolicy;
}
/**
* Configuration options for the es-fetch package.
*
* @interface FetchConfiguration
*/
interface FetchConfiguration {
baseUrl?: string;
accessToken?: string;
authMethod?: 'bearer';
cache?: FetchOptions['cache'];
credentials?: FetchOptions['credentials'];
headers?: FetchOptions['headers'];
mode?: FetchOptions['mode'];
}
/**
* Global configuration fetch.
*
* @type {FetchConfiguration}
*/
export declare let configuration: FetchConfiguration;
/**
* Configures the global fetch settings.
*
* @param {FetchConfiguration} fetchConfiguration - Global fetch settings, as defined in the `FetchConfiguration` interface.
*/
declare const configureFetcher: (fetchConfiguration: FetchConfiguration) => void;
/**
* Create an absolute URL based on the base URL and provided URL.
*
* @param {string} url - Relative or absolute URL.
* @param {string} [base] - Optional base URL.
* @returns {string} - The absolute URL.
*/
declare const createAbsoluteUrl: (url: string, base?: string) => string;
/**
* Makes fetch request using the provided URL and options and returns the fetched data.
*
* @param {string} url - The URL to fetch data from. It can be either an absolute or relative URL.
* @param {FetchOptions} [options] - Custom options for the fetch request, as defined in the `FetchOptions` interface.
* @returns {Promise<any>} - A promise that resolves with the fetched data.
*/
export declare const fetchData: (url: string, options?: FetchOptions) => Promise<any>;
/**
* A custom React hook for making API requests and managing loading state.
*
* @param {string} url - The URL to fetch data from. It can be either an absolute or relative URL.
* @param {FetchOptions} [options] - Custom options for the fetch request, as defined in the FetchOptions interface.
* @returns {{ loading: boolean, fetchedData: any, error?: string }} - An object containing loading state, data, and error.
*/
declare const useFetch: (url: string, options?: FetchOptions) => {
loading: boolean;
fetchedData: any;
error?: string;
};
export { fetchData as fetch, useFetch, configureFetcher, deleteMemoryCache, deleteMemoryCaches, clearMemoryCache, createAbsoluteUrl, FetchOptions };