1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-23 09:50:31 +01:00
onion/js/utils/url_resolver.js
Brett Sun 7014514654 Use UrlResolver to resolve api urls based on white labelling rather than updating ApiUrl's export
Keeping an export constant is more predictable and less surprising for
most people.
2016-06-14 17:58:00 +02:00

33 lines
784 B
JavaScript

let URL_MAPPING;
export function resolveUrl(url) {
let apiUrl = url;
if (!url) {
throw new Error('Url was not defined');
} else if (!url.match(/^http/)) {
apiUrl = URL_MAPPING && URL_MAPPING[url];
if (!apiUrl) {
if (process.env.NODE_ENV === 'production' &&
!(URL_MAPPING && Object.keys(URL_MAPPING).length)) {
// eslint-disable-next-line no-console
console.warn('No url mapping was defined yet for ApiUrlResolver.');
}
throw new Error(`Could not find a url mapping for "${url}"`);
}
}
return apiUrl;
}
export function setUrlMapping(urlMapping) {
URL_MAPPING = urlMapping;
}
export default {
setUrlMapping,
resolve: resolveUrl
};