screw-axios
介绍
axios 的扩展包,封装了常规方法并且具备以下特性
- 可以通过配置取消单个请求,也可以通过方法取消所有请求
- 可以通过开启 去除重复连接调用(通过 url、method、参数作为唯一标识)
- 可以为单个请求或者全部连接,设置断线重连,包括设置重连次数和请求延时
- 可以将请求结果缓存到 localstorage,并且可以为缓存设置时间
- 可以为单个连接设置延迟请求
安装
npm 安装
npm i screw-axios -S
yarn 安装
yarn add screw-axios -S
pnpm 安装
pnpm add screw-axios -S
使用
新建一个 request.js 文件,文件名可以随便起,然后在这个文件中创建 ScrewAxios 的实
import ScrewAxios from "ScrewAxios";
const request = new ScrewAxios({
headers: {},
retry:3,
retryDelay:3000,
baseURL: "",
timeout: 200000,
canRepeat: true,
hasTimestamp: true,
reqInterceptor(config) {},
resInterceptor(config) {},
responseHandle: {
200: res => {
console.log("返回了200")
return res
},
},
})
export default request
import request from "./request"
export default {
middleViewData: data => request.get('/jscApi/middleViewData', { data }),
cancelReq: data => request.post('http://localhost:3003/jscApi/middleViewData', { data, cancelRequest: true }),
reqAgainSend: data => request.get('/equ/equTypeList11', { data, retry: 3, retryDelay: 1000 }),
cacheEquList: data => request.get('/equ/equList', { data, cache: true, setExpireTime: 30000 }),
cacheEquListParams: data => request.get('/equ/equList', { data, cache: true })
};
请求的方法
const request = new ScrewAxios({
...
})
request.axios({
url: "http://localhost:3000/api/img2",
method: "get",
data: { a: 1 },
retry: 3,
retryDelay: 1000,
}).then(res => {
console.log(res)
})
request.get(url[, data[, config]])
request.delete(url[, data[, config]])
request.head(url[, data[, config]])
request.options(url[, config])
request.post(url[, data[, config]])
request.put(url[, data[, config]])
request.patch(url[, data[, config]])
request.get("/jscApi/middleViewData", { a: 1 })
request.post("/jscApi/middleViewData", { a: 1 })
request.delete("/jscApi/middleViewData", { a: 1 })
request.put("/jscApi/middleViewData", { a: 1 })
request.head("/jscApi/middleViewData", { a: 1 })
request.options("/jscApi/middleViewData", { a: 1 })
避免重复请求
初始化配置时 canRepeat 为 false
const request = new ScrewAxios({
...
canRepeat:false
})
request.get('/jscApi/middleViewData', {a:1})
request.get('/jscApi/middleViewData', {a:1})
request.get('/jscApi/middleViewData', {a:1})
上述三个请求只会请求一次。
取消其中一次的请求
const request = new ScrewAxios({
...
})
const source1 = request.getCancelTokenSource()
request
.get("http://localhost:3000/api/img", {
data: { a: 3 },
cancelTokenSource: source1,
})
.then(res => {
console.log(res)
})
source1.cancel() 或者 source1.cancel('这个请求取消了')
取消所有请求
const request = new ScrewAxios({
...
})
request.cancelAll()
连接设置断线后 定时重连
const request = new ScrewAxios({
...
retry:3,
retryDelay:4000,
})
request.get('/equ/equTypeList11',data, { retry: 3, retryDelay: 1000 })
设置请求的类型
后端接收前端请求常用类型分为
- application/json
- multipart/form-data
- application/x-www-form-urlencoded
- text/xml
其中 application/json 是 axios 的默认接收类型
如果想改变接收类型可以如下操作:
const request = new ScrewAxios({
...
})
request.axios({
url: "http://localhost:3000/api/img2",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
method: "post",
data: { a: 1 },
})
request.post( "http://localhost:3000/api/img2",{ a: 1 },{
headers: {
"Content-Type": "application/x-www-form-urlencoded",
}
})
let formData = new FormData()
formData.append("appkey", "njssdjtkj")
formData.append("sign", 111)
formData.append("sign_method", "MD5")
formData.append("mobile", "15951971791")
formData.append("engine_no", "070939")
formData.append("plate_type", "02")
formData.append("plate_no", "苏EXW365")
request
.axios({
url: "http://localhost:3000/api/img2",
headers: {
"Content-Type": "multipart/form-data",
},
method: "post",
data: formData,
})