2025-06-24 20:45:43 -04:00
|
|
|
// rewrite URLs to use local proxy server
|
|
|
|
|
const rewriteUrl = (_url) => {
|
2025-07-07 12:26:59 -04:00
|
|
|
if (!_url) {
|
|
|
|
|
throw new Error(`rewriteUrl called with invalid argument: '${_url}' (${typeof _url})`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle relative URLs early: return them as-is since they don't need rewriting
|
2025-06-24 20:45:43 -04:00
|
|
|
if (typeof _url === 'string' && !_url.startsWith('http')) {
|
|
|
|
|
return _url;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-07 12:26:59 -04:00
|
|
|
if (typeof _url !== 'string' && !(_url instanceof URL)) {
|
|
|
|
|
throw new Error(`rewriteUrl expects a URL string or URL object, received: ${typeof _url}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Convert to URL object (for URL objects, creates a copy to avoid mutating the original)
|
|
|
|
|
const url = new URL(_url);
|
2025-06-24 20:45:43 -04:00
|
|
|
|
2025-06-26 17:17:17 -04:00
|
|
|
if (!window.WS4KP_SERVER_AVAILABLE) {
|
2025-07-07 12:26:59 -04:00
|
|
|
// If running standalone in the browser, simply return a URL object without rewriting
|
2025-06-26 17:17:17 -04:00
|
|
|
return url;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-24 20:45:43 -04:00
|
|
|
// Rewrite the origin to use local proxy server
|
|
|
|
|
if (url.origin === 'https://api.weather.gov') {
|
|
|
|
|
url.protocol = window.location.protocol;
|
|
|
|
|
url.host = window.location.host;
|
|
|
|
|
url.pathname = `/api${url.pathname}`;
|
2025-06-24 12:38:14 -04:00
|
|
|
} else if (url.origin === 'https://forecast.weather.gov') {
|
|
|
|
|
url.protocol = window.location.protocol;
|
|
|
|
|
url.host = window.location.host;
|
|
|
|
|
url.pathname = `/forecast${url.pathname}`;
|
2025-06-24 20:45:43 -04:00
|
|
|
} else if (url.origin === 'https://www.spc.noaa.gov') {
|
|
|
|
|
url.protocol = window.location.protocol;
|
|
|
|
|
url.host = window.location.host;
|
|
|
|
|
url.pathname = `/spc${url.pathname}`;
|
|
|
|
|
} else if (url.origin === 'https://radar.weather.gov') {
|
|
|
|
|
url.protocol = window.location.protocol;
|
|
|
|
|
url.host = window.location.host;
|
|
|
|
|
url.pathname = `/radar${url.pathname}`;
|
|
|
|
|
} else if (url.origin === 'https://mesonet.agron.iastate.edu') {
|
|
|
|
|
url.protocol = window.location.protocol;
|
|
|
|
|
url.host = window.location.host;
|
|
|
|
|
url.pathname = `/mesonet${url.pathname}`;
|
2026-04-07 14:57:23 -04:00
|
|
|
} else if (url.origin === 'https://api.open-meteo.com') {
|
|
|
|
|
url.protocol = window.location.protocol;
|
|
|
|
|
url.host = window.location.host;
|
|
|
|
|
url.pathname = `/open-meteo${url.pathname}`;
|
2025-06-24 20:45:43 -04:00
|
|
|
} else if (typeof OVERRIDES !== 'undefined' && OVERRIDES?.RADAR_HOST && url.origin === `https://${OVERRIDES.RADAR_HOST}`) {
|
|
|
|
|
// Handle override radar host
|
|
|
|
|
url.protocol = window.location.protocol;
|
|
|
|
|
url.host = window.location.host;
|
|
|
|
|
url.pathname = `/mesonet${url.pathname}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return url;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export {
|
|
|
|
|
// eslint-disable-next-line import/prefer-default-export
|
|
|
|
|
rewriteUrl,
|
|
|
|
|
};
|