import { Inject, Injectable } from '@angular/core'; import { CONFIG_TOKEN, UserService, EuiAppConfig, UserDetails, UserPreferences, I18nService, } from '@eui/core'; import { HttpClient } from '@angular/common/http'; import { Observable, of, zip, catchError } from 'rxjs'; import { switchMap } from 'rxjs/operators'; @Injectable({ providedIn: 'root', }) export class AppStarterService { defaultUserPreferences: UserPreferences; constructor( protected userService: UserService, protected i18nService: I18nService, @Inject(CONFIG_TOKEN) private config: EuiAppConfig, protected http: HttpClient, ) { } start(): Observable { return zip( this.initUserService().pipe( switchMap((userStatus) => { return this.i18nService.init(); }), ), ); } /** * Fetches user details, * create user: UserState object * then initialise to the UserService on run time */ initUserService(): Observable { return zip( this.fetchUserDetails(), ).pipe( switchMap(([userDetails]) => { return this.userService.init(userDetails); }), catchError((err) => { return of(null); }) ); } /** * Fetches user details */ private fetchUserDetails(): Observable { // const url = this.config.modules.your_custom_module.your_custom_endpoint const moduleCoreApi = this.config.modules.core; const url = `${moduleCoreApi.base}${moduleCoreApi.userDetails}`; const user = { userId: 'anonymous' }; if (!url) { return of(user); } return this.http.get(url) .pipe(catchError(err => { return of(null); })); } }