@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.
Files changed (43) hide show
  1. package/.eslintrc.js +6 -6
  2. package/README.md +64 -64
  3. package/babel.config.js +3 -3
  4. package/dist/http-error-subtypes.js +57 -57
  5. package/dist/http-error-subtypes.js.map +1 -1
  6. package/dist/http-error.js +25 -25
  7. package/dist/http-error.js.map +1 -1
  8. package/dist/index.js.map +1 -1
  9. package/dist/interceptors/http-status.js +13 -13
  10. package/dist/interceptors/http-status.js.map +1 -1
  11. package/dist/lib/detect.js +6 -6
  12. package/dist/lib/detect.js.map +1 -1
  13. package/dist/lib/interceptor.js +34 -34
  14. package/dist/lib/interceptor.js.map +1 -1
  15. package/dist/lib/xhr.js +2 -2
  16. package/dist/lib/xhr.js.map +1 -1
  17. package/dist/progress-event.js +6 -6
  18. package/dist/progress-event.js.map +1 -1
  19. package/dist/request/index.js +11 -11
  20. package/dist/request/index.js.map +1 -1
  21. package/dist/request/request.js +14 -14
  22. package/dist/request/request.js.map +1 -1
  23. package/dist/request/request.shim.js +65 -65
  24. package/dist/request/request.shim.js.map +1 -1
  25. package/jest.config.js +3 -3
  26. package/package.json +17 -18
  27. package/process +1 -1
  28. package/src/http-error-subtypes.js +187 -187
  29. package/src/http-error.js +147 -147
  30. package/src/index.js +58 -58
  31. package/src/interceptors/http-status.js +63 -63
  32. package/src/lib/detect.js +33 -33
  33. package/src/lib/interceptor.js +95 -95
  34. package/src/lib/xhr.js +258 -258
  35. package/src/progress-event.js +37 -37
  36. package/src/request/index.js +62 -62
  37. package/src/request/request.js +109 -109
  38. package/src/request/request.shim.js +304 -304
  39. package/test/integration/spec/http-error.js +188 -188
  40. package/test/integration/spec/interceptor.js +71 -71
  41. package/test/integration/spec/progress-event.js +83 -83
  42. package/test/integration/spec/request.js +310 -310
  43. package/test/unit/spec/interceptors/http-status.js +49 -49
