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' ;
44
5- var encodeBody = function ( options = { } ) {
6- let body = options . body ;
7- let headers = options . headers || { } ;
5+ var encodeBody = function ( { body, headers = { } } ) {
86 if ( typeof body !== 'object' ) {
9- return options ;
7+ return { body , headers } ;
108 }
119 var contentTypeKeys = Object . keys ( headers ) . filter ( ( key ) => {
1210 return key . match ( / c o n t e n t - t y p e / i) != null ;
@@ -15,23 +13,27 @@ var encodeBody = function(options = {}) {
1513 if ( contentTypeKeys . length == 0 ) {
1614 // no content type
1715 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' ;
2118 } catch ( e ) {
2219 // do nothing;
2320 }
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
2527 var contentType = contentTypeKeys [ 0 ] ;
2628 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 ) ;
2830 } 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 ) {
3032 return `${ key } =${ encodeURIComponent ( body [ key ] ) } `
3133 } ) . join ( "&" ) ;
3234 }
3335 }
34- return options ;
36+ return { body , headers } ;
3537}
3638
3739module . exports = function ( options ) {
@@ -43,7 +45,7 @@ module.exports = function(options) {
4345 delete options . success ;
4446 delete options . error ;
4547 delete options . uri ; // not supported
46- options = encodeBody ( options ) ;
48+ options = Object . assign ( options , encodeBody ( options ) ) ;
4749 // set follow redirects to false by default
4850 options . followRedirect = options . followRedirects == true ;
4951 // force the response as a buffer
0 commit comments