Merge branch 'master' into 14078-obtain-configuration-data-from-discovery-endpoint
[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     auth: {};
11     basePath: string;
12     baseUrl: string;
13     batchPath: string;
14     blobSignatureTtl: number;
15     crunchLimitLogBytesPerJob: number;
16     crunchLogBytesPerEvent: number;
17     crunchLogPartialLineThrottlePeriod: number;
18     crunchLogSecondsBetweenEvents: number;
19     crunchLogThrottleBytes: number;
20     crunchLogThrottleLines: number;
21     crunchLogThrottlePeriod: number;
22     defaultCollectionReplication: number;
23     defaultTrashLifetime: number;
24     description: string;
25     discoveryVersion: string;
26     dockerImageFormats: string[];
27     documentationLink: string;
28     generatedAt: string;
29     gitUrl: string;
30     id: string;
31     keepWebServiceUrl: string;
32     kind: string;
33     maxRequestSize: number;
34     name: string;
35     packageVersion: string;
36     parameters: {};
37     protocol: string;
38     remoteHosts: string;
39     remoteHostsViaDNS: boolean;
40     resources: {};
41     revision: string;
42     rootUrl: string;
43     schemas: {};
44     servicePath: string;
45     sourceVersion: string;
46     source_version: string;
47     title: string;
48     uuidPrefix: string;
49     version: string;
50     websocketUrl: string;
51     workbenchUrl: string;
52 }
53
54 export const fetchConfig = () => {
55     return Axios
56         .get<ConfigJSON>(CONFIG_URL + "?nocache=" + (new Date()).getTime())
57         .then(response => response.data)
58         .catch(() => Promise.resolve(getDefaultConfig()))
59         .then(config => Axios.get<Config>(getDiscoveryURL(config.API_HOST)))
60         .then(response => response.data);
61 };
62
63 export const mockConfig = (config: Partial<Config>): Config => ({
64     auth: {},
65     basePath: '',
66     baseUrl: '',
67     batchPath: '',
68     blobSignatureTtl: 0,
69     crunchLimitLogBytesPerJob: 0,
70     crunchLogBytesPerEvent: 0,
71     crunchLogPartialLineThrottlePeriod: 0,
72     crunchLogSecondsBetweenEvents: 0,
73     crunchLogThrottleBytes: 0,
74     crunchLogThrottleLines: 0,
75     crunchLogThrottlePeriod: 0,
76     defaultCollectionReplication: 0,
77     defaultTrashLifetime: 0,
78     description: '',
79     discoveryVersion: '',
80     dockerImageFormats: [],
81     documentationLink: '',
82     generatedAt: '',
83     gitUrl: '',
84     id: '',
85     keepWebServiceUrl: '',
86     kind: '',
87     maxRequestSize: 0,
88     name: '',
89     packageVersion: '',
90     parameters: {},
91     protocol: '',
92     remoteHosts: '',
93     remoteHostsViaDNS: false,
94     resources: {},
95     revision: '',
96     rootUrl: '',
97     schemas: {},
98     servicePath: '',
99     sourceVersion: '',
100     source_version: '',
101     title: '',
102     uuidPrefix: '',
103     version: '',
104     websocketUrl: '',
105     workbenchUrl: '',
106     ...config
107 });
108
109 interface ConfigJSON {
110     API_HOST: string;
111 }
112
113 const getDefaultConfig = (): ConfigJSON => ({
114     API_HOST: process.env.REACT_APP_ARVADOS_API_HOST || "",
115 });
116
117 const getDiscoveryURL = (apiHost: string) => `${window.location.protocol}//${apiHost}/discovery/v1/apis/arvados/v1/rest`;