@uni-helper/axios-adapter 1.5.3 → 1.16.0
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 +3 -3
- package/dist/chunk-CKQMccvm.cjs +28 -0
- package/dist/index.cjs +299 -337
- package/dist/index.d.cts +15 -14
- package/dist/index.d.ts +15 -14
- package/dist/index.js +293 -328
- package/dist/types-BSLo1MIA.d.cts +7 -0
- package/dist/types-C-Kh3_5J.d.ts +7 -0
- package/dist/unplugin-B0XXJPh7.cjs +27 -0
- package/dist/unplugin-GFuC6tIl.js +21 -0
- package/dist/vite.cjs +3 -7
- package/dist/vite.d.cts +5 -5
- package/dist/vite.d.ts +6 -5
- package/dist/vite.js +3 -4
- package/dist/webpack.cjs +3 -7
- package/dist/webpack.d.cts +3 -3
- package/dist/webpack.d.ts +4 -3
- package/dist/webpack.js +3 -4
- package/package.json +18 -20
- package/dist/chunk-IWHKX3RU.cjs +0 -43
- package/dist/chunk-MB4L234N.js +0 -37
- package/dist/types-76de936f.d.ts +0 -10
package/dist/index.js
CHANGED
|
@@ -1,350 +1,315 @@
|
|
|
1
|
-
import { Axios,
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import settle from
|
|
6
|
-
|
|
7
|
-
// src/index.ts
|
|
1
|
+
import { Axios, AxiosError, AxiosHeaders, CanceledError } from "axios";
|
|
2
|
+
import buildFullPath from "axios/unsafe/core/buildFullPath";
|
|
3
|
+
import buildURL from "axios/unsafe/helpers/buildURL";
|
|
4
|
+
import speedometer from "axios/unsafe/helpers/speedometer";
|
|
5
|
+
import settle from "axios/unsafe/core/settle";
|
|
6
|
+
//#region src/utils.ts
|
|
8
7
|
function getMethodType(config) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
return "upload";
|
|
16
|
-
default:
|
|
17
|
-
return "request";
|
|
18
|
-
}
|
|
8
|
+
const { method: rawMethod = "GET" } = config;
|
|
9
|
+
switch (rawMethod.toLocaleLowerCase()) {
|
|
10
|
+
case "download": return "download";
|
|
11
|
+
case "upload": return "upload";
|
|
12
|
+
default: return "request";
|
|
13
|
+
}
|
|
19
14
|
}
|
|
20
15
|
function resolveOptions(userOptions) {
|
|
21
|
-
|
|
22
|
-
...userOptions
|
|
23
|
-
};
|
|
16
|
+
return { ...userOptions };
|
|
24
17
|
}
|
|
25
18
|
function resolveUniAppRequestOptions(config, _options) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
header,
|
|
57
|
-
method,
|
|
58
|
-
responseType,
|
|
59
|
-
dataType,
|
|
60
|
-
timeout,
|
|
61
|
-
formData
|
|
62
|
-
};
|
|
19
|
+
const data = config.data;
|
|
20
|
+
const responseType = config.responseType === "arraybuffer" ? "arraybuffer" : "text";
|
|
21
|
+
const dataType = responseType === "text" ? "json" : void 0;
|
|
22
|
+
const { headers, baseURL, ...requestConfig } = config;
|
|
23
|
+
const requestHeaders = AxiosHeaders.from(serializeObject(headers)).normalize(false);
|
|
24
|
+
if (config.auth) {
|
|
25
|
+
const username = config.auth.username || "";
|
|
26
|
+
const password = config.auth.password ? unescape(encodeURIComponent(config.auth.password)) : "";
|
|
27
|
+
requestHeaders.set("Authorization", `Basic ${btoa(`${username}:${password}`)}`);
|
|
28
|
+
}
|
|
29
|
+
const fullPath = buildFullPath(baseURL, config.url);
|
|
30
|
+
const method = config?.method?.toUpperCase() ?? "GET";
|
|
31
|
+
const url = buildURL(fullPath, config.params, config.paramsSerializer);
|
|
32
|
+
const timeout = config.timeout || 6e4;
|
|
33
|
+
let formData = {};
|
|
34
|
+
if (data && typeof data === "string") try {
|
|
35
|
+
formData = JSON.parse(data);
|
|
36
|
+
} catch {}
|
|
37
|
+
const header = requestHeaders.toJSON();
|
|
38
|
+
return {
|
|
39
|
+
...requestConfig,
|
|
40
|
+
url,
|
|
41
|
+
data,
|
|
42
|
+
header,
|
|
43
|
+
method,
|
|
44
|
+
responseType,
|
|
45
|
+
dataType,
|
|
46
|
+
timeout,
|
|
47
|
+
formData
|
|
48
|
+
};
|
|
63
49
|
}
|
|
64
50
|
function progressEventReducer(listener, isDownloadStream) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
51
|
+
let bytesNotified = 0;
|
|
52
|
+
const _speedometer = speedometer(50, 250);
|
|
53
|
+
return (result) => {
|
|
54
|
+
const loaded = result.totalBytesWritten;
|
|
55
|
+
const total = result.totalBytesExpectedToWrite;
|
|
56
|
+
const progressBytes = loaded - bytesNotified;
|
|
57
|
+
const rate = _speedometer(progressBytes);
|
|
58
|
+
const inRange = loaded <= total;
|
|
59
|
+
bytesNotified = loaded;
|
|
60
|
+
const data = {
|
|
61
|
+
loaded,
|
|
62
|
+
total,
|
|
63
|
+
progress: total ? loaded / total : void 0,
|
|
64
|
+
bytes: progressBytes,
|
|
65
|
+
rate: rate || void 0,
|
|
66
|
+
estimated: rate && total && inRange ? (total - loaded) / rate : void 0,
|
|
67
|
+
event: result
|
|
68
|
+
};
|
|
69
|
+
data[isDownloadStream ? "download" : "upload"] = true;
|
|
70
|
+
listener(data);
|
|
71
|
+
};
|
|
86
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* ### 对象序列化
|
|
75
|
+
* - 将一个对象序列化为一个纯净的、类似JSON的新对象。
|
|
76
|
+
* - 此过程会过滤掉值为 null、undefined 或 false 的属性。
|
|
77
|
+
*
|
|
78
|
+
* @link https://github.com/axios/axios/blob/ef36347fb559383b04c755b07f1a8d11897fab7f/lib/core/AxiosHeaders.js#L238-L246
|
|
79
|
+
* @param {Record<string, any> | null | undefined} sourceObj 要进行序列化的源对象。
|
|
80
|
+
* @param {SerializeOptions} [options] 序列化选项。
|
|
81
|
+
* @returns {Record<string, any>} 返回一个新的、纯净的 JavaScript 对象。
|
|
82
|
+
*/
|
|
87
83
|
function serializeObject(sourceObj, options) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
return resultObj;
|
|
84
|
+
const resultObj = Object.create(null);
|
|
85
|
+
if (!sourceObj) return resultObj;
|
|
86
|
+
const { asStrings = false } = options || {};
|
|
87
|
+
forEach(sourceObj, (value, key) => {
|
|
88
|
+
if (value != null && value !== false) resultObj[key] = asStrings && Array.isArray(value) ? value.join(", ") : value;
|
|
89
|
+
});
|
|
90
|
+
return resultObj;
|
|
98
91
|
}
|
|
92
|
+
/**
|
|
93
|
+
* Iterate over an Array or an Object invoking a function for each item.
|
|
94
|
+
*
|
|
95
|
+
* If `obj` is an Array callback will be called passing
|
|
96
|
+
* the value, index, and complete array for each item.
|
|
97
|
+
*
|
|
98
|
+
* If 'obj' is an Object callback will be called passing
|
|
99
|
+
* the value, key, and complete object for each property.
|
|
100
|
+
*
|
|
101
|
+
* @link https://github.com/axios/axios/blob/v1.x/lib/utils.js#L240
|
|
102
|
+
*
|
|
103
|
+
* @param {object | Array} obj The object to iterate
|
|
104
|
+
* @param {Function} fn The callback to invoke for each item
|
|
105
|
+
* @param {object} [options]
|
|
106
|
+
* @param {boolean} [options.allOwnKeys]
|
|
107
|
+
*/
|
|
99
108
|
function forEach(obj, fn, { allOwnKeys = false } = {}) {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
for (let i = 0; i < len; i++) {
|
|
113
|
-
key = keys[i];
|
|
114
|
-
fn(obj[key], key, obj);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
109
|
+
if (obj === null || typeof obj === "undefined") return;
|
|
110
|
+
if (typeof obj !== "object") obj = [obj];
|
|
111
|
+
if (Array.isArray(obj)) for (let i = 0, l = obj.length; i < l; i++) fn(obj[i], i, obj);
|
|
112
|
+
else {
|
|
113
|
+
const keys = allOwnKeys ? Object.getOwnPropertyNames(obj) : Object.keys(obj);
|
|
114
|
+
const len = keys.length;
|
|
115
|
+
let key;
|
|
116
|
+
for (let i = 0; i < len; i++) {
|
|
117
|
+
key = keys[i];
|
|
118
|
+
fn(obj[key], key, obj);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
117
121
|
}
|
|
122
|
+
//#endregion
|
|
123
|
+
//#region src/methods/onCanceled.ts
|
|
118
124
|
var OnCanceled = class {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
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
|
-
}
|
|
125
|
+
config;
|
|
126
|
+
onCanceled;
|
|
127
|
+
constructor(config) {
|
|
128
|
+
this.config = config;
|
|
129
|
+
}
|
|
130
|
+
subscribe(task, reject) {
|
|
131
|
+
if (this.config.cancelToken || this.config.signal) {
|
|
132
|
+
this.onCanceled = (cancel) => {
|
|
133
|
+
if (!task) return;
|
|
134
|
+
reject(!cancel || cancel.type ? new CanceledError(void 0, void 0, this.config, task) : cancel);
|
|
135
|
+
task.abort();
|
|
136
|
+
task = null;
|
|
137
|
+
};
|
|
138
|
+
if (this.config.cancelToken) this.config.cancelToken.subscribe(this.onCanceled);
|
|
139
|
+
if (this.config.signal && this.config.signal.addEventListener) this.config.signal.aborted ? this.onCanceled() : this.config.signal.addEventListener("abort", this.onCanceled);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
unsubscribe() {
|
|
143
|
+
if (this.config.cancelToken) this.config.cancelToken.unsubscribe(this.onCanceled);
|
|
144
|
+
if (this.config.signal && this.config.signal.removeEventListener) this.config.signal.removeEventListener("abort", this.onCanceled);
|
|
145
|
+
}
|
|
148
146
|
};
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
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
|
-
});
|
|
147
|
+
//#endregion
|
|
148
|
+
//#region src/methods/download.ts
|
|
149
|
+
const download = (config, options) => {
|
|
150
|
+
return new Promise((resolve, reject) => {
|
|
151
|
+
const requestOptions = resolveUniAppRequestOptions(config, options);
|
|
152
|
+
const responseConfig = config;
|
|
153
|
+
responseConfig.headers = new AxiosHeaders(requestOptions.header);
|
|
154
|
+
const onCanceled = new OnCanceled(config);
|
|
155
|
+
let task = uni.downloadFile({
|
|
156
|
+
...requestOptions,
|
|
157
|
+
success(result) {
|
|
158
|
+
if (!task) return;
|
|
159
|
+
settle(resolve, reject, {
|
|
160
|
+
config: responseConfig,
|
|
161
|
+
data: result.tempFilePath,
|
|
162
|
+
headers: {},
|
|
163
|
+
status: result.statusCode,
|
|
164
|
+
statusText: result.errMsg ?? "OK",
|
|
165
|
+
request: task
|
|
166
|
+
});
|
|
167
|
+
task = null;
|
|
168
|
+
},
|
|
169
|
+
fail(error) {
|
|
170
|
+
const { errMsg = "" } = error ?? {};
|
|
171
|
+
if (errMsg) {
|
|
172
|
+
if (errMsg === "downloadFile:fail timeout") reject(new AxiosError(errMsg, AxiosError.ETIMEDOUT, responseConfig, task));
|
|
173
|
+
if (errMsg === "downloadFile:fail") reject(new AxiosError(errMsg, AxiosError.ERR_NETWORK, responseConfig, task));
|
|
174
|
+
}
|
|
175
|
+
reject(new AxiosError(error.errMsg, void 0, responseConfig, task));
|
|
176
|
+
task = null;
|
|
177
|
+
},
|
|
178
|
+
complete() {
|
|
179
|
+
onCanceled.unsubscribe();
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
if (typeof config.onDownloadProgress === "function") task.onProgressUpdate(progressEventReducer(config.onDownloadProgress, true));
|
|
183
|
+
if (typeof config.onHeadersReceived === "function") task.onHeadersReceived(config.onHeadersReceived);
|
|
184
|
+
onCanceled.subscribe(task, reject);
|
|
185
|
+
});
|
|
204
186
|
};
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
187
|
+
//#endregion
|
|
188
|
+
//#region src/methods/request.ts
|
|
189
|
+
const request = (config, options) => {
|
|
190
|
+
return new Promise((resolve, reject) => {
|
|
191
|
+
const requestOptions = resolveUniAppRequestOptions(config, options);
|
|
192
|
+
const responseConfig = config;
|
|
193
|
+
responseConfig.headers = new AxiosHeaders(requestOptions.header);
|
|
194
|
+
const onCanceled = new OnCanceled(config);
|
|
195
|
+
let task = uni.request({
|
|
196
|
+
...requestOptions,
|
|
197
|
+
success(result) {
|
|
198
|
+
if (!task) return;
|
|
199
|
+
if (Array.isArray(result.header)) {
|
|
200
|
+
const dingHeader = {};
|
|
201
|
+
result.header.forEach((h) => Object.assign(dingHeader, h));
|
|
202
|
+
result.header = dingHeader;
|
|
203
|
+
}
|
|
204
|
+
const headers = new AxiosHeaders(result.header);
|
|
205
|
+
settle(resolve, reject, {
|
|
206
|
+
config: responseConfig,
|
|
207
|
+
data: result.data,
|
|
208
|
+
headers,
|
|
209
|
+
status: result.statusCode,
|
|
210
|
+
statusText: result.errMsg ?? "OK",
|
|
211
|
+
request: task,
|
|
212
|
+
cookies: result.cookies
|
|
213
|
+
});
|
|
214
|
+
task = null;
|
|
215
|
+
},
|
|
216
|
+
fail(error) {
|
|
217
|
+
const { errMsg = "" } = error ?? {};
|
|
218
|
+
if (errMsg) {
|
|
219
|
+
const isStatusError = errMsg === "request:fail http status error";
|
|
220
|
+
const isTimeoutError = errMsg === "request:fail timeout";
|
|
221
|
+
const isNetworkError = errMsg === "request:fail";
|
|
222
|
+
if (isTimeoutError) reject(new AxiosError(errMsg, AxiosError.ETIMEDOUT, responseConfig, task));
|
|
223
|
+
if (isNetworkError) reject(new AxiosError(errMsg, AxiosError.ERR_NETWORK, responseConfig, task));
|
|
224
|
+
if (isStatusError) {
|
|
225
|
+
const response = error ?? {};
|
|
226
|
+
reject(new AxiosError(errMsg, response.statusCode, responseConfig, task, response));
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
reject(new AxiosError(error.errMsg, void 0, responseConfig, task));
|
|
230
|
+
task = null;
|
|
231
|
+
},
|
|
232
|
+
complete() {
|
|
233
|
+
onCanceled.unsubscribe();
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
if (typeof config.onHeadersReceived === "function") task.onHeadersReceived(config.onHeadersReceived);
|
|
237
|
+
onCanceled.subscribe(task, reject);
|
|
238
|
+
});
|
|
253
239
|
};
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
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
|
-
});
|
|
240
|
+
//#endregion
|
|
241
|
+
//#region src/methods/upload.ts
|
|
242
|
+
const upload = (config, options) => {
|
|
243
|
+
return new Promise((resolve, reject) => {
|
|
244
|
+
const requestOptions = resolveUniAppRequestOptions(config, options);
|
|
245
|
+
const responseConfig = config;
|
|
246
|
+
responseConfig.headers = new AxiosHeaders(requestOptions.header);
|
|
247
|
+
const onCanceled = new OnCanceled(config);
|
|
248
|
+
let task = uni.uploadFile({
|
|
249
|
+
...requestOptions,
|
|
250
|
+
success(result) {
|
|
251
|
+
if (!task) return;
|
|
252
|
+
settle(resolve, reject, {
|
|
253
|
+
config: responseConfig,
|
|
254
|
+
data: result.data,
|
|
255
|
+
headers: {},
|
|
256
|
+
status: result.statusCode,
|
|
257
|
+
statusText: result.errMsg ?? "OK",
|
|
258
|
+
request: task
|
|
259
|
+
});
|
|
260
|
+
task = null;
|
|
261
|
+
},
|
|
262
|
+
fail(error) {
|
|
263
|
+
const { errMsg = "" } = error ?? {};
|
|
264
|
+
if (errMsg) {
|
|
265
|
+
const isTimeoutError = errMsg === "uploadFile:fail timeout";
|
|
266
|
+
const isNetworkError = errMsg === "uploadFile:fail file error";
|
|
267
|
+
if (isTimeoutError) reject(new AxiosError(errMsg, AxiosError.ETIMEDOUT, responseConfig, task));
|
|
268
|
+
if (isNetworkError) reject(new AxiosError(errMsg, AxiosError.ERR_NETWORK, responseConfig, task));
|
|
269
|
+
}
|
|
270
|
+
reject(new AxiosError(error.errMsg, void 0, responseConfig, task));
|
|
271
|
+
task = null;
|
|
272
|
+
},
|
|
273
|
+
complete() {
|
|
274
|
+
onCanceled.unsubscribe();
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
if (typeof config.onHeadersReceived === "function") task.onHeadersReceived(config.onHeadersReceived);
|
|
278
|
+
onCanceled.subscribe(task, reject);
|
|
279
|
+
});
|
|
309
280
|
};
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
// src/methods/index.ts
|
|
281
|
+
//#endregion
|
|
282
|
+
//#region src/methods/index.ts
|
|
313
283
|
function getMethod(config) {
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
return upload_default;
|
|
320
|
-
default:
|
|
321
|
-
return request_default;
|
|
322
|
-
}
|
|
284
|
+
switch (getMethodType(config)) {
|
|
285
|
+
case "download": return download;
|
|
286
|
+
case "upload": return upload;
|
|
287
|
+
default: return request;
|
|
288
|
+
}
|
|
323
289
|
}
|
|
324
|
-
|
|
325
|
-
|
|
290
|
+
//#endregion
|
|
291
|
+
//#region src/index.ts
|
|
326
292
|
function createUniAppAxiosAdapter(userOptions = {}) {
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
return uniappAdapter;
|
|
293
|
+
const options = resolveOptions(userOptions);
|
|
294
|
+
Axios.prototype.download = function(url, config) {
|
|
295
|
+
return this.request({
|
|
296
|
+
url,
|
|
297
|
+
method: "download",
|
|
298
|
+
...config
|
|
299
|
+
});
|
|
300
|
+
};
|
|
301
|
+
Axios.prototype.upload = function(url, data, config) {
|
|
302
|
+
return this.request({
|
|
303
|
+
url,
|
|
304
|
+
method: "upload",
|
|
305
|
+
data,
|
|
306
|
+
...config
|
|
307
|
+
});
|
|
308
|
+
};
|
|
309
|
+
const uniappAdapter = (config) => {
|
|
310
|
+
return getMethod(config)(config, options);
|
|
311
|
+
};
|
|
312
|
+
return uniappAdapter;
|
|
348
313
|
}
|
|
349
|
-
|
|
314
|
+
//#endregion
|
|
350
315
|
export { createUniAppAxiosAdapter };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
interface Options {}
|
|
3
|
+
interface UserOptions extends Partial<Options> {}
|
|
4
|
+
interface UnpluginOptions {}
|
|
5
|
+
interface UserUnpluginOptions extends Partial<UnpluginOptions> {}
|
|
6
|
+
//#endregion
|
|
7
|
+
export { UserUnpluginOptions as n, UserOptions as t };
|