Skip to content

Commit

Permalink
feat: create History from DataManager Source
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-scherzinger committed Mar 21, 2018
1 parent 07dff53 commit 3f1793e
Show file tree
Hide file tree
Showing 8 changed files with 2,759 additions and 3 deletions.
35 changes: 35 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@types/superagent": "^3.5.7",
"@types/traverson": "^2.0.28",
"browser-cookies": "^1.1.0",
"eventsource": "^1.0.5",
"halfred": "^1.0.0",
"json-schema-remote": "^1.3.3",
"jwt-decode": "^2.2.0",
Expand Down
51 changes: 49 additions & 2 deletions src/DataManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as validator from 'json-schema-remote';
import * as EventSource from 'eventsource';

import Core, { environment, options } from './Core';
import DataManagerResource from './resources/datamanager/DataManagerResource';
Expand All @@ -8,13 +9,15 @@ import DMStatsResource from './resources/datamanager/DMStatsResource';
import TemplateList from './resources/datamanager/TemplateList';
import TemplateResource from './resources/datamanager/TemplateResource';
import { filterOptions } from './resources/ListResource';
import { get, post, superagentGet } from './helper';
import { get, getUrl, optionsToQuery, post, superagentGet } from './helper';
import TokenStoreFactory, { TokenStore } from './TokenStore';

validator.setLoggingFunction(() => {
});

const environmentSymbol = Symbol.for('environment');
const relationsSymbol = Symbol.for('relations');
const tokenStoreSymbol = Symbol.for('tokenStore');

const urls = {
live: 'https://datamanager.entrecode.de/',
Expand All @@ -32,7 +35,7 @@ const urls = {
* @param {environment?} environment the environment to connect to
*/
export default class DataManager extends Core {
constructor(envOrOptions?: environment|options) {
constructor(envOrOptions?: environment | options) {
super(urls, envOrOptions);

this[relationsSymbol] = {
Expand Down Expand Up @@ -178,6 +181,50 @@ export default class DataManager extends Core {
.then((res) => res.url);
}

/**
* Creates a new History EventSource with the given filter options.
*
* @param {filterOptions | any} options The filter options
* @return {Promise<EventSource>} The created EventSource.
*/
newHistory(options?: filterOptions | any) {
return Promise.resolve()
.then(() => this.follow('ec:history'))
.then(request => {
request.follow('ec:entry-history');

if (options) {
request.withTemplateParameters(optionsToQuery(options));
}

return getUrl(this[environmentSymbol], request)
})
.then(([url]) => {
const eventSourceInitDict = {
headers: {},
};

const store = this[tokenStoreSymbol];
let secondStore: TokenStore;
if (!store.hasToken()) {
// when no token is present see if we have a public environment (with shortID)
// if so look in second store
const result = /^(live|stage|nightly|develop|test)[A-Fa-f0-9]{8}$/.exec(this[environmentSymbol]);
if (result) {
secondStore = TokenStoreFactory(<environment>result[1]);
}
}

if (store.hasToken()) {
eventSourceInitDict.headers['Authorization'] = `Bearer ${store.getToken()}`;
} else if (secondStore && secondStore.hasToken()) {
eventSourceInitDict.headers['Authorization'] = `Bearer ${secondStore.getToken()}`;
}

return new EventSource(url, eventSourceInitDict);
});
}

/**
* Load a single {@link DMStatsResource}.
*
Expand Down
17 changes: 17 additions & 0 deletions test/datamanager/DataManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,23 @@ describe('DataManager class', () => {
return new DataManager('live').getImageThumbUrl()
.should.be.rejectedWith('assetID must be defined');
});
it('should create history resource', () => {
nock.reset();
const dm = new DataManager('live');
const getStub = sinon.stub(helper, 'get');
getStub.onFirstCall().returns(resolver('dm-list.json'));
getStub.onSecondCall().returns(resolver('dm-history-root.json'));
getStub.onThirdCall().returns(resolver('dm-history-response.json'));
const urlStub = sinon.stub(helper, 'getUrl');
urlStub.onFirstCall().returns(Promise.resolve(['https://dm-history.entrecode.de/entryhistory']));

return dm.newHistory()
.then((history) => {
history.should.exist;
getStub.restore();
urlStub.restore();
});
});
});

describe('DataManager ListResource', () => {
Expand Down
8 changes: 8 additions & 0 deletions test/mocks/TraversonMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ class TraversonMock {
withTemplateParameters() {
return this;
}

withRequestOptions() {
return this;
}

addRequestOptions() {
return this;
}
}

module.exports = TraversonMock;
Loading

0 comments on commit 3f1793e

Please sign in to comment.