Skip to content

Commit 18969bc

Browse files
committed
fix: preserving backward compatibility
breaking: version is set to internal if omitted
1 parent 45e3c0f commit 18969bc

17 files changed

Lines changed: 49 additions & 83 deletions

src/Api.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

test/Api.spec.js

Lines changed: 0 additions & 42 deletions
This file was deleted.

test/Api.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ describe('Create new instance for API', function() {
4848

4949
it('should set correct API path based on passed configuration (version is not passed)', function() {
5050
const api = new Api({url: 'http://localhost'})
51-
should(api._httpOptions.path).equal('/attask/api')
51+
should(api._httpOptions.path).equal('/attask/api-internal')
5252
})
5353

54-
it('should set correct API path based on passed configuration (version="internal")', function() {
55-
const api = new Api({url: 'http://localhost', version: 'internal'})
56-
should(api._httpOptions.path).equal('/attask/api-internal')
54+
it('should set correct API path based on passed configuration (version="asp")', function() {
55+
const api = new Api({url: 'http://localhost', version: 'asp'})
56+
should(api._httpOptions.path).equal('/attask/api-asp')
5757
})
5858
})

test/integration/copy.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('Copy', function() {
3838
describe('success', function() {
3939
beforeEach(function() {
4040
fetchMock.mock(
41-
`begin:${API_URL}/attask/api/`,
41+
`begin:${API_URL}/attask/api`,
4242
require('../../fixtures/copy.json'),
4343
{
4444
name: 'copy'

test/integration/count.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('Count', function() {
3737

3838
beforeEach(function() {
3939
fetchMock.mock(
40-
`begin:${API_URL}/attask/api/`,
40+
`begin:${API_URL}/attask/api`,
4141
require('../../fixtures/count.json'),
4242
{
4343
name: 'count'

test/integration/create.spec.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('Create', function() {
3737

3838
beforeEach(function() {
3939
fetchMock.mock(
40-
`begin:${API_URL}/attask/api/`,
40+
`begin:${API_URL}/attask/api`,
4141
require('../../fixtures/create.json'),
4242
{
4343
name: 'create'
@@ -73,16 +73,20 @@ describe('Create', function() {
7373
})
7474
})
7575
it('should create an object using the old api (passing updates property)', function() {
76+
const api = new Workfront.Api({
77+
url: API_URL,
78+
version: '4.0'
79+
})
7680
const params = {
7781
updates: JSON.stringify({name: 'test'})
7882
}
7983
const objCode = 'TASK'
8084

81-
return this.api.create(objCode, params).then(function(data) {
85+
return api.create(objCode, params).then(function(data) {
8286
const [url, opts] = fetchMock.lastCall('create')
8387
should(url).endWith(objCode)
8488
should(opts.method).equal('POST')
85-
should(opts.body).equal(params.updates)
89+
should(opts.body).equal('updates=' + encodeURIComponent(params.updates))
8690

8791
should(data).have.properties(['ID', 'name', 'objCode'])
8892
should(data.objCode).equal(objCode)

test/integration/edit.spec.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('Edit', function() {
3838

3939
beforeEach(function() {
4040
fetchMock.mock(
41-
`begin:${API_URL}/attask/api/`,
41+
`begin:${API_URL}/attask/api`,
4242
require('../../fixtures/edit.json'),
4343
{
4444
name: 'edit'
@@ -66,18 +66,21 @@ describe('Edit', function() {
6666
})
6767
})
6868
it('should edit an object using the old api (passing updates property)', function() {
69+
const api = new Workfront.Api({
70+
url: API_URL,
71+
version: '4.0'
72+
})
6973
const params = {
7074
updates: JSON.stringify({name: 'api test 2'})
7175
}
7276
const objCode = 'PROJ',
7377
objID = 'foobar'
7478

75-
return this.api.edit(objCode, objID, params).then(function(data) {
79+
return api.edit(objCode, objID, params).then(function(data) {
7680
const [url, opts] = fetchMock.lastCall('edit')
7781
should(opts.method).equal('PUT')
7882
should(url).endWith(objCode + '/' + objID)
79-
should(opts.headers.get('apiKey')).equal('testapikey')
80-
should(opts.body).containEql(params.updates)
83+
should(opts.body).equal('updates=' + encodeURIComponent(params.updates))
8184

8285
should(data.name).equal('api test 2')
8386
should(data).have.properties(['ID', 'name', 'objCode'])

test/integration/editUsingGet.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('Edit', function() {
3939

4040
beforeEach(function() {
4141
fetchMock.mock(
42-
`begin:${API_URL}/attask/api/`,
42+
`begin:${API_URL}/attask/api`,
4343
require('../../fixtures/edit.json'),
4444
{
4545
name: 'edit'

test/integration/execute.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('Execute', function() {
3737

3838
beforeEach(function() {
3939
fetchMock.mock(
40-
`begin:${API_URL}/attask/api/`,
40+
`begin:${API_URL}/attask/api`,
4141
require('../../fixtures/execute.json'),
4242
{
4343
name: 'execute'

test/integration/get.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('Get', function() {
3838
describe('Call with single objId (String)', function() {
3939
beforeEach(function() {
4040
fetchMock.mock(
41-
`begin:${API_URL}/attask/api/`,
41+
`begin:${API_URL}/attask/api`,
4242
require('../../fixtures/get.json'),
4343
{
4444
name: 'get'
@@ -89,7 +89,7 @@ describe('Get', function() {
8989
describe('Call with single objId (String)', function() {
9090
beforeEach(function() {
9191
fetchMock.mock(
92-
`begin:${API_URL}/attask/api/`,
92+
`begin:${API_URL}/attask/api`,
9393
require('../../fixtures/get_multiple.json'),
9494
{
9595
name: 'get'

0 commit comments

Comments
 (0)