@@ -7,11 +7,17 @@ type KlageId = string | number;
7
7
export const LOGGED_IN_PATH = '/loggedin-redirect' ;
8
8
9
9
export class EnvironmentInitError extends Error {
10
- constructor ( appUrl ? : string , apiUrl ?: string , loginUrl ? : string ) {
11
- super ( `Environment failed to initialize. App URL: "${ appUrl } ", API URL : "${ apiUrl } ", login URL: " ${ loginUrl } "` ) ;
10
+ constructor ( env : string | null = null , variabels : string | null = null ) {
11
+ super ( `Environment failed to initialize for environment "${ env } ". Variables : "${ variabels } "` ) ;
12
12
}
13
13
}
14
14
15
+ export enum EnvString {
16
+ PROD = 'prod-gcp' ,
17
+ DEV = 'dev-gcp' ,
18
+ LOCAL = 'local'
19
+ }
20
+
15
21
interface Env {
16
22
REACT_APP_URL ?: string ;
17
23
REACT_APP_API_URL ?: string ;
@@ -22,56 +28,70 @@ export class Environment {
22
28
public appUrl : string ;
23
29
public apiUrl : string ;
24
30
public loginServiceUrl : string ;
31
+ public environment : EnvString ;
25
32
26
33
constructor ( ) {
27
- const { appUrl, apiUrl, loginUrl } = this . getEnvironment ( ) ;
28
- this . appUrl = appUrl ;
29
- this . apiUrl = apiUrl ;
30
- this . loginServiceUrl = loginUrl ;
31
- }
34
+ const environmentElement = document . getElementById ( 'environment' ) ;
35
+ if ( environmentElement === null ) {
36
+ if (
37
+ typeof process . env . REACT_APP_API_URL !== 'string' ||
38
+ typeof process . env . REACT_APP_LOGINSERVICE_URL !== 'string'
39
+ ) {
40
+ throw new EnvironmentInitError ( ) ;
41
+ }
42
+ this . appUrl = `${ window . location . protocol } //${ window . location . host } ` ;
43
+ this . apiUrl = process . env . REACT_APP_API_URL ;
44
+ this . loginServiceUrl = process . env . REACT_APP_LOGINSERVICE_URL ;
45
+ this . environment = EnvString . LOCAL ;
46
+ return ;
47
+ }
48
+
49
+ const environment = this . getEnvironment ( environmentElement ) ;
32
50
33
- private getEnvironment ( ) {
34
- const realEnv = this . parseJsonEnvironment ( ) ;
35
- if ( realEnv ! == null ) {
36
- return realEnv ;
51
+ const jsonText = environmentElement . textContent ;
52
+ const variables = this . parseJsonEnvironment ( jsonText ) ;
53
+ if ( variables = == null ) {
54
+ throw new EnvironmentInitError ( environment , jsonText ) ;
37
55
}
38
- const appUrl = `${ window . location . protocol } //${ window . location . host } ` ;
39
- const devEnv = this . ensureEnvironment (
40
- appUrl ,
41
- process . env . REACT_APP_API_URL ,
42
- process . env . REACT_APP_LOGINSERVICE_URL
43
- ) ;
44
- if ( devEnv !== null ) {
45
- return devEnv ;
56
+
57
+ if ( environment === EnvString . LOCAL ) {
58
+ variables . REACT_APP_URL = `${ window . location . protocol } //${ window . location . host } ` ;
46
59
}
47
- throw new EnvironmentInitError ( appUrl , process . env . REACT_APP_API_URL , process . env . REACT_APP_LOGINSERVICE_URL ) ;
60
+
61
+ if (
62
+ typeof variables . REACT_APP_URL !== 'string' ||
63
+ typeof variables . REACT_APP_API_URL !== 'string' ||
64
+ typeof variables . REACT_APP_LOGINSERVICE_URL !== 'string'
65
+ ) {
66
+ throw new EnvironmentInitError ( environment , jsonText ) ;
67
+ }
68
+ this . appUrl = variables . REACT_APP_URL ;
69
+ this . apiUrl = variables . REACT_APP_API_URL ;
70
+ this . loginServiceUrl = variables . REACT_APP_LOGINSERVICE_URL ;
71
+ this . environment = environment ;
48
72
}
49
73
50
- private parseJsonEnvironment ( ) {
51
- const environmentElement = document . getElementById ( ' environment') ;
52
- if ( environmentElement === null ) {
53
- return null ;
74
+ private getEnvironment ( environmentElement : HTMLElement ) {
75
+ const env = environmentElement . getAttribute ( 'data- environment') ;
76
+ if ( env === EnvString . PROD || env === EnvString . DEV || env === EnvString . LOCAL ) {
77
+ return env ;
54
78
}
55
- const jsonText = environmentElement . textContent ;
79
+ return EnvString . LOCAL ;
80
+ }
81
+
82
+ private parseJsonEnvironment ( jsonText : string | null ) {
56
83
if ( jsonText === null || jsonText . length <= 10 ) {
57
84
return null ;
58
85
}
59
86
try {
60
87
const json : Env = JSON . parse ( jsonText ) ;
61
- return this . ensureEnvironment ( json . REACT_APP_URL , json . REACT_APP_API_URL , json . REACT_APP_LOGINSERVICE_URL ) ;
88
+ return json ;
62
89
} catch ( err ) {
63
90
logError ( err , `Failed to parse environment JSON: ${ jsonText } ` ) ;
64
91
return null ;
65
92
}
66
93
}
67
94
68
- private ensureEnvironment ( appUrl ?: string , apiUrl ?: string , loginUrl ?: string ) {
69
- if ( typeof appUrl === 'undefined' || typeof apiUrl === 'undefined' || typeof loginUrl === 'undefined' ) {
70
- return null ;
71
- }
72
- return { appUrl, apiUrl, loginUrl } ;
73
- }
74
-
75
95
get loginUrl ( ) {
76
96
return `${ this . loginServiceUrl } ?redirect=${ this . appUrl } ${ LOGGED_IN_PATH } ` ;
77
97
}
0 commit comments