etherreq
v1.2.9
Published
A lightweight custom HTTP request library with TypeScript support.
Maintainers
Readme
EtherReq
一个轻量级、无感知的 HTTP 请求库,基于 Fetch 封装,支持自动 Token 注入、拦截器、TypeScript 类型定义和智能缓存功能。
特性
- 🚀 轻量级: 基于原生 Fetch API 构建
- 🔐 自动认证: login 方法自动管理 token
- 🔄 智能缓存: 支持内存和 localStorage 双重缓存
- 🎯 TypeScript: 完整的类型支持
- ⚡ 拦截器: 请求/响应拦截器支持
- 📦 零依赖: 无需额外依赖包
安装
npm install etherreq
# 或者
yarn add etherreq快速开始
import { etherreq } from 'etherreq';
// GET 请求(自动缓存)
const users = await etherreq.get('http://localhost:8081/api/users');
console.log(users);
// POST 请求
const result = await etherreq.post('/login', {
username: 'test',
password: '123456'
});
console.log('登录成功:', result);
// 带数据的POST请求
const user = {
id: 1,
name: '张三',
sex: '男',
age: 18,
};
const newUser = await etherreq.post('/add', user);
console.log(newUser);登录认证
// 登录方法(自动保存 token)
const login = async () => {
const userData = {
id: 1,
username: "zhy",
password: "123456"
};
const loginResult = await etherreq.login('users/login', userData);
console.log(loginResult);
// 后续请求会自动携带 token
};缓存功能
自动缓存
GET 请求默认启用智能缓存:
- 内存缓存(快速访问)
- localStorage 持久化(页面刷新后仍有效)
- 自动过期清理(默认5分钟)
缓存控制选项
// 禁用缓存
const data = await etherreq.get('/api/data', {
cache: false
});
// 自定义缓存时间
const data = await etherreq.get('/api/data', {
cache: {
enabled: true,
ttl: 10 * 60 * 1000 // 10分钟
}
});
// 指定存储策略
const data = await etherreq.get('/api/data', {
cache: {
storage: 'memory+local', // 'memory' | 'local' | 'memory+local'
ttl: 300000
}
});带参数的请求缓存
// 不同参数生成不同缓存键
const page1 = await etherreq.get('/users', {
params: { page: 1, limit: 10 }
});
const page2 = await etherreq.get('/users', {
params: { page: 2, limit: 10 }
});
// 这两个请求会有独立的缓存API 接口
请求方法
etherreq(url, options?) // 默认 GET
etherreq.get(url, options?)
etherreq.post(url, data?, options?)
etherreq.put(url, data?, options?)
etherreq.delete(url, options?)
etherreq.del(url, options?) // delete 别名
etherreq.head(url, options?)
etherreq.options(url, options?)
etherreq.patch(url, data?, options?)
etherreq.login(url, data?, options?)配置选项
interface EtherRequestOptions {
method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
headers?: Record<string, string>;
body?: any;
params?: Record<string, string | number | boolean>;
baseURL?: string;
cache?: CacheOptions | false;
}缓存选项
interface CacheOptions {
enabled?: boolean; // 是否启用缓存
storage?: 'memory' | 'local' | 'memory+local'; // 存储策略
ttl?: number; // 过期时间(毫秒)
}高级用法
设置基础URL
import { setBaseURL } from 'etherreq';
setBaseURL('https://api.example.com');
// 后续请求将使用此基础URL并发请求
const [users, posts] = await Promise.all([
etherreq.get('/users'),
etherreq.get('/posts')
]);错误处理
try {
const data = await etherreq.get('/api/data');
} catch (error) {
console.error('请求失败:', error.message);
}TypeScript 支持
完整的类型定义支持:
interface User {
id: number;
name: string;
email: string;
}
// 类型安全的请求
const user: User = await etherreq.get<User>('/users/1');
const users: User[] = await etherreq.get<User[]>('/users');浏览器兼容性
- Chrome 67+
- Firefox 61+
- Safari 12+
- Edge 79+
需要 Promise 和 fetch API 支持。
License
MIT
