0.0.5 • Published 2 years ago
@kinvirgo/request v0.0.5
小程序请求二次封装
基本使用
import Request from "request";
// 创建实例
const instace = new Request(config);
// 发送请求
instace.request({
url: "http://example.com",
});
拦截器 : instace.interceptor
// 请求拦截器
instace.interceptor.request.use(
(config) => {
return Promise.resolve(config);
},
(config) => {
return Promise.reject(config);
}
);
// 响应拦截器
instace.interceptor.response.use(
(response) => {
return Promise.resolve(response);
},
(config) => {
return Promise.reject(response);
}
);
tips:拦截器都是以 promise.then(fulfilled,rejected)执行的
取消请求 : CancelToken()
// 实例取消
const cancelToken = new Request.CancelToken();
// 发送请求
instace.request({
url: "http://example.com",
cancelToken: cancelToken,
});
// 取消
cancelToken.cancel(message);
tips: CancelToken 实例一次,只能使用一次
防重复请求(节流): Throttle(config)
// 实例节流
const throttle = new Request.Throttle();
// 发送请求
instace.request({
url: "http://example.com",
throttle: throttle,
});
生命周期
请求拦截器 -> 网络请求 -> 响应拦截器
API
属性 | 说明 | 类型 | 默认值 |
---|---|---|---|
属性 | 说明 | 类型 | 默认值 |
0.0.5
2 years ago
0.0.4
2 years ago
0.0.3
2 years ago
0.0.2
2 years ago
0.0.2-alpha
2 years ago
0.0.1
2 years ago
0.0.1-alpha
2 years ago