@uni-helper/axios-adapter 0.0.2 → 0.0.4

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
@@ -1,39 +1,32 @@
1
- # @uni-helper/axios-adapter
2
-
3
- this is an Axios adapter for uni-app
4
-
5
- English | [简体中文](./README.zh-CN.md)
6
-
7
- ## Installation
8
-
9
- ```
10
- pnpm i @uni-helper/axios-adapter axios
11
- ```
12
-
13
- ## Usage
14
-
15
- ```ts
16
- import axios from "axios";
17
- import { createUniAppAxiosAdapter } from "@uni-helper/axios-adapter";
18
-
19
- const $axios = axios.create({
20
- adapter: createUniAppAxiosAdapter(),
21
- });
22
- ```
23
-
24
- ## Client Types
25
-
26
- ```ts
27
- /// <reference types="@uni-helper/axios-adapter/client" />
28
- ```
29
-
30
- ## Configuration
31
-
32
- see [types.ts](./src/types.ts)
33
-
34
-
35
- ## Notes
36
-
37
- Axios-adapter uses the built-in utility functions of Axios, but Axios does not export `"./lib/*": "./lib/*"`, so you need to use a tool like `pnpm patch` to export it.
38
-
39
- Here's an example: [axios@1.2.1.patch](./patches/axios%401.2.1.patch)
1
+ # @uni-helper/axios-adapter
2
+
3
+ this is an Axios adapter for uni-app
4
+
5
+ English | [简体中文](./README.zh-CN.md)
6
+
7
+ ## Installation
8
+
9
+ ```
10
+ pnpm i @uni-helper/axios-adapter axios
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```ts
16
+ import axios from "axios";
17
+ import { createUniAppAxiosAdapter } from "@uni-helper/axios-adapter";
18
+
19
+ const $axios = axios.create({
20
+ adapter: createUniAppAxiosAdapter(),
21
+ });
22
+ ```
23
+
24
+ ## Client Types
25
+
26
+ ```ts
27
+ /// <reference types="@uni-helper/axios-adapter/client" />
28
+ ```
29
+
30
+ ## Configuration
31
+
32
+ see [types.ts](./src/types.ts)
package/README.zh-CN.md CHANGED
@@ -1,39 +1,32 @@
1
- # @uni-helper/axios-adapter
2
-
3
- **WIP** 这是一个用于 uni-app 的 Axios 适配器
4
-
5
- [English](./README.md) | 简体中文
6
-
7
- ## 安装
8
-
9
- ```
10
- pnpm i @uni-helper/axios-adapter axios
11
- ```
12
-
13
- ## 使用
14
-
15
- ```ts
16
- import axios from "axios";
17
- import { createUniAppAxiosAdapter } from "@uni-helper/axios-adapter";
18
-
19
- const $axios = axios.create({
20
- adapter: createUniAppAxiosAdapter(),
21
- });
22
- ```
23
-
24
- ## 客户端类型
25
-
26
- ```ts
27
- /// <reference types="@uni-helper/axios-adapter/client" />
28
- ```
29
-
30
- ## 配置
31
-
32
- 见 [types.ts](./src/types.ts)
33
-
34
-
35
- ## 注意事项
36
-
37
- `axios-adapter` 使用了 `axios` 内置的工具库函数,但是 axios 未导出 `"./lib/*": "./lib/*"`,因此您需要使用类似 `pnpm patch` 的工具导出。
38
-
39
- 这里有一个例子: [axios@1.2.1.patch](./patches/axios%401.2.1.patch)
1
+ # @uni-helper/axios-adapter
2
+
3
+ 这是一个用于 uni-app 的 Axios 适配器
4
+
5
+ [English](./README.md) | 简体中文
6
+
7
+ ## 安装
8
+
9
+ ```
10
+ pnpm i @uni-helper/axios-adapter axios
11
+ ```
12
+
13
+ ## 使用
14
+
15
+ ```ts
16
+ import axios from "axios";
17
+ import { createUniAppAxiosAdapter } from "@uni-helper/axios-adapter";
18
+
19
+ const $axios = axios.create({
20
+ adapter: createUniAppAxiosAdapter(),
21
+ });
22
+ ```
23
+
24
+ ## 客户端类型
25
+
26
+ ```ts
27
+ /// <reference types="@uni-helper/axios-adapter/client" />
28
+ ```
29
+
30
+ ## 配置
31
+
32
+ 见 [types.ts](./src/types.ts)
package/client.d.ts CHANGED
@@ -1,22 +1,22 @@
1
- declare module "axios" {
2
- export interface AxiosRequestConfig
3
- extends Omit<
4
- UniApp.RequestOptions,
5
- "success" | "fail" | "complete" | "header"
6
- >,
7
- Omit<
8
- UniApp.UploadFileOption,
9
- "success" | "fail" | "complete" | "header" | "formData"
10
- >,
11
- Omit<
12
- UniApp.DownloadFileOption,
13
- "success" | "fail" | "complete" | "header"
14
- >,
15
- Partial<Pick<UniApp.RequestTask, "onHeadersReceived">> {}
16
-
17
- export interface AxiosResponse {
18
- cookies?: string[];
19
- }
20
- }
21
-
22
- export {};
1
+ declare module "axios" {
2
+ export interface AxiosRequestConfig
3
+ extends Omit<
4
+ UniApp.RequestOptions,
5
+ "success" | "fail" | "complete" | "header"
6
+ >,
7
+ Omit<
8
+ UniApp.UploadFileOption,
9
+ "success" | "fail" | "complete" | "header" | "formData"
10
+ >,
11
+ Omit<
12
+ UniApp.DownloadFileOption,
13
+ "success" | "fail" | "complete" | "header"
14
+ >,
15
+ Partial<Pick<UniApp.RequestTask, "onHeadersReceived">> {}
16
+
17
+ export interface AxiosResponse {
18
+ cookies?: string[];
19
+ }
20
+ }
21
+
22
+ export {};
package/dist/index.cjs CHANGED
@@ -1,353 +1,10 @@
1
1
  'use strict';
2
2
 
3
- var buildURL = require('axios/lib/helpers/buildURL.js');
4
- var buildFullPath = require('axios/lib/core/buildFullPath.js');
5
- var speedometer = require('axios/lib/helpers/speedometer.js');
3
+ var Ae = require('form-data');
6
4
  var axios = require('axios');
7
- var settle = require('axios/lib/core/settle.js');
8
5
 
9
- // src/utils.ts
10
- var getMethodType = (config) => {
11
- const { method: rawMethod = "GET" } = config;
12
- const method = rawMethod.toLocaleLowerCase();
13
- switch (method) {
14
- case "download":
15
- return "download";
16
- case "upload":
17
- return "upload";
18
- default:
19
- return "request";
20
- }
21
- };
22
- var resolveOptions = (userOptions) => {
23
- return {
24
- ...userOptions
25
- };
26
- };
27
- var resolveUniAppRequestOptions = (config, options) => {
28
- var _a, _b, _c, _d, _e;
29
- const data = config.data;
30
- const responseType = config.responseType === "arraybuffer" ? "arraybuffer" : "text";
31
- const dataType = responseType === "text" ? "json" : void 0;
32
- const requestHeaders = config.headers;
33
- if (config.auth) {
34
- const username = config.auth.username || "";
35
- const password = config.auth.password ? unescape(encodeURIComponent(config.auth.password)) : "";
36
- requestHeaders.set(
37
- "Authorization",
38
- "Basic " + btoa(username + ":" + password)
39
- );
40
- }
41
- const fullPath = buildFullPath(config.baseURL, config.url);
42
- const method = (_b = (_a = config == null ? void 0 : config.method) == null ? void 0 : _a.toUpperCase()) != null ? _b : "GET";
43
- const url = buildURL(fullPath, config.params, config.paramsSerializer);
44
- const timeout = config.timeout || 6e4;
45
- const withCredentials = (_c = config.withCredentials) != null ? _c : false;
46
- const sslVerify = (_d = config.sslVerify) != null ? _d : true;
47
- const firstIpv4 = (_e = config.firstIpv4) != null ? _e : false;
48
- let formData = {};
49
- if (data && typeof data === "string") {
50
- try {
51
- formData = JSON.parse(data);
52
- } catch (error) {
53
- }
54
- }
55
- const header = requestHeaders.toJSON();
56
- return {
57
- ...config,
58
- url,
59
- data,
60
- header,
61
- method,
62
- responseType,
63
- dataType,
64
- timeout,
65
- withCredentials,
66
- sslVerify,
67
- firstIpv4,
68
- formData
69
- };
70
- };
71
- var progressEventReducer = (listener, isDownloadStream) => {
72
- let bytesNotified = 0;
73
- const _speedometer = speedometer(50, 250);
74
- return (result) => {
75
- const loaded = result.totalBytesWritten;
76
- const total = result.totalBytesExpectedToWrite;
77
- const progressBytes = loaded - bytesNotified;
78
- const rate = _speedometer(progressBytes);
79
- const inRange = loaded <= total;
80
- bytesNotified = loaded;
81
- const data = {
82
- loaded,
83
- total,
84
- progress: total ? loaded / total : void 0,
85
- bytes: progressBytes,
86
- rate: rate ? rate : void 0,
87
- estimated: rate && total && inRange ? (total - loaded) / rate : void 0,
88
- event: result
89
- };
90
- data[isDownloadStream ? "download" : "upload"] = true;
91
- listener(data);
92
- };
93
- };
94
- var OnCanceled = class {
95
- constructor(config) {
96
- this.config = config;
97
- }
98
- subscribe(task, reject) {
99
- if (this.config.cancelToken || this.config.signal) {
100
- this.onCanceled = (cancel) => {
101
- if (!task) {
102
- return;
103
- }
104
- reject(
105
- !cancel || cancel.type ? new axios.CanceledError(void 0, void 0, this.config, task) : cancel
106
- );
107
- task.abort();
108
- task = null;
109
- };
110
- if (this.config.cancelToken) {
111
- this.config.cancelToken.subscribe(this.onCanceled);
112
- }
113
- if (this.config.signal && this.config.signal.addEventListener) {
114
- this.config.signal.aborted ? this.onCanceled() : this.config.signal.addEventListener("abort", this.onCanceled);
115
- }
116
- }
117
- }
118
- unsubscribe() {
119
- if (this.config.cancelToken) {
120
- this.config.cancelToken.unsubscribe(this.onCanceled);
121
- }
122
- if (this.config.signal && this.config.signal.removeEventListener) {
123
- this.config.signal.removeEventListener("abort", this.onCanceled);
124
- }
125
- }
126
- };
6
+ var{toString:ne}=Object.prototype,{getPrototypeOf:F}=Object,_=(e=>t=>{let n=ne.call(t);return e[n]||(e[n]=n.slice(8,-1).toLowerCase())})(Object.create(null)),E=e=>(e=e.toLowerCase(),t=>_(t)===e),oe=E("URLSearchParams"),re=e=>{if(_(e)!=="object")return !1;let t=F(e);return (t===null||t===Object.prototype||Object.getPrototypeOf(t)===null)&&!(Symbol.toStringTag in e)&&!(Symbol.iterator in e)},{isArray:q}=Array,se=(e,t,n)=>{e=String(e),(n===void 0||n>e.length)&&(n=e.length),n-=t.length;let o=e.indexOf(t,n);return o!==-1&&o===n},M=e=>t=>typeof t===e,ie=M("function"),ae=e=>e!==null&&typeof e=="object",ue=(e,t,n,o)=>{let s,a,u,y={};if(t=t||{},e==null)return t;do{for(s=Object.getOwnPropertyNames(e),a=s.length;a-- >0;)u=s[a],(!o||o(u,e,t))&&!y[u]&&(t[u]=e[u],y[u]=!0);e=n!==!1&&F(e);}while(e&&(!n||n(e,t))&&e!==Object.prototype);return t},le=E("FileList"),ce=M("undefined"),de=E("Date"),fe=E("Blob"),pe=E("ArrayBuffer"),me=(e=>t=>e&&t instanceof e)(typeof Uint8Array!="undefined"&&F(Uint8Array)),ye=e=>{if(!e)return null;if(q(e))return e;let t=e.length;if(!be(t))return null;let n=new Array(t);for(;t-- >0;)n[t]=e[t];return n};function he(e,t,{allOwnKeys:n=!1}={}){if(e===null||typeof e=="undefined")return;let o,s;if(typeof e!="object"&&(e=[e]),q(e))for(o=0,s=e.length;o<s;o++)t.call(null,e[o],o,e);else {let a=n?Object.getOwnPropertyNames(e):Object.keys(e),u=a.length,y;for(o=0;o<u;o++)y=a[o],t.call(null,e[y],y,e);}}var we=M("string"),be=M("number"),l={isURLSearchParams:oe,isPlainObject:re,isArray:q,endsWith:se,isFunction:ie,isObject:ae,toFlatObject:ue,isUndefined:ce,isDate:de,isBlob:fe,isArrayBuffer:pe,isTypedArray:me,isFileList:le,toArray:ye,forEach:he,isString:we};var H=Ae;function P(e){return l.isPlainObject(e)||l.isArray(e)}function W(e){return l.endsWith(e,"[]")?e.slice(0,-2):e}function I(e,t,n){return e?e.concat(t).map(function(s,a){return s=W(s),!n&&a?"["+s+"]":s}).join(n?".":""):t}function Re(e){return l.isArray(e)&&!e.some(P)}var ge=l.toFlatObject(l,{},null,function(t){return /^is[A-Z]/.test(t)});function Te(e){return e&&l.isFunction(e.append)&&e[Symbol.toStringTag]==="FormData"&&e[Symbol.iterator]}function Ee(e,t,n){if(!l.isObject(e))throw new TypeError("target must be an object");t=t||new(H||FormData),n=l.toFlatObject(n,{metaTokens:!0,dots:!1,indexes:!1},!1,function(i,d){return !l.isUndefined(d[i])});let o=n.metaTokens,s=n.visitor||f,a=n.dots,u=n.indexes,h=(n.Blob||typeof Blob!="undefined"&&Blob)&&Te(t);if(!l.isFunction(s))throw new TypeError("visitor must be a function");function c(r){if(r===null)return "";if(l.isDate(r))return r.toISOString();if(!h&&l.isBlob(r))throw new axios.AxiosError("Blob is not supported. Use a Buffer instead.");return l.isArrayBuffer(r)||l.isTypedArray(r)?h&&typeof Blob=="function"?new Blob([r]):Buffer.from(r):r}function f(r,i,d){let p=r;if(r&&!d&&typeof r=="object"){if(l.endsWith(i,"{}"))i=o?i:i.slice(0,-2),r=JSON.stringify(r);else if(l.isArray(r)&&Re(r)||l.isFileList(r)||l.endsWith(i,"[]")&&(p=l.toArray(r)))return i=W(i),p.forEach(function(x,S){!(l.isUndefined(x)||x===null)&&t.append(u===!0?I([i],S,a):u===null?i:i+"[]",c(x));}),!1}return P(r)?!0:(t.append(I(d,i,a),c(r)),!1)}let m=[],b=Object.assign(ge,{defaultVisitor:f,convertValue:c,isVisitable:P});function A(r,i){if(!l.isUndefined(r)){if(m.indexOf(r)!==-1)throw Error("Circular reference detected in "+i.join("."));m.push(r),l.forEach(r,function(p,w){(!(l.isUndefined(p)||p===null)&&s.call(t,p,l.isString(w)?w.trim():w,i,b))===!0&&A(p,i?i.concat(w):[w]);}),m.pop();}}if(!l.isObject(e))throw new TypeError("data must be an object");return A(e),t}var k=Ee;function z(e){let t={"!":"%21","'":"%27","(":"%28",")":"%29","~":"%7E","%20":"+","%00":"\0"};return encodeURIComponent(e).replace(/[!'()~]|%20|%00/g,function(o){return t[o]})}function K(e,t){this._pairs=[],e&&k(e,this,t);}var V=K.prototype;V.append=function(t,n){this._pairs.push([t,n]);};V.toString=function(t){let n=t?o=>t.call(this,o,z):z;return this._pairs.map(function(s){return n(s[0])+"="+n(s[1])},"").join("&")};var J=K;function Oe(e){return encodeURIComponent(e).replace(/%3A/gi,":").replace(/%24/g,"$").replace(/%2C/gi,",").replace(/%20/g,"+").replace(/%5B/gi,"[").replace(/%5D/gi,"]")}function B(e,t,n){if(!t)return e;let o=n&&n.encode||Oe,s=n&&n.serialize,a;if(s?a=s(t,n):a=l.isURLSearchParams(t)?t.toString():new J(t,n).toString(o),a){let u=e.indexOf("#");u!==-1&&(e=e.slice(0,u)),e+=(e.indexOf("?")===-1?"?":"&")+a;}return e}function L(e){return /^([a-z][a-z\d+\-.]*:)?\/\//i.test(e)}function N(e,t){return t?e.replace(/\/+$/,"")+"/"+t.replace(/^\/+/,""):e}function v(e,t){return e&&!L(t)?N(e,t):t}function Ue(e,t){e=e||10;let n=new Array(e),o=new Array(e),s=0,a=0,u;return t=t!==void 0?t:1e3,function(h){let c=Date.now(),f=o[a];u||(u=c),n[s]=h,o[s]=c;let m=a,b=0;for(;m!==s;)b+=n[m++],m=m%e;if(s=(s+1)%e,s===a&&(a=(a+1)%e),c-u<t)return;let A=f&&c-f;return A?Math.round(b*1e3/A):void 0}}var G=Ue;var $=e=>{let{method:t="GET"}=e;switch(t.toLocaleLowerCase()){case"download":return "download";case"upload":return "upload";default:return "request"}},Q=e=>({...e}),T=(e,t)=>{var i,d,p,w,x;let n=e.data,o=e.responseType==="arraybuffer"?"arraybuffer":"text",s=o==="text"?"json":void 0,a=e.headers;if(e.auth){let S=e.auth.username||"",te=e.auth.password?unescape(encodeURIComponent(e.auth.password)):"";a.set("Authorization","Basic "+btoa(S+":"+te));}let u=v(e.baseURL,e.url),y=(d=(i=e==null?void 0:e.method)==null?void 0:i.toUpperCase())!=null?d:"GET",h=B(u,e.params,e.paramsSerializer),c=e.timeout||6e4,f=(p=e.withCredentials)!=null?p:!1,m=(w=e.sslVerify)!=null?w:!0,b=(x=e.firstIpv4)!=null?x:!1,A={};if(n&&typeof n=="string")try{A=JSON.parse(n);}catch(S){}let r=a.toJSON();return {...e,url:h,data:n,header:r,method:y,responseType:o,dataType:s,timeout:c,withCredentials:f,sslVerify:m,firstIpv4:b,formData:A}},Z=(e,t)=>{let n=0,o=G(50,250);return s=>{let a=s.totalBytesWritten,u=s.totalBytesExpectedToWrite,y=a-n,h=o(y),c=a<=u;n=a;let f={loaded:a,total:u,progress:u?a/u:void 0,bytes:y,rate:h||void 0,estimated:h&&u&&c?(u-a)/h:void 0,event:s};f[t?"download":"upload"]=!0,e(f);}};function g(e,t,n){let o=n.config.validateStatus;!n.status||!o||o(n.status)?e(n):t(new axios.AxiosError("Request failed with status code "+n.status,[axios.AxiosError.ERR_BAD_REQUEST,axios.AxiosError.ERR_BAD_RESPONSE][Math.floor(n.status/100)-4],n.config,n.request,n));}var R=class{constructor(t){this.config=t;}subscribe(t,n){(this.config.cancelToken||this.config.signal)&&(this.onCanceled=o=>{!t||(n(!o||o.type?new axios.CanceledError(void 0,void 0,this.config,t):o),t.abort(),t=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 Se=(e,t)=>new Promise((n,o)=>{let{url:s,header:a,timeout:u,filePath:y}=T(e),h=new R(e),c=uni.downloadFile({url:s,header:a,timeout:u,filePath:y,success(f){var b;if(!c)return;let m={config:e,data:f.tempFilePath,headers:{},status:f.statusCode,statusText:(b=f.errMsg)!=null?b:"OK",request:c};g(n,o,m),c=null;},fail(f){let{errMsg:m=""}=f!=null?f:{};m&&(m==="downloadFile:fail timeout"&&o(new axios.AxiosError(m,axios.AxiosError.ETIMEDOUT,e,c)),m==="downloadFile:fail"&&o(new axios.AxiosError(m,axios.AxiosError.ERR_NETWORK,e,c))),o(new axios.AxiosError(f.errMsg,void 0,e,c)),c=null;},complete(){h.unsubscribe();}});typeof e.onDownloadProgress=="function"&&c.onProgressUpdate(Z(e.onDownloadProgress,!0)),typeof e.onHeadersReceived=="function"&&c.onHeadersReceived(e.onHeadersReceived),h.subscribe(c,o);}),X=Se;var Me=(e,t)=>new Promise((n,o)=>{let{url:s,files:a,fileType:u,file:y,filePath:h,name:c,header:f,timeout:m,formData:b}=T(e),A=new R(e),r=uni.uploadFile({url:s,files:a,fileType:u,file:y,filePath:h,name:c,header:f,timeout:m,formData:b,success(i){var p;if(!r)return;let d={config:e,data:i.data,headers:{},status:i.statusCode,statusText:(p=i.errMsg)!=null?p:"OK",request:r};g(n,o,d),r=null;},fail(i){let{errMsg:d=""}=i!=null?i:{};if(d){let p=d==="uploadFile:fail timeout",w=d==="uploadFile:fail file error";p&&o(new axios.AxiosError(d,axios.AxiosError.ETIMEDOUT,e,r)),w&&o(new axios.AxiosError(d,axios.AxiosError.ERR_NETWORK,e,r));}o(new axios.AxiosError(i.errMsg,void 0,e,r)),r=null;},complete(){A.unsubscribe();}});typeof e.onHeadersReceived=="function"&&r.onHeadersReceived(e.onHeadersReceived),A.subscribe(r,o);}),Y=Me;var Fe=(e,t)=>new Promise((n,o)=>{let{url:s,data:a,header:u,method:y,timeout:h,dataType:c,responseType:f,sslVerify:m,withCredentials:b,firstIpv4:A}=T(e),r=new R(e),i=uni.request({url:s,data:a,header:u,method:y,timeout:h,dataType:c,responseType:f,sslVerify:m,withCredentials:b,firstIpv4:A,success(d){var w;if(!i)return;let p={config:e,data:d.data,headers:d.header,status:d.statusCode,statusText:(w=d.errMsg)!=null?w:"OK",request:i,cookies:d.cookies};g(n,o,p),i=null;},fail(d){let{errMsg:p=""}=d!=null?d:{};if(p){let w=p==="request:fail timeout",x=p==="request:fail";w&&o(new axios.AxiosError(p,axios.AxiosError.ETIMEDOUT,e,i)),x&&o(new axios.AxiosError(p,axios.AxiosError.ERR_NETWORK,e,i));}o(new axios.AxiosError(d.errMsg,void 0,e,i)),i=null;},complete(){r.unsubscribe();}});typeof e.onHeadersReceived=="function"&&i.onHeadersReceived(e.onHeadersReceived),r.subscribe(i,o);}),j=Fe;var ee=e=>{switch($(e)){case"download":return X;case"upload":return Y;default:return j}};var Mt=(e={})=>{let t=Q(e);return o=>ee(o)(o,t)};
127
7
 
128
- // src/methods/download.ts
129
- var download = (config, options) => {
130
- return new Promise((resolve, reject) => {
131
- const { url, header, timeout, filePath } = resolveUniAppRequestOptions(
132
- config);
133
- const onCanceled = new OnCanceled(config);
134
- let task = uni.downloadFile({
135
- url,
136
- header,
137
- timeout,
138
- filePath,
139
- success(result) {
140
- var _a;
141
- if (!task) {
142
- return;
143
- }
144
- const response = {
145
- config,
146
- data: result.tempFilePath,
147
- headers: {},
148
- status: result.statusCode,
149
- statusText: (_a = result.errMsg) != null ? _a : "OK",
150
- request: task
151
- };
152
- settle(resolve, reject, response);
153
- task = null;
154
- },
155
- fail(error) {
156
- const { errMsg = "" } = error != null ? error : {};
157
- if (errMsg) {
158
- const isTimeoutError = errMsg === "downloadFile:fail timeout";
159
- if (isTimeoutError) {
160
- reject(new axios.AxiosError(errMsg, axios.AxiosError.ETIMEDOUT, config, task));
161
- }
162
- const isNetworkError = errMsg === "downloadFile:fail";
163
- if (isNetworkError) {
164
- reject(
165
- new axios.AxiosError(errMsg, axios.AxiosError.ERR_NETWORK, config, task)
166
- );
167
- }
168
- }
169
- reject(new axios.AxiosError(error.errMsg, void 0, config, task));
170
- task = null;
171
- },
172
- complete() {
173
- onCanceled.unsubscribe();
174
- }
175
- });
176
- if (typeof config.onDownloadProgress === "function") {
177
- task.onProgressUpdate(
178
- progressEventReducer(config.onDownloadProgress, true)
179
- );
180
- }
181
- if (typeof config.onHeadersReceived === "function") {
182
- task.onHeadersReceived(config.onHeadersReceived);
183
- }
184
- onCanceled.subscribe(task, reject);
185
- });
186
- };
187
- var download_default = download;
188
- var upload = (config, options) => {
189
- return new Promise((resolve, reject) => {
190
- const {
191
- url,
192
- files,
193
- fileType,
194
- file,
195
- filePath,
196
- name,
197
- header,
198
- timeout,
199
- formData
200
- } = resolveUniAppRequestOptions(config);
201
- const onCanceled = new OnCanceled(config);
202
- let task = uni.uploadFile({
203
- url,
204
- files,
205
- fileType,
206
- file,
207
- filePath,
208
- name,
209
- header,
210
- timeout,
211
- formData,
212
- success(result) {
213
- var _a;
214
- if (!task) {
215
- return;
216
- }
217
- const response = {
218
- config,
219
- data: result.data,
220
- headers: {},
221
- status: result.statusCode,
222
- statusText: (_a = result.errMsg) != null ? _a : "OK",
223
- request: task
224
- };
225
- settle(resolve, reject, response);
226
- task = null;
227
- },
228
- fail(error) {
229
- const { errMsg = "" } = error != null ? error : {};
230
- if (errMsg) {
231
- const isTimeoutError = errMsg === "uploadFile:fail timeout";
232
- const isNetworkError = errMsg === "uploadFile:fail file error";
233
- if (isTimeoutError) {
234
- reject(new axios.AxiosError(errMsg, axios.AxiosError.ETIMEDOUT, config, task));
235
- }
236
- if (isNetworkError) {
237
- reject(
238
- new axios.AxiosError(errMsg, axios.AxiosError.ERR_NETWORK, config, task)
239
- );
240
- }
241
- }
242
- reject(new axios.AxiosError(error.errMsg, void 0, config, task));
243
- task = null;
244
- },
245
- complete() {
246
- onCanceled.unsubscribe();
247
- }
248
- });
249
- if (typeof config.onHeadersReceived === "function") {
250
- task.onHeadersReceived(config.onHeadersReceived);
251
- }
252
- onCanceled.subscribe(task, reject);
253
- });
254
- };
255
- var upload_default = upload;
256
- var request = (config, options) => {
257
- return new Promise((resolve, reject) => {
258
- const {
259
- url,
260
- data,
261
- header,
262
- method,
263
- timeout,
264
- dataType,
265
- responseType,
266
- sslVerify,
267
- withCredentials,
268
- firstIpv4
269
- } = resolveUniAppRequestOptions(config);
270
- const onCanceled = new OnCanceled(config);
271
- let task = uni.request({
272
- url,
273
- data,
274
- header,
275
- method,
276
- timeout,
277
- dataType,
278
- responseType,
279
- sslVerify,
280
- withCredentials,
281
- firstIpv4,
282
- success(result) {
283
- var _a;
284
- if (!task) {
285
- return;
286
- }
287
- const response = {
288
- config,
289
- data: result.data,
290
- headers: result.header,
291
- status: result.statusCode,
292
- statusText: (_a = result.errMsg) != null ? _a : "OK",
293
- request: task,
294
- cookies: result.cookies
295
- };
296
- settle(resolve, reject, response);
297
- task = null;
298
- },
299
- fail(error) {
300
- const { errMsg = "" } = error != null ? error : {};
301
- if (errMsg) {
302
- const isTimeoutError = errMsg === "request:fail timeout";
303
- const isNetworkError = errMsg === "request:fail";
304
- if (isTimeoutError) {
305
- reject(new axios.AxiosError(errMsg, axios.AxiosError.ETIMEDOUT, config, task));
306
- }
307
- if (isNetworkError) {
308
- reject(
309
- new axios.AxiosError(errMsg, axios.AxiosError.ERR_NETWORK, config, task)
310
- );
311
- }
312
- }
313
- reject(new axios.AxiosError(error.errMsg, void 0, config, task));
314
- task = null;
315
- },
316
- complete() {
317
- onCanceled.unsubscribe();
318
- }
319
- });
320
- if (typeof config.onHeadersReceived === "function") {
321
- task.onHeadersReceived(config.onHeadersReceived);
322
- }
323
- onCanceled.subscribe(task, reject);
324
- });
325
- };
326
- var request_default = request;
327
-
328
- // src/methods/index.ts
329
- var getMethod = (config) => {
330
- const methodType = getMethodType(config);
331
- switch (methodType) {
332
- case "download":
333
- return download_default;
334
- case "upload":
335
- return upload_default;
336
- default:
337
- return request_default;
338
- }
339
- };
340
-
341
- // src/index.ts
342
- var createUniAppAxiosAdapter = (userOptions = {}) => {
343
- const options = resolveOptions(userOptions);
344
- const uniappAdapter = (config) => {
345
- const method = getMethod(config);
346
- return method(config, options);
347
- };
348
- return uniappAdapter;
349
- };
350
-
351
- exports.createUniAppAxiosAdapter = createUniAppAxiosAdapter;
8
+ exports.createUniAppAxiosAdapter = Mt;
352
9
  //# sourceMappingURL=out.js.map
353
10
  //# sourceMappingURL=index.cjs.map