-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmp-xhr.js
More file actions
161 lines (135 loc) · 4.93 KB
/
mp-xhr.js
File metadata and controls
161 lines (135 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
'use strict';
import utils from './../utils.js';
import settle from './../core/settle.js';
import buildURL from './../helpers/buildURL.js';
import buildFullPath from '../core/buildFullPath.js';
import AxiosError from '../core/AxiosError.js';
import CanceledError from '../cancel/CanceledError.js';
import parseProtocol from '../helpers/parseProtocol.js';
import platform from '../platform/index.js';
import AxiosHeaders from '../core/AxiosHeaders.js';
// import speedometer from '../helpers/speedometer.js';
// function progressEventReducer(listener, isDownloadStream) {
// let bytesNotified = 0;
// const _speedometer = speedometer(50, 250);
// return e => {
// const loaded = e.loaded;
// const total = e.lengthComputable ? e.total : undefined;
// const progressBytes = loaded - bytesNotified;
// const rate = _speedometer(progressBytes);
// const inRange = loaded <= total;
// bytesNotified = loaded;
// const data = {
// loaded,
// total,
// progress: total ? (loaded / total) : undefined,
// bytes: progressBytes,
// rate: rate ? rate : undefined,
// estimated: rate && total && inRange ? (total - loaded) / rate : undefined,
// event: e
// };
// data[isDownloadStream ? 'download' : 'upload'] = true;
// listener(data);
// };
// }
export default function (config) {
return new Promise(function dispatchXhrRequest(resolve, reject) {
let requestData = config.data;
const requestHeaders = AxiosHeaders.from(config.headers).normalize();
const responseType = config.responseType;
let onCanceled;
function done() {
if (config.cancelToken) {
config.cancelToken.unsubscribe(onCanceled);
}
if (config.signal) {
config.signal.removeEventListener('abort', onCanceled);
}
}
if (utils.isFormData(requestData)) {
requestHeaders.setContentType('multipart/form-data;', false); // mobile/desktop app frameworks
}
// HTTP basic authentication
if (config.auth) {
const username = config.auth.username || '';
const password = config.auth.password ? unescape(encodeURIComponent(config.auth.password)) : '';
requestHeaders.set('Authorization', 'Basic ' + btoa(username + ':' + password));
}
const fullPath = buildFullPath(config.baseURL, config.url);
let request = null;
function onloadend(data, status, responseHeaders) {
if (!request) {
return;
}
// Prepare the response
const response = {
data,
status,
statusText: undefined,
headers: responseHeaders,
config,
request
};
settle(function _resolve(value) {
resolve(value);
done();
}, function _reject(err) {
reject(err);
done();
}, response);
// Clean up request
request = null;
}
// // Add withCredentials to request if needed
// if (!utils.isUndefined(config.withCredentials)) {
// request.withCredentials = !!config.withCredentials;
// }
// // Handle progress if needed
// if (typeof config.onDownloadProgress === 'function') {
// request.addEventListener('progress', progressEventReducer(config.onDownloadProgress, true));
// }
// // Not all browsers support upload events
// if (typeof config.onUploadProgress === 'function' && request.upload) {
// request.upload.addEventListener('progress', progressEventReducer(config.onUploadProgress));
// }
if (config.cancelToken || config.signal) {
// Handle cancellation
// eslint-disable-next-line func-names
onCanceled = cancel => {
if (!request) {
return;
}
reject(!cancel || cancel.type ? new CanceledError(null, config, request) : cancel);
request.abort();
request = null;
};
config.cancelToken && config.cancelToken.subscribe(onCanceled);
if (config.signal) {
config.signal.aborted ? onCanceled() : config.signal.addEventListener('abort', onCanceled);
}
}
const protocol = parseProtocol(fullPath);
if (protocol && platform.protocols.indexOf(protocol) === -1) {
reject(new AxiosError('Unsupported protocol ' + protocol + ':', AxiosError.ERR_BAD_REQUEST, config));
return;
}
const encoding = config.responseEncoding == null ? 'utf8' : config.responseEncoding;
request = wx.request({
url: buildURL(fullPath, config.params, config.paramsSerializer),
enableHttp2: true,
dataType: config.responseType,
responseType: encoding === 'utf8' ? 'text' : 'arraybuffer',
method: config.method,
data: config.data,
headers: requestHeaders.toJSON(),
timeout: config.timeout,
...config.wx,
success({data, statusCode: status, header: headers}) {
onloadend(data, status, headers);
},
fail({errno, errMsg: errorMessage}) {
reject(AxiosError.from(new Error(errorMessage), errno, config));
},
});
});
}