1
- var request = require ( " request" ) ,
2
- Parse = require ( 'parse/node' ) . Parse ,
3
- HTTPResponse = require ( './HTTPResponse' ) . HTTPResponse ;
1
+ import request from ' request' ;
2
+ import Parse from 'parse/node' ;
3
+ import HTTPResponse from './HTTPResponse' ;
4
4
5
- var encodeBody = function ( options = { } ) {
6
- let body = options . body ;
7
- let headers = options . headers || { } ;
5
+ var encodeBody = function ( { body, headers = { } } ) {
8
6
if ( typeof body !== 'object' ) {
9
- return options ;
7
+ return { body , headers } ;
10
8
}
11
9
var contentTypeKeys = Object . keys ( headers ) . filter ( ( key ) => {
12
10
return key . match ( / c o n t e n t - t y p e / i) != null ;
@@ -15,23 +13,27 @@ var encodeBody = function(options = {}) {
15
13
if ( contentTypeKeys . length == 0 ) {
16
14
// no content type
17
15
try {
18
- options . body = JSON . stringify ( body ) ;
19
- options . headers = options . headers || { } ;
20
- options . headers [ 'Content-Type' ] = 'application/json' ;
16
+ body = JSON . stringify ( body ) ;
17
+ headers [ 'Content-Type' ] = 'application/json' ;
21
18
} catch ( e ) {
22
19
// do nothing;
23
20
}
24
- } else if ( contentTypeKeys . length == 1 ) {
21
+ } else {
22
+ /* istanbul ignore next */
23
+ if ( contentTypeKeys . length > 1 ) {
24
+ console . error ( 'multiple content-type headers are set.' ) ;
25
+ }
26
+ // There maybe many, we'll just take the 1st one
25
27
var contentType = contentTypeKeys [ 0 ] ;
26
28
if ( headers [ contentType ] . match ( / a p p l i c a t i o n \/ j s o n / i) ) {
27
- options . body = JSON . stringify ( body ) ;
29
+ body = JSON . stringify ( body ) ;
28
30
} else if ( headers [ contentType ] . match ( / a p p l i c a t i o n \/ x - w w w - f o r m - u r l e n c o d e d / i) ) {
29
- options . body = Object . keys ( body ) . map ( function ( key ) {
31
+ body = Object . keys ( body ) . map ( function ( key ) {
30
32
return `${ key } =${ encodeURIComponent ( body [ key ] ) } `
31
33
} ) . join ( "&" ) ;
32
34
}
33
35
}
34
- return options ;
36
+ return { body , headers } ;
35
37
}
36
38
37
39
module . exports = function ( options ) {
@@ -43,7 +45,7 @@ module.exports = function(options) {
43
45
delete options . success ;
44
46
delete options . error ;
45
47
delete options . uri ; // not supported
46
- options = encodeBody ( options ) ;
48
+ options = Object . assign ( options , encodeBody ( options ) ) ;
47
49
// set follow redirects to false by default
48
50
options . followRedirect = options . followRedirects == true ;
49
51
// force the response as a buffer
0 commit comments