Skip to content

Commit 1695414

Browse files
authored
Merge pull request #4293 from Countly/SER-727-Replace-deprecated-option-rejectUnauthorized-in-countly-request
[SER-727] rejectUnauthorized option is fixed
2 parents 980e69b + b84786b commit 1695414

File tree

4 files changed

+792
-8
lines changed

4 files changed

+792
-8
lines changed

api/utils/countly-request/index.js

100644100755
+37-7
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,55 @@ var convertOptionsToGot = function(options) {
4242

4343
var requestOptions = {};
4444

45-
//define for got and request differences
45+
// Define for got and request differences
4646
var keyMap = {
4747
"qs": "searchParams",
48-
"strictSSL": "rejectUnauthorized",
48+
"strictSSL": "https.rejectUnauthorized",
4949
"gzip": "decompress",
5050
"jar": "cookieJar",
5151
"baseUrl": "prefixUrl",
5252
"uri": "url"
5353
};
5454

55+
/***
56+
* Assigns a value to a nested object property
57+
* @param {object} obj - The object to assign the value to
58+
* @param {string} keyPath - The path to the property to assign the value to
59+
* @param {*} value - The value to assign
60+
*/
61+
function assignDeep(obj, keyPath, value) {
62+
var keys = keyPath.split('.');
63+
var lastKey = keys.pop();
64+
var nestedObj = obj;
65+
66+
keys.forEach(function(key) {
67+
if (!nestedObj[key] || typeof nestedObj[key] !== 'object') {
68+
nestedObj[key] = {};
69+
}
70+
nestedObj = nestedObj[key];
71+
});
72+
73+
nestedObj[lastKey] = value;
74+
}
75+
5576
for (let key in options) {
5677
if (!Object.prototype.hasOwnProperty.call(requestOptions, key) && keyMap[key]) {
57-
requestOptions[keyMap[key]] = options[key];
78+
var mappedKey = keyMap[key];
79+
if (mappedKey.includes('.')) {
80+
assignDeep(requestOptions, mappedKey, options[key]);
81+
}
82+
else {
83+
requestOptions[mappedKey] = options[key];
84+
}
5885
}
5986
else {
6087
requestOptions[key] = options[key];
6188
}
6289
}
6390

64-
//backward compatability. in got json is not boolean. it is the object.
65-
//request body and json are mutally exclusive. if request.json and body exists one of them must be deleted
91+
// Backward compatibility: in got, json is not a boolean, it is an object.
92+
// Request body and json are mutually exclusive.
93+
// If request.json and body exist, one of them must be deleted.
6694
if (requestOptions.json && typeof requestOptions.json === 'boolean' && requestOptions.body) {
6795
requestOptions.json = requestOptions.body;
6896
delete requestOptions.json;
@@ -71,12 +99,12 @@ var convertOptionsToGot = function(options) {
7199
if (requestOptions.prefixUrl && options.uri && requestOptions.url) {
72100
requestOptions.uri = options.uri;
73101
delete requestOptions.url;
74-
75102
}
76103

77104
return requestOptions;
78105
};
79106

107+
80108
module.exports = function(uri, options, callback) {
81109

82110
if (typeof uri === 'undefined') {
@@ -144,4 +172,6 @@ module.exports.post = function(uri, options, callback) {
144172
//Add a get method to the request object
145173
module.exports.get = function(uri, options, callback) {
146174
module.exports(uri, options, callback);
147-
};
175+
};
176+
177+
module.exports.convertOptionsToGot = convertOptionsToGot;

0 commit comments

Comments
 (0)