aboutsummaryrefslogtreecommitdiffstats
path: root/src/api/apiBase.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/apiBase.js')
-rw-r--r--src/api/apiBase.js38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/api/apiBase.js b/src/api/apiBase.js
new file mode 100644
index 000000000..561b025f0
--- /dev/null
+++ b/src/api/apiBase.js
@@ -0,0 +1,38 @@
1/**
2 * Get API base URL from store
3 */
4import {
5 API_VERSION,
6} from '../environment';
7import {
8 LOCAL_SERVER,
9} from '../config';
10
11const apiBase = () => {
12 let url;
13
14 if (!window.ferdi
15 || !window.ferdi.stores.settings
16 || !window.ferdi.stores.settings.all
17 || !window.ferdi.stores.settings.all.app.server) {
18 // Stores have not yet been loaded - send invalid URL to force a retry when stores are loaded
19 // "Why 1.1.1.1 as the default, invalid URL?"
20 // 1.1.1.1 is the server for Cloudflare's DNS service and will be the same across most networks.
21 // Using a random IP could result in unwanted connections, using localhost could unwantedly
22 // connect to local develoment servers.
23 // 1.1.1.1 also sends a status 400 response for invalid routes. Other servers may return status 401
24 // on some routes. This would result in Ferdi deleting its current authToken as it thinks it
25 // has gone invalid.
26 url = 'https://1.1.1.1';
27 } else if (window.ferdi.stores.settings.all.app.server === LOCAL_SERVER) {
28 // Use URL for local server
29 url = `http://127.0.0.1:${window.ferdi.stores.requests.localServerPort}`;
30 } else {
31 // Load URL from store
32 url = window.ferdi.stores.settings.all.app.server;
33 }
34
35 return `${url}/${API_VERSION}`;
36};
37
38export default apiBase;