@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
|
@@ -22,7 +22,15 @@ import detect from '../lib/detect';
|
|
|
22
22
|
*/
|
|
23
23
|
export default function _request(options) {
|
|
24
24
|
return new Promise((resolve) => {
|
|
25
|
-
const params = pick(
|
|
25
|
+
const params = pick(
|
|
26
|
+
options,
|
|
27
|
+
'method',
|
|
28
|
+
'uri',
|
|
29
|
+
'withCredentials',
|
|
30
|
+
'headers',
|
|
31
|
+
'timeout',
|
|
32
|
+
'responseType'
|
|
33
|
+
);
|
|
26
34
|
|
|
27
35
|
// Set `response` to `true` to approximate an `HttpResponse` object
|
|
28
36
|
params.response = true;
|
|
@@ -37,7 +45,9 @@ export default function _request(options) {
|
|
|
37
45
|
setPayload(params, options);
|
|
38
46
|
setQs(params, options);
|
|
39
47
|
|
|
40
|
-
options.logger.debug(
|
|
48
|
+
options.logger.debug(
|
|
49
|
+
`start http ${options.method ? options.method : 'request'} to ${options.uri}`
|
|
50
|
+
);
|
|
41
51
|
|
|
42
52
|
const x = xhr(params, (error, response) => {
|
|
43
53
|
/* istanbul ignore next */
|
|
@@ -48,43 +58,48 @@ export default function _request(options) {
|
|
|
48
58
|
/* istanbul ignore else */
|
|
49
59
|
if (response) {
|
|
50
60
|
if (response.statusCode >= 400) {
|
|
51
|
-
options.logger.warn(
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
61
|
+
options.logger.warn(
|
|
62
|
+
`http ${options.method ? options.method : 'request'} to ${options.uri} result: ${
|
|
63
|
+
response.statusCode
|
|
64
|
+
}`
|
|
65
|
+
);
|
|
66
|
+
} else {
|
|
67
|
+
options.logger.debug(
|
|
68
|
+
`http ${options.method ? options.method : 'request'} to ${options.uri} result: ${
|
|
69
|
+
response.statusCode
|
|
70
|
+
}`
|
|
71
|
+
);
|
|
55
72
|
}
|
|
56
73
|
response.options = options;
|
|
57
74
|
processResponseJson(response, params);
|
|
58
75
|
resolve(response);
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
76
|
+
} else {
|
|
61
77
|
resolve({
|
|
62
78
|
statusCode: 0,
|
|
63
79
|
options,
|
|
64
80
|
headers: options.headers,
|
|
65
81
|
method: options.method,
|
|
66
82
|
url: options.uri,
|
|
67
|
-
body: error
|
|
83
|
+
body: error,
|
|
68
84
|
});
|
|
69
85
|
}
|
|
70
86
|
});
|
|
71
87
|
|
|
72
88
|
x.onprogress = options.download.emit.bind(options.download, 'progress');
|
|
73
|
-
})
|
|
74
|
-
.
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
});
|
|
89
|
+
}).catch((error) => {
|
|
90
|
+
options.logger.warn(error);
|
|
91
|
+
|
|
92
|
+
/* eslint arrow-body-style: [0] */
|
|
93
|
+
/* istanbul ignore next */
|
|
94
|
+
return {
|
|
95
|
+
statusCode: 0,
|
|
96
|
+
options,
|
|
97
|
+
headers: options.headers,
|
|
98
|
+
method: options.method,
|
|
99
|
+
url: options.uri,
|
|
100
|
+
body: error,
|
|
101
|
+
};
|
|
102
|
+
});
|
|
88
103
|
|
|
89
104
|
/**
|
|
90
105
|
* @param {Object} params
|
|
@@ -121,8 +136,7 @@ export default function _request(options) {
|
|
|
121
136
|
if (o.auth) {
|
|
122
137
|
if (o.auth.bearer) {
|
|
123
138
|
params.headers.authorization = `Bearer ${o.auth.bearer}`;
|
|
124
|
-
}
|
|
125
|
-
else {
|
|
139
|
+
} else {
|
|
126
140
|
const user = o.auth.user || o.auth.username;
|
|
127
141
|
const pass = o.auth.pass || o.auth.password;
|
|
128
142
|
|
|
@@ -157,7 +171,7 @@ export default function _request(options) {
|
|
|
157
171
|
// raynos/xhr defaults withCredentials to true if cors is true, so we need
|
|
158
172
|
// to make it explicitly false by default
|
|
159
173
|
withCredentials: false,
|
|
160
|
-
timeout: 0
|
|
174
|
+
timeout: 0,
|
|
161
175
|
};
|
|
162
176
|
|
|
163
177
|
defaults(params, pick(o, Object.keys(defs)), defs);
|
|
@@ -184,7 +198,7 @@ export default function _request(options) {
|
|
|
184
198
|
async function setContentType(params, o) {
|
|
185
199
|
if (o.body instanceof Blob || o.body instanceof ArrayBuffer) {
|
|
186
200
|
o.json = params.json = false;
|
|
187
|
-
params.headers['content-type'] = params.headers['content-type'] || await detect(o.body);
|
|
201
|
+
params.headers['content-type'] = params.headers['content-type'] || (await detect(o.body));
|
|
188
202
|
}
|
|
189
203
|
}
|
|
190
204
|
|
|
@@ -238,8 +252,7 @@ export default function _request(options) {
|
|
|
238
252
|
if (value.name) {
|
|
239
253
|
value.filename = value.name;
|
|
240
254
|
form.append(key, value, value.name);
|
|
241
|
-
}
|
|
242
|
-
else {
|
|
255
|
+
} else {
|
|
243
256
|
form.append(key, value);
|
|
244
257
|
}
|
|
245
258
|
}
|
|
@@ -253,13 +266,11 @@ export default function _request(options) {
|
|
|
253
266
|
function setPayload(params, o) {
|
|
254
267
|
if ((!('json' in o) || o.json === true) && o.body) {
|
|
255
268
|
params.json = o.body;
|
|
256
|
-
}
|
|
257
|
-
else if (o.form) {
|
|
269
|
+
} else if (o.form) {
|
|
258
270
|
params.headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
|
259
271
|
params.body = qs.stringify(o.form);
|
|
260
272
|
Reflect.deleteProperty(params, 'json');
|
|
261
|
-
}
|
|
262
|
-
else if (o.formData) {
|
|
273
|
+
} else if (o.formData) {
|
|
263
274
|
params.body = Object.keys(o.formData).reduce((fd, key) => {
|
|
264
275
|
const value = o.formData[key];
|
|
265
276
|
|
|
@@ -267,8 +278,7 @@ export default function _request(options) {
|
|
|
267
278
|
|
|
268
279
|
return fd;
|
|
269
280
|
}, new FormData());
|
|
270
|
-
}
|
|
271
|
-
else {
|
|
281
|
+
} else {
|
|
272
282
|
params.body = o.body;
|
|
273
283
|
Reflect.deleteProperty(params, 'json');
|
|
274
284
|
}
|
|
@@ -286,8 +296,7 @@ export default function _request(options) {
|
|
|
286
296
|
if (!params.json && typeof response.body !== 'object') {
|
|
287
297
|
try {
|
|
288
298
|
response.body = JSON.parse(response.body);
|
|
289
|
-
}
|
|
290
|
-
catch (e) {
|
|
299
|
+
} catch (e) {
|
|
291
300
|
/* eslint no-empty: [0] */
|
|
292
301
|
}
|
|
293
302
|
}
|
|
@@ -71,7 +71,7 @@ describe('http-core', () => {
|
|
|
71
71
|
|
|
72
72
|
it('falls back to a default error message', () => {
|
|
73
73
|
const res = {
|
|
74
|
-
statusCode: 400
|
|
74
|
+
statusCode: 400,
|
|
75
75
|
};
|
|
76
76
|
|
|
77
77
|
const error = new HttpError(res);
|
|
@@ -83,7 +83,7 @@ describe('http-core', () => {
|
|
|
83
83
|
const message = 'an error occurred';
|
|
84
84
|
const res = {
|
|
85
85
|
statusCode: 400,
|
|
86
|
-
body: message
|
|
86
|
+
body: message,
|
|
87
87
|
};
|
|
88
88
|
|
|
89
89
|
const error = new HttpError(res);
|
|
@@ -93,12 +93,12 @@ describe('http-core', () => {
|
|
|
93
93
|
|
|
94
94
|
it('parses JSON responses', () => {
|
|
95
95
|
const message = {
|
|
96
|
-
data: 'an error'
|
|
96
|
+
data: 'an error',
|
|
97
97
|
};
|
|
98
98
|
|
|
99
99
|
const res = {
|
|
100
100
|
statusCode: 400,
|
|
101
|
-
body: message
|
|
101
|
+
body: message,
|
|
102
102
|
};
|
|
103
103
|
|
|
104
104
|
const error = new HttpError(res);
|
|
@@ -108,12 +108,12 @@ describe('http-core', () => {
|
|
|
108
108
|
|
|
109
109
|
it('parses stringified JSON responses', () => {
|
|
110
110
|
const message = JSON.stringify({
|
|
111
|
-
data: 'an error'
|
|
111
|
+
data: 'an error',
|
|
112
112
|
});
|
|
113
113
|
|
|
114
114
|
const res = {
|
|
115
115
|
statusCode: 400,
|
|
116
|
-
body: message
|
|
116
|
+
body: message,
|
|
117
117
|
};
|
|
118
118
|
|
|
119
119
|
const error = new HttpError(res);
|
|
@@ -126,8 +126,8 @@ describe('http-core', () => {
|
|
|
126
126
|
const res = {
|
|
127
127
|
statusCode: 400,
|
|
128
128
|
body: {
|
|
129
|
-
error: message
|
|
130
|
-
}
|
|
129
|
+
error: message,
|
|
130
|
+
},
|
|
131
131
|
};
|
|
132
132
|
|
|
133
133
|
const error = new HttpError(res);
|
|
@@ -141,9 +141,9 @@ describe('http-core', () => {
|
|
|
141
141
|
statusCode: 400,
|
|
142
142
|
body: {
|
|
143
143
|
error: {
|
|
144
|
-
errorString: message
|
|
145
|
-
}
|
|
146
|
-
}
|
|
144
|
+
errorString: message,
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
147
|
};
|
|
148
148
|
|
|
149
149
|
const error = new HttpError(res);
|
|
@@ -13,15 +13,21 @@ describe('http-core', () => {
|
|
|
13
13
|
describe('interceptor', () => {
|
|
14
14
|
let webex;
|
|
15
15
|
|
|
16
|
-
before('create users', () =>
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
16
|
+
before('create users', () =>
|
|
17
|
+
testUsers
|
|
18
|
+
.create({count: 1})
|
|
19
|
+
.then(
|
|
20
|
+
([user]) =>
|
|
21
|
+
new Promise((resolve) => {
|
|
22
|
+
setTimeout(() => resolve(user), 3000);
|
|
23
|
+
})
|
|
24
|
+
)
|
|
25
|
+
.then((user) => {
|
|
26
|
+
webex = new WebexCore({credentials: user.token});
|
|
27
|
+
})
|
|
28
|
+
.then(() => webex.internal.device.register())
|
|
29
|
+
.then(() => webex.internal.services.waitForCatalog('postauth', 10))
|
|
30
|
+
);
|
|
25
31
|
|
|
26
32
|
describe('logOptions', () => {
|
|
27
33
|
let flagged;
|
|
@@ -46,10 +52,11 @@ describe('http-core', () => {
|
|
|
46
52
|
it('calls logger plugin', () => {
|
|
47
53
|
const spy = sinon.spy(webex.logger, 'info');
|
|
48
54
|
|
|
49
|
-
return webex
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
55
|
+
return webex
|
|
56
|
+
.request({
|
|
57
|
+
service: 'hydra',
|
|
58
|
+
resource: 'people/me',
|
|
59
|
+
})
|
|
53
60
|
.then(() => {
|
|
54
61
|
assert.called(spy);
|
|
55
62
|
});
|
|
@@ -47,35 +47,35 @@ describe('http-core', () => {
|
|
|
47
47
|
{
|
|
48
48
|
loaded: undefined,
|
|
49
49
|
total: undefined,
|
|
50
|
-
result: false
|
|
50
|
+
result: false,
|
|
51
51
|
},
|
|
52
52
|
{
|
|
53
53
|
loaded: 10,
|
|
54
54
|
total: undefined,
|
|
55
|
-
result: false
|
|
55
|
+
result: false,
|
|
56
56
|
},
|
|
57
57
|
{
|
|
58
58
|
loaded: undefined,
|
|
59
59
|
total: 10,
|
|
60
|
-
result: false
|
|
60
|
+
result: false,
|
|
61
61
|
},
|
|
62
62
|
{
|
|
63
63
|
loaded: 10,
|
|
64
64
|
total: 10,
|
|
65
|
-
result: true
|
|
65
|
+
result: true,
|
|
66
66
|
},
|
|
67
67
|
{
|
|
68
68
|
loaded: 10,
|
|
69
69
|
total: 0,
|
|
70
|
-
result: false
|
|
70
|
+
result: false,
|
|
71
71
|
},
|
|
72
72
|
{
|
|
73
73
|
loaded: 0,
|
|
74
74
|
total: 0,
|
|
75
|
-
result: false
|
|
76
|
-
}
|
|
75
|
+
result: false,
|
|
76
|
+
},
|
|
77
77
|
].forEach((item) => {
|
|
78
|
-
assert.equal(
|
|
78
|
+
assert.equal(new ProgressEvent(item.loaded, item.total).lengthComputable, item.result);
|
|
79
79
|
});
|
|
80
80
|
});
|
|
81
81
|
});
|