Skip to content

Commit 9e903b4

Browse files
authored
Merge pull request #272 from navikt/fix-dev-server
Fix dev server
2 parents fec5d46 + 7f0a52d commit 9e903b4

File tree

1 file changed

+41
-17
lines changed

1 file changed

+41
-17
lines changed

src/environment/environment.ts

+41-17
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,40 @@ interface Env {
2424
REACT_APP_LOGINSERVICE_URL?: string;
2525
}
2626

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 {
2835
public appUrl: string;
2936
public apiUrl: string;
3037
public loginServiceUrl: string;
3138
public environment: EnvString;
3239

3340
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 {
3449
const environmentElement = document.getElementById('environment');
3550
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();
4752
}
4853

4954
const environment = this.getEnvironment(environmentElement);
50-
5155
const jsonText = environmentElement.textContent;
5256
const variables = this.parseJsonEnvironment(jsonText);
5357
if (variables === null) {
58+
if (environment === EnvString.LOCAL) {
59+
return this.getDevServerEnvironment();
60+
}
5461
throw new EnvironmentInitError(environment, jsonText);
5562
}
5663

@@ -65,10 +72,12 @@ export class Environment {
6572
) {
6673
throw new EnvironmentInitError(environment, jsonText);
6774
}
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+
};
7281
}
7382

7483
private getEnvironment(environmentElement: HTMLElement) {
@@ -92,6 +101,21 @@ export class Environment {
92101
}
93102
}
94103

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+
95119
get loginUrl() {
96120
return `${this.loginServiceUrl}?redirect=${this.appUrl}${LOGGED_IN_PATH}`;
97121
}

0 commit comments

Comments
 (0)