@liukangjie/uni-request-queue
Version:
A smart request queue manager for UniApp with retry, offline cache, priority, cancel, and caching support.
82 lines (64 loc) • 2.09 kB
Markdown
# uni-request-queue
> 🚀 专为 UniApp 打造的生产级请求库 —— 支持队列、缓存、重试、离线队列、多实例、独立拦截器
[](https://www.npmjs.com/package/@liukangjie/uni-request-queue)
[](https://www.npmjs.com/package/@liukangjie/uni-request-queue)
[](LICENSE)
`uni-request-queue` 是一个轻量、健壮、功能丰富的 UniApp 请求库,支持:
- ✅ 请求队列与优先级
- ✅ 全局 Loading 自动控制
- ✅ 请求缓存(GET)
- ✅ 失败重试 & 延迟
- ✅ 离线队列(断网自动重发)
- ✅ 多实例 & **独立拦截器**(v1.1.0 新增)
- ✅ 防抖去重
- ✅ 401 自动跳转登录
- ✅ 取消请求
---
## 📦 安装
```bash
npm install @liukangjie/uni-request-queue
```
## 🚀 快速开始
```bash
import request from '@liukangjie/uni-request-queue';
// GET 请求
request.get('/api/user/profile').then(data => {
console.log(data);
});
// POST 请求
request.post('/api/login', {
username: 'admin',
password: '123456'
}).then(res => {
console.log('登录成功');
});
```
## 🌟 核心特性
```bash
// 用户 API 实例
const apiUser = request.create({
baseUrl: 'https://api.example.com/user',
timeout: 10000
});
// 自动携带 token
apiUser.interceptors.request.use(config => {
const token = uni.getStorageSync('token');
if (token) {
config.header['Authorization'] = 'Bearer ' + token;
}
return config;
});
// 上传 API 实例
const apiUpload = request.create({
baseUrl: 'https://api.example.com/upload',
timeout: 30000
});
// 自动设置 multipart
apiUpload.interceptors.request.use(config => {
config.header['Content-Type'] = 'multipart/form-data';
return config;
});
// ✅ 互不影响
apiUser.get('/profile'); // 有 token,无 multipart
apiUpload.post('/file'); // 有 multipart,无 token
```