vue-axios-optimize 1.0.8 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -93
- package/index.js +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# vue-axios-optimize使用说明
|
|
2
2
|
|
|
3
|
-
vue-axios-optimize是一个对axios请求优化的vue
|
|
3
|
+
vue-axios-optimize是一个对axios请求优化的vue插件包,引入此包之后,并进行相关配置,即可轻松实现全局请求加载动画,重复请求时取消前面的请求或者阻止后面的请求,并且可实现请求并发数量控制。
|
|
4
4
|
|
|
5
5
|
## 安装
|
|
6
6
|
|
|
@@ -49,7 +49,7 @@ const AxiosOptimize = new axiosOptimize(axios, instance, {
|
|
|
49
49
|
}
|
|
50
50
|
})
|
|
51
51
|
|
|
52
|
-
export default AxiosOptimize
|
|
52
|
+
export default AxiosOptimize
|
|
53
53
|
```
|
|
54
54
|
|
|
55
55
|
**配置无感知刷新token时** axios-optimize.js文件内容大致如下
|
|
@@ -97,7 +97,7 @@ const AxiosOptimize = new axiosOptimize(axios, instance, {
|
|
|
97
97
|
}
|
|
98
98
|
})
|
|
99
99
|
|
|
100
|
-
export default AxiosOptimize
|
|
100
|
+
export default AxiosOptimize
|
|
101
101
|
```
|
|
102
102
|
|
|
103
103
|
|
|
@@ -176,114 +176,42 @@ import axiosOptimize from '@/api/axios-optimize.js'
|
|
|
176
176
|
// 需要配置 请求接口时 不加载全局动画 则 配置 noShowLoading: true
|
|
177
177
|
// 需要配置 重复请求时 取消前面的请求 则 配置 preventDuplicateRequestsType: "cancel"
|
|
178
178
|
// 需要配置 重复请求时 禁用后面的请求 则 配置 preventDuplicateRequestsType: "prevent"
|
|
179
|
+
// 需要配置 接口缓存的 则 配置 cache: true 否则 配置为false 或者不配置 配置后该接口将进行缓存 (V2.0.1及以上)
|
|
179
180
|
|
|
180
|
-
// get 请求demo
|
|
181
|
-
export function
|
|
181
|
+
// get 请求demo
|
|
182
|
+
export function getDemo(
|
|
182
183
|
data = {},
|
|
183
184
|
config = {
|
|
184
185
|
noShowLoading: true, // 配置不展示加载动画
|
|
185
186
|
preventDuplicateRequestsType: "cancel" // 配置重复请求时 取消前面的请求
|
|
186
187
|
}
|
|
187
188
|
) {
|
|
188
|
-
return axiosOptimize.get(`/xiaobu-admin/
|
|
189
|
+
return axiosOptimize.get(`/xiaobu-admin/getDemo`, { params: data, ...config })
|
|
189
190
|
}
|
|
190
191
|
|
|
191
|
-
//
|
|
192
|
-
export function
|
|
193
|
-
|
|
194
|
-
config = {
|
|
195
|
-
preventDuplicateRequestsType: "prevent" // 配置重复请求时 后面的请求无效
|
|
196
|
-
}
|
|
197
|
-
) {
|
|
198
|
-
return axiosOptimize({
|
|
199
|
-
method: 'get',
|
|
200
|
-
url: `/xiaobu-admin/getDemo2`,
|
|
201
|
-
params: data,
|
|
202
|
-
...config
|
|
203
|
-
})
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
// post 请求demo 1
|
|
207
|
-
export function postDemo1(data = {}, config = {}) {
|
|
208
|
-
return axiosOptimize.post('/xiaobu-admin/postDemo1', data, config)
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
// post 请求demo 2
|
|
212
|
-
export function postDemo2(data = {}, config = {}) {
|
|
213
|
-
return axiosOptimize({
|
|
214
|
-
url: '/xiaobu-admin/postDemo2',
|
|
215
|
-
method: 'post',
|
|
216
|
-
data,
|
|
217
|
-
...config
|
|
218
|
-
})
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
// delete 请求demo 1 使用params作为参数 参数会拼接在请求路径上
|
|
222
|
-
export function deleteDemo1(
|
|
223
|
-
data = {},
|
|
224
|
-
config = {
|
|
225
|
-
noShowLoading: true // 配置不展示加载动画
|
|
226
|
-
}
|
|
227
|
-
) {
|
|
228
|
-
return axiosOptimize.delete(`/xiaobu-admin/deleteDemo1`, { params: data, ...config })
|
|
192
|
+
// post 请求demo
|
|
193
|
+
export function postDemo(data = {}, config = {}) {
|
|
194
|
+
return axiosOptimize.post(`/xiaobu-admin/postDemo`, data, config)
|
|
229
195
|
}
|
|
230
196
|
|
|
231
|
-
// delete 请求
|
|
232
|
-
export function
|
|
233
|
-
data
|
|
234
|
-
config = {}
|
|
235
|
-
) {
|
|
236
|
-
return axiosOptimize({
|
|
237
|
-
method: 'delete',
|
|
238
|
-
url: `/xiaobu-admin/deleteDemo2`,
|
|
239
|
-
params: data,
|
|
240
|
-
...config
|
|
241
|
-
})
|
|
197
|
+
// delete 请求demo1 使用data作为参数 参数不展示在 请求路径上
|
|
198
|
+
export function deleteDemo1(data = {}, config = {}) {
|
|
199
|
+
return axiosOptimize.delete(`/xiaobu-admin/deleteDemo1`, { data, ...config })
|
|
242
200
|
}
|
|
243
201
|
|
|
244
|
-
// delete 请求
|
|
245
|
-
export function
|
|
246
|
-
return axiosOptimize.delete(`/xiaobu-admin/
|
|
202
|
+
// delete 请求demo2 使用params作为参数 参数展示在 请求路径上
|
|
203
|
+
export function deleteDemo2(data = {}, config = {}) {
|
|
204
|
+
return axiosOptimize.delete(`/xiaobu-admin/deleteDemo2`, { params: data, ...config })
|
|
247
205
|
}
|
|
248
206
|
|
|
249
|
-
//
|
|
250
|
-
export function deleteDemo4(data = {}, config = {}) {
|
|
251
|
-
return axiosOptimize({
|
|
252
|
-
method: 'delete',
|
|
253
|
-
url: `/xiaobu-admin/deleteDemo4`,
|
|
254
|
-
data,
|
|
255
|
-
...config
|
|
256
|
-
})
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
// put 请求demo 1
|
|
207
|
+
// put 请求demo
|
|
260
208
|
export function putDemo1(data = {}, config = {}) {
|
|
261
|
-
return axiosOptimize.put(
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
// put 请求demo 2
|
|
265
|
-
export function putDemo2(data = {}, config = {}) {
|
|
266
|
-
return axiosOptimize({
|
|
267
|
-
url: '/xiaobu-admin/putDemo2',
|
|
268
|
-
method: 'put',
|
|
269
|
-
data,
|
|
270
|
-
...config
|
|
271
|
-
})
|
|
209
|
+
return axiosOptimize.put(`/xiaobu-admin/putDemo`, data, config)
|
|
272
210
|
}
|
|
273
211
|
|
|
274
|
-
// patch 请求demo
|
|
212
|
+
// patch 请求demo
|
|
275
213
|
export function patchDemo1(data = {}, config = {}) {
|
|
276
|
-
return axiosOptimize.patch(
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
// patch 请求demo 2
|
|
280
|
-
export function patchDemo2(data = {}, config = {}) {
|
|
281
|
-
return axiosOptimize({
|
|
282
|
-
url: '/xiaobu-admin/patchDemo2',
|
|
283
|
-
method: 'patch',
|
|
284
|
-
data,
|
|
285
|
-
...config
|
|
286
|
-
})
|
|
214
|
+
return axiosOptimize.patch(`/xiaobu-admin/patchDemo`, data, config)
|
|
287
215
|
}
|
|
288
216
|
|
|
289
217
|
```
|
|
@@ -313,6 +241,7 @@ export function patchDemo2(data = {}, config = {}) {
|
|
|
313
241
|
| accessTokenExpirationCode | number | 配置accessToken过期返回的业务状态码 | 非必传 | 401 |
|
|
314
242
|
| refreshTokenExpirationCode | number | 配置refreshToken过期返回的业务状态码 | 非必传 | 403 |
|
|
315
243
|
| responseTypesStr(v1.0.2) | String | 配置数据返回类型,实现当此类型时返回原封不动的响应数据的data数据 | 非必传 | 无 |
|
|
244
|
+
| maxReqNum (v2.0.0) | Number | 最大同时请求数量 | 非必传 | 4 |
|
|
316
245
|
|
|
317
246
|
## store/modules/user.js代码大致如下
|
|
318
247
|
|
|
@@ -422,7 +351,7 @@ module.exports = {
|
|
|
422
351
|
|
|
423
352
|
如有疑问,可关注微信公众号【爆米花小布】进行咨询。
|
|
424
353
|
|
|
425
|
-

|
|
426
355
|
|
|
427
356
|
微信公众号【爆米花小布】,抖音号【爆米花小布】 更多好玩的插件
|
|
428
357
|
|
|
@@ -438,6 +367,20 @@ module.exports = {
|
|
|
438
367
|
|
|
439
368
|
## 更新日志
|
|
440
369
|
|
|
370
|
+
### 2.0.1
|
|
371
|
+
|
|
372
|
+
1. 【功能】实现接口缓存配置 options新增配置 cacheNum 配置最大缓存数量, 接口配置处可配置 cache: true。
|
|
373
|
+
注意:如下几种情况不建议配置缓存
|
|
374
|
+
1. 表单提交的接口
|
|
375
|
+
2. 数据频繁更新的接口
|
|
376
|
+
3. 删除数据接口
|
|
377
|
+
|
|
378
|
+
### 2.0.0
|
|
379
|
+
|
|
380
|
+
1. 【功能】实现并发请求数量控制,options新增maxReqNum参数配置 默认值为 4 ,表示同时可请求4个接口,剩余的接口陆续排队
|
|
381
|
+
2. 【优化】当接口返回数据格式为对象格式时,会多添加一个 IS_COMPLETE 表示当前全部请求任务是否已完成
|
|
382
|
+
3. 【调整】2.0.0版本 axios-optimize.js 文件不暴露 instance实例,且接口调用方法仅 支持 .get .post .delete .patch .put 形式调用, 具体查看文档示例
|
|
383
|
+
|
|
441
384
|
### 1.0.8
|
|
442
385
|
|
|
443
386
|
1. 【优化】废除options的setAccessTokenFun配置项
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
class e{instance;requestObj={};requestingNum=0;authErrorArr=[];queue=[];isRefresh=!1;isShowReLoginDialog=!1;
|
|
1
|
+
class e{instance;requestObj={};requestingNum=0;authErrorArr=[];queue=[];isRefresh=!1;isShowReLoginDialog=!1;runningCount=0;maxReqNum=4;tasks=[];cache;cacheNum=10;add(e){return new Promise(((t,s)=>{this.tasks.push({task:e,resolve:t,reject:s}),this._run()}))}_run(){for(;this.runningCount<this.maxReqNum&&this.tasks.length>0;){const{task:e,resolve:t,reject:s}=this.tasks.shift();this.runningCount++,e().then((e=>{this.runningCount--,"[object Object]"!==Object.prototype.toString.call(e)?t(e):t({...e,IS_COMPLETE:0===this.runningCount})})).catch((e=>{this.runningCount--,s(e)})).finally((()=>{this._run()}))}}getCache(e,t){const{url:s,data:r={},config:i,method:n}=t;return new Promise(((t,h)=>{if(!this.cache.has(e)){delete i.cache;return void(["get","delete"].includes(n)?this[n](s,i):this[n](s,r,i)).then((s=>{this.setCache(e,s),t(s)}))}const a=this.cache.get(e);this.cache.delete(e),this.cache.set(e,a),t(a)}))}setCache(e,t){if(this.cache.has(e)&&this.cache.delete(e),this.cache.set(e,t),this.cache.size>this.cacheNum){const e=this.cache.keys().next().value;this.cache.delete(e)}}get(e,t){if(t.cache){const s={url:e,config:t,method:"get"},r=t.params,i=this.generateKey(s,r);return this.getCache(i,s)}const s=t.params;return delete t.params,this.add((()=>this.instance.request({url:e,params:s,...t,method:"GET"})))}post(e,t={},s={}){if(s.cache){const r={url:e,data:t,config:s,method:"post"},i=this.generateKey(r,t);return this.getCache(i,r)}return this.add((()=>this.instance.request({url:e,data:t,...s,method:"POST"})))}delete(e,t={}){if(t.cache){const s={url:e,config:t,method:"delete"},r=t.params||t.data||{},i=this.generateKey(s,r);return this.getCache(i,s)}if(t.params){const s=t?.params||{};return delete t.params,this.add((()=>this.instance.request({url:e,params:s,...t,method:"DELETE"})))}const s=t?.data||{};return delete t.data,this.add((()=>this.instance.request({url:e,data:s,...t,method:"DELETE"})))}patch(e,t={},s={}){if(s.cache){const r={url:e,data:t,config:s,method:"patch"},i=this.generateKey(r,t);return this.getCache(i,r)}return this.add((()=>this.instance.request({url:e,data:t,...s,method:"PATCH"})))}put(e,t={},s={}){if(s.cache){const r={url:e,data:t,config:s,method:"put"},i=this.generateKey(r,t);return this.getCache(i,r)}return this.add((()=>this.instance.request({url:e,data:t,...s,method:"PUT"})))}generateKey(e,t){const{method:s,url:r}=e;let i=s+"#"+r;const n=[];for(const[e,s]of Object.entries(t))n.push(`${e}=${s}`);return n.length>0&&(i+="?"+n.join("&")),i}constructor(e,t,s){this.maxReqNum=s?.maxReqNum||this.maxReqNum,this.cacheNum=s?.cacheNum||this.cacheNum,this.cache=new Map,this.instance=t,this.instance.interceptors.request.use((t=>{t?.noShowLoading||(this.requestingNum++,s.showLoadingFun&&s.showLoadingFun(t,this.requestingNum));const r=t.url;if(s.openRefresh){-1===this.authErrorArr.indexOf(r)&&this.isRefresh&&r!==s.refreshApiUrl&&this.authErrorArr.push(r)}if(r&&t.preventDuplicateRequestsType)if("cancel"===t.preventDuplicateRequestsType){this.requestObj[r]&&this.requestObj[r].cancel({config:t,isCancel:!0});const s=e.CancelToken;this.requestObj[r]=s.source(),t.cancelToken=this.requestObj[r].token}else if("prevent"===t.preventDuplicateRequestsType){if("requesting"===this.requestObj[r])return Promise.reject({config:t,isPrevent:!0});this.requestObj[r]="requesting"}return t}),(e=>Promise.reject(e))),this.instance.interceptors.response.use((async e=>{const{config:t,responseErr:r}=e;if(t?.noShowLoading||(this.requestingNum--,this.requestingNum<=0&&(this.requestObj={},s.hideLoadingFun&&s.hideLoadingFun(t,this.requestingNum))),t&&this.requestObj[t.url]&&delete this.requestObj[t.url],r){if(s.openRefresh){const r=this.authErrorArr.indexOf(t.url);if(-1!==r)return this.authErrorArr.splice(r,1),new Promise((e=>{this.queue.push((()=>{e(this.instance.request(t))}))}));const i=s.accessTokenExpirationCode||401;if(Number(e.code)===i){const r=s.getRefreshTokenFun();if(!r){if(this.isShowReLoginDialog)return;return this.isShowReLoginDialog=!0,s.reloginFun(e),Promise.reject(new Error(e.message||"Error"))}if(this.isRefresh)return new Promise((e=>{this.queue.push((()=>{e(this.instance.request(t))}))}));this.isRefresh=!0;try{return await s.refreshTokenStore(r),this.queue.forEach((e=>e())),this.instance.request(t)}catch{if(this.isShowReLoginDialog)return;return this.isShowReLoginDialog=!0,s.reloginFun(e),Promise.reject(new Error(e.message||"Error"))}finally{this.queue=[],this.isRefresh=!1}}const n=s.refreshTokenExpirationCode||403;if(Number(e.code)===n){if(this.isShowReLoginDialog)return;return this.isShowReLoginDialog=!0,s.reloginFun(e),Promise.reject(new Error(e.message||"Error"))}}return Promise.reject(new Error(e.message||"Error"))}if(s?.responseTypesStr?.toLocaleLowerCase().split(",").includes(t?.responseType?.toLocaleLowerCase()))return e.data;const i=JSON.parse(JSON.stringify(e));return delete i.responseErr,delete i.config,s.responseResultFun?s.responseResultFun(i):i}),(e=>{const t=e.config||e?.message?.config||{};return t?.noShowLoading||(this.requestingNum--,this.requestingNum<=0&&(this.requestObj={},s.hideLoadingFun&&s.hideLoadingFun(t,this.requestingNum))),e.isPrevent||e?.message?.isCancel||delete this.requestObj[t.url],Promise.reject(e)}))}}export default e;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vue-axios-optimize",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "vue项目对axios
|
|
3
|
+
"version": "2.0.1",
|
|
4
|
+
"description": "vue项目对axios请求的优化,实现可配置展示全局加载动画,可配置是否重复请求时取消前面的请求或者阻止后面的请求,已经控制请求并发数量,接口缓存",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
|