@@ -56,8 +56,10 @@ export class Api {
5656 POST : 'POST'
5757 }
5858 _httpOptions : IHttpOptions
59+ serverAcceptsJSON : boolean
5960
6061 constructor ( config ) {
62+ this . serverAcceptsJSON = true
6163 this . _httpOptions = {
6264 url : config . url ,
6365 alwaysUseGet : config . alwaysUseGet ,
@@ -68,13 +70,16 @@ export class Api {
6870 }
6971 // Append version to path if provided
7072 let path
71- if ( [ 'internal' , 'unsupported' , 'asp' ] . indexOf ( config . version ) >= 0 ) {
72- path = '/attask/api-' + config . version
73+ const { version = 'internal' } : {
74+ version : string
75+ } = config
76+ if ( [ 'internal' , 'unsupported' , 'asp' ] . indexOf ( version ) >= 0 ) {
77+ path = '/attask/api-' + version
7378 }
7479 else {
75- path = '/attask/api'
76- if ( config . version ) {
77- path = path + '/v' + config . version
80+ path = '/attask/api/v' + version
81+ if ( version === '2.0' || version === '3.0' || version === '4.0' ) {
82+ this . serverAcceptsJSON = false
7883 }
7984 }
8085 this . _httpOptions . path = path
@@ -173,7 +178,7 @@ export class Api {
173178 * @returns {Promise } A promise which will resolved with the ID and any other specified fields of newly created object
174179 */
175180 create ( objCode : string , params : any , fields ?: TFields ) {
176- if ( params . hasOwnProperty ( 'updates' ) && ! ( params . updates instanceof Array ) ) {
181+ if ( params . hasOwnProperty ( 'updates' ) ) {
177182 return this . request ( objCode , params , fields , Api . Methods . POST )
178183 }
179184 return this . request ( objCode , { updates : params } , fields , Api . Methods . POST )
@@ -189,7 +194,7 @@ export class Api {
189194 * @return {Promise } A promise which will resolved with results if everything went ok and rejected otherwise
190195 */
191196 edit ( objCode : string , objID : string , updates : any , fields ?: TFields ) {
192- if ( updates . hasOwnProperty ( 'updates' ) && ! ( updates . updates instanceof Array ) ) {
197+ if ( updates . hasOwnProperty ( 'updates' ) ) {
193198 return this . request ( objCode + '/' + objID , updates , fields , Api . Methods . PUT )
194199 }
195200 return this . request ( objCode + '/' + objID , { updates : updates } , fields , Api . Methods . PUT )
@@ -392,13 +397,9 @@ export class Api {
392397 bodyParams = clonedParams
393398 }
394399 else {
395- if ( clonedParams . hasOwnProperty ( ' updates' ) && ( options . method === Api . Methods . POST || options . method === Api . Methods . PUT ) ) {
400+ if ( this . serverAcceptsJSON && typeof clonedParams . updates === 'object' && ( options . method === Api . Methods . POST || options . method === Api . Methods . PUT ) ) {
396401 headers . append ( 'Content-Type' , 'application/json' )
397- if ( typeof clonedParams . updates === 'string' ) {
398- bodyParams = clonedParams . updates
399- } else {
400- bodyParams = JSON . stringify ( clonedParams . updates )
401- }
402+ bodyParams = JSON . stringify ( clonedParams . updates )
402403
403404 delete clonedParams . updates
404405 const qs = queryStringify ( clonedParams )
0 commit comments