Skip to content

Commit b79da7e

Browse files
committed
Make config static on build
1 parent 0b2365b commit b79da7e

File tree

7 files changed

+39
-112
lines changed

7 files changed

+39
-112
lines changed

.env

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
INLINE_RUNTIME_CHUNK=false
22
REACT_APP_URL=http://localhost:8080
33
REACT_APP_LOGINSERVICE_URL=https://loginservice.dev.nav.no/login
4-
REACT_API_URL=https://klage-dittnav-api.dev.nav.no
4+
REACT_APP_API_URL=https://klage-dittnav-api.dev.nav.no
55
REACT_APP_MOCK_DATA=false
66
FRONTENDLOGGER_BASE_URL=""

docker-compose.backend.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ services:
1010
INLINE_RUNTIME_CHUNK: 'false'
1111
REACT_APP_URL: http://localhost:8080
1212
REACT_APP_LOGINSERVICE_URL: https://loginservice.dev.nav.no/login
13-
REACT_API_URL: https://klage-dittnav-api.dev.nav.no
13+
REACT_APP_API_URL: https://klage-dittnav-api.dev.nav.no

nais/dev.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"externals": ["dekoratoren.dev.nav.no"],
44
"env": {
55
"REACT_APP_URL": "https://klage-dittnav.dev.nav.no",
6-
"REACT_API_URL": "https://klage-dittnav-api.dev.nav.no",
6+
"REACT_APP_API_URL": "https://klage-dittnav-api.dev.nav.no",
77
"REACT_APP_LOGINSERVICE_URL": "https://loginservice.dev.nav.no/login",
88
"FRONTENDLOGGER_BASE_URL": "https://www-q0.nav.no/frontendlogger"
99
}

nais/prod.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"externals": ["www.nav.no"],
44
"env": {
55
"REACT_APP_URL": "https://klage-dittnav.nav.no",
6-
"REACT_API_URL": "https://klage-dittnav-api.nav.no",
6+
"REACT_APP_API_URL": "https://klage-dittnav-api.nav.no",
77
"REACT_APP_LOGINSERVICE_URL": "https://loginservice.nav.no/login",
88
"FRONTENDLOGGER_BASE_URL": "https://www.nav.no/frontendlogger"
99
}

src/environment/environment-loader.tsx

-41
This file was deleted.

src/environment/environment.ts

+20-49
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,33 @@
1-
import { getJSON } from '../api/fetch';
2-
3-
interface InboundEnvironment {
4-
readonly appUrl: string;
5-
readonly loginserviceUrl: string;
6-
readonly apiUrl: string;
7-
}
8-
91
type KlageId = string | number;
102

113
export const LOGGED_IN_PATH = '/loggedin-redirect';
124

