Skip to content

Commit 51183cd

Browse files
jwiggins78citizensas
authored andcommitted
Bug fixes for problems experienced using apiKey and edit calls (#99)
* Fixing alwaysUseGet feature and adding a test for it. Moving apiKey into headers for all requests when apiKey is used and adding to edit test
1 parent aa6871f commit 51183cd

4 files changed

Lines changed: 94 additions & 12 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "workfront-api",
3-
"version": "3.0.2",
3+
"version": "3.0.3",
44
"description": "A Workfront API for the Node.js and the Web",
55
"main": "dist/workfront.js",
66
"module": "es/index.js",

src/Api.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ export interface IHttpOptions {
3131
method?: string
3232
url: string
3333
alwaysUseGet?: boolean
34-
headers: {sessionID?: string}
34+
headers: {
35+
sessionID?: string,
36+
apiKey?: string
37+
}
3538
}
3639
export type TFields = string | string[]
3740

@@ -60,8 +63,12 @@ export class Api {
6063
constructor(config) {
6164
this._httpOptions = {
6265
url: config.url,
66+
alwaysUseGet: config.alwaysUseGet,
6367
headers: {}
6468
}
69+
if (config.apiKey) {
70+
this._httpOptions.headers.apiKey = config.apiKey
71+
}
6572
// Append version to path if provided
6673
let path
6774
if (['internal', 'unsupported', 'asp'].indexOf(config.version) >= 0) {
@@ -85,20 +92,20 @@ export class Api {
8592
*/
8693
getApiKey(username: string, password: string): Promise<string> {
8794
return new Promise((resolve, reject) => {
88-
if (typeof this._httpParams.apiKey !== 'undefined') {
89-
resolve(this._httpParams.apiKey)
95+
if (typeof this._httpOptions.headers.apiKey !== 'undefined') {
96+
resolve(this._httpOptions.headers.apiKey)
9097
}
9198
else {
9299
this.execute('USER', null, 'getApiKey', {username, password}).then((getApiKeyData) => {
93100
if (getApiKeyData.result === '') {
94101
this.execute('USER', null, 'generateApiKey', {username, password}).then((generateApiKeyData) => {
95-
this._httpParams.apiKey = generateApiKeyData.result
96-
resolve(this._httpParams.apiKey)
102+
this._httpOptions.headers.apiKey = generateApiKeyData.result
103+
resolve(this._httpOptions.headers.apiKey)
97104
}, reject)
98105
}
99106
else {
100-
this._httpParams.apiKey = getApiKeyData.result
101-
resolve(this._httpParams.apiKey)
107+
this._httpOptions.headers.apiKey = getApiKeyData.result
108+
resolve(this._httpOptions.headers.apiKey)
102109
}
103110
}, reject)
104111
}
@@ -151,7 +158,7 @@ export class Api {
151158
return new Promise((resolve, reject) => {
152159
this.execute('USER', null, 'clearApiKey').then((result) => {
153160
if (result) {
154-
delete this._httpParams.apiKey
161+
delete this._httpOptions.headers.apiKey
155162
resolve()
156163
} else {
157164
reject()
@@ -362,6 +369,9 @@ export class Api {
362369
if (this._httpOptions.headers.sessionID) {
363370
headers.append('sessionID', this._httpOptions.headers.sessionID)
364371
}
372+
else if (this._httpOptions.headers.apiKey) {
373+
headers.append('apiKey', this._httpOptions.headers.apiKey)
374+
}
365375

366376
let bodyParams = null, queryString = ''
367377
if (NodeFormData && params instanceof NodeFormData) {
@@ -385,7 +395,7 @@ export class Api {
385395
}
386396

387397
return fetch(options.url + options.path + queryString, {
388-
method: method,
398+
method: alwaysUseGet ? 'GET' : method,
389399
headers: headers,
390400
body: bodyParams,
391401
credentials: 'same-origin'
@@ -413,7 +423,7 @@ export class Api {
413423
* @return {string} returns the given api key value
414424
*/
415425
setApiKey(apiKey) {
416-
return this._httpParams.apiKey = apiKey
426+
return this._httpOptions.headers.apiKey = apiKey
417427
}
418428

419429
/**

test/integration/edit.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ describe('Edit', function() {
2828

2929
beforeEach(function() {
3030
this.api = new Workfront.Api({
31-
url: API_URL
31+
url: API_URL,
32+
apiKey: 'testapikey'
3233
})
3334
})
3435
afterEach(function() {
@@ -56,6 +57,7 @@ describe('Edit', function() {
5657
const [url, opts] = fetchMock.lastCall('edit')
5758
should(opts.method).equal('PUT')
5859
should(url).endWith(objCode + '/' + objID)
60+
should(opts.headers.get('apiKey')).equal('testapikey')
5961
should(opts.body).containEql('name=' + encodeURIComponent('api test 2'))
6062

6163
should(data).have.properties(['ID', 'name', 'objCode'])
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Copyright 2015 Workfront
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import * as fetchMock from 'fetch-mock'
18+
import * as should from 'should'
19+
20+
import * as Workfront from '../../src/index'
21+
22+
const API_URL = 'http://foobar:8080'
23+
24+
describe('Edit', function() {
25+
26+
afterEach(fetchMock.reset)
27+
afterEach(fetchMock.restore)
28+
29+
beforeEach(function() {
30+
this.api = new Workfront.Api({
31+
url: API_URL,
32+
apiKey: 'testapikey',
33+
alwaysUseGet: true
34+
})
35+
})
36+
afterEach(function() {
37+
this.api = undefined
38+
})
39+
40+
beforeEach(function() {
41+
fetchMock.mock(
42+
`begin:${API_URL}/attask/api/`,
43+
require('../../fixtures/edit.json'),
44+
{
45+
name: 'edit'
46+
}
47+
)
48+
})
49+
50+
it('makes a GET request with method=PUT and with proper params and return the edited object', function() {
51+
const params = {
52+
name: 'api test 2'
53+
}
54+
const objCode = 'PROJ',
55+
objID = 'foobar'
56+
57+
return this.api.edit(objCode, objID, params).then(function(data) {
58+
const [url, opts] = fetchMock.lastCall('edit')
59+
should(opts.method).equal('GET')
60+
should(url).endWith(objCode + '/' + objID)
61+
should(opts.headers.get('apiKey')).equal('testapikey')
62+
should(opts.body).containEql('name=' + encodeURIComponent('api test 2'))
63+
should(opts.body).containEql('method=PUT')
64+
65+
should(data).have.properties(['ID', 'name', 'objCode'])
66+
should(data.objCode).equal(objCode)
67+
should(data.name).equal(params.name)
68+
})
69+
})
70+
})

0 commit comments

Comments
 (0)