UNPKG

@alwatr/fetch

Version:

`@alwatr/fetch` is an enhanced, lightweight, and dependency-free wrapper for the native `fetch` API. It provides modern features like caching strategies, request retries, timeouts, and intelligent duplicate request handling, all in a compact package.

57 lines 2.13 kB
/** * @module @alwatr/fetch * * An enhanced, lightweight, and dependency-free wrapper for the native `fetch` API. * It provides modern features like caching strategies, request retries, timeouts, and * duplicate request handling. */ import type { FetchOptions } from './type.js'; export { cacheSupported }; export type * from './type.js'; /** * A boolean flag indicating whether the browser's Cache API is supported. */ declare const cacheSupported: boolean; /** * An enhanced wrapper for the native `fetch` function. * * This function extends the standard `fetch` with additional features such as: * - **Timeout**: Aborts the request if it takes too long. * - **Retry Pattern**: Automatically retries the request on failure (e.g., server errors or network issues). * - **Duplicate Request Handling**: Prevents sending multiple identical requests in parallel. * - **Cache Strategies**: Provides various caching mechanisms using the browser's Cache API. * - **Simplified API**: Offers convenient options for adding query parameters, JSON bodies, and auth tokens. * * @see {@link FetchOptions} for a detailed list of available options. * * @param {string} url - The URL to fetch. * @param {FetchOptions} options - Optional configuration for the fetch request. * @returns {Promise<Response>} A promise that resolves to the `Response` object for the request. * * @example * ```typescript * async function fetchProducts() { * try { * const response = await fetch("/api/products", { * queryParams: { limit: 10, category: "electronics" }, * timeout: 5_000, // 5 seconds * retry: 3, * cacheStrategy: "stale_while_revalidate", * }); * * if (!response.ok) { * throw new Error(`HTTP error! status: ${response.status}`); * } * * const data = await response.json(); * console.log("Products:", data); * } catch (error) { * console.error("Failed to fetch products:", error); * } * } * * fetchProducts(); * ``` */ export declare function fetch(url: string, options: FetchOptions): Promise<Response>; //# sourceMappingURL=main.d.ts.map