diff options
author | Markus Hatvan <markus_hatvan@aon.at> | 2021-09-14 10:34:04 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-14 10:34:04 +0200 |
commit | 979ec02c9a1019152be08705986337e470eabb57 (patch) | |
tree | 6021179ad8649112717a499780f475275af487f2 /src/api/apiBase.ts | |
parent | refactor: defensive programming to avoid javascript error for unread badges (diff) | |
download | ferdium-app-979ec02c9a1019152be08705986337e470eabb57.tar.gz ferdium-app-979ec02c9a1019152be08705986337e470eabb57.tar.zst ferdium-app-979ec02c9a1019152be08705986337e470eabb57.zip |
chore: codebase improvements (#1930)
Diffstat (limited to 'src/api/apiBase.ts')
-rw-r--r-- | src/api/apiBase.ts | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/api/apiBase.ts b/src/api/apiBase.ts new file mode 100644 index 000000000..dc10fad91 --- /dev/null +++ b/src/api/apiBase.ts | |||
@@ -0,0 +1,46 @@ | |||
1 | /** | ||
2 | * Get API base URL from store | ||
3 | */ | ||
4 | import { API_VERSION } from '../environment'; | ||
5 | import { | ||
6 | DEV_API_FRANZ_WEBSITE, | ||
7 | LIVE_FRANZ_API, | ||
8 | LOCAL_HOSTNAME, | ||
9 | LOCAL_SERVER, | ||
10 | SERVER_NOT_LOADED, | ||
11 | } from '../config'; | ||
12 | |||
13 | // Note: This cannot be used from the internal-server since we are not running within the context of a browser window | ||
14 | const apiBase = (withVersion = true) => { | ||
15 | let url: string; | ||
16 | |||
17 | if ( | ||
18 | !(window as any).ferdi || | ||
19 | !(window as any).ferdi.stores.settings || | ||
20 | !(window as any).ferdi.stores.settings.all || | ||
21 | !(window as any).ferdi.stores.settings.all.app.server | ||
22 | ) { | ||
23 | // Stores have not yet been loaded - return SERVER_NOT_LOADED to force a retry when stores are loaded | ||
24 | return SERVER_NOT_LOADED; | ||
25 | } | ||
26 | if ((window as any).ferdi.stores.settings.all.app.server === LOCAL_SERVER) { | ||
27 | // Use URL for local server | ||
28 | url = `http://${LOCAL_HOSTNAME}:${ | ||
29 | (window as any).ferdi.stores.requests.localServerPort | ||
30 | }`; | ||
31 | } else { | ||
32 | // Load URL from store | ||
33 | url = (window as any).ferdi.stores.settings.all.app.server; | ||
34 | } | ||
35 | |||
36 | return withVersion ? `${url}/${API_VERSION}` : url; | ||
37 | }; | ||
38 | |||
39 | export default apiBase; | ||
40 | |||
41 | export function termsBase() { | ||
42 | // TODO: This needs to handle local vs ferdi vs franz servers | ||
43 | return (window as any).ferdi.stores.settings.all.app.server !== LIVE_FRANZ_API | ||
44 | ? (window as any).ferdi.stores.settings.all.app.server | ||
45 | : DEV_API_FRANZ_WEBSITE; | ||
46 | } | ||