package/src/index.js CHANGED
@@ -1,58 +1,58 @@
1
- /*!
2
- * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
3
- */
4
-
5
- import {assign, curry, defaults as lodashDefaults, isString} from 'lodash';
6
-
7
- import HttpStatusInterceptor from './interceptors/http-status';
8
- import _request from './request';
9
-
10
- // Curry protorequest so we generate a function with default options built in.
11
- const protorequest = curry(function protorequest(defaultOptions, options) {
12
- // allow for options to be a string (and therefore expect options in the third
13
- // position) to match request's api.
14
- if (isString(options)) {
15
- const uri = options;
16
-
17
- /* eslint prefer-rest-params: [0] */
18
- options = arguments[2] || {};
19
- options.uri = uri;
20
- }
21
-
22
- // Hide useless elements from logs
23
- ['download', 'interceptors', 'logger', 'upload'].forEach((prop) => {
24
- let descriptor = Reflect.getOwnPropertyDescriptor(options, prop);
25
-
26
- descriptor = assign({}, descriptor, {
27
- enumerable: false,
28
- writable: true,
29
- });
30
- Reflect.defineProperty(options, prop, descriptor);
31
- });
32
-
33
- lodashDefaults(options, defaultOptions);
34
-
35
- if (!options.json && options.json !== false) {
36
- Reflect.deleteProperty(options, 'json');
37
- }
38
-
39
- options.logger = options.logger || this.logger || console;
40
-
41
- return _request(options);
42
- });
43
-
44
- const defaultOptions = {
45
- json: true,
46
- interceptors: [
47
- // Reminder: this is supposed to be an instantiated interceptor.
48
- HttpStatusInterceptor.create(),
49
- ],
50
- };
51
-
52
- export const defaults = protorequest;
53
- export const request = protorequest(defaultOptions);
54
- export {default as ProgressEvent} from './progress-event';
55
- export {default as Interceptor} from './lib/interceptor';
56
- export {default as HttpError} from './http-error';
57
- export {default as HttpStatusInterceptor} from './interceptors/http-status';
58
- export {default as detect} from './lib/detect';
1
+ /*!
2
+ * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
3
+ */
4
+
5
+ import {assign, curry, defaults as lodashDefaults, isString} from 'lodash';
6
+
7
+ import HttpStatusInterceptor from './interceptors/http-status';
8
+ import _request from './request';
9
+
10
+ // Curry protorequest so we generate a function with default options built in.
11
+ const protorequest = curry(function protorequest(defaultOptions, options) {
12
+ // allow for options to be a string (and therefore expect options in the third
13
+ // position) to match request's api.
14
+ if (isString(options)) {
15
+ const uri = options;
16
+
17
+ /* eslint prefer-rest-params: [0] */
18
+ options = arguments[2] || {};
19
+ options.uri = uri;
20
+ }
21
+
22
+ // Hide useless elements from logs
23
+ ['download', 'interceptors', 'logger', 'upload'].forEach((prop) => {
24
+ let descriptor = Reflect.getOwnPropertyDescriptor(options, prop);
25
+
26
+ descriptor = assign({}, descriptor, {
27
+ enumerable: false,
28
+ writable: true,
29
+ });
30
+ Reflect.defineProperty(options, prop, descriptor);
31
+ });
32
+
33
+ lodashDefaults(options, defaultOptions);
34
+
35
+ if (!options.json && options.json !== false) {
36
+ Reflect.deleteProperty(options, 'json');
37
+ }
38
+
39
+ options.logger = options.logger || this.logger || console;
40
+
41
+ return _request(options);
42
+ });
43
+
44
+ const defaultOptions = {
45
+ json: true,
46
+ interceptors: [
47
+ // Reminder: this is supposed to be an instantiated interceptor.
48
+ HttpStatusInterceptor.create(),
49
+ ],
50
+ };
51
+
52
+ export const defaults = protorequest;
53
+ export const request = protorequest(defaultOptions);
54
+ export {default as ProgressEvent} from './progress-event';
55
+ export {default as Interceptor} from './lib/interceptor';
56
+ export {default as HttpError} from './http-error';
57
+ export {default as HttpStatusInterceptor} from './interceptors/http-status';
58
+ export {default as detect} from './lib/detect';
@@ -1,63 +1,63 @@
1
- /*!
2
- * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
3
- */
4
-
5
- import HttpError from '../http-error';
6
- import Interceptor from '../lib/interceptor';
7
-
8
- const LOCUS_REDIRECT_ERROR = 2000002;
9
-
10
- /**
11
- * @class
12
- */
13
- export default class HttpStatusInterceptor extends Interceptor {
14
- /**
15
- * @param {Object} webex
16
- * @param {Object} options
17
- * @returns {HttpStatusInterceptor}
18
- */
19
- constructor(webex, options) {
20
- super(webex);
21
- const ErrorConstructor = (options && (options.error || options.ErrorConstructor)) || HttpError;
22
-
23
- Object.defineProperties(this, {
24
- ErrorConstructor: {
25
- value: ErrorConstructor,
26
- },
27
- });
28
- }
29
-
30
- /**
31
- * @param {Object} options
32
- * @returns {HttpStatusInterceptor}
33
- */
34
- static create(options) {
35
- return new HttpStatusInterceptor(this, options);
36
- }
37
-
38
- /**
39
- * @param {Object} options
40
- * @param {HttpResponse} response
41
- * @returns {Promise}
42
- */
43
- onResponse(options, response) {
44
- if (response.statusCode) {
45
- if (response.statusCode < 400) {
46
- return Promise.resolve(response);
47
- }
48
- // to handle locus redirects
49
- if (
50
- response.statusCode === 404 &&
51
- response.body &&
52
- response.body.errorCode === LOCUS_REDIRECT_ERROR
53
- ) {
54
- return Promise.resolve(response);
55
- }
56
- }
57
-
58
- // Note: the extra parenthesis below are required to make sure `new` is
59
- // applied to the correct method (i.e., the result of `select()`, not
60
- // `select()` itself).
61
- return Promise.reject(new (this.ErrorConstructor.select(response.statusCode))(response));
62
- }
63
- }
1
+ /*!
2
+ * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
3
+ */
4
+
5
+ import HttpError from '../http-error';
6
+ import Interceptor from '../lib/interceptor';
7
+
8
+ const LOCUS_REDIRECT_ERROR = 2000002;
9
+
10
+ /**
11
+ * @class
12
+ */
13
+ export default class HttpStatusInterceptor extends Interceptor {
14
+ /**
15
+ * @param {Object} webex
16
+ * @param {Object} options
17
+ * @returns {HttpStatusInterceptor}
18
+ */
19
+ constructor(webex, options) {
20
+ super(webex);
21
+ const ErrorConstructor = (options && (options.error || options.ErrorConstructor)) || HttpError;
22
+
23
+ Object.defineProperties(this, {
24
+ ErrorConstructor: {
25
+ value: ErrorConstructor,
26
+ },
27
+ });
28
+ }
29
+
30
+ /**
31
+ * @param {Object} options
32
+ * @returns {HttpStatusInterceptor}
33
+ */
34
+ static create(options) {
35
+ return new HttpStatusInterceptor(this, options);
36
+ }
37
+
38
+ /**
39
+ * @param {Object} options
40
+ * @param {HttpResponse} response
41
+ * @returns {Promise}
42
+ */
43
+ onResponse(options, response) {
44
+ if (response.statusCode) {
45
+ if (response.statusCode < 400) {
46
+ return Promise.resolve(response);
47
+ }
48
+ // to handle locus redirects
49
+ if (
50
+ response.statusCode === 404 &&
51
+ response.body &&
52
+ response.body.errorCode === LOCUS_REDIRECT_ERROR
53
+ ) {
54
+ return Promise.resolve(response);
55
+ }
56
+ }
57
+
58
+ // Note: the extra parenthesis below are required to make sure `new` is
59
+ // applied to the correct method (i.e., the result of `select()`, not
60
+ // `select()` itself).
61
+ return Promise.reject(new (this.ErrorConstructor.select(response.statusCode))(response));
62
+ }
63
+ }
package/src/lib/detect.js CHANGED
@@ -1,33 +1,33 @@
1
- /*!
2
- * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
3
- */
4
-
5
- import {fromBuffer} from 'file-type';
6
-
7
- /**
8
- * Determine mimeType for the specified buffer;
9
- * @param {Buffer|Uint8Array|ArrayBuffer} buffer
10
- * @returns {Promise<string>}
11
- */
12
- export default async function detect(buffer) {
13
- if (
14
- !(buffer instanceof Blob) &&
15
- !(buffer instanceof ArrayBuffer) &&
16
- !(buffer instanceof Uint8Array)
17
- ) {
18
- throw new Error('`detect` requires a buffer of type Blob, ArrayBuffer, or Uint8Array');
19
- }
20
-
21
- if (buffer instanceof Blob) {
22
- return buffer.type;
23
- }
24
-
25
- // `fromBuffer()` can take a buffer that is either a ArrayBuffer or Uinit8Array
26
- const fileType = await fromBuffer(buffer);
27
-
28
- if (!fileType) {
29
- return 'application/octet-stream';
30
- }
31
-
32
- return fileType.mime;
33
- }
1
+ /*!
2
+ * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
3
+ */
4
+
5
+ import {fromBuffer} from 'file-type';
6
+
7
+ /**
8
+ * Determine mimeType for the specified buffer;
9
+ * @param {Buffer|Uint8Array|ArrayBuffer} buffer
10
+ * @returns {Promise<string>}
11
+ */
12
+ export default async function detect(buffer) {
13
+ if (
14
+ !(buffer instanceof Blob) &&
15
+ !(buffer instanceof ArrayBuffer) &&
16
+ !(buffer instanceof Uint8Array)
17
+ ) {
18
+ throw new Error('`detect` requires a buffer of type Blob, ArrayBuffer, or Uint8Array');
19
+ }
20
+
21
+ if (buffer instanceof Blob) {
22
+ return buffer.type;
23
+ }
24
+
25
+ // `fromBuffer()` can take a buffer that is either a ArrayBuffer or Uinit8Array
26
+ const fileType = await fromBuffer(buffer);
27
+
28
+ if (!fileType) {
29
+ return 'application/octet-stream';
30
+ }
31
+
32
+ return fileType.mime;
33
+ }
@@ -1,95 +1,95 @@
1
- /*!
2
- * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
3
- */
4
-
5
- import {get} from 'lodash';
6
-
7
- /**
8
- * @class
9
- */
10
- export default class Interceptor {
11
- /**
12
- * @constructor
13
- * @param {Object} attrs
14
- * @returns {UrlInterceptor}
15
- */
16
- constructor(attrs) {
17
- if (attrs) {
18
- Object.keys(attrs).forEach((key) => {
19
- const value = attrs[key];
20
-
21
- Reflect.defineProperty(this, key, {
22
- enumerable: true,
23
- value,
24
- });
25
- });
26
- }
27
- }
28
-
29
- /**
30
- * Logs the options of a request. This should be utilized
31
- * during the intercepting process, but can be used at any
32
- * time otherwise.
33
- * @param {object} options
34
- * @returns {void}
35
- */
36
- logOptions(options = {}) {
37
- const logger = get(this, 'webex.logger', console);
38
-
39
- if (!process.env.ENABLE_VERBOSE_NETWORK_LOGGING || !logger) {
40
- return;
41
- }
42
-
43
- // prepend a header for the interceptor
44
- logger.info('/***** Interceptor ****************************************************\\');
45
-
46
- logger.info(`${this.constructor.name} - ${JSON.stringify(options, null, 2)}`);
47
- }
48
-
49
- /**
50
- * @abstract
51
- * @returns {Interceptor}
52
- */
53
- static create() {
54
- throw new Error('`Interceptor.create()` must be defined');
55
- }
56
-
57
- /**
58
- * Transform request options before sending them
59
- * @param {Object} options
60
- * @returns {Promise<Object>}
61
- */
62
- onRequest(options) {
63
- return Promise.resolve(options);
64
- }
65
-
66
- /**
67
- * Handle request failures
68
- * @param {Object} options
69
- * @param {Error} reason
70
- * @returns {RejectedPromise<Error>}
71
- */
72
- onRequestError(options, reason) {
73
- return Promise.reject(reason);
74
- }
75
-
76
- /**
77
- * Transform response before returning it
78
- * @param {Object} options
79
- * @param {HttpResponse} response
80
- * @returns {Promise<HttpResponse>}
81
- */
82
- onResponse(options, response) {
83
- return Promise.resolve(response);
84
- }
85
-
86
- /**
87
- * Handle response errors
88
- * @param {Object} options
89
- * @param {WebexHttpError} reason
90
- * @returns {Promise<WebexHttpError>}
91
- */
92
- onResponseError(options, reason) {
93
- return Promise.reject(reason);
94
- }
95
- }
1
+ /*!
2
+ * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
3
+ */
4
+
5
+ import {get} from 'lodash';
6
+
7
+ /**
8
+ * @class
9
+ */
10
+ export default class Interceptor {
11
+ /**
12
+ * @constructor
13
+ * @param {Object} attrs
14
+ * @returns {UrlInterceptor}
15
+ */
16
+ constructor(attrs) {
17
+ if (attrs) {
18
+ Object.keys(attrs).forEach((key) => {
19
+ const value = attrs[key];
20
+
21
+ Reflect.defineProperty(this, key, {
22
+ enumerable: true,
23
+ value,
24
+ });
25
+ });
26
+ }
27
+ }
28
+
29
+ /**
30
+ * Logs the options of a request. This should be utilized
31
+ * during the intercepting process, but can be used at any
32
+ * time otherwise.
33
+ * @param {object} options
34
+ * @returns {void}
35
+ */
36
+ logOptions(options = {}) {
37
+ const logger = get(this, 'webex.logger', console);
38
+
39
+ if (!process.env.ENABLE_VERBOSE_NETWORK_LOGGING || !logger) {
40
+ return;
41
+ }
42
+
43
+ // prepend a header for the interceptor
44
+ logger.info('/***** Interceptor ****************************************************\\');
45
+
46
+ logger.info(`${this.constructor.name} - ${JSON.stringify(options, null, 2)}`);
47
+ }
48
+
49
+ /**
50
+ * @abstract
51
+ * @returns {Interceptor}
52
+ */
53
+ static create() {
54
+ throw new Error('`Interceptor.create()` must be defined');
55
+ }
56
+
57
+ /**
58
+ * Transform request options before sending them
59
+ * @param {Object} options
60
+ * @returns {Promise<Object>}
61
+ */
62
+ onRequest(options) {
63
+ return Promise.resolve(options);
64
+ }
65
+
66
+ /**
67
+ * Handle request failures
68
+ * @param {Object} options
69
+ * @param {Error} reason
70
+ * @returns {RejectedPromise<Error>}
71
+ */
72
+ onRequestError(options, reason) {
73
+ return Promise.reject(reason);
74
+ }
75
+
76
+ /**
77
+ * Transform response before returning it
78
+ * @param {Object} options
79
+ * @param {HttpResponse} response
80
+ * @returns {Promise<HttpResponse>}
81
+ */
82
+ onResponse(options, response) {
83
+ return Promise.resolve(response);
84
+ }
85
+
86
+ /**
87
+ * Handle response errors
88
+ * @param {Object} options
89
+ * @param {WebexHttpError} reason
90
+ * @returns {Promise<WebexHttpError>}
91
+ */
92
+ onResponseError(options, reason) {
93
+ return Promise.reject(reason);
94
+ }
95
+ }