Commit 4e42251c authored by Sheetal Umesh Kumar's avatar Sheetal Umesh Kumar

Read apiUrl from services.

We need to be able to support the client to work with different annotation
services, other than just `https://hypothes.is/api`.
If the settings from the host doesn't specify a service, the client should
fall back to using the default apiUrl which would be `https://hypothes.is/api`.

https://github.com/hypothesis/product-backlog/issues/278
parent 2a99b84b
'use strict'; 'use strict';
var addAnalytics = require('./ga'); var addAnalytics = require('./ga');
var getApiUrl = require('./get-api-url');
var serviceConfig = require('./service-config'); var serviceConfig = require('./service-config');
require('../shared/polyfills'); require('../shared/polyfills');
...@@ -19,6 +20,8 @@ if (settings.raven) { ...@@ -19,6 +20,8 @@ if (settings.raven) {
var hostPageConfig = require('./host-config'); var hostPageConfig = require('./host-config');
Object.assign(settings, hostPageConfig(window)); Object.assign(settings, hostPageConfig(window));
settings.apiUrl = getApiUrl(settings);
// Disable Angular features that are not compatible with CSP. // Disable Angular features that are not compatible with CSP.
// //
// See https://docs.angularjs.org/api/ng/directive/ngCsp // See https://docs.angularjs.org/api/ng/directive/ngCsp
......
'use strict';
var serviceConfig = require('./service-config');
/**
* Function that returns apiUrl from the settings object.
*
* @param {object} settings - The settings object
* @returns {string} The apiUrl from the service or the default apiUrl from the settings
* @throws {Error} If the settings has a service but the service doesn't have an apiUrl
*
*/
function getApiUrl(settings) {
var service = serviceConfig(settings);
if (service) {
// If the host page contains a service setting then the client should default to
// using that apiUrl.
if (service.apiUrl) {
return service.apiUrl;
} else {
throw new Error('Service should contain an apiUrl value.');
}
}
return settings.apiUrl;
}
module.exports = getApiUrl;
'use strict';
var getApiUrl = require('../get-api-url');
describe('sidebar.getApiUrl', function () {
context('when there is a service object in settings', function () {
it('returns apiUrl from the service object', function () {
var settings = {
apiUrl: 'someApiUrl',
services: [{
apiUrl: 'someOtherApiUrl',
}],
};
assert.equal(getApiUrl(settings), settings.services[0].apiUrl);
});
});
context('when there is no service object in settings', function () {
it('returns apiUrl from the settings object', function () {
var settings = {
apiUrl: 'someApiUrl',
};
assert.equal(getApiUrl(settings), settings.apiUrl);
});
});
context('when there is a service object in settings but does not contain an apiUrl key', function () {
it('throws error', function () {
var settings = {
apiUrl: 'someApiUrl',
services: [{}],
};
assert.throws(
function() { getApiUrl(settings); },
Error,
'Service should contain an apiUrl value');
});
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment