@ryanuo/utils
Version:
提供多种实用工具函数,涵盖算法、浏览器操作、网络请求等多个领域
52 lines (51 loc) • 1.73 kB
TypeScript
/**
* 带超时的 fetch 请求
* @category Http
* @param url 请求地址
* @param options 请求配置
* @param timeout 超时时间(默认 5s)
*/
declare function fetchWithTimeout(url: string, options?: RequestInit, timeout?: number): Promise<Response>;
/**
* 封装 GET/POST 请求(自动处理 JSON)
* @category Http
*/
declare function request<T = any>(method: 'GET' | 'POST' | 'PUT' | 'DELETE', url: string, data?: object, headers?: Record<string, string>): Promise<T>;
/**
* 控制并发数的请求队列
* @category Http
* @param requests 请求函数数组
* @param concurrency 最大并发数(默认 5)
*/
declare function parallelRequests<T>(requests: (() => Promise<T>)[], concurrency?: number): Promise<T[]>;
/**
* 获取客户端的 IP 地址
* @category Http
*/
declare function getClientIP(): Promise<string>;
/**
* 获取 IndexedDB 缓存
* @example
* ```ts twoslash
* import { getIndexedDBCache } from '@ryanuo/utils';
* // 使用示例
* const cache = await getIndexedDBCache('api-cache', 'responses');
* await cache.set('users', [{ id: 1, name: 'Alice' }]);
* const users = await cache.get('users');
* ```
* @category Http
* @param dbName indexedDB 数据库名称
* @param storeName 缓存对象名称
* @returns IndexedDB 缓存对象
*/
declare function getIndexedDBCache(dbName: string, storeName: string): Promise<{
get<T>(key: string): Promise<T | undefined>;
set(key: string, value: any): Promise<void>;
}>;
/**
* 检测网络连接状态
* @category Http
* @returns 是否在线
*/
declare function checkNetworkStatus(): Promise<boolean>;
export { checkNetworkStatus, getIndexedDBCache, getClientIP, parallelRequests, fetchWithTimeout, request, };