aboutsummaryrefslogtreecommitdiffstats
path: root/src/api/apiBase.ts
blob: 522891fa0ed08206573d3dfce8bda231d8e2894a (plain) (blame)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import {
  DEV_API_FRANZ_WEBSITE,
  LIVE_FERDIUM_API,
  LIVE_FRANZ_API,
  LOCAL_HOSTNAME,
  LOCAL_SERVER,
  SERVER_NOT_LOADED,
} from '../config';
/**
 * Get API base URL from store
 */
import { API_VERSION } from '../environment-remote';
import { fixUrl } from '../helpers/url-helpers';

// Note: This cannot be used from the internal-server since we are not running within the context of a browser window
export default function apiBase(withVersion = true) {
  if (!(window as any).ferdium?.stores.settings?.all?.app.server) {
    // Stores have not yet been loaded - return SERVER_NOT_LOADED to force a retry when stores are loaded
    return SERVER_NOT_LOADED;
  }

  const url =
    (window as any).ferdium.stores.settings.all.app.server === LOCAL_SERVER
      ? `http://${LOCAL_HOSTNAME}:${
          (window as any).ferdium.stores.requests.localServerPort
        }`
      : (window as any).ferdium.stores.settings.all.app.server;

  return fixUrl(withVersion ? `${url}/${API_VERSION}` : url);
}

export function needsToken(): boolean {
  return (
    (window as any).ferdium.stores.settings.all.app.server === LOCAL_SERVER
  );
}

export function localServerToken(): string | undefined {
  return needsToken()
    ? (window as any).ferdium.stores.requests.localServerToken
    : undefined;
}

export function importExportURL() {
  const base = apiBase(false);
  return needsToken() ? `${base}/token/${localServerToken()}` : base;
}

export function serverBase() {
  const serverType = (window as any).ferdium.stores.settings.all.app.server;
  const noServerFerdi = 'You are using Ferdi without a server';
  const noServerFerdium = 'You are using Ferdium without a server';

  let terms;
  switch (serverType) {
    case LIVE_FRANZ_API: {
      terms = DEV_API_FRANZ_WEBSITE;
      break;
    }
    case noServerFerdi: {
      terms = LIVE_FERDIUM_API;
      break;
    }
    case noServerFerdium: {
      terms = LIVE_FERDIUM_API;
      break;
    }
    default: {
      terms = serverType;
    }
  }

  return fixUrl(terms);
}

export function serverName(): string {
  const serverType = (window as any).ferdium.stores.settings.all.app.server;
  const noServerFerdi = 'You are using Ferdi without a server';
  const noServerFerdium = 'You are using Ferdium without a server';

  let nameServer;
  switch (serverType) {
    case LIVE_FRANZ_API: {
      nameServer = 'Franz';
      break;
    }
    case LIVE_FERDIUM_API: {
      nameServer = 'Ferdium';
      break;
    }
    case noServerFerdi: {
      nameServer = 'No';
      break;
    }
    case noServerFerdium: {
      nameServer = 'No';
      break;
    }
    default: {
      nameServer = 'Custom';
    }
  }

  return nameServer;
}