@@ -42,27 +42,55 @@ var convertOptionsToGot = function(options) {
42
42
43
43
var requestOptions = { } ;
44
44
45
- //define for got and request differences
45
+ // Define for got and request differences
46
46
var keyMap = {
47
47
"qs" : "searchParams" ,
48
- "strictSSL" : "rejectUnauthorized" ,
48
+ "strictSSL" : "https. rejectUnauthorized" ,
49
49
"gzip" : "decompress" ,
50
50
"jar" : "cookieJar" ,
51
51
"baseUrl" : "prefixUrl" ,
52
52
"uri" : "url"
53
53
} ;
54
54
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
+
55
76
for ( let key in options ) {
56
77
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
+ }
58
85
}
59
86
else {
60
87
requestOptions [ key ] = options [ key ] ;
61
88
}
62
89
}
63
90
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.
66
94
if ( requestOptions . json && typeof requestOptions . json === 'boolean' && requestOptions . body ) {
67
95
requestOptions . json = requestOptions . body ;
68
96
delete requestOptions . json ;
@@ -71,12 +99,12 @@ var convertOptionsToGot = function(options) {
71
99
if ( requestOptions . prefixUrl && options . uri && requestOptions . url ) {
72
100
requestOptions . uri = options . uri ;
73
101
delete requestOptions . url ;
74
-
75
102
}
76
103
77
104
return requestOptions ;
78
105
} ;
79
106
107
+
80
108
module . exports = function ( uri , options , callback ) {
81
109
82
110
if ( typeof uri === 'undefined' ) {
@@ -144,4 +172,6 @@ module.exports.post = function(uri, options, callback) {
144
172
//Add a get method to the request object
145
173
module . exports . get = function ( uri , options , callback ) {
146
174
module . exports ( uri , options , callback ) ;
147
- } ;
175
+ } ;
176
+
177
+ module . exports . convertOptionsToGot = convertOptionsToGot ;
0 commit comments