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