@uni-helper/axios-adapter 1.5.1 → 1.5.2
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 +8 -4
- package/dist/index.cjs +7 -7
- package/dist/index.js +6 -6
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -84,8 +84,13 @@ axios.request({
|
|
|
84
84
|
|
|
85
85
|
### 小程序
|
|
86
86
|
|
|
87
|
-
axios
|
|
87
|
+
自 axios 1.4.0 开始,axios 内部已经处理了小程序的兼容性问题
|
|
88
88
|
|
|
89
|
+
<details>
|
|
90
|
+
<summary>我要使用 `FormData` 和 `Blob` 对象</summary>
|
|
91
|
+
|
|
92
|
+
小程序没有 `FormData` 和 `Blob` 对象,
|
|
93
|
+
使用 `pnpm add miniprogram-formdata miniprogram-blob` 来安装对应的 polyfill,然后使用插件
|
|
89
94
|
```ts
|
|
90
95
|
// vite.config.js
|
|
91
96
|
import uniAxiosAdapter from "@uni-helper/axios-adapter/vite";
|
|
@@ -99,11 +104,10 @@ export default {
|
|
|
99
104
|
}
|
|
100
105
|
```
|
|
101
106
|
|
|
102
|
-
> [!WARNING]
|
|
103
|
-
> 这个插件通过将 `FormData` 和 `Blob` 导出为空 `class`来解决兼容性问题,如果你确实需要的话,使用 `pnpm add miniprogram-formdata miniprogram-blob` 来安装对应的 polyfill 即可,插件会自动使用。
|
|
104
|
-
|
|
105
107
|
如果你使用的是 Vue CLI,改用 `@uni-helper/axios-adapter/webpack` 即可
|
|
106
108
|
|
|
109
|
+
</details>
|
|
110
|
+
|
|
107
111
|
### 版本
|
|
108
112
|
|
|
109
113
|
自 1.4.0 开始,请始终保持主版本号和次版本号与 axios 一致。例如当你安装了 1.5.1 版本时,你可以安装 axios 的 1.5.x 版本。
|
package/dist/index.cjs
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var axios = require('axios');
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
var
|
|
4
|
+
var I = require('axios/unsafe/helpers/buildURL');
|
|
5
|
+
var N = require('axios/unsafe/helpers/speedometer');
|
|
6
|
+
var P = require('axios/unsafe/core/buildFullPath');
|
|
7
7
|
var W = require('axios/unsafe/core/settle');
|
|
8
8
|
|
|
9
9
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
10
|
|
|
11
|
-
var D__default = /*#__PURE__*/_interopDefault(D);
|
|
12
11
|
var I__default = /*#__PURE__*/_interopDefault(I);
|
|
13
|
-
var
|
|
12
|
+
var N__default = /*#__PURE__*/_interopDefault(N);
|
|
13
|
+
var P__default = /*#__PURE__*/_interopDefault(P);
|
|
14
14
|
var W__default = /*#__PURE__*/_interopDefault(W);
|
|
15
15
|
|
|
16
|
-
var
|
|
16
|
+
var g=e=>{let{method:i="GET"}=e;switch(i.toLocaleLowerCase()){case"download":return "download";case"upload":return "upload";default:return "request"}},T=e=>({...e}),c=(e,i)=>{var A,w;let d=e.data,s=e.responseType==="arraybuffer"?"arraybuffer":"text",a=s==="text"?"json":void 0,{headers:n,baseURL:u,...t}=e,o=axios.AxiosHeaders.from(n).normalize(!1);if(e.auth){let C=e.auth.username||"",H=e.auth.password?unescape(encodeURIComponent(e.auth.password)):"";o.set("Authorization","Basic "+btoa(C+":"+H));}let r=P__default.default(u,e.url),p=(w=(A=e==null?void 0:e.method)==null?void 0:A.toUpperCase())!=null?w:"GET",l=I__default.default(r,e.params,e.paramsSerializer),U=e.timeout||6e4,x={};if(d&&typeof d=="string")try{x=JSON.parse(d);}catch(C){}let k=o.toJSON();return {...t,url:l,data:d,header:k,method:p,responseType:s,dataType:a,timeout:U,formData:x}},q=(e,i)=>{let d=0,s=N__default.default(50,250);return a=>{let n=a.totalBytesWritten,u=a.totalBytesExpectedToWrite,t=n-d,o=s(t),r=n<=u;d=n;let p={loaded:n,total:u,progress:u?n/u:void 0,bytes:t,rate:o||void 0,estimated:o&&u&&r?(u-n)/o:void 0,event:a};p[i?"download":"upload"]=!0,e(p);}};var f=class{constructor(i){this.config=i;}subscribe(i,d){(this.config.cancelToken||this.config.signal)&&(this.onCanceled=s=>{i&&(d(!s||s.type?new axios.CanceledError(void 0,void 0,this.config,i):s),i.abort(),i=null);},this.config.cancelToken&&this.config.cancelToken.subscribe(this.onCanceled),this.config.signal&&this.config.signal.addEventListener&&(this.config.signal.aborted?this.onCanceled():this.config.signal.addEventListener("abort",this.onCanceled)));}unsubscribe(){this.config.cancelToken&&this.config.cancelToken.unsubscribe(this.onCanceled),this.config.signal&&this.config.signal.removeEventListener&&this.config.signal.removeEventListener("abort",this.onCanceled);}};var K=(e,i)=>new Promise((d,s)=>{let a=c(e),n=e;n.headers=new axios.AxiosHeaders(a.header);let u=new f(e),t=uni.downloadFile({...a,success(o){var p;if(!t)return;let r={config:n,data:o.tempFilePath,headers:{},status:o.statusCode,statusText:(p=o.errMsg)!=null?p:"OK",request:t};W__default.default(d,s,r),t=null;},fail(o){let{errMsg:r=""}=o!=null?o:{};r&&(r==="downloadFile:fail timeout"&&s(new axios.AxiosError(r,axios.AxiosError.ETIMEDOUT,n,t)),r==="downloadFile:fail"&&s(new axios.AxiosError(r,axios.AxiosError.ERR_NETWORK,n,t))),s(new axios.AxiosError(o.errMsg,void 0,n,t)),t=null;},complete(){u.unsubscribe();}});typeof e.onDownloadProgress=="function"&&t.onProgressUpdate(q(e.onDownloadProgress,!0)),typeof e.onHeadersReceived=="function"&&t.onHeadersReceived(e.onHeadersReceived),u.subscribe(t,s);}),b=K;var z=(e,i)=>new Promise((d,s)=>{let a=c(e),n=e;n.headers=new axios.AxiosHeaders(a.header);let u=new f(e),t=uni.uploadFile({...a,success(o){var p;if(!t)return;let r={config:n,data:o.data,headers:{},status:o.statusCode,statusText:(p=o.errMsg)!=null?p:"OK",request:t};W__default.default(d,s,r),t=null;},fail(o){let{errMsg:r=""}=o!=null?o:{};if(r){let p=r==="uploadFile:fail timeout",l=r==="uploadFile:fail file error";p&&s(new axios.AxiosError(r,axios.AxiosError.ETIMEDOUT,n,t)),l&&s(new axios.AxiosError(r,axios.AxiosError.ERR_NETWORK,n,t));}s(new axios.AxiosError(o.errMsg,void 0,n,t)),t=null;},complete(){u.unsubscribe();}});typeof e.onHeadersReceived=="function"&&t.onHeadersReceived(e.onHeadersReceived),u.subscribe(t,s);}),y=z;var G=(e,i)=>new Promise((d,s)=>{let a=c(e),n=e;n.headers=new axios.AxiosHeaders(a.header);let u=new f(e),t=uni.request({...a,success(o){var l;if(!t)return;let r=new axios.AxiosHeaders(o.header),p={config:n,data:o.data,headers:r,status:o.statusCode,statusText:(l=o.errMsg)!=null?l:"OK",request:t,cookies:o.cookies};W__default.default(d,s,p),t=null;},fail(o){let{errMsg:r=""}=o!=null?o:{};if(r){let p=r==="request:fail timeout",l=r==="request:fail";p&&s(new axios.AxiosError(r,axios.AxiosError.ETIMEDOUT,n,t)),l&&s(new axios.AxiosError(r,axios.AxiosError.ERR_NETWORK,n,t));}s(new axios.AxiosError(o.errMsg,void 0,n,t)),t=null;},complete(){u.unsubscribe();}});typeof e.onHeadersReceived=="function"&&t.onHeadersReceived(e.onHeadersReceived),u.subscribe(t,s);}),O=G;var v=e=>{switch(g(e)){case"download":return b;case"upload":return y;default:return O}};var Pe=(e={})=>{let i=T(e);return axios.Axios.prototype.download=function(s,a){return this.request({url:s,method:"download",...a})},axios.Axios.prototype.upload=function(s,a,n){return this.request({url:s,method:"upload",data:a,...n})},s=>v(s)(s,i)};
|
|
17
17
|
|
|
18
|
-
exports.createUniAppAxiosAdapter =
|
|
18
|
+
exports.createUniAppAxiosAdapter = Pe;
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { Axios, AxiosError, CanceledError } from 'axios';
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
1
|
+
import { Axios, AxiosHeaders, AxiosError, CanceledError } from 'axios';
|
|
2
|
+
import I from 'axios/unsafe/helpers/buildURL';
|
|
3
|
+
import N from 'axios/unsafe/helpers/speedometer';
|
|
4
|
+
import P from 'axios/unsafe/core/buildFullPath';
|
|
5
5
|
import W from 'axios/unsafe/core/settle';
|
|
6
6
|
|
|
7
|
-
var
|
|
7
|
+
var g=e=>{let{method:i="GET"}=e;switch(i.toLocaleLowerCase()){case"download":return "download";case"upload":return "upload";default:return "request"}},T=e=>({...e}),c=(e,i)=>{var A,w;let d=e.data,s=e.responseType==="arraybuffer"?"arraybuffer":"text",a=s==="text"?"json":void 0,{headers:n,baseURL:u,...t}=e,o=AxiosHeaders.from(n).normalize(!1);if(e.auth){let C=e.auth.username||"",H=e.auth.password?unescape(encodeURIComponent(e.auth.password)):"";o.set("Authorization","Basic "+btoa(C+":"+H));}let r=P(u,e.url),p=(w=(A=e==null?void 0:e.method)==null?void 0:A.toUpperCase())!=null?w:"GET",l=I(r,e.params,e.paramsSerializer),U=e.timeout||6e4,x={};if(d&&typeof d=="string")try{x=JSON.parse(d);}catch(C){}let k=o.toJSON();return {...t,url:l,data:d,header:k,method:p,responseType:s,dataType:a,timeout:U,formData:x}},q=(e,i)=>{let d=0,s=N(50,250);return a=>{let n=a.totalBytesWritten,u=a.totalBytesExpectedToWrite,t=n-d,o=s(t),r=n<=u;d=n;let p={loaded:n,total:u,progress:u?n/u:void 0,bytes:t,rate:o||void 0,estimated:o&&u&&r?(u-n)/o:void 0,event:a};p[i?"download":"upload"]=!0,e(p);}};var f=class{constructor(i){this.config=i;}subscribe(i,d){(this.config.cancelToken||this.config.signal)&&(this.onCanceled=s=>{i&&(d(!s||s.type?new CanceledError(void 0,void 0,this.config,i):s),i.abort(),i=null);},this.config.cancelToken&&this.config.cancelToken.subscribe(this.onCanceled),this.config.signal&&this.config.signal.addEventListener&&(this.config.signal.aborted?this.onCanceled():this.config.signal.addEventListener("abort",this.onCanceled)));}unsubscribe(){this.config.cancelToken&&this.config.cancelToken.unsubscribe(this.onCanceled),this.config.signal&&this.config.signal.removeEventListener&&this.config.signal.removeEventListener("abort",this.onCanceled);}};var K=(e,i)=>new Promise((d,s)=>{let a=c(e),n=e;n.headers=new AxiosHeaders(a.header);let u=new f(e),t=uni.downloadFile({...a,success(o){var p;if(!t)return;let r={config:n,data:o.tempFilePath,headers:{},status:o.statusCode,statusText:(p=o.errMsg)!=null?p:"OK",request:t};W(d,s,r),t=null;},fail(o){let{errMsg:r=""}=o!=null?o:{};r&&(r==="downloadFile:fail timeout"&&s(new AxiosError(r,AxiosError.ETIMEDOUT,n,t)),r==="downloadFile:fail"&&s(new AxiosError(r,AxiosError.ERR_NETWORK,n,t))),s(new AxiosError(o.errMsg,void 0,n,t)),t=null;},complete(){u.unsubscribe();}});typeof e.onDownloadProgress=="function"&&t.onProgressUpdate(q(e.onDownloadProgress,!0)),typeof e.onHeadersReceived=="function"&&t.onHeadersReceived(e.onHeadersReceived),u.subscribe(t,s);}),b=K;var z=(e,i)=>new Promise((d,s)=>{let a=c(e),n=e;n.headers=new AxiosHeaders(a.header);let u=new f(e),t=uni.uploadFile({...a,success(o){var p;if(!t)return;let r={config:n,data:o.data,headers:{},status:o.statusCode,statusText:(p=o.errMsg)!=null?p:"OK",request:t};W(d,s,r),t=null;},fail(o){let{errMsg:r=""}=o!=null?o:{};if(r){let p=r==="uploadFile:fail timeout",l=r==="uploadFile:fail file error";p&&s(new AxiosError(r,AxiosError.ETIMEDOUT,n,t)),l&&s(new AxiosError(r,AxiosError.ERR_NETWORK,n,t));}s(new AxiosError(o.errMsg,void 0,n,t)),t=null;},complete(){u.unsubscribe();}});typeof e.onHeadersReceived=="function"&&t.onHeadersReceived(e.onHeadersReceived),u.subscribe(t,s);}),y=z;var G=(e,i)=>new Promise((d,s)=>{let a=c(e),n=e;n.headers=new AxiosHeaders(a.header);let u=new f(e),t=uni.request({...a,success(o){var l;if(!t)return;let r=new AxiosHeaders(o.header),p={config:n,data:o.data,headers:r,status:o.statusCode,statusText:(l=o.errMsg)!=null?l:"OK",request:t,cookies:o.cookies};W(d,s,p),t=null;},fail(o){let{errMsg:r=""}=o!=null?o:{};if(r){let p=r==="request:fail timeout",l=r==="request:fail";p&&s(new AxiosError(r,AxiosError.ETIMEDOUT,n,t)),l&&s(new AxiosError(r,AxiosError.ERR_NETWORK,n,t));}s(new AxiosError(o.errMsg,void 0,n,t)),t=null;},complete(){u.unsubscribe();}});typeof e.onHeadersReceived=="function"&&t.onHeadersReceived(e.onHeadersReceived),u.subscribe(t,s);}),O=G;var v=e=>{switch(g(e)){case"download":return b;case"upload":return y;default:return O}};var Pe=(e={})=>{let i=T(e);return Axios.prototype.download=function(s,a){return this.request({url:s,method:"download",...a})},Axios.prototype.upload=function(s,a,n){return this.request({url:s,method:"upload",data:a,...n})},s=>v(s)(s,i)};
|
|
8
8
|
|
|
9
|
-
export {
|
|
9
|
+
export { Pe as createUniAppAxiosAdapter };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uni-helper/axios-adapter",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.5.
|
|
4
|
+
"version": "1.5.2",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "kejunmao",
|
|
@@ -70,7 +70,8 @@
|
|
|
70
70
|
"rimraf": "^5.0.1",
|
|
71
71
|
"tsup": "^7.2.0",
|
|
72
72
|
"typescript": "^5.2.2",
|
|
73
|
-
"vite": "^4.4.9"
|
|
73
|
+
"vite": "^4.4.9",
|
|
74
|
+
"vitest": "^0.34.4"
|
|
74
75
|
},
|
|
75
76
|
"peerDependencies": {
|
|
76
77
|
"axios": "^1.5.0"
|