mirror of
https://github.com/ascribe/onion.git
synced 2024-12-23 01:39:36 +01:00
7014514654
Keeping an export constant is more predictable and less surprising for most people.
33 lines
784 B
JavaScript
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
|
|
};
|