Skip to content

Commit

Permalink
fix(spa): dynamic config not available for app module imports (#655)
Browse files Browse the repository at this point in the history
  • Loading branch information
timonmasberg authored Feb 1, 2024
1 parent 06f1d93 commit 3c29e35
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
12 changes: 1 addition & 11 deletions apps/spa/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { registerLocaleData } from '@angular/common';
import { HttpClient, HttpClientModule } from '@angular/common/http';
import { HttpClientModule } from '@angular/common/http';
import de from '@angular/common/locales/de';
import { APP_INITIALIZER, NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule } from '@angular/router';
import DOMPurify from 'dompurify';
import { NZ_I18N, de_DE } from 'ng-zorro-antd/i18n';
import { firstValueFrom } from 'rxjs';

import { AuthModule, DevAuthModule } from '@kordis/spa/auth';
import {
Expand Down Expand Up @@ -63,15 +62,6 @@ registerLocaleData(de);
},
multi: true,
},
{
provide: APP_INITIALIZER,
useFactory: (http: HttpClient) => async () => {
const config = await firstValueFrom(http.get('./assets/config.json'));
Object.assign(environment, { ...config, ...environment });
},
deps: [HttpClient],
multi: true,
},
{ provide: NZ_I18N, useValue: de_DE },
],
bootstrap: [AppComponent],
Expand Down
11 changes: 11 additions & 0 deletions apps/spa/src/environments/dynamic-config.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { AuthConfig } from 'angular-oauth2-oidc';

export interface DynamicConfig {
environmentName: string;
apiUrl: string;
sentryKey?: string;
oauth?: {
config: AuthConfig;
discoveryDocumentUrl: string;
};
}
13 changes: 2 additions & 11 deletions apps/spa/src/environments/environment.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import { AuthConfig } from 'angular-oauth2-oidc';
import { DynamicConfig } from './dynamic-config.model';

export const environment = {
production: false,

releaseVersion: '0.0.0-development',
} as {
production: boolean;
releaseVersion: string;
// following properties are from the runtime dependent assets/config.json
environmentName: string;
apiUrl: string;
sentryKey?: string;
oauth?: {
config: AuthConfig;
discoveryDocumentUrl: string;
};
};
} & DynamicConfig; // DynamicConfig properties are from the runtime dependent assets/config.json
19 changes: 15 additions & 4 deletions apps/spa/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app/app.module';
import { DynamicConfig } from './environments/dynamic-config.model';
import { environment } from './environments/environment';

platformBrowserDynamic()
.bootstrapModule(AppModule)
// eslint-disable-next-line no-console
.catch((err) => console.error(err));
fetch('./assets/config.json')
.then((response) => response.json())
.then((config: unknown) => {
Object.assign(environment, {
...(config as DynamicConfig),
...environment,
});

platformBrowserDynamic()
.bootstrapModule(AppModule)
// eslint-disable-next-line no-console
.catch((err) => console.error(err));
});

0 comments on commit 3c29e35

Please sign in to comment.