@uni-helper/axios-adapter 1.5.1 → 1.5.3

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 CHANGED
@@ -36,74 +36,76 @@ pnpm i @uni-helper/axios-adapter axios
36
36
  ### 基本用法
37
37
 
38
38
  ```ts
39
- import axios from "axios";
40
- import { createUniAppAxiosAdapter } from "@uni-helper/axios-adapter";
39
+ import axios from 'axios'
40
+ import { createUniAppAxiosAdapter } from '@uni-helper/axios-adapter'
41
41
 
42
- axios.defaults.adapter = createUniAppAxiosAdapter();
42
+ axios.defaults.adapter = createUniAppAxiosAdapter()
43
43
  ```
44
44
 
45
45
  或者创建自定义实例
46
46
 
47
47
  ```ts
48
- import axios from "axios";
49
- import { createUniAppAxiosAdapter } from "@uni-helper/axios-adapter";
48
+ import axios from 'axios'
49
+ import { createUniAppAxiosAdapter } from '@uni-helper/axios-adapter'
50
50
 
51
- const instance = axios.create({ adapter: createUniAppAxiosAdapter() });
51
+ const instance = axios.create({ adapter: createUniAppAxiosAdapter() })
52
52
  ```
53
53
 
54
54
  ### 与 [useAxios](https://vueuse.org/integrations/useAxios/) 一起使用
55
55
 
56
56
  ```ts
57
- import axios from "axios";
58
- import { createUniAppAxiosAdapter } from "@uni-helper/axios-adapter";
57
+ import axios from 'axios'
58
+ import { createUniAppAxiosAdapter } from '@uni-helper/axios-adapter'
59
59
 
60
- axios.defaults.adapter = createUniAppAxiosAdapter();
61
- const { data, isFinished } = useAxios("/posts");
60
+ axios.defaults.adapter = createUniAppAxiosAdapter()
61
+ const { data, isFinished } = useAxios('/posts')
62
62
  ```
63
63
 
64
64
  ### 上传和下载
65
65
 
66
66
  ```ts
67
67
  // 下载
68
- axios.download("/");
68
+ axios.download('/')
69
69
  // or
70
70
  axios.request({
71
- url: "/",
72
- method: "download",
73
- });
71
+ url: '/',
72
+ method: 'download',
73
+ })
74
74
 
75
75
  // 上传
76
- axios.upload("/", new File([new Blob()], "fake file"));
76
+ axios.upload('/', new File([new Blob()], 'fake file'))
77
77
  // or
78
78
  axios.request({
79
- url: "/",
80
- method: "upload",
81
- data: new File([new Blob()], "fake file"),
82
- });
79
+ url: '/',
80
+ method: 'upload',
81
+ data: new File([new Blob()], 'fake file'),
82
+ })
83
83
  ```
84
84
 
85
85
  ### 小程序
86
86
 
87
- axios 依赖了 `FormData` 和 `Blob` 对象, 而小程序没有,使用提供的插件来解决这一问题
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
- import uniAxiosAdapter from "@uni-helper/axios-adapter/vite";
96
+ import uniAxiosAdapter from '@uni-helper/axios-adapter/vite'
92
97
 
93
98
  export default {
94
99
  plugins: [
95
- ...
96
- uniAxiosAdapter()
97
- ...
98
- ],
100
+ uniAxiosAdapter()
101
+ ]
99
102
  }
