Skip to content

Commit

Permalink
setToken now function of Core instead of constructor option
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-scherzinger committed Jan 23, 2017
1 parent 2ea2e2c commit dce46dd
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 18 deletions.
16 changes: 15 additions & 1 deletion src/Core.js
Expand Up @@ -2,7 +2,6 @@

import traverson from 'traverson';
import HalAdapter from 'traverson-hal';

import Problem from './Problem';
import events from './EventEmitter';

Expand Down Expand Up @@ -71,6 +70,21 @@ export default class Core {
.addRequestOptions({ headers: { Accept: 'application/hal+json' } });
}

/**
* Set an existing accessToken
*
* @param {string} token the existing token
* @returns {Core} this for chainability
*/
setToken(token) {
if (!token) {
throw new Error('Token must be defined');
}

this.traversal.addRequestOptions({ headers: { Authorization: `Bearer ${token}` } });
return this;
}

/**
* Creates a new {@link
* https://github.com/basti1302/traverson/blob/master/api.markdown#request-builder
Expand Down
14 changes: 4 additions & 10 deletions src/DataManager.js
Expand Up @@ -22,21 +22,15 @@ export default class DataManager extends Core {
* Creates a new instance of {@link DataManager} module. Can be used to work with DataManager
* API.
*
* @param {string} environment the environment to connect to. 'live', 'stage', 'nightly', or
* @param {?string} environment the environment to connect to. 'live', 'stage', 'nightly', or
* 'develop'.
* @param {?string} token optionally a jwt token to use for authentication.
*/
constructor(environment, token) {
if (!{}.hasOwnProperty.call(urls, environment)) {
constructor(environment) {
if (environment && !{}.hasOwnProperty.call(urls, environment)) {
throw new Error('invalid environment specified');
}

super(urls[environment]);

this.resourceName = 'ec:datamanager';
if (token) {
this.traversal.addRequestOptions({ headers: { Authorization: `Bearer ${token}` } });
}
super(urls[environment || 'live']);
}

/**
Expand Down
13 changes: 13 additions & 0 deletions test/Core.test.js
Expand Up @@ -35,6 +35,19 @@ describe('Core', () => {
const throws = () => new Core.default();
throws.should.throw(Error);
});
it('should set token', () => {
core.setToken('test');
core.should.have.deep.property('traversal.requestOptions.headers.Authorization', 'Bearer test');
});
it('should throw on undefined token', () => {
const throws = () => core.setToken();
throws.should.throw(Error);
});
it('should only add one header', () => {
core.setToken('test');
core.setToken('der zweite');
core.should.have.deep.property('traversal.requestOptions.headers.Authorization', 'Bearer der zweite');
});
it('should return traverson Builder', () => {
core.newRequest().should.be.instanceOf(traverson._Builder);
});
Expand Down
16 changes: 10 additions & 6 deletions test/DataManager.test.js
Expand Up @@ -23,17 +23,21 @@ function capitalizeFirstLetter(string) {
}

describe('DataManager class', () => {
it('instantiate', () => {
it('should instantiate', () => {
new DataManager('live').should.be.instanceOf(DataManager);
});
it('instantiate with token', () => {
const dm = new DataManager('live', 'token');
it('should instantiate with empty environment', () => {
new DataManager().should.be.instanceOf(DataManager);
});
it('should set token with token', () => {
const dm = new DataManager('live');
dm.setToken('token');
dm.traversal.getRequestOptions().should.have.deep.property('headers.Authorization', 'Bearer token');
});
it('should throw error on undefined environment', () => {
it('should throw error on invalid environment', () => {
const fn = () => {
/* eslint no-new:0 */
new DataManager();
new DataManager('invalid');
};
fn.should.throw(Error);
});
Expand Down Expand Up @@ -67,7 +71,7 @@ describe('DataManager class', () => {
throw err;
});
});
it('should throws on get with undefined id', () => {
it('should throw on get with undefined id', () => {
const throws = () => new DataManager('live').get();
throws.should.throw(Error);
});
Expand Down
2 changes: 2 additions & 0 deletions typings/Core.d.ts
Expand Up @@ -2,4 +2,6 @@ export declare class Core {
constructor(url: string);

newRequest(): any;

setToken(token: string): Core;
}
2 changes: 1 addition & 1 deletion typings/DataManager.d.ts
Expand Up @@ -4,7 +4,7 @@ import { DataManagerList } from './resource/DataManagerList';
import { filterOptions } from './interfaces';

export declare class DataManager extends Core {
constructor(environment: string, token?: string);
constructor(environment?: string);

list(options?: filterOptions): Promise<DataManagerList>;

Expand Down

0 comments on commit dce46dd

Please sign in to comment.