@@ -24,33 +24,40 @@ interface Env {
24
24
REACT_APP_LOGINSERVICE_URL ?: string ;
25
25
}
26
26
27
- export class Environment {
27
+ interface EnvironmentVariables {
28
+ appUrl : string ;
29
+ apiUrl : string ;
30
+ loginServiceUrl : string ;
31
+ environment : EnvString ;
32
+ }
33
+
34
+ export class Environment implements EnvironmentVariables {
28
35
public appUrl : string ;
29
36
public apiUrl : string ;
30
37
public loginServiceUrl : string ;
31
38
public environment : EnvString ;
32
39
33
40
constructor ( ) {
41
+ const { apiUrl, appUrl, loginServiceUrl, environment } = this . init ( ) ;
42
+ this . apiUrl = apiUrl ;
43
+ this . appUrl = appUrl ;
44
+ this . loginServiceUrl = loginServiceUrl ;
45
+ this . environment = environment ;
46
+ }
47
+
48
+ private init ( ) : EnvironmentVariables {
34
49
const environmentElement = document . getElementById ( 'environment' ) ;
35
50
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 ;
51
+ return this . getDevServerEnvironment ( ) ;
47
52
}
48
53
49
54
const environment = this . getEnvironment ( environmentElement ) ;
50
-
51
55
const jsonText = environmentElement . textContent ;
52
56
const variables = this . parseJsonEnvironment ( jsonText ) ;
53
57
if ( variables === null ) {
58
+ if ( environment === EnvString . LOCAL ) {
59
+ return this . getDevServerEnvironment ( ) ;
60
+ }
54
61
throw new EnvironmentInitError ( environment , jsonText ) ;
55
62
}
56
63
@@ -65,10 +72,12 @@ export class Environment {
65
72
) {
66
73
throw new EnvironmentInitError ( environment , jsonText ) ;
67
74
}
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 ;
75
+ return {
76
+ appUrl : variables . REACT_APP_URL ,
77
+ apiUrl : variables . REACT_APP_API_URL ,
78
+ loginServiceUrl : variables . REACT_APP_LOGINSERVICE_URL ,
79
+ environment : environment
80
+ } ;
72
81
}
73
82
74
83
private getEnvironment ( environmentElement : HTMLElement ) {
@@ -92,6 +101,21 @@ export class Environment {
92
101
}
93
102
}
94
103
104
+ private getDevServerEnvironment ( ) : EnvironmentVariables {
105
+ if (
106
+ typeof process . env . REACT_APP_API_URL !== 'string' ||
107
+ typeof process . env . REACT_APP_LOGINSERVICE_URL !== 'string'
108
+ ) {
109
+ throw new EnvironmentInitError ( ) ;
110
+ }
111
+ return {
112
+ appUrl : `${ window . location . protocol } //${ window . location . host } ` ,
113
+ apiUrl : process . env . REACT_APP_API_URL ,
114
+ loginServiceUrl : process . env . REACT_APP_LOGINSERVICE_URL ,
115
+ environment : EnvString . LOCAL
116
+ } ;
117
+ }
118
+
95
119
get loginUrl ( ) {
96
120
return `${ this . loginServiceUrl } ?redirect=${ this . appUrl } ${ LOGGED_IN_PATH } ` ;
97
121
}
0 commit comments