Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/Api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@

import {INTERNAL_PREFIX} from '@workfront/api-constants'

export interface IImsCredentials {
imsToken: string
imsOrgId: string
}

export interface IHttpOptions {
path?: string
method?: string
Expand All @@ -30,6 +35,8 @@ export interface IHttpOptions {
sessionID?: string
'X-XSRF-TOKEN'?: string
apiKey?: string
Authorization?: string
'x-gw-ims-org-id'?: string
}
}
export interface IApiConfig {
Expand Down Expand Up @@ -575,6 +582,20 @@ export class Api {
delete this._httpOptions.headers['X-XSRF-TOKEN']
}
}
/**
* Sets the 'x-gw-ims-org-id' and 'Authorization' header
* @memberof Api
* @param {IImsCredentials} imsCredentials ims token and ims org id to set
*/
setImsCredentials(imsCredentials?: IImsCredentials) {
if (imsCredentials) {
this._httpOptions.headers.Authorization = 'Bearer ' + imsCredentials.imsToken
this._httpOptions.headers['x-gw-ims-org-id'] = imsCredentials.imsOrgId
} else {
delete this._httpOptions.headers.Authorization
delete this._httpOptions.headers['x-gw-ims-org-id']
}
}

uploadFileContent(fileContent, filename: string) {
const data = new FormData()
Expand All @@ -591,6 +612,12 @@ export class Api {
headers.append('X-XSRF-TOKEN', this._httpOptions.headers['X-XSRF-TOKEN'])
} else if (this._httpOptions.headers.apiKey) {
headers.append('apiKey', this._httpOptions.headers.apiKey)
} else if (
this._httpOptions.headers.Authorization &&
this._httpOptions.headers['x-gw-ims-org-id']
) {
headers.append('Authorization', this._httpOptions.headers.Authorization)
headers.append('x-gw-ims-org-id', this._httpOptions.headers['x-gw-ims-org-id'])
}
return headers
}
Expand Down
13 changes: 13 additions & 0 deletions test/Api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe('Create new instance for API', function () {
should(api.uploadFileContent).be.a.Function().and.has.lengthOf(2)
should(api.setApiKey).be.a.Function().and.has.lengthOf(1)
should(api.clearApiKey).be.a.Function().and.has.lengthOf(0)
should(api.setImsCredentials).be.a.Function().and.has.lengthOf(1)
})

it('should set correct API path based on passed configuration (version is passed)', function () {
Expand All @@ -54,4 +55,16 @@ describe('Create new instance for API', function () {
const api = new Api({url: 'http://localhost', version: 'asp'})
should(api._httpOptions.path).equal('/attask/api-asp')
})

it('should store IMS headers when credentials are set', function () {
const api = new Api({url: 'http://localhost'})
api.setImsCredentials({imsToken: 'abc123', imsOrgId: 'org-1'})

should(api._httpOptions.headers.Authorization).equal('Bearer abc123')
should(api._httpOptions.headers['x-gw-ims-org-id']).equal('org-1')

const headers = (api as any).getHeaders()
should(headers.get('Authorization')).equal('Bearer abc123')
should(headers.get('x-gw-ims-org-id')).equal('org-1')
})
})
Loading