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