-
Notifications
You must be signed in to change notification settings - Fork 57
/
envUtil.ts
102 lines (86 loc) · 3.44 KB
/
envUtil.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { useSelector } from 'react-redux';
import { RootState } from 'store/store';
/**
* @param url or endpoint to clean
* @returns a `string` with no whitespaces, leading or trailing slashes
*/
export function cleanURL(url: string): string {
return url?.trim().replace(/^\/|\/$/g, ''); // Remove leading and trailing slashes
}
/**
* Injects the `username` into the `baseURL` and `endpoint` to create a link.
* @param baseURL Example `https://foo.com` Any leading or trailing slashes will be removed.
* @param endpoint (optional). Example `bar` Any leading or trailing slashes will be removed.
* @returns a complete URL: `baseUrl` / `username` / `endpoint`
*/
const useUserLink = (baseURL: string, endpoint?: string): string => {
const username = useSelector((state: RootState) => state.auth).userName;
const cleanBaseURL = cleanURL(baseURL);
const cleanEndpoint = cleanURL(endpoint ?? '');
return `${cleanBaseURL}/${username}/${cleanEndpoint}`;
};
export function useURLforDT(): string {
return useUserLink(useAppURL(), window.env.REACT_APP_URL_DTLINK);
}
export function useURLbasename(): string {
return cleanURL(window.env.REACT_APP_URL_BASENAME);
}
export function useURLforLIB(): string {
return useUserLink(useAppURL(), window.env.REACT_APP_URL_LIBLINK);
}
export function useAppURL(): string {
return `${cleanURL(window.env.REACT_APP_URL)}/${useURLbasename()}`;
}
export interface KeyLinkPair {
key: string;
link: string;
}
/**
* @returns an array of `KeyLinkPair` objects, where each object contains a `key` and a `link`.
*
* The `key` is the `key` of the environment variable, with the prefix *"REACT_APP_WORKBENCHLINK_"* removed.
* For example, if the `key` of the environment variable is *"REACT_APP_WORKBENCHLINK_MYWORKBENCH"*, then the `key` will be *"MYWORKBENCH"*.
*
* The `link` is constructed by appending the `username` to the end of the *REACT_APP_URL_WORKBENCH*, and then appending the value of the environment variable to the end of that.
* For example, if the *REACT_APP_URL_WORKBENCH* is https://foo.com, the `username` is *"user1"*, and the value of the environment variable is "/my-workbench", then the link will be https://foo.com/user1/my-workbench.
*/
export function getWorkbenchLinkValues(): KeyLinkPair[] {
const prefix = 'REACT_APP_WORKBENCHLINK_';
const workbenchLinkValues: KeyLinkPair[] = [];
Object.keys(window.env)
.filter((key) => key.startsWith(prefix))
.forEach((key) => {
const value = window.env[key];
if (value !== undefined) {
const keyWithoutPrefix = key.slice(prefix.length);
const linkValue =
keyWithoutPrefix === 'DT_PREVIEW' ||
keyWithoutPrefix === 'LIBRARY_PREVIEW'
? value
: useUserLink(useAppURL(), value);
workbenchLinkValues.push({
key: keyWithoutPrefix,
link: linkValue,
});
}
});
return workbenchLinkValues;
}
export function useGetDTPagePreviewLink(): string {
return useUserLink(useAppURL(), 'preview/digitaltwins');
}
export function getClientID(): string {
return window.env.REACT_APP_CLIENT_ID;
}
export function getAuthority(): string {
return window.env.REACT_APP_AUTH_AUTHORITY;
}
export function getRedirectURI(): string {
return window.env.REACT_APP_REDIRECT_URI;
}
export function getLogoutRedirectURI(): string {
return window.env.REACT_APP_LOGOUT_REDIRECT_URI;
}
export function getGitLabScopes(): string {
return window.env.REACT_APP_GITLAB_SCOPES;
}