Skip to content

Commit 8308250

Browse files
committed
fix: make send JSON body optional
1 parent 59da12e commit 8308250

1 file changed

Lines changed: 32 additions & 16 deletions

File tree

src/Api.ts

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,11 @@ export class Api {
175175
* @param {String|String[]} [fields] Which fields of newly created object to return. See {@link https://developers.attask.com/api-docs/api-explorer/|Workfront API Explorer} for the list of available fields for the given objCode.
176176
* @returns {Promise} A promise which will resolved with the ID and any other specified fields of newly created object
177177
*/
178-
create(objCode: string, params: object, fields?: TFields) {
179-
return this.request(objCode, params, fields, Api.Methods.POST)
178+
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)
181+
}
182+
return this.request(objCode, params, fields, Api.Methods.POST, true)
180183
}
181184

182185
/**
@@ -188,8 +191,11 @@ export class Api {
188191
* @param {String|String[]} [fields] Which fields to return. See {@link https://developers.attask.com/api-docs/api-explorer/|Workfront API Explorer} for the list of available fields for the given objCode.
189192
* @return {Promise} A promise which will resolved with results if everything went ok and rejected otherwise
190193
*/
191-
edit(objCode: string, objID: string, updates: object, fields?: TFields) {
192-
return this.request(objCode + '/' + objID, updates, fields, Api.Methods.PUT)
194+
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)
197+
}
198+
return this.request(objCode + '/' + objID, updates, fields, Api.Methods.PUT, false)
193199
}
194200

195201
/**
@@ -338,7 +344,17 @@ export class Api {
338344
return this.request(objCode + '/report', query, null, Api.Methods.GET)
339345
}
340346

341-
request(path: string, params: THttpParams, fields?: TFields, method: string = Api.Methods.GET): Promise<any> {
347+
/**
348+
* Do the request using Fetch API.
349+
* @memberOf Api
350+
* @param {String} path URI path where the request calls
351+
* @param {Object} params An object with params
352+
* @param {Object} [fields] Fields to query for the request
353+
* @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
355+
* @return {Promise} A promise which will resolved with results if everything went ok and rejected otherwise
356+
*/
357+
request(path: string, params: THttpParams, fields?: TFields, method: string = Api.Methods.GET, sendJSONBody = false): Promise<any> {
342358
params = objectAssign(params || {}, this._httpParams)
343359

344360
const alwaysUseGet = this._httpOptions.alwaysUseGet
@@ -383,21 +399,21 @@ export class Api {
383399
bodyParams = params
384400
}
385401
else {
386-
if (options.method === Api.Methods.PUT || options.method === Api.Methods.POST) {
402+
if (sendJSONBody && (options.method === Api.Methods.POST || options.method === Api.Methods.PUT)) {
387403
headers.append('Content-Type', 'application/json')
404+
bodyParams = JSON.stringify(params)
388405
} else {
389406
headers.append('Content-Type', 'application/x-www-form-urlencoded')
390-
}
391-
392-
bodyParams = JSON.stringify(params)
393-
if (options.method === Api.Methods.GET) {
394-
if (bodyParams && Object.keys(params).length > 0) {
395-
queryString = '?' + Object.keys(params).reduce(function(a, k) {
396-
a.push(k + '=' + encodeURIComponent(params[k]))
397-
return a
398-
}, []).join('&')
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) {
412+
if (bodyParams) {
413+
queryString = '?' + bodyParams
414+
}
415+
bodyParams = null
399416
}
400-
bodyParams = null
401417
}
402418
}
403419

0 commit comments

Comments
 (0)