@@ -42,41 +42,62 @@ 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
+ // Helper function to assign value to nested keys
56
+ function assignDeep ( obj , keyPath , value ) {
57
+ var keys = keyPath . split ( '.' ) ;
58
+ var lastKey = keys . pop ( ) ;
59
+ var nestedObj = obj ;
60
+
61
+ keys . forEach ( function ( key ) {
62
+ if ( ! nestedObj [ key ] || typeof nestedObj [ key ] !== 'object' ) {
63
+ nestedObj [ key ] = { } ;
64
+ }
65
+ nestedObj = nestedObj [ key ] ;
66
+ } ) ;
67
+
68
+ nestedObj [ lastKey ] = value ;
69
+ }
70
+
55
71
for ( let key in options ) {
56
72
if ( ! Object . prototype . hasOwnProperty . call ( requestOptions , key ) && keyMap [ key ] ) {
57
- requestOptions [ keyMap [ key ] ] = options [ key ] ;
58
- }
59
- else {
73
+ var mappedKey = keyMap [ key ] ;
74
+ if ( mappedKey . includes ( '.' ) ) {
75
+ assignDeep ( requestOptions , mappedKey , options [ key ] ) ;
76
+ } else {
77
+ requestOptions [ mappedKey ] = options [ key ] ;
78
+ }
79
+ } else {
60
80
requestOptions [ key ] = options [ key ] ;
61
81
}
62
82
}
63
83
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
84
+ // Backward compatibility: in got, json is not a boolean, it is an object.
85
+ // Request body and json are mutually exclusive.
86
+ // If request.json and body exist, one of them must be deleted.
66
87
if ( requestOptions . json && typeof requestOptions . json === 'boolean' && requestOptions . body ) {
67
88
requestOptions . json = requestOptions . body ;
68
- delete requestOptions . json ;
89
+ delete requestOptions . body ;
69
90
}
70
91
71
92
if ( requestOptions . prefixUrl && options . uri && requestOptions . url ) {
72
93
requestOptions . uri = options . uri ;
73
94
delete requestOptions . url ;
74
-
75
95
}
76
96
77
97
return requestOptions ;
78
98
} ;
79
99
100
+
80
101
module . exports = function ( uri , options , callback ) {
81
102
82
103
if ( typeof uri === 'undefined' ) {
@@ -144,4 +165,6 @@ module.exports.post = function(uri, options, callback) {
144
165
//Add a get method to the request object
145
166
module . exports . get = function ( uri , options , callback ) {
146
167
module . exports ( uri , options , callback ) ;
147
- } ;
168
+ } ;
169
+
170
+ module . exports . convertOptionsToGot = convertOptionsToGot ;
0 commit comments