Files

14 lines
689 B
JavaScript

// The server supplies this value; never infer it from the URL or proxy headers.
const basePath = (document.querySelector('meta[name="ranch-web-base-path"]')?.content || '').replace(/\/+$/, '');
export const webPath = path => `${basePath}${path}`;
export function apiPath(path) {
const url = new URL(path, location.origin);
if (url.origin !== location.origin || url.username || url.password) throw new TypeError('Invalid API URL');
let pathname = url.pathname;
if (basePath && pathname.startsWith(`${basePath}/`)) pathname = pathname.slice(basePath.length);
if (!pathname.startsWith('/api/')) throw new TypeError('Invalid API path');
return webPath(pathname) + url.search;
}