13-
export class NotInitializedError extends Error {
14-
constructor() {
15-
super('Environment was accessed before it has been initialized.');
5+
export class EnvironmentInitError extends Error {
6+
constructor(appUrl: string | undefined, apiUrl: string | undefined, loginUrl: string | undefined) {
7+
super(`Environment failed to initialize. App URL: "${appUrl}", API URL: "${apiUrl}", login URL: "${loginUrl}"`);
168
}
179
}
1810

1911
export class Environment {
20-
private initialized = false;
21-
private _appUrl: string | null = null;
22-
private _apiUrl: string | null = null;
23-
private _loginserviceUrl: string | null = null;
24-
25-
isInitialized = () => this.initialized;
26-
27-
async init() {
28-
if (this.initialized) {
29-
return this;
30-
}
12+
public appUrl: string;
13+
public apiUrl: string;
14+
public loginServiceUrl: string;
3115

32-
const { appUrl, apiUrl, loginserviceUrl } = await getJSON<InboundEnvironment>(
33-
'/config',
34-
'Fant ikke konfigurasjonsendepunktet.'
35-
);
36-
this._apiUrl = apiUrl;
37-
this._appUrl = appUrl;
38-
this._loginserviceUrl = loginserviceUrl;
39-
this.initialized = true;
40-
return this;
41-
}
42-
43-
get apiUrl() {
44-
if (this.initialized && this._apiUrl !== null) {
45-
return this._apiUrl;
46-
}
47-
throw new NotInitializedError();
48-
}
49-
get appUrl() {
50-
if (this.initialized && this._appUrl !== null) {
51-
return this._appUrl;
52-
}
53-
throw new NotInitializedError();
54-
}
55-
get loginServiceUrl() {
56-
if (this.initialized && this._loginserviceUrl !== null) {
57-
return this._loginserviceUrl;
16+
constructor() {
17+
if (
18+
typeof process.env.REACT_APP_URL === 'undefined' ||
19+
typeof process.env.REACT_APP_API_URL === 'undefined' ||
20+
typeof process.env.REACT_APP_LOGINSERVICE_URL === 'undefined'
21+
) {
22+
throw new EnvironmentInitError(
23+
process.env.REACT_APP_URL,
24+
process.env.REACT_APP_API_URL,
25+
process.env.REACT_APP_LOGINSERVICE_URL
26+
);
5827
}
59-
throw new NotInitializedError();
28+
this.appUrl = process.env.REACT_APP_URL;
29+
this.apiUrl = process.env.REACT_APP_API_URL;
30+
this.loginServiceUrl = process.env.REACT_APP_LOGINSERVICE_URL;
6031
}
6132

6233
get loginUrl() {

src/routes/routes.tsx

+15-18
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import KlageLoader from '../klage/klage-loader';
1010
import Oppsummering from './klageskjema/oppsummering';
1111
import KvitteringPage from './klageskjema/kvittering/kvittering-page';
1212
import { INNGANG_KATEGORIER, Kategori } from '../kategorier/kategorier';
13-
import EnvironmentLoader from '../environment/environment-loader';
1413
import AppContextComponenet from '../app-context/app-context';
1514
import { MainContainer } from '../styled-components/common';
1615
import { Klage } from '../klage/klage';
@@ -27,23 +26,21 @@ const App = () => (
2726
<AppContextComponenet>
2827
<MainContainer>
2928
<ErrorBoundary>
30-
<EnvironmentLoader>
31-
<Switch>
32-
{innsendingsRoutes}
33-
{kategoriRoutes}
34-
<Route path={'/ny'} exact>
35-
<UserLoader>
36-
<CreateKlage />
37-
</UserLoader>
38-
</Route>
39-
<Route path={LOGGED_IN_PATH} render={loggedInRedirect} exact />
40-
<Route path={'/:klageId/begrunnelse'} render={begrunnelse} exact />
41-
<Route path={'/:klageId/oppsummering'} render={oppsummering} exact />
42-
<Route path={'/:klageId/kvittering'} render={kvittering} exact />
43-
<Route path={'/'} component={RootWithQuery} exact />
44-
<Route component={NotFoundPage} />
45-
</Switch>
46-
</EnvironmentLoader>
29+
<Switch>
30+
{innsendingsRoutes}
31+
{kategoriRoutes}
32+
<Route path={'/ny'} exact>
33+
<UserLoader>
34+
<CreateKlage />
35+
</UserLoader>
36+
</Route>
37+
<Route path={LOGGED_IN_PATH} render={loggedInRedirect} exact />
38+
<Route path={'/:klageId/begrunnelse'} render={begrunnelse} exact />
39+
<Route path={'/:klageId/oppsummering'} render={oppsummering} exact />
40+
<Route path={'/:klageId/kvittering'} render={kvittering} exact />
41+
<Route path={'/'} component={RootWithQuery} exact />
42+
<Route component={NotFoundPage} />
43+
</Switch>
4744
</ErrorBoundary>
4845
</MainContainer>
4946
</AppContextComponenet>

0 commit comments

Comments
 (0)