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