Merge branch 'origin/master' into 14478-log-in-into-clusters
[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: {
39         [key: string]: string
40     };
41     remoteHostsViaDNS: boolean;
42     resources: {};
43     revision: string;
44     rootUrl: string;
45     schemas: {};
46     servicePath: string;
47     sourceVersion: string;
48     source_version: string;
49     title: string;
50     uuidPrefix: string;
51     version: string;
52     websocketUrl: string;
53     workbenchUrl: string;
54     vocabularyUrl: string;
55     fileViewersConfigUrl: string;
56 }
57
58 export const fetchConfig = () => {
59     return Axios
60         .get<ConfigJSON>(CONFIG_URL + "?nocache=" + (new Date()).getTime())
61         .then(response => response.data)
62         .catch(() => Promise.resolve(getDefaultConfig()))
63         .then(config => Axios
64             .get<Config>(getDiscoveryURL(config.API_HOST))
65             .then(response => ({
66                 // TODO: After tests delete `|| '/vocabulary-example.json'`
67                 // TODO: After tests delete `|| '/file-viewers-example.json'`
68                 config: {
69                     ...response.data,
70                     vocabularyUrl: config.VOCABULARY_URL || '/vocabulary-example.json',
71                     fileViewersConfigUrl: config.FILE_VIEWERS_CONFIG_URL || '/file-viewers-example.json'
72                 },
73                 apiHost: config.API_HOST,
74             })));
75
76 };
77
78 export const mockConfig = (config: Partial<Config>): Config => ({
79     auth: {},
80     basePath: '',
81     baseUrl: '',
82     batchPath: '',
83     blobSignatureTtl: 0,
84     crunchLimitLogBytesPerJob: 0,
85     crunchLogBytesPerEvent: 0,
86     crunchLogPartialLineThrottlePeriod: 0,
87     crunchLogSecondsBetweenEvents: 0,
88     crunchLogThrottleBytes: 0,
89     crunchLogThrottleLines: 0,
90     crunchLogThrottlePeriod: 0,
91     defaultCollectionReplication: 0,
92     defaultTrashLifetime: 0,
93     description: '',
94     discoveryVersion: '',
95     dockerImageFormats: [],
96     documentationLink: '',
97     generatedAt: '',
98     gitUrl: '',
99     id: '',
100     keepWebServiceUrl: '',
101     kind: '',
102     maxRequestSize: 0,
103     name: '',
104     packageVersion: '',
105     parameters: {},
106     protocol: '',
107     remoteHosts: {},
108     remoteHostsViaDNS: false,
109     resources: {},
110     revision: '',
111     rootUrl: '',
112     schemas: {},
113     servicePath: '',
114     sourceVersion: '',
115     source_version: '',
116     title: '',
117     uuidPrefix: '',
118     version: '',
119     websocketUrl: '',
120     workbenchUrl: '',
121     vocabularyUrl: '',
122     fileViewersConfigUrl: '',
123     ...config
124 });
125
126 interface ConfigJSON {
127     API_HOST: string;
128     VOCABULARY_URL: string;
129     FILE_VIEWERS_CONFIG_URL: string;
130 }
131
132 const getDefaultConfig = (): ConfigJSON => ({
133     API_HOST: process.env.REACT_APP_ARVADOS_API_HOST || "",
134     VOCABULARY_URL: "",
135     FILE_VIEWERS_CONFIG_URL: "",
136 });
137
138 export const DISCOVERY_URL = 'discovery/v1/apis/arvados/v1/rest';
139 const getDiscoveryURL = (apiHost: string) => `${window.location.protocol}//${apiHost}/${DISCOVERY_URL}`;