Merge branch 'master' into 13833-edit-project
[arvados-workbench2.git] / src / common / config.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import Axios from "axios";
6
7 export const CONFIG_URL = process.env.REACT_APP_ARVADOS_CONFIG_URL || "/config.json";
8
9 export interface Config {
10     apiHost: string;
11     keepWebHost: string;
12 }
13
14 export const fetchConfig = () => {
15     return Axios
16         .get<ConfigJSON>(CONFIG_URL + "?nocache=" + (new Date()).getTime())
17         .then(response => response.data)
18         .catch(() => Promise.resolve(getDefaultConfig()))
19         .then(mapConfig);
20 };
21
22 interface ConfigJSON {
23     API_HOST: string;
24     KEEP_WEB_HOST: string;
25 }
26
27 const mapConfig = (config: ConfigJSON): Config => ({
28     apiHost: addProtocol(config.API_HOST),
29     keepWebHost: addProtocol(config.KEEP_WEB_HOST)
30 });
31
32 const getDefaultConfig = (): ConfigJSON => ({
33     API_HOST: process.env.REACT_APP_ARVADOS_API_HOST || "",
34     KEEP_WEB_HOST: process.env.REACT_APP_ARVADOS_KEEP_WEB_HOST || ""
35 });
36
37 const addProtocol = (url: string) => `${window.location.protocol}//${url}`;