aboutsummaryrefslogtreecommitdiffstats
path: root/src/api/apiBase.js
blob: ce02922acf2620e206b1a2151888374facf79468 (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
/**
 * Get API base URL from store
 */
import {
  API_VERSION,
  API,
} from '../environment';

const apiBase = () => {
  let url;
  if (!window.ferdi
    || !window.ferdi.stores.settings
    || !window.ferdi.stores.settings.all) {
    // Stores have not yet been loaded - send invalid URL to force a retry when stores are loaded
    //  "Why 1.1.1.1 as the default, invalid URL?"
    //    1.1.1.1 is the server for Cloudflare's DNS service and will be the same across most networks.
    //    Using a random IP could result in unwanted connections, using localhost could unwantedly
    //    connect to local develoment servers.
    //    1.1.1.1 also sends a status 400 response for invalid routes. Other servers may return status 401
    //    on some routes. This would result in Ferdi deleting its current authToken as it thinks it
    //    has gone invalid.
    url = 'https://1.1.1.1';
  } else if (window.ferdi.stores.settings.all.app.server) {
    // Load URL from store
    url = window.ferdi.stores.settings.all.app.server;
  } else {
    // Use default server url
    url = API;
  }

  return `${url}/${API_VERSION}`;
};

export default apiBase;