All files / utils fileUrl.js

100% Statements 23/23
100% Branches 18/18
100% Functions 3/3
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 315x     5x     7x 6x 6x 7x       9x 8x 5x 1x       4x 3x 3x 3x 3x 2x 2x   1x    
const DEFAULT_API_BASE_URL = "/api/dark-room-library/v1";
 
export const API_BASE_URL =
  import.meta.env.VITE_API_BASE_URL || DEFAULT_API_BASE_URL;
 
export function buildApiUrl(path) {
  if (/^https?:\/\//i.test(path)) return path;
  const base = API_BASE_URL.replace(/\/$/, "");
  const suffix = path.startsWith("/") ? path : `/${path}`;
  return `${base}${suffix}`;
}
 
export function resolveFileUrl(url) {
  if (!url) return "";
  if (/^(https?:|data:|blob:)/i.test(url)) return url;
  if (/^\//.test(url)) return url;
  return `/${url}`;
}
 
export function toApiRequestPath(url) {
  if (!url) return "";
  const baseUrl = new URL(API_BASE_URL, window.location.origin);
  const targetUrl = new URL(resolveFileUrl(url), window.location.origin);
  const basePath = baseUrl.pathname.replace(/\/$/, "");
  if (targetUrl.pathname.startsWith(basePath)) {
    const relativePath = targetUrl.pathname.slice(basePath.length) || "/";
    return `${relativePath}${targetUrl.search}`;
  }
  return `${targetUrl.pathname}${targetUrl.search}`;
}