summaryrefslogtreecommitdiffhomepage
path: root/src/app/resolve-http-url.ts
blob: 1e20eb2cd1f940e3cd8f288ce27e8a376ee85990 (plain)
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
export interface HttpUrlEnv {
  readonly VITE_HTTP_URL?: string;
  readonly VITE_HTTP_PORT?: string;
}

export interface HttpUrlLocation {
  readonly protocol: string;
  readonly hostname: string;
}

const DEFAULT_PORT = "24203";
const DEFAULT_FALLBACK = "http://localhost:24203";

export function resolveHttpUrl(env: HttpUrlEnv, location?: HttpUrlLocation): string {
  if (env.VITE_HTTP_URL !== undefined && env.VITE_HTTP_URL !== "") {
    return env.VITE_HTTP_URL;
  }

  if (location === undefined) {
    return DEFAULT_FALLBACK;
  }

  const port =
    env.VITE_HTTP_PORT !== undefined && env.VITE_HTTP_PORT !== ""
      ? env.VITE_HTTP_PORT
      : DEFAULT_PORT;
  return `${location.protocol}//${location.hostname}:${port}`;
}