File size: 671 Bytes
d660fa9
 
 
 
 
 
 
 
 
 
 
f4542ce
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * api — URL helpers for REST and WebSocket calls.
 *
 * apiUrl() prefixes REST paths with the configured VITE_API_URL base;
 * wsUrl() converts an http(s) base to the ws(s) scheme for sockets.
 *
 * Architecture: imported by every component that talks to the backend.
 *
 * Design: keeps the base URL configuration in one place.
 */

const BASE = import.meta.env.VITE_API_URL || "";

export function apiUrl(path) {
  return `${BASE}${path}`;
}

export function wsUrl(path) {
  if (BASE) {
    return `${BASE.replace(/^http/, "ws")}${path}`;
  }
  const protocol = location.protocol === "https:" ? "wss:" : "ws:";
  return `${protocol}//${location.host}${path}`;
}