@webex/http-core 3.0.0-beta.8 → 3.0.0-bnr.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 +0 -1
- package/dist/http-error-subtypes.js +2 -147
- package/dist/http-error-subtypes.js.map +1 -1
- package/dist/http-error.js +9 -38
- package/dist/http-error.js.map +1 -1
- package/dist/index.js +5 -29
- package/dist/index.js.map +1 -1
- package/dist/interceptors/http-status.js +7 -30
- package/dist/interceptors/http-status.js.map +1 -1
- package/dist/lib/detect.js +28 -48
- package/dist/lib/detect.js.map +1 -1
- package/dist/lib/interceptor.js +7 -23
- package/dist/lib/interceptor.js.map +1 -1
- package/dist/lib/xhr.js +44 -93
- package/dist/lib/xhr.js.map +1 -1
- package/dist/progress-event.js +0 -7
- package/dist/progress-event.js.map +1 -1
- package/dist/request/index.js +1 -15
- package/dist/request/index.js.map +1 -1
- package/dist/request/request.js +5 -23
- package/dist/request/request.js.map +1 -1
- package/dist/request/request.shim.js +38 -90
- package/dist/request/request.shim.js.map +1 -1
- package/dist/types/http-error-subtypes.d.ts +8 -0
- package/dist/types/http-error.d.ts +41 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/interceptors/http-status.d.ts +17 -0
- package/dist/types/lib/detect.d.ts +6 -0
- package/dist/types/lib/interceptor.d.ts +51 -0
- package/dist/types/lib/xhr.d.ts +6 -0
- package/dist/types/progress-event.d.ts +18 -0
- package/dist/types/request/index.d.ts +5 -0
- package/dist/types/request/request.d.ts +6 -0
- package/dist/types/request/request.shim.d.ts +6 -0
- package/package.json +10 -10
- package/src/http-error-subtypes.js +1 -1
- package/src/http-error.js +15 -23
- package/src/index.js +4 -9
- package/src/interceptors/http-status.js +7 -5
- package/src/lib/detect.js +0 -1
- package/src/lib/interceptor.js +2 -4
- package/src/lib/xhr.js +197 -194
- package/src/progress-event.js +10 -5
- package/src/request/request.js +16 -14
- package/src/request/request.shim.js +47 -38
- package/test/integration/spec/http-error.js +11 -11
- package/test/integration/spec/interceptor.js +20 -13
- package/test/integration/spec/progress-event.js +8 -8
- package/test/integration/spec/request.js +135 -127
- package/test/unit/spec/interceptors/http-status.js +14 -11
package/src/lib/xhr.js
CHANGED
|
@@ -11,243 +11,246 @@
|
|
|
11
11
|
// we're trying to diverge as little as possible.
|
|
12
12
|
/* eslint-disable */
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
var window = require(
|
|
16
|
-
var isFunction = require(
|
|
17
|
-
var parseHeaders = require(
|
|
18
|
-
var xtend = require(
|
|
19
|
-
|
|
20
|
-
createXHR.XMLHttpRequest = window.XMLHttpRequest || noop
|
|
21
|
-
createXHR.XDomainRequest =
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
14
|
+
'use strict';
|
|
15
|
+
var window = require('global/window');
|
|
16
|
+
var isFunction = require('is-function');
|
|
17
|
+
var parseHeaders = require('parse-headers');
|
|
18
|
+
var xtend = require('xtend');
|
|
19
|
+
|
|
20
|
+
createXHR.XMLHttpRequest = window.XMLHttpRequest || noop;
|
|
21
|
+
createXHR.XDomainRequest =
|
|
22
|
+
'withCredentials' in new createXHR.XMLHttpRequest()
|
|
23
|
+
? createXHR.XMLHttpRequest
|
|
24
|
+
: window.XDomainRequest;
|
|
25
|
+
|
|
26
|
+
forEachArray(['get', 'put', 'post', 'patch', 'head', 'delete'], function (method) {
|
|
27
|
+
createXHR[method === 'delete' ? 'del' : method] = function (uri, options, callback) {
|
|
28
|
+
options = initParams(uri, options, callback);
|
|
29
|
+
options.method = method.toUpperCase();
|
|
30
|
+
return _createXHR(options);
|
|
31
|
+
};
|
|
32
|
+
});
|
|
30
33
|
|
|
31
34
|
function forEachArray(array, iterator) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
+
for (var i = 0; i < array.length; i += 1) {
|
|
36
|
+
iterator(array[i]);
|
|
37
|
+
}
|
|
35
38
|
}
|
|
36
39
|
|
|
37
|
-
function isEmpty(obj){
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
function isEmpty(obj) {
|
|
41
|
+
for (var i in obj) {
|
|
42
|
+
if (obj.hasOwnProperty(i)) return false;
|
|
43
|
+
}
|
|
44
|
+
return true;
|
|
42
45
|
}
|
|
43
46
|
|
|
44
47
|
function initParams(uri, options, callback) {
|
|
45
|
-
|
|
48
|
+
var params = uri;
|
|
46
49
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
} else {
|
|
53
|
-
params = xtend(options, {uri: uri})
|
|
50
|
+
if (isFunction(options)) {
|
|
51
|
+
callback = options;
|
|
52
|
+
if (typeof uri === 'string') {
|
|
53
|
+
params = {uri: uri};
|
|
54
54
|
}
|
|
55
|
+
} else {
|
|
56
|
+
params = xtend(options, {uri: uri});
|
|
57
|
+
}
|
|
55
58
|
|
|
56
|
-
|
|
57
|
-
|
|
59
|
+
params.callback = callback;
|
|
60
|
+
return params;
|
|
58
61
|
}
|
|
59
62
|
|
|
60
63
|
function createXHR(uri, options, callback) {
|
|
61
|
-
|
|
62
|
-
|
|
64
|
+
options = initParams(uri, options, callback);
|
|
65
|
+
return _createXHR(options);
|
|
63
66
|
}
|
|
64
67
|
|
|
65
68
|
function _createXHR(options) {
|
|
66
|
-
|
|
67
|
-
|
|
69
|
+
if (typeof options.callback === 'undefined') {
|
|
70
|
+
throw new Error('callback argument missing');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
var called = false;
|
|
74
|
+
var callback = function cbOnce(err, response, body) {
|
|
75
|
+
if (!called) {
|
|
76
|
+
called = true;
|
|
77
|
+
options.callback(err, response, body);
|
|
68
78
|
}
|
|
79
|
+
};
|
|
69
80
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
called = true
|
|
74
|
-
options.callback(err, response, body)
|
|
75
|
-
}
|
|
81
|
+
function readystatechange() {
|
|
82
|
+
if (xhr.readyState === 4) {
|
|
83
|
+
setTimeout(loadFunc, 0);
|
|
76
84
|
}
|
|
85
|
+
}
|
|
77
86
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function getBody() {
|
|
85
|
-
// Chrome with requestType=blob throws errors arround when even testing access to responseText
|
|
86
|
-
var body = undefined
|
|
87
|
-
|
|
88
|
-
if (xhr.response) {
|
|
89
|
-
body = xhr.response
|
|
90
|
-
} else {
|
|
91
|
-
body = xhr.responseText || getXml(xhr)
|
|
92
|
-
}
|
|
87
|
+
function getBody() {
|
|
88
|
+
// Chrome with requestType=blob throws errors arround when even testing access to responseText
|
|
89
|
+
var body = undefined;
|
|
93
90
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
return body
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
function errorFunc(evt) {
|
|
104
|
-
clearTimeout(timeoutTimer)
|
|
105
|
-
if(!(evt instanceof Error)){
|
|
106
|
-
evt = new Error("" + (evt || "Unknown XMLHttpRequest Error") )
|
|
107
|
-
}
|
|
108
|
-
evt.statusCode = 0
|
|
109
|
-
return callback(evt, failureResponse)
|
|
91
|
+
if (xhr.response) {
|
|
92
|
+
body = xhr.response;
|
|
93
|
+
} else {
|
|
94
|
+
body = xhr.responseText || getXml(xhr);
|
|
110
95
|
}
|
|
111
96
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
clearTimeout(timeoutTimer)
|
|
117
|
-
if(options.useXDR && xhr.status===undefined) {
|
|
118
|
-
//IE8 CORS GET successful response doesn't have a status field, but body is fine
|
|
119
|
-
status = 200
|
|
120
|
-
} else {
|
|
121
|
-
status = (xhr.status === 1223 ? 204 : xhr.status)
|
|
122
|
-
}
|
|
123
|
-
var response = failureResponse
|
|
124
|
-
var err = null
|
|
125
|
-
|
|
126
|
-
if (status !== 0){
|
|
127
|
-
response = {
|
|
128
|
-
body: getBody(),
|
|
129
|
-
statusCode: status,
|
|
130
|
-
method: method,
|
|
131
|
-
headers: {},
|
|
132
|
-
url: uri,
|
|
133
|
-
rawRequest: xhr
|
|
134
|
-
}
|
|
135
|
-
if(xhr.getAllResponseHeaders){ //remember xhr can in fact be XDR for CORS in IE
|
|
136
|
-
response.headers = parseHeaders(xhr.getAllResponseHeaders())
|
|
137
|
-
}
|
|
138
|
-
} else {
|
|
139
|
-
err = new Error("Internal XMLHttpRequest Error")
|
|
140
|
-
}
|
|
141
|
-
return callback(err, response, response.body)
|
|
97
|
+
if (isJson) {
|
|
98
|
+
try {
|
|
99
|
+
body = JSON.parse(body);
|
|
100
|
+
} catch (e) {}
|
|
142
101
|
}
|
|
143
102
|
|
|
144
|
-
|
|
103
|
+
return body;
|
|
104
|
+
}
|
|
145
105
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
xhr = new createXHR.XMLHttpRequest()
|
|
151
|
-
}
|
|
106
|
+
function errorFunc(evt) {
|
|
107
|
+
clearTimeout(timeoutTimer);
|
|
108
|
+
if (!(evt instanceof Error)) {
|
|
109
|
+
evt = new Error('' + (evt || 'Unknown XMLHttpRequest Error'));
|
|
152
110
|
}
|
|
111
|
+
evt.statusCode = 0;
|
|
112
|
+
return callback(evt, failureResponse);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// will load the data & process the response in a special response object
|
|
116
|
+
function loadFunc() {
|
|
117
|
+
if (aborted) return;
|
|
118
|
+
var status;
|
|
119
|
+
clearTimeout(timeoutTimer);
|
|
120
|
+
if (options.useXDR && xhr.status === undefined) {
|
|
121
|
+
//IE8 CORS GET successful response doesn't have a status field, but body is fine
|
|
122
|
+
status = 200;
|
|
123
|
+
} else {
|
|
124
|
+
status = xhr.status === 1223 ? 204 : xhr.status;
|
|
125
|
+
}
|
|
126
|
+
var response = failureResponse;
|
|
127
|
+
var err = null;
|
|
153
128
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
var body = options.body || options.data
|
|
159
|
-
var headers = options.headers || {}
|
|
160
|
-
var sync = !!options.sync
|
|
161
|
-
var isJson = false
|
|
162
|
-
var timeoutTimer
|
|
163
|
-
var failureResponse = {
|
|
164
|
-
body: undefined,
|
|
165
|
-
headers: {},
|
|
166
|
-
statusCode: 0,
|
|
129
|
+
if (status !== 0) {
|
|
130
|
+
response = {
|
|
131
|
+
body: getBody(),
|
|
132
|
+
statusCode: status,
|
|
167
133
|
method: method,
|
|
134
|
+
headers: {},
|
|
168
135
|
url: uri,
|
|
169
|
-
rawRequest: xhr
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
body = JSON.stringify(options.json === true ? body : options.json)
|
|
178
|
-
}
|
|
136
|
+
rawRequest: xhr,
|
|
137
|
+
};
|
|
138
|
+
if (xhr.getAllResponseHeaders) {
|
|
139
|
+
//remember xhr can in fact be XDR for CORS in IE
|
|
140
|
+
response.headers = parseHeaders(xhr.getAllResponseHeaders());
|
|
141
|
+
}
|
|
142
|
+
} else {
|
|
143
|
+
err = new Error('Internal XMLHttpRequest Error');
|
|
179
144
|
}
|
|
145
|
+
return callback(err, response, response.body);
|
|
146
|
+
}
|
|
180
147
|
|
|
181
|
-
|
|
182
|
-
xhr.onload = loadFunc
|
|
183
|
-
xhr.onerror = errorFunc
|
|
184
|
-
// IE9 must have onprogress be set to a unique function.
|
|
185
|
-
xhr.onprogress = function () {
|
|
186
|
-
// IE must die
|
|
187
|
-
}
|
|
188
|
-
xhr.onabort = function(){
|
|
189
|
-
aborted = true;
|
|
190
|
-
}
|
|
191
|
-
xhr.ontimeout = errorFunc
|
|
192
|
-
xhr.open(method, uri, !sync, options.username, options.password)
|
|
193
|
-
//has to be after open
|
|
194
|
-
if(!sync) {
|
|
195
|
-
xhr.withCredentials = !!options.withCredentials
|
|
196
|
-
}
|
|
197
|
-
// Cannot set timeout with sync request
|
|
198
|
-
// not setting timeout on the xhr object, because of old webkits etc. not handling that correctly
|
|
199
|
-
// both npm's request and jquery 1.x use this kind of timeout, so this is being consistent
|
|
200
|
-
if (!sync && options.timeout > 0 ) {
|
|
201
|
-
timeoutTimer = setTimeout(function(){
|
|
202
|
-
if (aborted) return
|
|
203
|
-
aborted = true//IE9 may still call readystatechange
|
|
204
|
-
xhr.abort("timeout")
|
|
205
|
-
var e = new Error("XMLHttpRequest timeout")
|
|
206
|
-
e.code = "ETIMEDOUT"
|
|
207
|
-
errorFunc(e)
|
|
208
|
-
}, options.timeout )
|
|
209
|
-
}
|
|
148
|
+
var xhr = options.xhr || null;
|
|
210
149
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
}
|
|
217
|
-
} else if (options.headers && !isEmpty(options.headers)) {
|
|
218
|
-
throw new Error("Headers cannot be set on an XDomainRequest object")
|
|
150
|
+
if (!xhr) {
|
|
151
|
+
if (options.cors || options.useXDR) {
|
|
152
|
+
xhr = new createXHR.XDomainRequest();
|
|
153
|
+
} else {
|
|
154
|
+
xhr = new createXHR.XMLHttpRequest();
|
|
219
155
|
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
var key;
|
|
159
|
+
var aborted;
|
|
160
|
+
var uri = options.uri || options.url;
|
|
161
|
+
var method = options.method || 'GET';
|
|
162
|
+
var body = options.body || options.data;
|
|
163
|
+
var headers = options.headers || {};
|
|
164
|
+
var sync = !!options.sync;
|
|
165
|
+
var isJson = false;
|
|
166
|
+
var timeoutTimer;
|
|
167
|
+
var failureResponse = {
|
|
168
|
+
body: undefined,
|
|
169
|
+
headers: {},
|
|
170
|
+
statusCode: 0,
|
|
171
|
+
method: method,
|
|
172
|
+
url: uri,
|
|
173
|
+
rawRequest: xhr,
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
if ('json' in options && options.json !== false) {
|
|
177
|
+
isJson = true;
|
|
178
|
+
headers['accept'] || headers['Accept'] || (headers['Accept'] = 'application/json'); //Don't override existing accept header declared by user
|
|
179
|
+
if (method !== 'GET' && method !== 'HEAD') {
|
|
180
|
+
headers['content-type'] ||
|
|
181
|
+
headers['Content-Type'] ||
|
|
182
|
+
(headers['Content-Type'] = 'application/json'); //Don't override existing accept header declared by user
|
|
183
|
+
body = JSON.stringify(options.json === true ? body : options.json);
|
|
223
184
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
xhr.onreadystatechange = readystatechange;
|
|
188
|
+
xhr.onload = loadFunc;
|
|
189
|
+
xhr.onerror = errorFunc;
|
|
190
|
+
// IE9 must have onprogress be set to a unique function.
|
|
191
|
+
xhr.onprogress = function () {
|
|
192
|
+
// IE must die
|
|
193
|
+
};
|
|
194
|
+
xhr.onabort = function () {
|
|
195
|
+
aborted = true;
|
|
196
|
+
};
|
|
197
|
+
xhr.ontimeout = errorFunc;
|
|
198
|
+
xhr.open(method, uri, !sync, options.username, options.password);
|
|
199
|
+
//has to be after open
|
|
200
|
+
if (!sync) {
|
|
201
|
+
xhr.withCredentials = !!options.withCredentials;
|
|
202
|
+
}
|
|
203
|
+
// Cannot set timeout with sync request
|
|
204
|
+
// not setting timeout on the xhr object, because of old webkits etc. not handling that correctly
|
|
205
|
+
// both npm's request and jquery 1.x use this kind of timeout, so this is being consistent
|
|
206
|
+
if (!sync && options.timeout > 0) {
|
|
207
|
+
timeoutTimer = setTimeout(function () {
|
|
208
|
+
if (aborted) return;
|
|
209
|
+
aborted = true; //IE9 may still call readystatechange
|
|
210
|
+
xhr.abort('timeout');
|
|
211
|
+
var e = new Error('XMLHttpRequest timeout');
|
|
212
|
+
e.code = 'ETIMEDOUT';
|
|
213
|
+
errorFunc(e);
|
|
214
|
+
}, options.timeout);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (xhr.setRequestHeader) {
|
|
218
|
+
for (key in headers) {
|
|
219
|
+
if (headers.hasOwnProperty(key)) {
|
|
220
|
+
xhr.setRequestHeader(key, headers[key]);
|
|
221
|
+
}
|
|
229
222
|
}
|
|
223
|
+
} else if (options.headers && !isEmpty(options.headers)) {
|
|
224
|
+
throw new Error('Headers cannot be set on an XDomainRequest object');
|
|
225
|
+
}
|
|
230
226
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
xhr.send(body || null)
|
|
227
|
+
if ('responseType' in options) {
|
|
228
|
+
xhr.responseType = options.responseType;
|
|
229
|
+
}
|
|
235
230
|
|
|
236
|
-
|
|
231
|
+
if ('beforeSend' in options && typeof options.beforeSend === 'function') {
|
|
232
|
+
options.beforeSend(xhr);
|
|
233
|
+
}
|
|
237
234
|
|
|
235
|
+
// Microsoft Edge browser sends "undefined" when send is called with undefined value.
|
|
236
|
+
// XMLHttpRequest spec says to pass null as body to indicate no body
|
|
237
|
+
// See https://github.com/naugtur/xhr/issues/100.
|
|
238
|
+
xhr.send(body || null);
|
|
238
239
|
|
|
240
|
+
return xhr;
|
|
239
241
|
}
|
|
240
242
|
|
|
241
243
|
function getXml(xhr) {
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
244
|
+
if (xhr.responseType === 'document') {
|
|
245
|
+
return xhr.responseXML;
|
|
246
|
+
}
|
|
247
|
+
var firefoxBugTakenEffect =
|
|
248
|
+
xhr.responseXML && xhr.responseXML.documentElement.nodeName === 'parsererror';
|
|
249
|
+
if (xhr.responseType === '' && !firefoxBugTakenEffect) {
|
|
250
|
+
return xhr.responseXML;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return null;
|
|
251
254
|
}
|
|
252
255
|
|
|
253
256
|
function noop() {}
|
package/src/progress-event.js
CHANGED
|
@@ -16,17 +16,22 @@ export default function ProgressEvent(loaded, total) {
|
|
|
16
16
|
loaded: {
|
|
17
17
|
enumerable: true,
|
|
18
18
|
value: loaded,
|
|
19
|
-
writable: false
|
|
19
|
+
writable: false,
|
|
20
20
|
},
|
|
21
21
|
total: {
|
|
22
22
|
enumerable: true,
|
|
23
23
|
value: total,
|
|
24
|
-
writable: false
|
|
24
|
+
writable: false,
|
|
25
25
|
},
|
|
26
26
|
lengthComputable: {
|
|
27
27
|
enumerable: true,
|
|
28
|
-
value:
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
value:
|
|
29
|
+
isNumber(loaded) &&
|
|
30
|
+
!Number.isNaN(loaded) &&
|
|
31
|
+
isNumber(total) &&
|
|
32
|
+
!Number.isNaN(total) &&
|
|
33
|
+
total > 0,
|
|
34
|
+
writable: false,
|
|
35
|
+
},
|
|
31
36
|
});
|
|
32
37
|
}
|
package/src/request/request.js
CHANGED
|
@@ -24,12 +24,11 @@ function prepareOptions(options) {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
if (isBuffer(options.body)) {
|
|
27
|
-
return detect(options.body)
|
|
28
|
-
.
|
|
29
|
-
options.headers['content-type'] = type;
|
|
27
|
+
return detect(options.body).then((type) => {
|
|
28
|
+
options.headers['content-type'] = type;
|
|
30
29
|
|
|
31
|
-
|
|
32
|
-
|
|
30
|
+
return options;
|
|
31
|
+
});
|
|
33
32
|
}
|
|
34
33
|
|
|
35
34
|
return Promise.resolve(options);
|
|
@@ -54,24 +53,28 @@ function doRequest(options) {
|
|
|
54
53
|
|
|
55
54
|
// I'm not sure why this line is necessary. request seems to be creating
|
|
56
55
|
// buffers that aren't Buffers.
|
|
57
|
-
if (
|
|
56
|
+
if (
|
|
57
|
+
options.responseType === 'buffer' &&
|
|
58
|
+
response.body.type === 'Buffer' &&
|
|
59
|
+
!isBuffer(response.body)
|
|
60
|
+
) {
|
|
58
61
|
response.body = Buffer.from(response.body);
|
|
59
62
|
}
|
|
60
63
|
|
|
61
64
|
if (isBuffer(response.body) && !response.body.type) {
|
|
62
|
-
resolve(
|
|
63
|
-
.then((type) => {
|
|
65
|
+
resolve(
|
|
66
|
+
detect(response.body).then((type) => {
|
|
64
67
|
response.body.type = type;
|
|
65
68
|
|
|
66
69
|
return response;
|
|
67
|
-
})
|
|
70
|
+
})
|
|
71
|
+
);
|
|
68
72
|
|
|
69
73
|
return;
|
|
70
74
|
}
|
|
71
75
|
|
|
72
76
|
resolve(response);
|
|
73
|
-
}
|
|
74
|
-
else {
|
|
77
|
+
} else {
|
|
75
78
|
// Make a network error behave like a browser network error.
|
|
76
79
|
resolve({
|
|
77
80
|
statusCode: 0,
|
|
@@ -79,7 +82,7 @@ function doRequest(options) {
|
|
|
79
82
|
headers: options.headers,
|
|
80
83
|
method: options.method,
|
|
81
84
|
url: options.url,
|
|
82
|
-
body: error
|
|
85
|
+
body: error,
|
|
83
86
|
});
|
|
84
87
|
}
|
|
85
88
|
});
|
|
@@ -102,6 +105,5 @@ function doRequest(options) {
|
|
|
102
105
|
* @returns {Promise}
|
|
103
106
|
*/
|
|
104
107
|
export default function _request(options) {
|
|
105
|
-
return prepareOptions(options)
|
|
106
|
-
.then(doRequest);
|
|
108
|
+
return prepareOptions(options).then(doRequest);
|
|
107
109
|
}
|