@@ -176,10 +176,10 @@ export class Api {
176176 * @returns {Promise } A promise which will resolved with the ID and any other specified fields of newly created object
177177 */
178178 create ( objCode : string , params : any , fields ?: TFields ) {
179- if ( params . hasOwnProperty ( 'updates' ) && typeof params . updates === 'string' ) {
180- return this . request ( objCode , params . updates , fields , Api . Methods . POST , true )
179+ if ( params . hasOwnProperty ( 'updates' ) && ! ( params . updates instanceof Array ) ) {
180+ return this . request ( objCode , params , fields , Api . Methods . POST )
181181 }
182- return this . request ( objCode , params , fields , Api . Methods . POST , true )
182+ return this . request ( objCode , { updates : params } , fields , Api . Methods . POST )
183183 }
184184
185185 /**
@@ -192,10 +192,10 @@ export class Api {
192192 * @return {Promise } A promise which will resolved with results if everything went ok and rejected otherwise
193193 */
194194 edit ( objCode : string , objID : string , updates : any , fields ?: TFields ) {
195- if ( updates . hasOwnProperty ( 'updates' ) && typeof updates . updates === 'string' ) {
196- return this . request ( objCode + '/' + objID , updates . updates , fields , Api . Methods . PUT , true )
195+ if ( updates . hasOwnProperty ( 'updates' ) && ! ( updates . updates instanceof Array ) ) {
196+ return this . request ( objCode + '/' + objID , updates , fields , Api . Methods . PUT )
197197 }
198- return this . request ( objCode + '/' + objID , updates , fields , Api . Methods . PUT , false )
198+ return this . request ( objCode + '/' + objID , { updates : updates } , fields , Api . Methods . PUT )
199199 }
200200
201201 /**
@@ -209,15 +209,12 @@ export class Api {
209209 */
210210 execute ( objCode : string , objID : string | null , action : string , actionArgs ?: object ) {
211211 let endPoint = objCode
212- let params = { }
212+ let params : any = { method : Api . Methods . PUT }
213213 if ( objID ) {
214214 endPoint += '/' + objID + '/' + action
215215 }
216216 else {
217- params = {
218- method : Api . Methods . PUT ,
219- action : action
220- }
217+ params . action = action
221218 }
222219 if ( actionArgs ) {
223220 params = objectAssign ( params , actionArgs )
@@ -351,17 +348,16 @@ export class Api {
351348 * @param {Object } params An object with params
352349 * @param {Object } [fields] Fields to query for the request
353350 * @param {String } [method=GET] The method which the request will do (GET|POST|PUT|DELETE)
354- * @param {String } [sendJSONBody=false] Whether the params payload is sent as JSON.stringify in the request body
355351 * @return {Promise } A promise which will resolved with results if everything went ok and rejected otherwise
356352 */
357- request ( path : string , params : THttpParams , fields ?: TFields , method : string = Api . Methods . GET , sendJSONBody = false ) : Promise < any > {
358- params = objectAssign ( params || { } , this . _httpParams )
353+ request ( path : string , params : THttpParams , fields ?: TFields , method : string = Api . Methods . GET ) : Promise < any > {
354+ const clonedParams = objectAssign ( { } , params || { } , this . _httpParams )
359355
360356 const alwaysUseGet = this . _httpOptions . alwaysUseGet
361357
362358 const options = objectAssign ( { } , this . _httpOptions )
363- if ( alwaysUseGet ) {
364- params . method = method
359+ if ( alwaysUseGet && path !== 'login' ) {
360+ clonedParams . method = method
365361 options . method = Api . Methods . GET
366362 } else {
367363 options . method = method
@@ -379,7 +375,7 @@ export class Api {
379375 fields = [ fields ]
380376 }
381377 if ( fields . length !== 0 ) {
382- params . fields = fields . join ( )
378+ clonedParams . fields = fields . join ( )
383379 }
384380
385381 const headers = new Headers ( )
@@ -392,23 +388,33 @@ export class Api {
392388 }
393389
394390 let bodyParams = null , queryString = ''
395- if ( NodeFormData && params instanceof NodeFormData ) {
396- bodyParams = params
391+ if ( NodeFormData && clonedParams instanceof NodeFormData ) {
392+ bodyParams = clonedParams
397393 }
398- else if ( GlobalScope . FormData && params instanceof GlobalScope . FormData ) {
399- bodyParams = params
394+ else if ( GlobalScope . FormData && clonedParams instanceof GlobalScope . FormData ) {
395+ bodyParams = clonedParams
400396 }
401397 else {
402- if ( sendJSONBody && ( options . method === Api . Methods . POST || options . method === Api . Methods . PUT ) ) {
398+ if ( clonedParams . hasOwnProperty ( 'updates' ) && ( options . method === Api . Methods . POST || options . method === Api . Methods . PUT ) ) {
403399 headers . append ( 'Content-Type' , 'application/json' )
404- bodyParams = JSON . stringify ( params )
400+ if ( typeof clonedParams . updates === 'string' ) {
401+ bodyParams = clonedParams . updates
402+ } else {
403+ bodyParams = JSON . stringify ( clonedParams . updates )
404+ }
405+
406+ delete clonedParams . updates
407+ const qs = queryStringify ( clonedParams )
408+ if ( qs ) {
409+ queryString = '?' + qs
410+ }
405411 } else {
406412 headers . append ( 'Content-Type' , 'application/x-www-form-urlencoded' )
407- bodyParams = Object . keys ( params ) . reduce ( function ( a , k ) {
408- a . push ( k + '=' + encodeURIComponent ( params [ k ] ) )
409- return a
410- } , [ ] ) . join ( '&' )
411- if ( options . method === Api . Methods . GET ) {
413+ if ( clonedParams . hasOwnProperty ( 'updates' ) && typeof clonedParams . updates !== 'string' ) {
414+ clonedParams . updates = JSON . stringify ( clonedParams . updates )
415+ }
416+ bodyParams = queryStringify ( clonedParams )
417+ if ( options . method === Api . Methods . GET || options . method === Api . Methods . DELETE ) {
412418 if ( bodyParams ) {
413419 queryString = '?' + bodyParams
414420 }
@@ -487,6 +493,13 @@ export class Api {
487493 }
488494}
489495
496+ const queryStringify = function ( params ) {
497+ return Object . keys ( params ) . reduce ( function ( a , k ) {
498+ a . push ( k + '=' + encodeURIComponent ( params [ k ] ) )
499+ return a
500+ } , [ ] ) . join ( '&' )
501+ }
502+
490503const ResponseHandler = {
491504 success : ( response ) => {
492505 if ( response . ok ) {
0 commit comments