@webex/http-core 3.0.0-beta.20 → 3.0.0-beta.200
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/dist/index.js +48 -1
- package/dist/index.js.map +1 -1
- package/dist/request/index.js +3 -30
- package/dist/request/index.js.map +1 -1
- package/dist/request/utils.js +91 -0
- package/dist/request/utils.js.map +1 -0
- package/package.json +10 -10
- package/src/index.js +55 -0
- package/src/request/index.js +4 -36
- package/src/request/utils.ts +78 -0
- package/test/integration/spec/request.js +2 -1
- package/test/unit/spec/index.js +58 -0
- package/test/unit/spec/request/utils.js +77 -0
package/dist/index.js
CHANGED
|
@@ -36,7 +36,7 @@ _Object$defineProperty(exports, "detect", {
|
|
|
36
36
|
return _detect.default;
|
|
37
37
|
}
|
|
38
38
|
});
|
|
39
|
-
exports.request = void 0;
|
|
39
|
+
exports.setTimingsAndFetch = exports.request = exports.protoprepareFetchOptions = void 0;
|
|
40
40
|
var _getOwnPropertyDescriptor = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/reflect/get-own-property-descriptor"));
|
|
41
41
|
var _defineProperty = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/reflect/define-property"));
|
|
42
42
|
var _deleteProperty = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/reflect/delete-property"));
|
|
@@ -46,6 +46,7 @@ var _curry2 = _interopRequireDefault(require("lodash/curry"));
|
|
|
46
46
|
var _assign2 = _interopRequireDefault(require("lodash/assign"));
|
|
47
47
|
var _httpStatus = _interopRequireDefault(require("./interceptors/http-status"));
|
|
48
48
|
var _request2 = _interopRequireDefault(require("./request"));
|
|
49
|
+
var _utils = require("./request/utils");
|
|
49
50
|
var _progressEvent = _interopRequireDefault(require("./progress-event"));
|
|
50
51
|
var _interceptor = _interopRequireDefault(require("./lib/interceptor"));
|
|
51
52
|
var _httpError = _interopRequireDefault(require("./http-error"));
|
|
@@ -78,6 +79,52 @@ var protorequest = (0, _curry2.default)(function protorequest(defaultOptions, op
|
|
|
78
79
|
options.logger = options.logger || this.logger || console;
|
|
79
80
|
return (0, _request2.default)(options);
|
|
80
81
|
});
|
|
82
|
+
var protoprepareFetchOptions = (0, _curry2.default)(function protoprepareFetchOptions(defaultOptions, options) {
|
|
83
|
+
// Hide useless elements from logs
|
|
84
|
+
['download', 'interceptors', 'logger', 'upload'].forEach(function (prop) {
|
|
85
|
+
var descriptor = (0, _getOwnPropertyDescriptor.default)(options, prop);
|
|
86
|
+
descriptor = (0, _assign2.default)({}, descriptor, {
|
|
87
|
+
enumerable: false,
|
|
88
|
+
writable: true
|
|
89
|
+
});
|
|
90
|
+
(0, _defineProperty.default)(options, prop, descriptor);
|
|
91
|
+
});
|
|
92
|
+
(0, _defaults2.default)(options, defaultOptions);
|
|
93
|
+
options.logger = options.logger || this.logger || console;
|
|
94
|
+
return (0, _utils.prepareFetchOptions)(options);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Sets the $timings value(s) before the request/fetch.
|
|
99
|
+
* This function is only useful if you are about to send a request
|
|
100
|
+
* using prepared fetch options; normally it is done in webex.request();
|
|
101
|
+
*
|
|
102
|
+
* @param {any} options
|
|
103
|
+
* @returns {any} the updated options object
|
|
104
|
+
*/
|
|
105
|
+
exports.protoprepareFetchOptions = protoprepareFetchOptions;
|
|
106
|
+
var setRequestTimings = function setRequestTimings(options) {
|
|
107
|
+
var now = new Date().getTime();
|
|
108
|
+
options.$timings = options.$timings || {};
|
|
109
|
+
options.$timings.requestStart = now;
|
|
110
|
+
options.$timings.networkStart = now;
|
|
111
|
+
return options;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Submits a metric from pre-built request options via the fetch API. Updates
|
|
116
|
+
* the "$timings" values to Date.now() since the existing times were set when
|
|
117
|
+
* the options were built (not submitted).
|
|
118
|
+
*
|
|
119
|
+
* @param {any} options - the pre-built request options for submitting a metric
|
|
120
|
+
* @returns {Promise} promise that resolves to a response object
|
|
121
|
+
*/
|
|
122
|
+
var setTimingsAndFetch = function setTimingsAndFetch(options) {
|
|
123
|
+
// call the fetch API
|
|
124
|
+
var opts = setRequestTimings(options);
|
|
125
|
+
return fetch(opts.uri, opts);
|
|
126
|
+
};
|
|
127
|
+
exports.setTimingsAndFetch = setTimingsAndFetch;
|
|
81
128
|
var defaultOptions = {
|
|
82
129
|
json: true,
|
|
83
130
|
interceptors: [
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["protorequest","defaultOptions","options","uri","arguments","forEach","prop","descriptor","enumerable","writable","json","logger","console","_request","interceptors","HttpStatusInterceptor","create","defaults","request"],"sources":["index.js"],"sourcesContent":["/*!\n * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.\n */\n\nimport {assign, curry, defaults as lodashDefaults, isString} from 'lodash';\n\nimport HttpStatusInterceptor from './interceptors/http-status';\nimport _request from './request';\n\n// Curry protorequest so we generate a function with default options built in.\nconst protorequest = curry(function protorequest(defaultOptions, options) {\n // allow for options to be a string (and therefore expect options in the third\n // position) to match request's api.\n if (isString(options)) {\n const uri = options;\n\n /* eslint prefer-rest-params: [0] */\n options = arguments[2] || {};\n options.uri = uri;\n }\n\n // Hide useless elements from logs\n ['download', 'interceptors', 'logger', 'upload'].forEach((prop) => {\n let descriptor = Reflect.getOwnPropertyDescriptor(options, prop);\n\n descriptor = assign({}, descriptor, {\n enumerable: false,\n writable: true,\n });\n Reflect.defineProperty(options, prop, descriptor);\n });\n\n lodashDefaults(options, defaultOptions);\n\n if (!options.json && options.json !== false) {\n Reflect.deleteProperty(options, 'json');\n }\n\n options.logger = options.logger || this.logger || console;\n\n return _request(options);\n});\n\nconst defaultOptions = {\n json: true,\n interceptors: [\n // Reminder: this is supposed to be an instantiated interceptor.\n HttpStatusInterceptor.create(),\n ],\n};\n\nexport const defaults = protorequest;\nexport const request = protorequest(defaultOptions);\nexport {default as ProgressEvent} from './progress-event';\nexport {default as Interceptor} from './lib/interceptor';\nexport {default as HttpError} from './http-error';\nexport {default as HttpStatusInterceptor} from './interceptors/http-status';\nexport {default as detect} from './lib/detect';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAMA;AACA;
|
|
1
|
+
{"version":3,"names":["protorequest","defaultOptions","options","uri","arguments","forEach","prop","descriptor","enumerable","writable","json","logger","console","_request","protoprepareFetchOptions","_prepareFetchOptions","setRequestTimings","now","Date","getTime","$timings","requestStart","networkStart","setTimingsAndFetch","opts","fetch","interceptors","HttpStatusInterceptor","create","defaults","request"],"sources":["index.js"],"sourcesContent":["/*!\n * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.\n */\n\nimport {assign, curry, defaults as lodashDefaults, isString} from 'lodash';\n\nimport HttpStatusInterceptor from './interceptors/http-status';\nimport _request from './request';\nimport {prepareFetchOptions as _prepareFetchOptions} from './request/utils';\n\n// Curry protorequest so we generate a function with default options built in.\nconst protorequest = curry(function protorequest(defaultOptions, options) {\n // allow for options to be a string (and therefore expect options in the third\n // position) to match request's api.\n if (isString(options)) {\n const uri = options;\n\n /* eslint prefer-rest-params: [0] */\n options = arguments[2] || {};\n options.uri = uri;\n }\n\n // Hide useless elements from logs\n ['download', 'interceptors', 'logger', 'upload'].forEach((prop) => {\n let descriptor = Reflect.getOwnPropertyDescriptor(options, prop);\n\n descriptor = assign({}, descriptor, {\n enumerable: false,\n writable: true,\n });\n Reflect.defineProperty(options, prop, descriptor);\n });\n\n lodashDefaults(options, defaultOptions);\n\n if (!options.json && options.json !== false) {\n Reflect.deleteProperty(options, 'json');\n }\n\n options.logger = options.logger || this.logger || console;\n\n return _request(options);\n});\n\nexport const protoprepareFetchOptions = curry(function protoprepareFetchOptions(\n defaultOptions,\n options\n) {\n // Hide useless elements from logs\n ['download', 'interceptors', 'logger', 'upload'].forEach((prop) => {\n let descriptor = Reflect.getOwnPropertyDescriptor(options, prop);\n\n descriptor = assign({}, descriptor, {\n enumerable: false,\n writable: true,\n });\n Reflect.defineProperty(options, prop, descriptor);\n });\n\n lodashDefaults(options, defaultOptions);\n\n options.logger = options.logger || this.logger || console;\n\n return _prepareFetchOptions(options);\n});\n\n/**\n * Sets the $timings value(s) before the request/fetch.\n * This function is only useful if you are about to send a request\n * using prepared fetch options; normally it is done in webex.request();\n *\n * @param {any} options\n * @returns {any} the updated options object\n */\nconst setRequestTimings = (options) => {\n const now = new Date().getTime();\n options.$timings = options.$timings || {};\n options.$timings.requestStart = now;\n options.$timings.networkStart = now;\n\n return options;\n};\n\n/**\n * Submits a metric from pre-built request options via the fetch API. Updates\n * the \"$timings\" values to Date.now() since the existing times were set when\n * the options were built (not submitted).\n *\n * @param {any} options - the pre-built request options for submitting a metric\n * @returns {Promise} promise that resolves to a response object\n */\nexport const setTimingsAndFetch = (options) => {\n // call the fetch API\n const opts = setRequestTimings(options);\n\n return fetch(opts.uri, opts);\n};\n\nconst defaultOptions = {\n json: true,\n interceptors: [\n // Reminder: this is supposed to be an instantiated interceptor.\n HttpStatusInterceptor.create(),\n ],\n};\n\nexport const defaults = protorequest;\nexport const request = protorequest(defaultOptions);\nexport {default as ProgressEvent} from './progress-event';\nexport {default as Interceptor} from './lib/interceptor';\nexport {default as HttpError} from './http-error';\nexport {default as HttpStatusInterceptor} from './interceptors/http-status';\nexport {default as detect} from './lib/detect';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAMA;AACA;AACA;AAoGA;AACA;AACA;AAEA;AAtGA;AACA,IAAMA,YAAY,GAAG,qBAAM,SAASA,YAAY,CAACC,cAAc,EAAEC,OAAO,EAAE;EACxE;EACA;EACA,IAAI,wBAASA,OAAO,CAAC,EAAE;IACrB,IAAMC,GAAG,GAAGD,OAAO;;IAEnB;IACAA,OAAO,GAAGE,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;IAC5BF,OAAO,CAACC,GAAG,GAAGA,GAAG;EACnB;;EAEA;EACA,CAAC,UAAU,EAAE,cAAc,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAACE,OAAO,CAAC,UAACC,IAAI,EAAK;IACjE,IAAIC,UAAU,GAAG,uCAAiCL,OAAO,EAAEI,IAAI,CAAC;IAEhEC,UAAU,GAAG,sBAAO,CAAC,CAAC,EAAEA,UAAU,EAAE;MAClCC,UAAU,EAAE,KAAK;MACjBC,QAAQ,EAAE;IACZ,CAAC,CAAC;IACF,6BAAuBP,OAAO,EAAEI,IAAI,EAAEC,UAAU,CAAC;EACnD,CAAC,CAAC;EAEF,wBAAeL,OAAO,EAAED,cAAc,CAAC;EAEvC,IAAI,CAACC,OAAO,CAACQ,IAAI,IAAIR,OAAO,CAACQ,IAAI,KAAK,KAAK,EAAE;IAC3C,6BAAuBR,OAAO,EAAE,MAAM,CAAC;EACzC;EAEAA,OAAO,CAACS,MAAM,GAAGT,OAAO,CAACS,MAAM,IAAI,IAAI,CAACA,MAAM,IAAIC,OAAO;EAEzD,OAAO,IAAAC,iBAAQ,EAACX,OAAO,CAAC;AAC1B,CAAC,CAAC;AAEK,IAAMY,wBAAwB,GAAG,qBAAM,SAASA,wBAAwB,CAC7Eb,cAAc,EACdC,OAAO,EACP;EACA;EACA,CAAC,UAAU,EAAE,cAAc,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAACG,OAAO,CAAC,UAACC,IAAI,EAAK;IACjE,IAAIC,UAAU,GAAG,uCAAiCL,OAAO,EAAEI,IAAI,CAAC;IAEhEC,UAAU,GAAG,sBAAO,CAAC,CAAC,EAAEA,UAAU,EAAE;MAClCC,UAAU,EAAE,KAAK;MACjBC,QAAQ,EAAE;IACZ,CAAC,CAAC;IACF,6BAAuBP,OAAO,EAAEI,IAAI,EAAEC,UAAU,CAAC;EACnD,CAAC,CAAC;EAEF,wBAAeL,OAAO,EAAED,cAAc,CAAC;EAEvCC,OAAO,CAACS,MAAM,GAAGT,OAAO,CAACS,MAAM,IAAI,IAAI,CAACA,MAAM,IAAIC,OAAO;EAEzD,OAAO,IAAAG,0BAAoB,EAACb,OAAO,CAAC;AACtC,CAAC,CAAC;;AAEF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAPA;AAQA,IAAMc,iBAAiB,GAAG,SAApBA,iBAAiB,CAAId,OAAO,EAAK;EACrC,IAAMe,GAAG,GAAG,IAAIC,IAAI,EAAE,CAACC,OAAO,EAAE;EAChCjB,OAAO,CAACkB,QAAQ,GAAGlB,OAAO,CAACkB,QAAQ,IAAI,CAAC,CAAC;EACzClB,OAAO,CAACkB,QAAQ,CAACC,YAAY,GAAGJ,GAAG;EACnCf,OAAO,CAACkB,QAAQ,CAACE,YAAY,GAAGL,GAAG;EAEnC,OAAOf,OAAO;AAChB,CAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,IAAMqB,kBAAkB,GAAG,SAArBA,kBAAkB,CAAIrB,OAAO,EAAK;EAC7C;EACA,IAAMsB,IAAI,GAAGR,iBAAiB,CAACd,OAAO,CAAC;EAEvC,OAAOuB,KAAK,CAACD,IAAI,CAACrB,GAAG,EAAEqB,IAAI,CAAC;AAC9B,CAAC;AAAC;AAEF,IAAMvB,cAAc,GAAG;EACrBS,IAAI,EAAE,IAAI;EACVgB,YAAY,EAAE;EACZ;EACAC,mBAAqB,CAACC,MAAM,EAAE;AAElC,CAAC;AAEM,IAAMC,QAAQ,GAAG7B,YAAY;AAAC;AAC9B,IAAM8B,OAAO,GAAG9B,YAAY,CAACC,cAAc,CAAC;AAAC"}
|
package/dist/request/index.js
CHANGED
|
@@ -6,9 +6,9 @@ _Object$defineProperty(exports, "__esModule", {
|
|
|
6
6
|
value: true
|
|
7
7
|
});
|
|
8
8
|
exports.default = request;
|
|
9
|
-
var _promise = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/promise"));
|
|
10
9
|
var _events = require("events");
|
|
11
10
|
var _request2 = _interopRequireDefault(require("./request"));
|
|
11
|
+
var _utils = require("./utils");
|
|
12
12
|
/*!
|
|
13
13
|
* Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
|
|
14
14
|
*/
|
|
@@ -25,7 +25,7 @@ function request(options) {
|
|
|
25
25
|
options.headers = options.headers || {};
|
|
26
26
|
options.download = new _events.EventEmitter();
|
|
27
27
|
options.upload = new _events.EventEmitter();
|
|
28
|
-
return intercept(options.interceptors, 'Request').then(function () {
|
|
28
|
+
return (0, _utils.intercept)(options, options.interceptors, 'Request').then(function () {
|
|
29
29
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
30
30
|
args[_key] = arguments[_key];
|
|
31
31
|
}
|
|
@@ -34,34 +34,7 @@ function request(options) {
|
|
|
34
34
|
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
|
35
35
|
args[_key2] = arguments[_key2];
|
|
36
36
|
}
|
|
37
|
-
return intercept.apply(void 0, [options.interceptors.slice().reverse(), 'Response'].concat(args));
|
|
37
|
+
return _utils.intercept.apply(void 0, [options, options.interceptors.slice().reverse(), 'Response'].concat(args));
|
|
38
38
|
});
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* @param {Array} interceptors
|
|
42
|
-
* @param {string} key
|
|
43
|
-
* @param {Object} res
|
|
44
|
-
* @private
|
|
45
|
-
* @returns {Promise}
|
|
46
|
-
*/
|
|
47
|
-
function intercept(interceptors, key, res) {
|
|
48
|
-
var successKey = "on".concat(key);
|
|
49
|
-
var errorKey = "on".concat(key, "Error");
|
|
50
|
-
return interceptors.reduce(function (promise, interceptor) {
|
|
51
|
-
return promise.then(function (result) {
|
|
52
|
-
interceptor.logOptions(options);
|
|
53
|
-
if (interceptor[successKey]) {
|
|
54
|
-
return interceptor[successKey](options, result);
|
|
55
|
-
}
|
|
56
|
-
return _promise.default.resolve(result);
|
|
57
|
-
}, function (reason) {
|
|
58
|
-
interceptor.logOptions(options);
|
|
59
|
-
if (interceptor[errorKey]) {
|
|
60
|
-
return interceptor[errorKey](options, reason);
|
|
61
|
-
}
|
|
62
|
-
return _promise.default.reject(reason);
|
|
63
|
-
});
|
|
64
|
-
}, _promise.default.resolve(res));
|
|
65
|
-
}
|
|
66
39
|
}
|
|
67
40
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["request","options","url","uri","headers","download","EventEmitter","upload","intercept","interceptors","then","args","_request","slice","reverse"
|
|
1
|
+
{"version":3,"names":["request","options","url","uri","headers","download","EventEmitter","upload","intercept","interceptors","then","args","_request","slice","reverse"],"sources":["index.js"],"sourcesContent":["/*!\n * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.\n */\n\nimport {EventEmitter} from 'events';\n\nimport _request from './request';\nimport {intercept} from './utils';\n\n/**\n * @param {Object} options\n * @returns {Promise}\n */\nexport default function request(options) {\n if (options.url) {\n options.uri = options.url;\n options.url = null;\n }\n\n options.headers = options.headers || {};\n\n options.download = new EventEmitter();\n options.upload = new EventEmitter();\n\n return intercept(options, options.interceptors, 'Request')\n .then((...args) => _request(options, ...args))\n .then((...args) =>\n intercept(options, options.interceptors.slice().reverse(), 'Response', ...args)\n );\n}\n"],"mappings":";;;;;;;;AAIA;AAEA;AACA;AAPA;AACA;AACA;;AAOA;AACA;AACA;AACA;AACe,SAASA,OAAO,CAACC,OAAO,EAAE;EACvC,IAAIA,OAAO,CAACC,GAAG,EAAE;IACfD,OAAO,CAACE,GAAG,GAAGF,OAAO,CAACC,GAAG;IACzBD,OAAO,CAACC,GAAG,GAAG,IAAI;EACpB;EAEAD,OAAO,CAACG,OAAO,GAAGH,OAAO,CAACG,OAAO,IAAI,CAAC,CAAC;EAEvCH,OAAO,CAACI,QAAQ,GAAG,IAAIC,oBAAY,EAAE;EACrCL,OAAO,CAACM,MAAM,GAAG,IAAID,oBAAY,EAAE;EAEnC,OAAO,IAAAE,gBAAS,EAACP,OAAO,EAAEA,OAAO,CAACQ,YAAY,EAAE,SAAS,CAAC,CACvDC,IAAI,CAAC;IAAA,kCAAIC,IAAI;MAAJA,IAAI;IAAA;IAAA,OAAKC,iBAAQ,gBAACX,OAAO,SAAKU,IAAI,EAAC;EAAA,EAAC,CAC7CD,IAAI,CAAC;IAAA,mCAAIC,IAAI;MAAJA,IAAI;IAAA;IAAA,OACZH,gBAAS,gBAACP,OAAO,EAAEA,OAAO,CAACQ,YAAY,CAACI,KAAK,EAAE,CAACC,OAAO,EAAE,EAAE,UAAU,SAAKH,IAAI,EAAC;EAAA,EAChF;AACL"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _Object$defineProperty = require("@babel/runtime-corejs2/core-js/object/define-property");
|
|
4
|
+
var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");
|
|
5
|
+
_Object$defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.intercept = intercept;
|
|
9
|
+
exports.prepareFetchOptions = prepareFetchOptions;
|
|
10
|
+
var _regenerator = _interopRequireDefault(require("@babel/runtime-corejs2/regenerator"));
|
|
11
|
+
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/asyncToGenerator"));
|
|
12
|
+
var _promise = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/promise"));
|
|
13
|
+
var _stringify = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/json/stringify"));
|
|
14
|
+
var _deleteProperty = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/reflect/delete-property"));
|
|
15
|
+
var _events = require("events");
|
|
16
|
+
/* eslint-disable import/prefer-default-export */
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @param {Object} options
|
|
20
|
+
* @param {Array<Object>} interceptors
|
|
21
|
+
* @param {string} key
|
|
22
|
+
* @param {Object | undefined} res
|
|
23
|
+
* @private
|
|
24
|
+
* @returns {Promise}
|
|
25
|
+
*/
|
|
26
|
+
function intercept(options, interceptors, key) {
|
|
27
|
+
var res = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : undefined;
|
|
28
|
+
var successKey = "on".concat(key);
|
|
29
|
+
var errorKey = "on".concat(key, "Error");
|
|
30
|
+
return interceptors.reduce(function (promise, interceptor) {
|
|
31
|
+
return promise.then(function (result) {
|
|
32
|
+
interceptor.logOptions(options);
|
|
33
|
+
if (interceptor[successKey]) {
|
|
34
|
+
return interceptor[successKey](options, result);
|
|
35
|
+
}
|
|
36
|
+
return _promise.default.resolve(result);
|
|
37
|
+
}, function (reason) {
|
|
38
|
+
interceptor.logOptions(options);
|
|
39
|
+
if (interceptor[errorKey]) {
|
|
40
|
+
return interceptor[errorKey](options, reason);
|
|
41
|
+
}
|
|
42
|
+
return _promise.default.reject(reason);
|
|
43
|
+
});
|
|
44
|
+
}, _promise.default.resolve(res));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Prepare options for a fetch.
|
|
49
|
+
* @param {object} options
|
|
50
|
+
* @returns {Promise}
|
|
51
|
+
*/
|
|
52
|
+
function prepareFetchOptions(_x) {
|
|
53
|
+
return _prepareFetchOptions.apply(this, arguments);
|
|
54
|
+
}
|
|
55
|
+
function _prepareFetchOptions() {
|
|
56
|
+
_prepareFetchOptions = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(options) {
|
|
57
|
+
return _regenerator.default.wrap(function _callee$(_context) {
|
|
58
|
+
while (1) switch (_context.prev = _context.next) {
|
|
59
|
+
case 0:
|
|
60
|
+
if (options.url) {
|
|
61
|
+
options.uri = options.url;
|
|
62
|
+
options.url = null;
|
|
63
|
+
}
|
|
64
|
+
options.headers = options.headers || {};
|
|
65
|
+
if (options.json) {
|
|
66
|
+
// don't override existing accept header declared by user
|
|
67
|
+
options.headers.accept = options.headers.accept || options.headers.Accept || 'application/json';
|
|
68
|
+
|
|
69
|
+
// don't override existing content-type header declared by user
|
|
70
|
+
if (options.method !== 'GET' && options.method !== 'HEAD') {
|
|
71
|
+
options.headers['content-type'] = options.headers['content-type'] || options.headers['Content-Type'] || 'application/json';
|
|
72
|
+
options.body = (0, _stringify.default)(options.json === true ? options.body : options.json);
|
|
73
|
+
}
|
|
74
|
+
} else if (options.json !== undefined) {
|
|
75
|
+
(0, _deleteProperty.default)(options, 'json');
|
|
76
|
+
}
|
|
77
|
+
options.download = new _events.EventEmitter();
|
|
78
|
+
options.upload = new _events.EventEmitter();
|
|
79
|
+
options.keepalive = true;
|
|
80
|
+
return _context.abrupt("return", intercept(options, options.interceptors, 'Request').then(function () {
|
|
81
|
+
return options;
|
|
82
|
+
}));
|
|
83
|
+
case 7:
|
|
84
|
+
case "end":
|
|
85
|
+
return _context.stop();
|
|
86
|
+
}
|
|
87
|
+
}, _callee);
|
|
88
|
+
}));
|
|
89
|
+
return _prepareFetchOptions.apply(this, arguments);
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["intercept","options","interceptors","key","res","undefined","successKey","errorKey","reduce","promise","interceptor","then","result","logOptions","resolve","reason","reject","prepareFetchOptions","url","uri","headers","json","accept","Accept","method","body","download","EventEmitter","upload","keepalive"],"sources":["utils.ts"],"sourcesContent":["/* eslint-disable import/prefer-default-export */\nimport {EventEmitter} from 'events';\nimport Interceptor from '../lib/interceptor';\n\n/**\n * @param {Object} options\n * @param {Array<Object>} interceptors\n * @param {string} key\n * @param {Object | undefined} res\n * @private\n * @returns {Promise}\n */\nexport function intercept(\n options: object,\n interceptors: Array<Interceptor>,\n key: string,\n res: object = undefined\n): Promise<any> {\n const successKey = `on${key}`;\n const errorKey = `on${key}Error`;\n\n return interceptors.reduce(\n (promise, interceptor) =>\n promise.then(\n (result) => {\n interceptor.logOptions(options);\n if (interceptor[successKey]) {\n return interceptor[successKey](options, result);\n }\n\n return Promise.resolve(result);\n },\n (reason) => {\n interceptor.logOptions(options);\n if (interceptor[errorKey]) {\n return interceptor[errorKey](options, reason);\n }\n\n return Promise.reject(reason);\n }\n ),\n Promise.resolve(res)\n );\n}\n\n/**\n * Prepare options for a fetch.\n * @param {object} options\n * @returns {Promise}\n */\nexport async function prepareFetchOptions(options: any): Promise<any> {\n if (options.url) {\n options.uri = options.url;\n options.url = null;\n }\n\n options.headers = options.headers || {};\n\n if (options.json) {\n // don't override existing accept header declared by user\n options.headers.accept = options.headers.accept || options.headers.Accept || 'application/json';\n\n // don't override existing content-type header declared by user\n if (options.method !== 'GET' && options.method !== 'HEAD') {\n options.headers['content-type'] =\n options.headers['content-type'] || options.headers['Content-Type'] || 'application/json';\n options.body = JSON.stringify(options.json === true ? options.body : options.json);\n }\n } else if (options.json !== undefined) {\n Reflect.deleteProperty(options, 'json');\n }\n\n options.download = new EventEmitter();\n options.upload = new EventEmitter();\n options.keepalive = true;\n\n return intercept(options, options.interceptors, 'Request').then(() => options);\n}\n"],"mappings":";;;;;;;;;;;;;;AACA;AADA;;AAIA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,SAAS,CACvBC,OAAe,EACfC,YAAgC,EAChCC,GAAW,EAEG;EAAA,IADdC,GAAW,uEAAGC,SAAS;EAEvB,IAAMC,UAAU,eAAQH,GAAG,CAAE;EAC7B,IAAMI,QAAQ,eAAQJ,GAAG,UAAO;EAEhC,OAAOD,YAAY,CAACM,MAAM,CACxB,UAACC,OAAO,EAAEC,WAAW;IAAA,OACnBD,OAAO,CAACE,IAAI,CACV,UAACC,MAAM,EAAK;MACVF,WAAW,CAACG,UAAU,CAACZ,OAAO,CAAC;MAC/B,IAAIS,WAAW,CAACJ,UAAU,CAAC,EAAE;QAC3B,OAAOI,WAAW,CAACJ,UAAU,CAAC,CAACL,OAAO,EAAEW,MAAM,CAAC;MACjD;MAEA,OAAO,iBAAQE,OAAO,CAACF,MAAM,CAAC;IAChC,CAAC,EACD,UAACG,MAAM,EAAK;MACVL,WAAW,CAACG,UAAU,CAACZ,OAAO,CAAC;MAC/B,IAAIS,WAAW,CAACH,QAAQ,CAAC,EAAE;QACzB,OAAOG,WAAW,CAACH,QAAQ,CAAC,CAACN,OAAO,EAAEc,MAAM,CAAC;MAC/C;MAEA,OAAO,iBAAQC,MAAM,CAACD,MAAM,CAAC;IAC/B,CAAC,CACF;EAAA,GACH,iBAAQD,OAAO,CAACV,GAAG,CAAC,CACrB;AACH;;AAEA;AACA;AACA;AACA;AACA;AAJA,SAKsBa,mBAAmB;EAAA;AAAA;AAAA;EAAA,+FAAlC,iBAAmChB,OAAY;IAAA;MAAA;QAAA;UACpD,IAAIA,OAAO,CAACiB,GAAG,EAAE;YACfjB,OAAO,CAACkB,GAAG,GAAGlB,OAAO,CAACiB,GAAG;YACzBjB,OAAO,CAACiB,GAAG,GAAG,IAAI;UACpB;UAEAjB,OAAO,CAACmB,OAAO,GAAGnB,OAAO,CAACmB,OAAO,IAAI,CAAC,CAAC;UAEvC,IAAInB,OAAO,CAACoB,IAAI,EAAE;YAChB;YACApB,OAAO,CAACmB,OAAO,CAACE,MAAM,GAAGrB,OAAO,CAACmB,OAAO,CAACE,MAAM,IAAIrB,OAAO,CAACmB,OAAO,CAACG,MAAM,IAAI,kBAAkB;;YAE/F;YACA,IAAItB,OAAO,CAACuB,MAAM,KAAK,KAAK,IAAIvB,OAAO,CAACuB,MAAM,KAAK,MAAM,EAAE;cACzDvB,OAAO,CAACmB,OAAO,CAAC,cAAc,CAAC,GAC7BnB,OAAO,CAACmB,OAAO,CAAC,cAAc,CAAC,IAAInB,OAAO,CAACmB,OAAO,CAAC,cAAc,CAAC,IAAI,kBAAkB;cAC1FnB,OAAO,CAACwB,IAAI,GAAG,wBAAexB,OAAO,CAACoB,IAAI,KAAK,IAAI,GAAGpB,OAAO,CAACwB,IAAI,GAAGxB,OAAO,CAACoB,IAAI,CAAC;YACpF;UACF,CAAC,MAAM,IAAIpB,OAAO,CAACoB,IAAI,KAAKhB,SAAS,EAAE;YACrC,6BAAuBJ,OAAO,EAAE,MAAM,CAAC;UACzC;UAEAA,OAAO,CAACyB,QAAQ,GAAG,IAAIC,oBAAY,EAAE;UACrC1B,OAAO,CAAC2B,MAAM,GAAG,IAAID,oBAAY,EAAE;UACnC1B,OAAO,CAAC4B,SAAS,GAAG,IAAI;UAAC,iCAElB7B,SAAS,CAACC,OAAO,EAAEA,OAAO,CAACC,YAAY,EAAE,SAAS,CAAC,CAACS,IAAI,CAAC;YAAA,OAAMV,OAAO;UAAA,EAAC;QAAA;QAAA;UAAA;MAAA;IAAA;EAAA,CAC/E;EAAA;AAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webex/http-core",
|
|
3
|
-
"version": "3.0.0-beta.
|
|
3
|
+
"version": "3.0.0-beta.200",
|
|
4
4
|
"description": "Core HTTP library for the Cisco Webex",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -24,18 +24,18 @@
|
|
|
24
24
|
]
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@webex/test-helper-chai": "3.0.0-beta.
|
|
28
|
-
"@webex/test-helper-file": "3.0.0-beta.
|
|
29
|
-
"@webex/test-helper-make-local-url": "3.0.0-beta.
|
|
30
|
-
"@webex/test-helper-mocha": "3.0.0-beta.
|
|
27
|
+
"@webex/test-helper-chai": "3.0.0-beta.200",
|
|
28
|
+
"@webex/test-helper-file": "3.0.0-beta.200",
|
|
29
|
+
"@webex/test-helper-make-local-url": "3.0.0-beta.200",
|
|
30
|
+
"@webex/test-helper-mocha": "3.0.0-beta.200",
|
|
31
31
|
"sinon": "^9.2.4"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@webex/common": "3.0.0-beta.
|
|
35
|
-
"@webex/http-core": "3.0.0-beta.
|
|
36
|
-
"@webex/internal-plugin-device": "3.0.0-beta.
|
|
37
|
-
"@webex/test-helper-test-users": "3.0.0-beta.
|
|
38
|
-
"@webex/webex-core": "3.0.0-beta.
|
|
34
|
+
"@webex/common": "3.0.0-beta.200",
|
|
35
|
+
"@webex/http-core": "3.0.0-beta.200",
|
|
36
|
+
"@webex/internal-plugin-device": "3.0.0-beta.200",
|
|
37
|
+
"@webex/test-helper-test-users": "3.0.0-beta.200",
|
|
38
|
+
"@webex/webex-core": "3.0.0-beta.200",
|
|
39
39
|
"file-type": "^16.0.1",
|
|
40
40
|
"global": "^4.4.0",
|
|
41
41
|
"is-function": "^1.0.1",
|
package/src/index.js
CHANGED
|
@@ -6,6 +6,7 @@ import {assign, curry, defaults as lodashDefaults, isString} from 'lodash';
|
|
|
6
6
|
|
|
7
7
|
import HttpStatusInterceptor from './interceptors/http-status';
|
|
8
8
|
import _request from './request';
|
|
9
|
+
import {prepareFetchOptions as _prepareFetchOptions} from './request/utils';
|
|
9
10
|
|
|
10
11
|
// Curry protorequest so we generate a function with default options built in.
|
|
11
12
|
const protorequest = curry(function protorequest(defaultOptions, options) {
|
|
@@ -41,6 +42,60 @@ const protorequest = curry(function protorequest(defaultOptions, options) {
|
|
|
41
42
|
return _request(options);
|
|
42
43
|
});
|
|
43
44
|
|
|
45
|
+
export const protoprepareFetchOptions = curry(function protoprepareFetchOptions(
|
|
46
|
+
defaultOptions,
|
|
47
|
+
options
|
|
48
|
+
) {
|
|
49
|
+
// Hide useless elements from logs
|
|
50
|
+
['download', 'interceptors', 'logger', 'upload'].forEach((prop) => {
|
|
51
|
+
let descriptor = Reflect.getOwnPropertyDescriptor(options, prop);
|
|
52
|
+
|
|
53
|
+
descriptor = assign({}, descriptor, {
|
|
54
|
+
enumerable: false,
|
|
55
|
+
writable: true,
|
|
56
|
+
});
|
|
57
|
+
Reflect.defineProperty(options, prop, descriptor);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
lodashDefaults(options, defaultOptions);
|
|
61
|
+
|
|
62
|
+
options.logger = options.logger || this.logger || console;
|
|
63
|
+
|
|
64
|
+
return _prepareFetchOptions(options);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Sets the $timings value(s) before the request/fetch.
|
|
69
|
+
* This function is only useful if you are about to send a request
|
|
70
|
+
* using prepared fetch options; normally it is done in webex.request();
|
|
71
|
+
*
|
|
72
|
+
* @param {any} options
|
|
73
|
+
* @returns {any} the updated options object
|
|
74
|
+
*/
|
|
75
|
+
const setRequestTimings = (options) => {
|
|
76
|
+
const now = new Date().getTime();
|
|
77
|
+
options.$timings = options.$timings || {};
|
|
78
|
+
options.$timings.requestStart = now;
|
|
79
|
+
options.$timings.networkStart = now;
|
|
80
|
+
|
|
81
|
+
return options;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Submits a metric from pre-built request options via the fetch API. Updates
|
|
86
|
+
* the "$timings" values to Date.now() since the existing times were set when
|
|
87
|
+
* the options were built (not submitted).
|
|
88
|
+
*
|
|
89
|
+
* @param {any} options - the pre-built request options for submitting a metric
|
|
90
|
+
* @returns {Promise} promise that resolves to a response object
|
|
91
|
+
*/
|
|
92
|
+
export const setTimingsAndFetch = (options) => {
|
|
93
|
+
// call the fetch API
|
|
94
|
+
const opts = setRequestTimings(options);
|
|
95
|
+
|
|
96
|
+
return fetch(opts.uri, opts);
|
|
97
|
+
};
|
|
98
|
+
|
|
44
99
|
const defaultOptions = {
|
|
45
100
|
json: true,
|
|
46
101
|
interceptors: [
|
package/src/request/index.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
import {EventEmitter} from 'events';
|
|
6
6
|
|
|
7
7
|
import _request from './request';
|
|
8
|
+
import {intercept} from './utils';
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* @param {Object} options
|
|
@@ -21,42 +22,9 @@ export default function request(options) {
|
|
|
21
22
|
options.download = new EventEmitter();
|
|
22
23
|
options.upload = new EventEmitter();
|
|
23
24
|
|
|
24
|
-
return intercept(options.interceptors, 'Request')
|
|
25
|
+
return intercept(options, options.interceptors, 'Request')
|
|
25
26
|
.then((...args) => _request(options, ...args))
|
|
26
|
-
.then((...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)
|
|
27
|
+
.then((...args) =>
|
|
28
|
+
intercept(options, options.interceptors.slice().reverse(), 'Response', ...args)
|
|
60
29
|
);
|
|
61
|
-
}
|
|
62
30
|
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/* eslint-disable import/prefer-default-export */
|
|
2
|
+
import {EventEmitter} from 'events';
|
|
3
|
+
import Interceptor from '../lib/interceptor';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @param {Object} options
|
|
7
|
+
* @param {Array<Object>} interceptors
|
|
8
|
+
* @param {string} key
|
|
9
|
+
* @param {Object | undefined} res
|
|
10
|
+
* @private
|
|
11
|
+
* @returns {Promise}
|
|
12
|
+
*/
|
|
13
|
+
export function intercept(
|
|
14
|
+
options: object,
|
|
15
|
+
interceptors: Array<Interceptor>,
|
|
16
|
+
key: string,
|
|
17
|
+
res: object = undefined
|
|
18
|
+
): Promise<any> {
|
|
19
|
+
const successKey = `on${key}`;
|
|
20
|
+
const errorKey = `on${key}Error`;
|
|
21
|
+
|
|
22
|
+
return interceptors.reduce(
|
|
23
|
+
(promise, interceptor) =>
|
|
24
|
+
promise.then(
|
|
25
|
+
(result) => {
|
|
26
|
+
interceptor.logOptions(options);
|
|
27
|
+
if (interceptor[successKey]) {
|
|
28
|
+
return interceptor[successKey](options, result);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return Promise.resolve(result);
|
|
32
|
+
},
|
|
33
|
+
(reason) => {
|
|
34
|
+
interceptor.logOptions(options);
|
|
35
|
+
if (interceptor[errorKey]) {
|
|
36
|
+
return interceptor[errorKey](options, reason);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return Promise.reject(reason);
|
|
40
|
+
}
|
|
41
|
+
),
|
|
42
|
+
Promise.resolve(res)
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Prepare options for a fetch.
|
|
48
|
+
* @param {object} options
|
|
49
|
+
* @returns {Promise}
|
|
50
|
+
*/
|
|
51
|
+
export async function prepareFetchOptions(options: any): Promise<any> {
|
|
52
|
+
if (options.url) {
|
|
53
|
+
options.uri = options.url;
|
|
54
|
+
options.url = null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
options.headers = options.headers || {};
|
|
58
|
+
|
|
59
|
+
if (options.json) {
|
|
60
|
+
// don't override existing accept header declared by user
|
|
61
|
+
options.headers.accept = options.headers.accept || options.headers.Accept || 'application/json';
|
|
62
|
+
|
|
63
|
+
// don't override existing content-type header declared by user
|
|
64
|
+
if (options.method !== 'GET' && options.method !== 'HEAD') {
|
|
65
|
+
options.headers['content-type'] =
|
|
66
|
+
options.headers['content-type'] || options.headers['Content-Type'] || 'application/json';
|
|
67
|
+
options.body = JSON.stringify(options.json === true ? options.body : options.json);
|
|
68
|
+
}
|
|
69
|
+
} else if (options.json !== undefined) {
|
|
70
|
+
Reflect.deleteProperty(options, 'json');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
options.download = new EventEmitter();
|
|
74
|
+
options.upload = new EventEmitter();
|
|
75
|
+
options.keepalive = true;
|
|
76
|
+
|
|
77
|
+
return intercept(options, options.interceptors, 'Request').then(() => options);
|
|
78
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {assert} from '@webex/test-helper-chai';
|
|
2
|
+
import sinon from 'sinon';
|
|
3
|
+
|
|
4
|
+
import * as utils from '@webex/http-core/src/request/utils';
|
|
5
|
+
import WebexTrackingIdInterceptor from '@webex/webex-core/src/interceptors/webex-tracking-id';
|
|
6
|
+
import UserAgentInterceptor from '@webex/webex-core/src/interceptors/webex-user-agent';
|
|
7
|
+
import {protoprepareFetchOptions, setTimingsAndFetch} from '@webex/http-core/src/index';
|
|
8
|
+
|
|
9
|
+
describe('http-core index tests', () => {
|
|
10
|
+
describe('#protoprepareFetchOptions()', () => {
|
|
11
|
+
it('uses default options and adds expected options', async () => {
|
|
12
|
+
const defaultOptions = {
|
|
13
|
+
interceptors: [WebexTrackingIdInterceptor.create(), UserAgentInterceptor.create()],
|
|
14
|
+
};
|
|
15
|
+
const options = {};
|
|
16
|
+
|
|
17
|
+
const prepareFetchOptions = protoprepareFetchOptions(defaultOptions);
|
|
18
|
+
|
|
19
|
+
await prepareFetchOptions(options);
|
|
20
|
+
|
|
21
|
+
assert.deepEqual(options, {
|
|
22
|
+
headers: {
|
|
23
|
+
trackingid: 'undefined_1',
|
|
24
|
+
'spark-user-agent': 'webex-js-sdk/development (node)',
|
|
25
|
+
},
|
|
26
|
+
keepalive: true,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
assert.equal(typeof options.logger, 'object');
|
|
30
|
+
assert.equal(typeof options.upload, 'object');
|
|
31
|
+
assert.equal(typeof options.download, 'object');
|
|
32
|
+
assert.isArray(options.interceptors);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
describe('#setTimingsAndFetch()', () => {
|
|
37
|
+
const now = Date.now();
|
|
38
|
+
let stubbedFetch;
|
|
39
|
+
|
|
40
|
+
beforeEach(() => {
|
|
41
|
+
global.fetch = sinon.stub();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('calls fetch with expected options', async () => {
|
|
45
|
+
sinon.useFakeTimers(now);
|
|
46
|
+
const options = {uri: 'foo'};
|
|
47
|
+
|
|
48
|
+
const newOptions = setTimingsAndFetch(options);
|
|
49
|
+
|
|
50
|
+
sinon.assert.calledOnce(global.fetch);
|
|
51
|
+
sinon.assert.calledWith(global.fetch, 'foo', {
|
|
52
|
+
uri: 'foo',
|
|
53
|
+
$timings: {requestStart: now, networkStart: now},
|
|
54
|
+
});
|
|
55
|
+
sinon.restore();
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import {assert} from '@webex/test-helper-chai';
|
|
2
|
+
import sinon from 'sinon';
|
|
3
|
+
|
|
4
|
+
import * as utils from '@webex/http-core/src/request/utils';
|
|
5
|
+
import WebexTrackingIdInterceptor from '@webex/webex-core/src/interceptors/webex-tracking-id';
|
|
6
|
+
import UserAgentInterceptor from '@webex/webex-core/src/interceptors/webex-user-agent';
|
|
7
|
+
|
|
8
|
+
describe('Request utils', () => {
|
|
9
|
+
describe('#intercept()', () => {
|
|
10
|
+
it('updates options from interceptors', async () => {
|
|
11
|
+
const options = {};
|
|
12
|
+
const interceptors = [WebexTrackingIdInterceptor.create(), UserAgentInterceptor.create()];
|
|
13
|
+
|
|
14
|
+
return utils.intercept(options, interceptors, 'Request').then(() => {
|
|
15
|
+
assert.equal(Object.keys(options.headers).length, 2);
|
|
16
|
+
assert.equal(options.headers.trackingid, 'undefined_1');
|
|
17
|
+
assert.equal(options.headers['spark-user-agent'], 'webex-js-sdk/development (node)');
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
describe('#prepareFetchOptions()', () => {
|
|
23
|
+
it('updates options as expected', async () => {
|
|
24
|
+
const options = {
|
|
25
|
+
json: true,
|
|
26
|
+
body: {foo: 'bar'},
|
|
27
|
+
headers: {},
|
|
28
|
+
interceptors: [WebexTrackingIdInterceptor.create(), UserAgentInterceptor.create()],
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
return utils.prepareFetchOptions(options).then(() => {
|
|
32
|
+
assert.deepEqual(options.headers, {
|
|
33
|
+
accept: 'application/json',
|
|
34
|
+
'content-type': 'application/json',
|
|
35
|
+
trackingid: 'undefined_1',
|
|
36
|
+
'spark-user-agent': 'webex-js-sdk/development (node)',
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
assert.equal(options.body, '{"foo":"bar"}');
|
|
40
|
+
|
|
41
|
+
assert.equal(options.download != undefined, true);
|
|
42
|
+
assert.equal(options.upload != undefined, true);
|
|
43
|
+
assert.equal(options.keepalive, true);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('updates options as expected when accept and content-type exist', async () => {
|
|
48
|
+
const options = {
|
|
49
|
+
json: true,
|
|
50
|
+
body: {foo: 'bar'},
|
|
51
|
+
headers: {accept: 'foo', 'content-type': 'bar'},
|
|
52
|
+
interceptors: [WebexTrackingIdInterceptor.create(), UserAgentInterceptor.create()],
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
return utils.prepareFetchOptions(options).then(() => {
|
|
56
|
+
assert.deepEqual(options.headers, {
|
|
57
|
+
accept: 'foo',
|
|
58
|
+
'content-type': 'bar',
|
|
59
|
+
trackingid: 'undefined_1',
|
|
60
|
+
'spark-user-agent': 'webex-js-sdk/development (node)',
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('updates body as expected when json = some object', async () => {
|
|
66
|
+
const options = {
|
|
67
|
+
json: {bar: 'baz'},
|
|
68
|
+
headers: {accept: 'foo', 'content-type': 'bar'},
|
|
69
|
+
interceptors: [WebexTrackingIdInterceptor.create(), UserAgentInterceptor.create()],
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
return utils.prepareFetchOptions(options).then(() => {
|
|
73
|
+
assert.equal(options.body, '{"bar":"baz"}');
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
});
|