Merge branch 'master' into 13883-arrow-animation-is-not-working-after-loading
[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 "../../node_modules/axios";
6
7 export const CONFIG_URL = process.env.REACT_APP_ARVADOS_CONFIG_URL || "/config.json";
8
9 export interface Config {
10     API_HOST: string;
11 }
12
13 export const fetchConfig = () => {
14     return Axios
15         .get<Config>(CONFIG_URL + "?nocache=" + (new Date()).getTime())
16         .then(response => response.data)
17         .catch(() => Promise.resolve(getDefaultConfig()))
18         .then(mapConfig);
19 };
20
21 const mapConfig = (config: Config): Config => ({
22     ...config,
23     API_HOST: addProtocol(config.API_HOST)
24 });
25
26 const getDefaultConfig = (): Config => ({
27     API_HOST: process.env.REACT_APP_ARVADOS_API_HOST || ""
28 });
29
30 const addProtocol = (url: string) => `${window.location.protocol}//${url}`;