100
103
  ```
101
104
 
102
- > [!WARNING]
103
- > 这个插件通过将 `FormData` 和 `Blob` 导出为空 `class`来解决兼容性问题,如果你确实需要的话,使用 `pnpm add miniprogram-formdata miniprogram-blob` 来安装对应的 polyfill 即可,插件会自动使用。
104
-
105
105
  如果你使用的是 Vue CLI,改用 `@uni-helper/axios-adapter/webpack` 即可
106
106
 
107
+ </details>
108
+
107
109
  ### 版本
108
110
 
109
111
  自 1.4.0 开始,请始终保持主版本号和次版本号与 axios 一致。例如当你安装了 1.5.1 版本时,你可以安装 axios 的 1.5.x 版本。
@@ -0,0 +1,43 @@
1
+ 'use strict';
2
+
3
+ var process = require('process');
4
+ var unplugin$1 = require('unplugin');
5
+ var localPkg = require('local-pkg');
6
+
7
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
8
+
9
+ var process__default = /*#__PURE__*/_interopDefault(process);
10
+
11
+ // src/unplugin.ts
12
+ var unplugin = unplugin$1.createUnplugin((_options = {}) => {
13
+ const hasFormDataPolyfill = localPkg.isPackageExists("miniprogram-formdata");
14
+ const hasBlobPolyfill = localPkg.isPackageExists("miniprogram-blob");
15
+ return {
16
+ name: "unplugin-uni-axios-adapter",
17
+ enforce: "pre",
18
+ transform(code, id) {
19
+ var _a;
20
+ if ((_a = process__default.default.env.UNI_PLATFORM) == null ? void 0 : _a.includes("mp")) {
21
+ if (id.includes("/form-data/lib/browser.js")) {
22
+ return {
23
+ code: code.replace("window", "globalThis")
24
+ };
25
+ }
26
+ if (id.includes("/axios/lib/platform/browser/classes/FormData.js")) {
27
+ return {
28
+ code: `${hasFormDataPolyfill ? "import FormData from 'miniprogram-formdata';" : "class FormData {};"}
29
+ export default FormData;`
30
+ };
31
+ }
32
+ if (id.includes("/axios/lib/platform/browser/classes/Blob.js")) {
33
+ return {
34
+ code: `${hasBlobPolyfill ? "import Blob from 'miniprogram-blob';" : "class Blob {};"}
35
+ export default Blob;`
36
+ };
37
+ }
38
+ }
39
+ }
40
+ };
41
+ });
42
+
43
+ exports.unplugin = unplugin;
@@ -0,0 +1,37 @@
1
+ import process from 'process';
2
+ import { createUnplugin } from 'unplugin';
3
+ import { isPackageExists } from 'local-pkg';
4
+
5
+ // src/unplugin.ts
6
+ var unplugin = createUnplugin((_options = {}) => {
7
+ const hasFormDataPolyfill = isPackageExists("miniprogram-formdata");
8
+ const hasBlobPolyfill = isPackageExists("miniprogram-blob");
9
+ return {
10
+ name: "unplugin-uni-axios-adapter",
11
+ enforce: "pre",
12
+ transform(code, id) {
13
+ var _a;
14
+ if ((_a = process.env.UNI_PLATFORM) == null ? void 0 : _a.includes("mp")) {
15
+ if (id.includes("/form-data/lib/browser.js")) {
16
+ return {
17
+ code: code.replace("window", "globalThis")
18
+ };
19
+ }
20
+ if (id.includes("/axios/lib/platform/browser/classes/FormData.js")) {
21
+ return {
22
+ code: `${hasFormDataPolyfill ? "import FormData from 'miniprogram-formdata';" : "class FormData {};"}
23
+ export default FormData;`
24
+ };
25
+ }
26
+ if (id.includes("/axios/lib/platform/browser/classes/Blob.js")) {
27
+ return {
28
+ code: `${hasBlobPolyfill ? "import Blob from 'miniprogram-blob';" : "class Blob {};"}
29
+ export default Blob;`
30
+ };
31
+ }
32
+ }
33
+ }
34
+ };
35
+ });
36
+
37
+ export { unplugin };
package/dist/index.cjs CHANGED
@@ -1,18 +1,359 @@
1
1
  'use strict';
2
2
 
3
3
  var axios = require('axios');
4
- var D = require('axios/unsafe/helpers/buildURL');
5
- var I = require('axios/unsafe/helpers/speedometer');
6
- var F = require('axios/unsafe/core/buildFullPath');
7
- var W = require('axios/unsafe/core/settle');
4
+ var buildURL = require('axios/unsafe/helpers/buildURL');
5
+ var speedometer = require('axios/unsafe/helpers/speedometer');
6
+ var buildFullPath = require('axios/unsafe/core/buildFullPath');
7
+ var settle = 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
- var I__default = /*#__PURE__*/_interopDefault(I);
13
- var F__default = /*#__PURE__*/_interopDefault(F);
14
- var W__default = /*#__PURE__*/_interopDefault(W);
11
+ var buildURL__default = /*#__PURE__*/_interopDefault(buildURL);
12
+ var speedometer__default = /*#__PURE__*/_interopDefault(speedometer);
13
+ var buildFullPath__default = /*#__PURE__*/_interopDefault(buildFullPath);
14
+ var settle__default = /*#__PURE__*/_interopDefault(settle);
15
15
 
16
- var q=e=>{let{method:n="GET"}=e;switch(n.toLocaleLowerCase()){case"download":return "download";case"upload":return "upload";default:return "request"}},M=e=>({...e}),A=(e,n)=>{var o,s,u,w,E;let i=e.data,t=e.responseType==="arraybuffer"?"arraybuffer":"text",p=t==="text"?"json":void 0,a=e.headers;if(e.auth){let v=e.auth.username||"",P=e.auth.password?unescape(encodeURIComponent(e.auth.password)):"";a.set("Authorization","Basic "+btoa(v+":"+P));}let l=F__default.default(e.baseURL,e.url),h=(s=(o=e==null?void 0:e.method)==null?void 0:o.toUpperCase())!=null?s:"GET",f=D__default.default(l,e.params,e.paramsSerializer),r=e.timeout||6e4,d=(u=e.withCredentials)!=null?u:!1,c=(w=e.sslVerify)!=null?w:!0,R=(E=e.firstIpv4)!=null?E:!1,T={};if(i&&typeof i=="string")try{T=JSON.parse(i);}catch(v){}let m=a.toJSON();return {...e,url:f,data:i,header:m,method:h,responseType:t,dataType:p,timeout:r,withCredentials:d,sslVerify:c,firstIpv4:R,formData:T}},O=(e,n)=>{let i=0,t=I__default.default(50,250);return p=>{let a=p.totalBytesWritten,l=p.totalBytesExpectedToWrite,h=a-i,f=t(h),r=a<=l;i=a;let d={loaded:a,total:l,progress:l?a/l:void 0,bytes:h,rate:f||void 0,estimated:f&&l&&r?(l-a)/f:void 0,event:p};d[n?"download":"upload"]=!0,e(d);}};var x=class{constructor(n){this.config=n;}subscribe(n,i){(this.config.cancelToken||this.config.signal)&&(this.onCanceled=t=>{n&&(i(!t||t.type?new axios.CanceledError(void 0,void 0,this.config,n):t),n.abort(),n=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,n)=>new Promise((i,t)=>{let{url:p,header:a,timeout:l,filePath:h}=A(e),f=new x(e),r=uni.downloadFile({url:p,header:a,timeout:l,filePath:h,success(d){var R;if(!r)return;let c={config:e,data:d.tempFilePath,headers:{},status:d.statusCode,statusText:(R=d.errMsg)!=null?R:"OK",request:r};W__default.default(i,t,c),r=null;},fail(d){let{errMsg:c=""}=d!=null?d:{};c&&(c==="downloadFile:fail timeout"&&t(new axios.AxiosError(c,axios.AxiosError.ETIMEDOUT,e,r)),c==="downloadFile:fail"&&t(new axios.AxiosError(c,axios.AxiosError.ERR_NETWORK,e,r))),t(new axios.AxiosError(d.errMsg,void 0,e,r)),r=null;},complete(){f.unsubscribe();}});typeof e.onDownloadProgress=="function"&&r.onProgressUpdate(O(e.onDownloadProgress,!0)),typeof e.onHeadersReceived=="function"&&r.onHeadersReceived(e.onHeadersReceived),f.subscribe(r,t);}),U=K;var _=(e,n)=>new Promise((i,t)=>{let{url:p,files:a,fileType:l,file:h,filePath:f,name:r,header:d,timeout:c,formData:R}=A(e),T=new x(e),m=uni.uploadFile({url:p,files:a,fileType:l,file:h,filePath:f,name:r,header:d,timeout:c,formData:R,success(o){var u;if(!m)return;let s={config:e,data:o.data,headers:{},status:o.statusCode,statusText:(u=o.errMsg)!=null?u:"OK",request:m};W__default.default(i,t,s),m=null;},fail(o){let{errMsg:s=""}=o!=null?o:{};if(s){let u=s==="uploadFile:fail timeout",w=s==="uploadFile:fail file error";u&&t(new axios.AxiosError(s,axios.AxiosError.ETIMEDOUT,e,m)),w&&t(new axios.AxiosError(s,axios.AxiosError.ERR_NETWORK,e,m));}t(new axios.AxiosError(o.errMsg,void 0,e,m)),m=null;},complete(){T.unsubscribe();}});typeof e.onHeadersReceived=="function"&&m.onHeadersReceived(e.onHeadersReceived),T.subscribe(m,t);}),k=_;var V=(e,n)=>new Promise((i,t)=>{let{url:p,data:a,header:l,method:h,timeout:f,dataType:r,responseType:d,sslVerify:c,withCredentials:R,firstIpv4:T}=A(e),m=new x(e),o=uni.request({url:p,data:a,header:l,method:h,timeout:f,dataType:r,responseType:d,sslVerify:c,withCredentials:R,firstIpv4:T,success(s){var w;if(!o)return;let u={config:e,data:s.data,headers:s.header,status:s.statusCode,statusText:(w=s.errMsg)!=null?w:"OK",request:o,cookies:s.cookies};W__default.default(i,t,u),o=null;},fail(s){let{errMsg:u=""}=s!=null?s:{};if(u){let w=u==="request:fail timeout",E=u==="request:fail";w&&t(new axios.AxiosError(u,axios.AxiosError.ETIMEDOUT,e,o)),E&&t(new axios.AxiosError(u,axios.AxiosError.ERR_NETWORK,e,o));}t(new axios.AxiosError(s.errMsg,void 0,e,o)),o=null;},complete(){m.unsubscribe();}});typeof e.onHeadersReceived=="function"&&o.onHeadersReceived(e.onHeadersReceived),m.subscribe(o,t);}),g=V;var H=e=>{switch(q(e)){case"download":return U;case"upload":return k;default:return g}};var Me=(e={})=>{let n=M(e);return axios.Axios.prototype.download=function(t,p){return this.request({url:t,method:"download",...p})},axios.Axios.prototype.upload=function(t,p,a){return this.request({url:t,method:"upload",data:p,...a})},t=>H(t)(t,n)};
16
+ // src/index.ts
17
+ function getMethodType(config) {
18
+ const { method: rawMethod = "GET" } = config;
19
+ const method = rawMethod.toLocaleLowerCase();
20
+ switch (method) {
21
+ case "download":
22
+ return "download";
23
+ case "upload":
24
+ return "upload";
25
+ default:
26
+ return "request";
27
+ }
28
+ }
29
+ function resolveOptions(userOptions) {
30
+ return {
31
+ ...userOptions
32
+ };
33
+ }
34
+ function resolveUniAppRequestOptions(config, _options) {
35
+ var _a, _b;
36
+ const data = config.data;
37
+ const responseType = config.responseType === "arraybuffer" ? "arraybuffer" : "text";
38
+ const dataType = responseType === "text" ? "json" : void 0;
39
+ const { headers, baseURL, ...requestConfig } = config;
40
+ const requestHeaders = axios.AxiosHeaders.from(serializeObject(headers)).normalize(false);
41
+ if (config.auth) {
42
+ const username = config.auth.username || "";
43
+ const password = config.auth.password ? unescape(encodeURIComponent(config.auth.password)) : "";
44
+ requestHeaders.set(
45
+ "Authorization",
46
+ `Basic ${btoa(`${username}:${password}`)}`
47
+ );
48
+ }
49
+ const fullPath = buildFullPath__default.default(baseURL, config.url);
50
+ const method = (_b = (_a = config == null ? void 0 : config.method) == null ? void 0 : _a.toUpperCase()) != null ? _b : "GET";
51
+ const url = buildURL__default.default(fullPath, config.params, config.paramsSerializer);
52
+ const timeout = config.timeout || 6e4;
53
+ let formData = {};
54
+ if (data && typeof data === "string") {
55
+ try {
56
+ formData = JSON.parse(data);
57
+ } catch (error) {
58
+ }
59
+ }
60
+ const header = requestHeaders.toJSON();
61
+ return {
62
+ ...requestConfig,
63
+ url,
64
+ data,
65
+ header,
66
+ method,
67
+ responseType,
68
+ dataType,
69
+ timeout,
70
+ formData
71
+ };
72
+ }
73
+ function progressEventReducer(listener, isDownloadStream) {
74
+ let bytesNotified = 0;
75
+ const _speedometer = speedometer__default.default(50, 250);
76
+ return (result) => {
77
+ const loaded = result.totalBytesWritten;
78
+ const total = result.totalBytesExpectedToWrite;
79
+ const progressBytes = loaded - bytesNotified;
80
+ const rate = _speedometer(progressBytes);
81
+ const inRange = loaded <= total;
82
+ bytesNotified = loaded;
83
+ const data = {
84
+ loaded,
85
+ total,
86
+ progress: total ? loaded / total : void 0,
87
+ bytes: progressBytes,
88
+ rate: rate || void 0,
89
+ estimated: rate && total && inRange ? (total - loaded) / rate : void 0,
90
+ event: result
91
+ };
92
+ data[isDownloadStream ? "download" : "upload"] = true;
93
+ listener(data);
94
+ };
95
+ }
96
+ function serializeObject(sourceObj, options) {
97
+ const resultObj = /* @__PURE__ */ Object.create(null);
98
+ if (!sourceObj)
99
+ return resultObj;
100
+ const { asStrings = false } = options || {};
101
+ forEach(sourceObj, (value, key) => {
102
+ if (value != null && value !== false) {
103
+ resultObj[key] = asStrings && Array.isArray(value) ? value.join(", ") : value;
104
+ }
105
+ });
106
+ return resultObj;
107
+ }
108
+ function forEach(obj, fn, { allOwnKeys = false } = {}) {
109
+ if (obj === null || typeof obj === "undefined")
110
+ return;
111
+ if (typeof obj !== "object")
112
+ obj = [obj];
113
+ if (Array.isArray(obj)) {
114
+ for (let i = 0, l = obj.length; i < l; i++) {
115
+ fn(obj[i], i, obj);
116
+ }
117
+ } else {
118
+ const keys = allOwnKeys ? Object.getOwnPropertyNames(obj) : Object.keys(obj);
119
+ const len = keys.length;
120
+ let key;
121
+ for (let i = 0; i < len; i++) {
122
+ key = keys[i];
123
+ fn(obj[key], key, obj);
124
+ }
125
+ }
126
+ }
127
+ var OnCanceled = class {
128
+ constructor(config) {
129
+ this.config = config;
130
+ }
131
+ subscribe(task, reject) {
132
+ if (this.config.cancelToken || this.config.signal) {
133
+ this.onCanceled = (cancel) => {
134
+ if (!task)
135
+ return;
136
+ reject(
137
+ !cancel || cancel.type ? new axios.CanceledError(void 0, void 0, this.config, task) : cancel
138
+ );
139
+ task.abort();
140
+ task = null;
141
+ };
142
+ if (this.config.cancelToken) {
143
+ this.config.cancelToken.subscribe(this.onCanceled);
144
+ }
145
+ if (this.config.signal && this.config.signal.addEventListener) {
146
+ this.config.signal.aborted ? this.onCanceled() : this.config.signal.addEventListener("abort", this.onCanceled);
147
+ }
148
+ }
149
+ }
150
+ unsubscribe() {
151
+ if (this.config.cancelToken) {
152
+ this.config.cancelToken.unsubscribe(this.onCanceled);
153
+ }
154
+ if (this.config.signal && this.config.signal.removeEventListener)
155
+ this.config.signal.removeEventListener("abort", this.onCanceled);
156
+ }
157
+ };
17
158
 
18
- exports.createUniAppAxiosAdapter = Me;
159
+ // src/methods/download.ts
160
+ var download = (config, options) => {
161
+ return new Promise((resolve, reject) => {
162
+ const requestOptions = resolveUniAppRequestOptions(
163
+ config);
164
+ const responseConfig = config;
165
+ responseConfig.headers = new axios.AxiosHeaders(requestOptions.header);
166
+ const onCanceled = new OnCanceled(config);
167
+ let task = uni.downloadFile({
168
+ ...requestOptions,
169
+ success(result) {
170
+ var _a;
171
+ if (!task)
172
+ return;
173
+ const response = {
174
+ config: responseConfig,
175
+ data: result.tempFilePath,
176
+ headers: {},
177
+ status: result.statusCode,
178
+ statusText: (_a = result.errMsg) != null ? _a : "OK",
179
+ request: task
180
+ };
181
+ settle__default.default(resolve, reject, response);
182
+ task = null;
183
+ },
184
+ fail(error) {
185
+ const { errMsg = "" } = error != null ? error : {};
186
+ if (errMsg) {
187
+ const isTimeoutError = errMsg === "downloadFile:fail timeout";
188
+ if (isTimeoutError)
189
+ reject(new axios.AxiosError(errMsg, axios.AxiosError.ETIMEDOUT, responseConfig, task));
190
+ const isNetworkError = errMsg === "downloadFile:fail";
191
+ if (isNetworkError) {
192
+ reject(
193
+ new axios.AxiosError(errMsg, axios.AxiosError.ERR_NETWORK, responseConfig, task)
194
+ );
195
+ }
196
+ }
197
+ reject(new axios.AxiosError(error.errMsg, void 0, responseConfig, task));
198
+ task = null;
199
+ },
200
+ complete() {
201
+ onCanceled.unsubscribe();
202
+ }
203
+ });
204
+ if (typeof config.onDownloadProgress === "function") {
205
+ task.onProgressUpdate(
206
+ progressEventReducer(config.onDownloadProgress, true)
207
+ );
208
+ }
209
+ if (typeof config.onHeadersReceived === "function")
210
+ task.onHeadersReceived(config.onHeadersReceived);
211
+ onCanceled.subscribe(task, reject);
212
+ });
213
+ };
214
+ var download_default = download;
215
+ var upload = (config, options) => {
216
+ return new Promise((resolve, reject) => {
217
+ const requestOptions = resolveUniAppRequestOptions(config);
218
+ const responseConfig = config;
219
+ responseConfig.headers = new axios.AxiosHeaders(requestOptions.header);
220
+ const onCanceled = new OnCanceled(config);
221
+ let task = uni.uploadFile({
222
+ ...requestOptions,
223
+ success(result) {
224
+ var _a;
225
+ if (!task)
226
+ return;
227
+ const response = {
228
+ config: responseConfig,
229
+ data: result.data,
230
+ headers: {},
231
+ status: result.statusCode,
232
+ statusText: (_a = result.errMsg) != null ? _a : "OK",
233
+ request: task
234
+ };
235
+ settle__default.default(resolve, reject, response);
236
+ task = null;
237
+ },
238
+ fail(error) {
239
+ const { errMsg = "" } = error != null ? error : {};
240
+ if (errMsg) {
241
+ const isTimeoutError = errMsg === "uploadFile:fail timeout";
242
+ const isNetworkError = errMsg === "uploadFile:fail file error";
243
+ if (isTimeoutError)
244
+ reject(new axios.AxiosError(errMsg, axios.AxiosError.ETIMEDOUT, responseConfig, task));
245
+ if (isNetworkError) {
246
+ reject(
247
+ new axios.AxiosError(errMsg, axios.AxiosError.ERR_NETWORK, responseConfig, task)
248
+ );
249
+ }
250
+ }
251
+ reject(new axios.AxiosError(error.errMsg, void 0, responseConfig, task));
252
+ task = null;
253
+ },
254
+ complete() {
255
+ onCanceled.unsubscribe();
256
+ }
257
+ });
258
+ if (typeof config.onHeadersReceived === "function")
259
+ task.onHeadersReceived(config.onHeadersReceived);
260
+ onCanceled.subscribe(task, reject);
261
+ });
262
+ };
263
+ var upload_default = upload;
264
+ var request = (config, options) => {
265
+ return new Promise((resolve, reject) => {
266
+ const requestOptions = resolveUniAppRequestOptions(config);
267
+ const responseConfig = config;
268
+ responseConfig.headers = new axios.AxiosHeaders(requestOptions.header);
269
+ const onCanceled = new OnCanceled(config);
270
+ let task = uni.request({
271
+ ...requestOptions,
272
+ success(result) {
273
+ var _a;
274
+ if (!task)
275
+ return;
276
+ if (Array.isArray(result.header)) {
277
+ const dingHeader = {};
278
+ result.header.forEach((h) => Object.assign(dingHeader, h));
279
+ result.header = dingHeader;
280
+ }
281
+ const headers = new axios.AxiosHeaders(result.header);
282
+ const response = {
283
+ config: responseConfig,
284
+ data: result.data,
285
+ headers,
286
+ status: result.statusCode,
287
+ statusText: (_a = result.errMsg) != null ? _a : "OK",
288
+ request: task,
289
+ cookies: result.cookies
290
+ };
291
+ settle__default.default(resolve, reject, response);
292
+ task = null;
293
+ },
294
+ fail(error) {
295
+ const { errMsg = "" } = error != null ? error : {};
296
+ if (errMsg) {
297
+ const isTimeoutError = errMsg === "request:fail timeout";
298
+ const isNetworkError = errMsg === "request:fail";
299
+ if (isTimeoutError)
300
+ reject(new axios.AxiosError(errMsg, axios.AxiosError.ETIMEDOUT, responseConfig, task));
301
+ if (isNetworkError) {
302
+ reject(
303
+ new axios.AxiosError(errMsg, axios.AxiosError.ERR_NETWORK, responseConfig, task)
304
+ );
305
+ }
306
+ }
307
+ reject(new axios.AxiosError(error.errMsg, void 0, responseConfig, task));
308
+ task = null;
309
+ },
310
+ complete() {
311
+ onCanceled.unsubscribe();
312
+ }
313
+ });
314
+ if (typeof config.onHeadersReceived === "function")
315
+ task.onHeadersReceived(config.onHeadersReceived);
316
+ onCanceled.subscribe(task, reject);
317
+ });
318
+ };
319
+ var request_default = request;
320
+
321
+ // src/methods/index.ts
322
+ function getMethod(config) {
323
+ const methodType = getMethodType(config);
324
+ switch (methodType) {
325
+ case "download":
326
+ return download_default;
327
+ case "upload":
328
+ return upload_default;
329
+ default:
330
+ return request_default;
331
+ }
332
+ }
333
+
334
+ // src/index.ts
335
+ function createUniAppAxiosAdapter(userOptions = {}) {
336
+ const options = resolveOptions(userOptions);
337
+ axios.Axios.prototype.download = function(url, config) {
338
+ return this.request({
339
+ url,
340
+ method: "download",
341
+ ...config
342
+ });
343
+ };
344
+ axios.Axios.prototype.upload = function(url, data, config) {
345
+ return this.request({
346
+ url,
347
+ method: "upload",
348
+ data,
349
+ ...config
350
+ });
351
+ };
352
+ const uniappAdapter = (config) => {
353
+ const method = getMethod(config);
354
+ return method(config, options);
355
+ };
356
+ return uniappAdapter;
357
+ }
358
+
359
+ exports.createUniAppAxiosAdapter = createUniAppAxiosAdapter;
package/dist/index.d.cts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { AxiosAdapter } from 'axios';
2
2
  import { U as UserOptions } from './types-76de936f.js';
3
3
 
4
- declare module "axios" {
5
- interface AxiosRequestConfig extends Omit<UniApp.RequestOptions, "success" | "fail" | "complete" | "header">, Omit<UniApp.UploadFileOption, "success" | "fail" | "complete" | "header" | "formData">, Omit<UniApp.DownloadFileOption, "success" | "fail" | "complete" | "header">, Partial<Pick<UniApp.RequestTask, "onHeadersReceived">> {
4
+ declare module 'axios' {
5
+ interface AxiosRequestConfig extends Omit<UniApp.RequestOptions, 'success' | 'fail' | 'complete' | 'header'>, Omit<UniApp.UploadFileOption, 'success' | 'fail' | 'complete' | 'header' | 'formData'>, Omit<UniApp.DownloadFileOption, 'success' | 'fail' | 'complete' | 'header'>, Partial<Pick<UniApp.RequestTask, 'onHeadersReceived'>> {
6
6
  }
7
7
  interface AxiosResponse {
8
8
  cookies?: string[];
@@ -13,6 +13,6 @@ declare module "axios" {
13
13
  }
14
14
  }
15
15
 
16
- declare const createUniAppAxiosAdapter: (userOptions?: UserOptions) => AxiosAdapter;
16
+ declare function createUniAppAxiosAdapter(userOptions?: UserOptions): AxiosAdapter;
17
17
 
18
18
  export { createUniAppAxiosAdapter };
package/dist/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { AxiosAdapter } from 'axios';
2
2
  import { U as UserOptions } from './types-76de936f.js';
3
3
 
4
- declare module "axios" {
5
- interface AxiosRequestConfig extends Omit<UniApp.RequestOptions, "success" | "fail" | "complete" | "header">, Omit<UniApp.UploadFileOption, "success" | "fail" | "complete" | "header" | "formData">, Omit<UniApp.DownloadFileOption, "success" | "fail" | "complete" | "header">, Partial<Pick<UniApp.RequestTask, "onHeadersReceived">> {
4
+ declare module 'axios' {
5
+ interface AxiosRequestConfig extends Omit<UniApp.RequestOptions, 'success' | 'fail' | 'complete' | 'header'>, Omit<UniApp.UploadFileOption, 'success' | 'fail' | 'complete' | 'header' | 'formData'>, Omit<UniApp.DownloadFileOption, 'success' | 'fail' | 'complete' | 'header'>, Partial<Pick<UniApp.RequestTask, 'onHeadersReceived'>> {
6
6
  }
7
7
  interface AxiosResponse {
8
8
  cookies?: string[];
@@ -13,6 +13,6 @@ declare module "axios" {
13
13
  }
14
14
  }
15
15
 
16
- declare const createUniAppAxiosAdapter: (userOptions?: UserOptions) => AxiosAdapter;
16
+ declare function createUniAppAxiosAdapter(userOptions?: UserOptions): AxiosAdapter;
17
17
 
18
18
  export { createUniAppAxiosAdapter };
package/dist/index.js CHANGED
@@ -1,9 +1,350 @@
1
- import { Axios, AxiosError, CanceledError } from 'axios';
2
- import D from 'axios/unsafe/helpers/buildURL';
3
- import I from 'axios/unsafe/helpers/speedometer';
4
- import F from 'axios/unsafe/core/buildFullPath';
5
- import W from 'axios/unsafe/core/settle';
1
+ import { Axios, AxiosHeaders, AxiosError, CanceledError } from 'axios';
2
+ import buildURL from 'axios/unsafe/helpers/buildURL';
3
+ import speedometer from 'axios/unsafe/helpers/speedometer';
4
+ import buildFullPath from 'axios/unsafe/core/buildFullPath';
5
+ import settle from 'axios/unsafe/core/settle';
6
6
 
7
- var q=e=>{let{method:n="GET"}=e;switch(n.toLocaleLowerCase()){case"download":return "download";case"upload":return "upload";default:return "request"}},M=e=>({...e}),A=(e,n)=>{var o,s,u,w,E;let i=e.data,t=e.responseType==="arraybuffer"?"arraybuffer":"text",p=t==="text"?"json":void 0,a=e.headers;if(e.auth){let v=e.auth.username||"",P=e.auth.password?unescape(encodeURIComponent(e.auth.password)):"";a.set("Authorization","Basic "+btoa(v+":"+P));}let l=F(e.baseURL,e.url),h=(s=(o=e==null?void 0:e.method)==null?void 0:o.toUpperCase())!=null?s:"GET",f=D(l,e.params,e.paramsSerializer),r=e.timeout||6e4,d=(u=e.withCredentials)!=null?u:!1,c=(w=e.sslVerify)!=null?w:!0,R=(E=e.firstIpv4)!=null?E:!1,T={};if(i&&typeof i=="string")try{T=JSON.parse(i);}catch(v){}let m=a.toJSON();return {...e,url:f,data:i,header:m,method:h,responseType:t,dataType:p,timeout:r,withCredentials:d,sslVerify:c,firstIpv4:R,formData:T}},O=(e,n)=>{let i=0,t=I(50,250);return p=>{let a=p.totalBytesWritten,l=p.totalBytesExpectedToWrite,h=a-i,f=t(h),r=a<=l;i=a;let d={loaded:a,total:l,progress:l?a/l:void 0,bytes:h,rate:f||void 0,estimated:f&&l&&r?(l-a)/f:void 0,event:p};d[n?"download":"upload"]=!0,e(d);}};var x=class{constructor(n){this.config=n;}subscribe(n,i){(this.config.cancelToken||this.config.signal)&&(this.onCanceled=t=>{n&&(i(!t||t.type?new CanceledError(void 0,void 0,this.config,n):t),n.abort(),n=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,n)=>new Promise((i,t)=>{let{url:p,header:a,timeout:l,filePath:h}=A(e),f=new x(e),r=uni.downloadFile({url:p,header:a,timeout:l,filePath:h,success(d){var R;if(!r)return;let c={config:e,data:d.tempFilePath,headers:{},status:d.statusCode,statusText:(R=d.errMsg)!=null?R:"OK",request:r};W(i,t,c),r=null;},fail(d){let{errMsg:c=""}=d!=null?d:{};c&&(c==="downloadFile:fail timeout"&&t(new AxiosError(c,AxiosError.ETIMEDOUT,e,r)),c==="downloadFile:fail"&&t(new AxiosError(c,AxiosError.ERR_NETWORK,e,r))),t(new AxiosError(d.errMsg,void 0,e,r)),r=null;},complete(){f.unsubscribe();}});typeof e.onDownloadProgress=="function"&&r.onProgressUpdate(O(e.onDownloadProgress,!0)),typeof e.onHeadersReceived=="function"&&r.onHeadersReceived(e.onHeadersReceived),f.subscribe(r,t);}),U=K;var _=(e,n)=>new Promise((i,t)=>{let{url:p,files:a,fileType:l,file:h,filePath:f,name:r,header:d,timeout:c,formData:R}=A(e),T=new x(e),m=uni.uploadFile({url:p,files:a,fileType:l,file:h,filePath:f,name:r,header:d,timeout:c,formData:R,success(o){var u;if(!m)return;let s={config:e,data:o.data,headers:{},status:o.statusCode,statusText:(u=o.errMsg)!=null?u:"OK",request:m};W(i,t,s),m=null;},fail(o){let{errMsg:s=""}=o!=null?o:{};if(s){let u=s==="uploadFile:fail timeout",w=s==="uploadFile:fail file error";u&&t(new AxiosError(s,AxiosError.ETIMEDOUT,e,m)),w&&t(new AxiosError(s,AxiosError.ERR_NETWORK,e,m));}t(new AxiosError(o.errMsg,void 0,e,m)),m=null;},complete(){T.unsubscribe();}});typeof e.onHeadersReceived=="function"&&m.onHeadersReceived(e.onHeadersReceived),T.subscribe(m,t);}),k=_;var V=(e,n)=>new Promise((i,t)=>{let{url:p,data:a,header:l,method:h,timeout:f,dataType:r,responseType:d,sslVerify:c,withCredentials:R,firstIpv4:T}=A(e),m=new x(e),o=uni.request({url:p,data:a,header:l,method:h,timeout:f,dataType:r,responseType:d,sslVerify:c,withCredentials:R,firstIpv4:T,success(s){var w;if(!o)return;let u={config:e,data:s.data,headers:s.header,status:s.statusCode,statusText:(w=s.errMsg)!=null?w:"OK",request:o,cookies:s.cookies};W(i,t,u),o=null;},fail(s){let{errMsg:u=""}=s!=null?s:{};if(u){let w=u==="request:fail timeout",E=u==="request:fail";w&&t(new AxiosError(u,AxiosError.ETIMEDOUT,e,o)),E&&t(new AxiosError(u,AxiosError.ERR_NETWORK,e,o));}t(new AxiosError(s.errMsg,void 0,e,o)),o=null;},complete(){m.unsubscribe();}});typeof e.onHeadersReceived=="function"&&o.onHeadersReceived(e.onHeadersReceived),m.subscribe(o,t);}),g=V;var H=e=>{switch(q(e)){case"download":return U;case"upload":return k;default:return g}};var Me=(e={})=>{let n=M(e);return Axios.prototype.download=function(t,p){return this.request({url:t,method:"download",...p})},Axios.prototype.upload=function(t,p,a){return this.request({url:t,method:"upload",data:p,...a})},t=>H(t)(t,n)};
7
+ // src/index.ts
8
+ function getMethodType(config) {
9
+ const { method: rawMethod = "GET" } = config;
10
+ const method = rawMethod.toLocaleLowerCase();
11
+ switch (method) {
12
+ case "download":
13
+ return "download";
14
+ case "upload":
15
+ return "upload";
16
+ default:
17
+ return "request";
18
+ }
19
+ }
20
+ function resolveOptions(userOptions) {
21
+ return {
22
+ ...userOptions
23
+ };
24
+ }
25
+ function resolveUniAppRequestOptions(config, _options) {
26
+ var _a, _b;
27
+ const data = config.data;
28
+ const responseType = config.responseType === "arraybuffer" ? "arraybuffer" : "text";
29
+ const dataType = responseType === "text" ? "json" : void 0;
30
+ const { headers, baseURL, ...requestConfig } = config;
31
+ const requestHeaders = AxiosHeaders.from(serializeObject(headers)).normalize(false);
32
+ if (config.auth) {
33
+ const username = config.auth.username || "";
34
+ const password = config.auth.password ? unescape(encodeURIComponent(config.auth.password)) : "";
35
+ requestHeaders.set(
36
+ "Authorization",
37
+ `Basic ${btoa(`${username}:${password}`)}`
38
+ );
39
+ }
40
+ const fullPath = buildFullPath(baseURL, config.url);
41
+ const method = (_b = (_a = config == null ? void 0 : config.method) == null ? void 0 : _a.toUpperCase()) != null ? _b : "GET";
42
+ const url = buildURL(fullPath, config.params, config.paramsSerializer);
43
+ const timeout = config.timeout || 6e4;
44
+ let formData = {};
45
+ if (data && typeof data === "string") {
46
+ try {
47
+ formData = JSON.parse(data);
48
+ } catch (error) {
49
+ }
50
+ }
51
+ const header = requestHeaders.toJSON();
52
+ return {
53
+ ...requestConfig,
54
+ url,
55
+ data,
56
+ header,
57
+ method,
58
+ responseType,
59
+ dataType,
60
+ timeout,
61
+ formData
62
+ };
63
+ }
64
+ function progressEventReducer(listener, isDownloadStream) {
65
+ let bytesNotified = 0;
66
+ const _speedometer = speedometer(50, 250);
67
+ return (result) => {
68
+ const loaded = result.totalBytesWritten;
69
+ const total = result.totalBytesExpectedToWrite;
70
+ const progressBytes = loaded - bytesNotified;
71
+ const rate = _speedometer(progressBytes);
72
+ const inRange = loaded <= total;
73
+ bytesNotified = loaded;
74
+ const data = {
75
+ loaded,
76
+ total,
77
+ progress: total ? loaded / total : void 0,
78
+ bytes: progressBytes,
79
+ rate: rate || void 0,
80
+ estimated: rate && total && inRange ? (total - loaded) / rate : void 0,
81
+ event: result
82
+ };
83
+ data[isDownloadStream ? "download" : "upload"] = true;
84
+ listener(data);
85
+ };
86
+ }
87
+ function serializeObject(sourceObj, options) {
88
+ const resultObj = /* @__PURE__ */ Object.create(null);
89
+ if (!sourceObj)
90
+ return resultObj;
91
+ const { asStrings = false } = options || {};
92
+ forEach(sourceObj, (value, key) => {
93
+ if (value != null && value !== false) {
94
+ resultObj[key] = asStrings && Array.isArray(value) ? value.join(", ") : value;
95
+ }
96
+ });
97
+ return resultObj;
98
+ }
99
+ function forEach(obj, fn, { allOwnKeys = false } = {}) {
100
+ if (obj === null || typeof obj === "undefined")
101
+ return;
102
+ if (typeof obj !== "object")
103
+ obj = [obj];
104
+ if (Array.isArray(obj)) {
105
+ for (let i = 0, l = obj.length; i < l; i++) {
106
+ fn(obj[i], i, obj);
107
+ }
108
+ } else {
109
+ const keys = allOwnKeys ? Object.getOwnPropertyNames(obj) : Object.keys(obj);
110
+ const len = keys.length;
111
+ let key;
112
+ for (let i = 0; i < len; i++) {
113
+ key = keys[i];
114
+ fn(obj[key], key, obj);
115
+ }
116
+ }
117
+ }
118
+ var OnCanceled = class {
119
+ constructor(config) {
120
+ this.config = config;
121
+ }
122
+ subscribe(task, reject) {
123
+ if (this.config.cancelToken || this.config.signal) {
124
+ this.onCanceled = (cancel) => {
125
+ if (!task)
126
+ return;
127
+ reject(
128
+ !cancel || cancel.type ? new CanceledError(void 0, void 0, this.config, task) : cancel
129
+ );
130
+ task.abort();
131
+ task = null;
132
+ };
133
+ if (this.config.cancelToken) {
134
+ this.config.cancelToken.subscribe(this.onCanceled);
135
+ }
136
+ if (this.config.signal && this.config.signal.addEventListener) {
137
+ this.config.signal.aborted ? this.onCanceled() : this.config.signal.addEventListener("abort", this.onCanceled);
138
+ }
139
+ }
140
+ }
141
+ unsubscribe() {
142
+ if (this.config.cancelToken) {
143
+ this.config.cancelToken.unsubscribe(this.onCanceled);
144
+ }
145
+ if (this.config.signal && this.config.signal.removeEventListener)
146
+ this.config.signal.removeEventListener("abort", this.onCanceled);
147
+ }
148
+ };
8
149
 
9
- export { Me as createUniAppAxiosAdapter };
150
+ // src/methods/download.ts
151
+ var download = (config, options) => {
152
+ return new Promise((resolve, reject) => {
153
+ const requestOptions = resolveUniAppRequestOptions(
154
+ config);
155
+ const responseConfig = config;
156
+ responseConfig.headers = new AxiosHeaders(requestOptions.header);
157
+ const onCanceled = new OnCanceled(config);
158
+ let task = uni.downloadFile({
159
+ ...requestOptions,
160
+ success(result) {
161
+ var _a;
162
+ if (!task)
163
+ return;
164
+ const response = {
165
+ config: responseConfig,
166
+ data: result.tempFilePath,
167
+ headers: {},
168
+ status: result.statusCode,
169
+ statusText: (_a = result.errMsg) != null ? _a : "OK",
170
+ request: task
171
+ };
172
+ settle(resolve, reject, response);
173
+ task = null;
174
+ },
175
+ fail(error) {
176
+ const { errMsg = "" } = error != null ? error : {};
177
+ if (errMsg) {
178
+ const isTimeoutError = errMsg === "downloadFile:fail timeout";
179
+ if (isTimeoutError)
180
+ reject(new AxiosError(errMsg, AxiosError.ETIMEDOUT, responseConfig, task));
181
+ const isNetworkError = errMsg === "downloadFile:fail";
182
+ if (isNetworkError) {
183
+ reject(
184
+ new AxiosError(errMsg, AxiosError.ERR_NETWORK, responseConfig, task)
185
+ );
186
+ }
187
+ }
188
+ reject(new AxiosError(error.errMsg, void 0, responseConfig, task));
189
+ task = null;
190
+ },
191
+ complete() {
192
+ onCanceled.unsubscribe();
193
+ }
194
+ });
195
+ if (typeof config.onDownloadProgress === "function") {
196
+ task.onProgressUpdate(
197
+ progressEventReducer(config.onDownloadProgress, true)
198
+ );
199
+ }
200
+ if (typeof config.onHeadersReceived === "function")
201
+ task.onHeadersReceived(config.onHeadersReceived);
202
+ onCanceled.subscribe(task, reject);
203
+ });
204
+ };
205
+ var download_default = download;
206
+ var upload = (config, options) => {
207
+ return new Promise((resolve, reject) => {
208
+ const requestOptions = resolveUniAppRequestOptions(config);
209
+ const responseConfig = config;
210
+ responseConfig.headers = new AxiosHeaders(requestOptions.header);
211
+ const onCanceled = new OnCanceled(config);
212
+ let task = uni.uploadFile({
213
+ ...requestOptions,
214
+ success(result) {
215
+ var _a;
216
+ if (!task)
217
+ return;
218
+ const response = {
219
+ config: responseConfig,
220
+ data: result.data,
221
+ headers: {},
222
+ status: result.statusCode,
223
+ statusText: (_a = result.errMsg) != null ? _a : "OK",
224
+ request: task
225
+ };
226
+ settle(resolve, reject, response);
227
+ task = null;
228
+ },
229
+ fail(error) {
230
+ const { errMsg = "" } = error != null ? error : {};
231
+ if (errMsg) {
232
+ const isTimeoutError = errMsg === "uploadFile:fail timeout";
233
+ const isNetworkError = errMsg === "uploadFile:fail file error";
234
+ if (isTimeoutError)
235
+ reject(new AxiosError(errMsg, AxiosError.ETIMEDOUT, responseConfig, task));
236
+ if (isNetworkError) {
237
+ reject(
238
+ new AxiosError(errMsg, AxiosError.ERR_NETWORK, responseConfig, task)
239
+ );
240
+ }
241
+ }
242
+ reject(new AxiosError(error.errMsg, void 0, responseConfig, task));
243
+ task = null;
244
+ },
245
+ complete() {
246
+ onCanceled.unsubscribe();
247
+ }
248
+ });
249
+ if (typeof config.onHeadersReceived === "function")
250
+ task.onHeadersReceived(config.onHeadersReceived);
251
+ onCanceled.subscribe(task, reject);
252
+ });
253
+ };
254
+ var upload_default = upload;
255
+ var request = (config, options) => {
256
+ return new Promise((resolve, reject) => {
257
+ const requestOptions = resolveUniAppRequestOptions(config);
258
+ const responseConfig = config;
259
+ responseConfig.headers = new AxiosHeaders(requestOptions.header);
260
+ const onCanceled = new OnCanceled(config);
261
+ let task = uni.request({
262
+ ...requestOptions,
263
+ success(result) {
264
+ var _a;
265
+ if (!task)
266
+ return;
267
+ if (Array.isArray(result.header)) {
268
+ const dingHeader = {};
269
+ result.header.forEach((h) => Object.assign(dingHeader, h));
270
+ result.header = dingHeader;
271
+ }
272
+ const headers = new AxiosHeaders(result.header);
273
+ const response = {
274
+ config: responseConfig,
275
+ data: result.data,
276
+ headers,
277
+ status: result.statusCode,
278
+ statusText: (_a = result.errMsg) != null ? _a : "OK",
279
+ request: task,
280
+ cookies: result.cookies
281
+ };
282
+ settle(resolve, reject, response);
283
+ task = null;
284
+ },
285
+ fail(error) {
286
+ const { errMsg = "" } = error != null ? error : {};
287
+ if (errMsg) {
288
+ const isTimeoutError = errMsg === "request:fail timeout";
289
+ const isNetworkError = errMsg === "request:fail";
290
+ if (isTimeoutError)
291
+ reject(new AxiosError(errMsg, AxiosError.ETIMEDOUT, responseConfig, task));
292
+ if (isNetworkError) {
293
+ reject(
294
+ new AxiosError(errMsg, AxiosError.ERR_NETWORK, responseConfig, task)
295
+ );
296
+ }
297
+ }
298
+ reject(new AxiosError(error.errMsg, void 0, responseConfig, task));
299
+ task = null;
300
+ },
301
+ complete() {
302
+ onCanceled.unsubscribe();
303
+ }
304
+ });
305
+ if (typeof config.onHeadersReceived === "function")
306
+ task.onHeadersReceived(config.onHeadersReceived);
307
+ onCanceled.subscribe(task, reject);
308
+ });
309
+ };
310
+ var request_default = request;
311
+
312
+ // src/methods/index.ts
313
+ function getMethod(config) {
314
+ const methodType = getMethodType(config);
315
+ switch (methodType) {
316
+ case "download":
317
+ return download_default;
318
+ case "upload":
319
+ return upload_default;
320
+ default:
321
+ return request_default;
322
+ }
323
+ }
324
+
325
+ // src/index.ts
326
+ function createUniAppAxiosAdapter(userOptions = {}) {
327
+ const options = resolveOptions(userOptions);
328
+ Axios.prototype.download = function(url, config) {
329
+ return this.request({
330
+ url,
331
+ method: "download",
332
+ ...config
333
+ });
334
+ };
335
+ Axios.prototype.upload = function(url, data, config) {
336
+ return this.request({
337
+ url,
338
+ method: "upload",
339
+ data,
340
+ ...config
341
+ });
342
+ };
343
+ const uniappAdapter = (config) => {
344
+ const method = getMethod(config);
345
+ return method(config, options);
346
+ };
347
+ return uniappAdapter;
348
+ }
349
+
350
+ export { createUniAppAxiosAdapter };
package/dist/vite.cjs CHANGED
@@ -1,7 +1,8 @@
1
1
  'use strict';
2
2
 
3
- var chunkY4IH4XRU_cjs = require('./chunk-Y4IH4XRU.cjs');
3
+ var chunkIWHKX3RU_cjs = require('./chunk-IWHKX3RU.cjs');
4
4
 
5
- var i=chunkY4IH4XRU_cjs.a.vite;
5
+ // src/vite.ts
6
+ var vite_default = chunkIWHKX3RU_cjs.unplugin.vite;
6
7
 
7
- module.exports = i;
8
+ module.exports = vite_default;
package/dist/vite.js CHANGED
@@ -1,5 +1,6 @@
1
- import { a } from './chunk-IWRE2NJT.js';
1
+ import { unplugin } from './chunk-MB4L234N.js';
2
2
 
3
- var i=a.vite;
3
+ // src/vite.ts
4
+ var vite_default = unplugin.vite;
4
5
 
5
- export { i as default };
6
+ export { vite_default as default };
package/dist/webpack.cjs CHANGED
@@ -1,7 +1,8 @@
1
1
  'use strict';
2
2
 
3
- var chunkY4IH4XRU_cjs = require('./chunk-Y4IH4XRU.cjs');
3
+ var chunkIWHKX3RU_cjs = require('./chunk-IWHKX3RU.cjs');
4
4
 
5
- var o=chunkY4IH4XRU_cjs.a.webpack;
5
+ // src/webpack.ts
6
+ var webpack_default = chunkIWHKX3RU_cjs.unplugin.webpack;
6
7
 
7
- module.exports = o;
8
+ module.exports = webpack_default;
package/dist/webpack.js CHANGED
@@ -1,5 +1,6 @@
1
- import { a } from './chunk-IWRE2NJT.js';
1
+ import { unplugin } from './chunk-MB4L234N.js';
2
2
 
3
- var o=a.webpack;
3
+ // src/webpack.ts
4
+ var webpack_default = unplugin.webpack;
4
5
 
5
- export { o as default };
6
+ export { webpack_default as default };
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@uni-helper/axios-adapter",
3
3
  "type": "module",
4
- "version": "1.5.1",
5
- "license": "MIT",
4
+ "version": "1.5.3",
5
+ "packageManager": "pnpm@8.9.2",
6
+ "description": "The Axios adapter for uniapp",
6
7
  "author": {
7
8
  "name": "kejunmao",
8
9
  "email": "kejun1997@gmail.com"
9
10
  },
10
- "packageManager": "pnpm@8.7.5",
11
- "description": "The Axios adapter for uniapp",
11
+ "license": "MIT",
12
12
  "homepage": "https://github.com/uni-helper/axios-adapter",
13
13
  "repository": {
14
14
  "type": "git",
@@ -57,27 +57,30 @@
57
57
  "module": "./dist/index.js",
58
58
  "types": "./dist/index.d.ts",
59
59
  "files": [
60
- "dist",
61
- "./client.d.ts"
60
+ "./client.d.ts",
61
+ "dist"
62
62
  ],
63
+ "peerDependencies": {
64
+ "axios": "^1.5.0"
65
+ },
66
+ "dependencies": {
67
+ "local-pkg": "^0.5.0",
68
+ "unplugin": "^1.4.0"
69
+ },
63
70
  "devDependencies": {
71
+ "@antfu/eslint-config": "1.0.0-beta.28",
64
72
  "@dcloudio/types": "^3.3.3",
65
73
  "@types/node": "^20.6.0",
66
74
  "axios": "^1.5.0",
67
75
  "bumpp": "^9.2.0",
68
76
  "eslint": "^8.49.0",
77
+ "msw": "^2.0.0",
69
78
  "pnpm": "^8.7.5",
70
79
  "rimraf": "^5.0.1",
71
80
  "tsup": "^7.2.0",
72
81
  "typescript": "^5.2.2",
73
- "vite": "^4.4.9"
74
- },
75
- "peerDependencies": {
76
- "axios": "^1.5.0"
77
- },
78
- "dependencies": {
79
- "local-pkg": "^0.4.3",
80
- "unplugin": "^1.4.0"
82
+ "vite": "^4.4.9",
83
+ "vitest": "^0.34.4"
81
84
  },
82
85
  "scripts": {
83
86
  "build": "tsup",
@@ -85,6 +88,8 @@
85
88
  "play": "npm -C playground run dev:h5",
86
89
  "release": "bumpp",
87
90
  "test": "vitest",
88
- "typecheck": "tsc --noEmit"
91
+ "typecheck": "tsc --noEmit",
92
+ "lint": "eslint .",
93
+ "lint:fix": "eslint . --fix"
89
94
  }
90
95
  }
@@ -1,8 +0,0 @@
1
- import { createUnplugin } from 'unplugin';
2
- import { isPackageExists } from 'local-pkg';
3
-
4
- var p=createUnplugin((e={})=>{let i=isPackageExists("miniprogram-formdata"),s=isPackageExists("miniprogram-blob");return {name:"unplugin-uni-axios-adapter",enforce:"pre",transform(l,o){var r;if((r=process.env.UNI_PLATFORM)!=null&&r.includes("mp")){if(o.includes("/form-data/lib/browser.js"))return {code:l.replace("window","globalThis")};if(o.includes("/axios/lib/platform/browser/classes/FormData.js"))return {code:`${i?"import FormData from 'miniprogram-formdata';":"class FormData {};"}
5
- export default FormData;`};if(o.includes("/axios/lib/platform/browser/classes/Blob.js"))return {code:`${s?"import Blob from 'miniprogram-blob';":"class Blob {};"}
6
- export default Blob;`}}}}});
7
-
8
- export { p as a };
@@ -1,10 +0,0 @@
1
- 'use strict';
2
-
3
- var unplugin = require('unplugin');
4
- var localPkg = require('local-pkg');
5
-
6
- var p=unplugin.createUnplugin((e={})=>{let i=localPkg.isPackageExists("miniprogram-formdata"),s=localPkg.isPackageExists("miniprogram-blob");return {name:"unplugin-uni-axios-adapter",enforce:"pre",transform(l,o){var r;if((r=process.env.UNI_PLATFORM)!=null&&r.includes("mp")){if(o.includes("/form-data/lib/browser.js"))return {code:l.replace("window","globalThis")};if(o.includes("/axios/lib/platform/browser/classes/FormData.js"))return {code:`${i?"import FormData from 'miniprogram-formdata';":"class FormData {};"}
7
- export default FormData;`};if(o.includes("/axios/lib/platform/browser/classes/Blob.js"))return {code:`${s?"import Blob from 'miniprogram-blob';":"class Blob {};"}
8
- export default Blob;`}}}}});
9
-
10
- exports.a = p;