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