refs #13989 Merge branch 'origin/13989-webdav-service'
[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<Config>(CONFIG_URL + "?nocache=" + (new Date()).getTime())
17         .then(response => response.data)
18         .catch(() => Promise.resolve(getDefaultConfig()))
19         .then(mapConfig);
20 };
21
22 const mapConfig = (config: Config): Config => ({
23     ...config,
24     apiHost: addProtocol(config.apiHost),
25     keepWebHost: addProtocol(config.keepWebHost)
26 });
27
28 const getDefaultConfig = (): Config => ({
29     apiHost: process.env.REACT_APP_ARVADOS_API_HOST || "",
30     keepWebHost: process.env.REACT_APP_ARVADOS_KEEP_WEB_HOST || ""
31 });
32
33 const addProtocol = (url: string) => `${window.location.protocol}//${url}`;