@webex/http-core 2.59.3-next.1 → 2.59.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/.eslintrc.js +6 -6
- package/README.md +64 -64
- package/babel.config.js +3 -3
- package/dist/http-error-subtypes.js +57 -57
- package/dist/http-error-subtypes.js.map +1 -1
- package/dist/http-error.js +25 -25
- package/dist/http-error.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/interceptors/http-status.js +13 -13
- package/dist/interceptors/http-status.js.map +1 -1
- package/dist/lib/detect.js +6 -6
- package/dist/lib/detect.js.map +1 -1
- package/dist/lib/interceptor.js +34 -34
- package/dist/lib/interceptor.js.map +1 -1
- package/dist/lib/xhr.js +2 -2
- package/dist/lib/xhr.js.map +1 -1
- package/dist/progress-event.js +6 -6
- package/dist/progress-event.js.map +1 -1
- package/dist/request/index.js +11 -11
- package/dist/request/index.js.map +1 -1
- package/dist/request/request.js +14 -14
- package/dist/request/request.js.map +1 -1
- package/dist/request/request.shim.js +65 -65
- package/dist/request/request.shim.js.map +1 -1
- package/jest.config.js +3 -3
- package/package.json +17 -18
- package/process +1 -1
- package/src/http-error-subtypes.js +187 -187
- package/src/http-error.js +147 -147
- package/src/index.js +58 -58
- package/src/interceptors/http-status.js +63 -63
- package/src/lib/detect.js +33 -33
- package/src/lib/interceptor.js +95 -95
- package/src/lib/xhr.js +258 -258
- package/src/progress-event.js +37 -37
- package/src/request/index.js +62 -62
- package/src/request/request.js +109 -109
- package/src/request/request.shim.js +304 -304
- package/test/integration/spec/http-error.js +188 -188
- package/test/integration/spec/interceptor.js +71 -71
- package/test/integration/spec/progress-event.js +83 -83
- package/test/integration/spec/request.js +310 -310
- package/test/unit/spec/interceptors/http-status.js +49 -49
package/src/request/index.js
CHANGED
|
@@ -1,62 +1,62 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import {EventEmitter} from 'events';
|
|
6
|
-
|
|
7
|
-
import _request from './request';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @param {Object} options
|
|
11
|
-
* @returns {Promise}
|
|
12
|
-
*/
|
|
13
|
-
export default function request(options) {
|
|
14
|
-
if (options.url) {
|
|
15
|
-
options.uri = options.url;
|
|
16
|
-
options.url = null;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
options.headers = options.headers || {};
|
|
20
|
-
|
|
21
|
-
options.download = new EventEmitter();
|
|
22
|
-
options.upload = new EventEmitter();
|
|
23
|
-
|
|
24
|
-
return intercept(options.interceptors, 'Request')
|
|
25
|
-
.then((...args) => _request(options, ...args))
|
|
26
|
-
.then((...args) => intercept(options.interceptors.slice().reverse(), 'Response', ...args));
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* @param {Array} interceptors
|
|
30
|
-
* @param {string} key
|
|
31
|
-
* @param {Object} res
|
|
32
|
-
* @private
|
|
33
|
-
* @returns {Promise}
|
|
34
|
-
*/
|
|
35
|
-
function intercept(interceptors, key, res) {
|
|
36
|
-
const successKey = `on${key}`;
|
|
37
|
-
const errorKey = `on${key}Error`;
|
|
38
|
-
|
|
39
|
-
return interceptors.reduce(
|
|
40
|
-
(promise, interceptor) =>
|
|
41
|
-
promise.then(
|
|
42
|
-
(result) => {
|
|
43
|
-
interceptor.logOptions(options);
|
|
44
|
-
if (interceptor[successKey]) {
|
|
45
|
-
return interceptor[successKey](options, result);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return Promise.resolve(result);
|
|
49
|
-
},
|
|
50
|
-
(reason) => {
|
|
51
|
-
interceptor.logOptions(options);
|
|
52
|
-
if (interceptor[errorKey]) {
|
|
53
|
-
return interceptor[errorKey](options, reason);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return Promise.reject(reason);
|
|
57
|
-
}
|
|
58
|
-
),
|
|
59
|
-
Promise.resolve(res)
|
|
60
|
-
);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {EventEmitter} from 'events';
|
|
6
|
+
|
|
7
|
+
import _request from './request';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @param {Object} options
|
|
11
|
+
* @returns {Promise}
|
|
12
|
+
*/
|
|
13
|
+
export default function request(options) {
|
|
14
|
+
if (options.url) {
|
|
15
|
+
options.uri = options.url;
|
|
16
|
+
options.url = null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
options.headers = options.headers || {};
|
|
20
|
+
|
|
21
|
+
options.download = new EventEmitter();
|
|
22
|
+
options.upload = new EventEmitter();
|
|
23
|
+
|
|
24
|
+
return intercept(options.interceptors, 'Request')
|
|
25
|
+
.then((...args) => _request(options, ...args))
|
|
26
|
+
.then((...args) => intercept(options.interceptors.slice().reverse(), 'Response', ...args));
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @param {Array} interceptors
|
|
30
|
+
* @param {string} key
|
|
31
|
+
* @param {Object} res
|
|
32
|
+
* @private
|
|
33
|
+
* @returns {Promise}
|
|
34
|
+
*/
|
|
35
|
+
function intercept(interceptors, key, res) {
|
|
36
|
+
const successKey = `on${key}`;
|
|
37
|
+
const errorKey = `on${key}Error`;
|
|
38
|
+
|
|
39
|
+
return interceptors.reduce(
|
|
40
|
+
(promise, interceptor) =>
|
|
41
|
+
promise.then(
|
|
42
|
+
(result) => {
|
|
43
|
+
interceptor.logOptions(options);
|
|
44
|
+
if (interceptor[successKey]) {
|
|
45
|
+
return interceptor[successKey](options, result);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return Promise.resolve(result);
|
|
49
|
+
},
|
|
50
|
+
(reason) => {
|
|
51
|
+
interceptor.logOptions(options);
|
|
52
|
+
if (interceptor[errorKey]) {
|
|
53
|
+
return interceptor[errorKey](options, reason);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return Promise.reject(reason);
|
|
57
|
+
}
|
|
58
|
+
),
|
|
59
|
+
Promise.resolve(res)
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
}
|
package/src/request/request.js
CHANGED
|
@@ -1,109 +1,109 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import request from 'request';
|
|
6
|
-
import {Buffer} from 'safe-buffer';
|
|
7
|
-
import {isBuffer} from '@webex/common';
|
|
8
|
-
|
|
9
|
-
import detect from '../lib/detect';
|
|
10
|
-
import ProgressEvent from '../progress-event';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* @param {Object} options
|
|
14
|
-
* @private
|
|
15
|
-
* @returns {Promise}
|
|
16
|
-
*/
|
|
17
|
-
function prepareOptions(options) {
|
|
18
|
-
if (options.responseType === 'buffer' || options.responseType === 'blob') {
|
|
19
|
-
options.encoding = null;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
if (options.withCredentials) {
|
|
23
|
-
options.jar = true;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
if (isBuffer(options.body)) {
|
|
27
|
-
return detect(options.body).then((type) => {
|
|
28
|
-
options.headers['content-type'] = type;
|
|
29
|
-
|
|
30
|
-
return options;
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return Promise.resolve(options);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* @param {Object} options
|
|
39
|
-
* @private
|
|
40
|
-
* @returns {Promise}
|
|
41
|
-
*/
|
|
42
|
-
function doRequest(options) {
|
|
43
|
-
return new Promise((resolve) => {
|
|
44
|
-
const {logger} = options;
|
|
45
|
-
|
|
46
|
-
const r = request(options, (error, response) => {
|
|
47
|
-
if (error) {
|
|
48
|
-
logger.warn(error);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (response) {
|
|
52
|
-
response.options = options;
|
|
53
|
-
|
|
54
|
-
// I'm not sure why this line is necessary. request seems to be creating
|
|
55
|
-
// buffers that aren't Buffers.
|
|
56
|
-
if (
|
|
57
|
-
options.responseType === 'buffer' &&
|
|
58
|
-
response.body.type === 'Buffer' &&
|
|
59
|
-
!isBuffer(response.body)
|
|
60
|
-
) {
|
|
61
|
-
response.body = Buffer.from(response.body);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
if (isBuffer(response.body) && !response.body.type) {
|
|
65
|
-
resolve(
|
|
66
|
-
detect(response.body).then((type) => {
|
|
67
|
-
response.body.type = type;
|
|
68
|
-
|
|
69
|
-
return response;
|
|
70
|
-
})
|
|
71
|
-
);
|
|
72
|
-
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
resolve(response);
|
|
77
|
-
} else {
|
|
78
|
-
// Make a network error behave like a browser network error.
|
|
79
|
-
resolve({
|
|
80
|
-
statusCode: 0,
|
|
81
|
-
options,
|
|
82
|
-
headers: options.headers,
|
|
83
|
-
method: options.method,
|
|
84
|
-
url: options.url,
|
|
85
|
-
body: error,
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
r.on('response', (response) => {
|
|
91
|
-
const total = parseInt(response.headers['content-length'], 10);
|
|
92
|
-
let loaded = 0;
|
|
93
|
-
|
|
94
|
-
response.on('data', (data) => {
|
|
95
|
-
loaded += data.length;
|
|
96
|
-
options.download.emit('progress', new ProgressEvent(loaded, total));
|
|
97
|
-
});
|
|
98
|
-
});
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* @name request
|
|
104
|
-
* @param {Object} options
|
|
105
|
-
* @returns {Promise}
|
|
106
|
-
*/
|
|
107
|
-
export default function _request(options) {
|
|
108
|
-
return prepareOptions(options).then(doRequest);
|
|
109
|
-
}
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import request from 'request';
|
|
6
|
+
import {Buffer} from 'safe-buffer';
|
|
7
|
+
import {isBuffer} from '@webex/common';
|
|
8
|
+
|
|
9
|
+
import detect from '../lib/detect';
|
|
10
|
+
import ProgressEvent from '../progress-event';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {Object} options
|
|
14
|
+
* @private
|
|
15
|
+
* @returns {Promise}
|
|
16
|
+
*/
|
|
17
|
+
function prepareOptions(options) {
|
|
18
|
+
if (options.responseType === 'buffer' || options.responseType === 'blob') {
|
|
19
|
+
options.encoding = null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (options.withCredentials) {
|
|
23
|
+
options.jar = true;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (isBuffer(options.body)) {
|
|
27
|
+
return detect(options.body).then((type) => {
|
|
28
|
+
options.headers['content-type'] = type;
|
|
29
|
+
|
|
30
|
+
return options;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return Promise.resolve(options);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @param {Object} options
|
|
39
|
+
* @private
|
|
40
|
+
* @returns {Promise}
|
|
41
|
+
*/
|
|
42
|
+
function doRequest(options) {
|
|
43
|
+
return new Promise((resolve) => {
|
|
44
|
+
const {logger} = options;
|
|
45
|
+
|
|
46
|
+
const r = request(options, (error, response) => {
|
|
47
|
+
if (error) {
|
|
48
|
+
logger.warn(error);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (response) {
|
|
52
|
+
response.options = options;
|
|
53
|
+
|
|
54
|
+
// I'm not sure why this line is necessary. request seems to be creating
|
|
55
|
+
// buffers that aren't Buffers.
|
|
56
|
+
if (
|
|
57
|
+
options.responseType === 'buffer' &&
|
|
58
|
+
response.body.type === 'Buffer' &&
|
|
59
|
+
!isBuffer(response.body)
|
|
60
|
+
) {
|
|
61
|
+
response.body = Buffer.from(response.body);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (isBuffer(response.body) && !response.body.type) {
|
|
65
|
+
resolve(
|
|
66
|
+
detect(response.body).then((type) => {
|
|
67
|
+
response.body.type = type;
|
|
68
|
+
|
|
69
|
+
return response;
|
|
70
|
+
})
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
resolve(response);
|
|
77
|
+
} else {
|
|
78
|
+
// Make a network error behave like a browser network error.
|
|
79
|
+
resolve({
|
|
80
|
+
statusCode: 0,
|
|
81
|
+
options,
|
|
82
|
+
headers: options.headers,
|
|
83
|
+
method: options.method,
|
|
84
|
+
url: options.url,
|
|
85
|
+
body: error,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
r.on('response', (response) => {
|
|
91
|
+
const total = parseInt(response.headers['content-length'], 10);
|
|
92
|
+
let loaded = 0;
|
|
93
|
+
|
|
94
|
+
response.on('data', (data) => {
|
|
95
|
+
loaded += data.length;
|
|
96
|
+
options.download.emit('progress', new ProgressEvent(loaded, total));
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* @name request
|
|
104
|
+
* @param {Object} options
|
|
105
|
+
* @returns {Promise}
|
|
106
|
+
*/
|
|
107
|
+
export default function _request(options) {
|
|
108
|
+
return prepareOptions(options).then(doRequest);
|
|
109
|
+
}
|