import { Injectable } from '@angular/core'; import { BaseHttpParams } from '@common/http/base-http-params'; import { InterceptorType } from '@common/http/interceptors/interceptor-type'; import { BaseHttpService } from '@common/base/base-http.service'; import { BaseComponent } from '@common/base/base.component'; import { catchError, takeUntil } from 'rxjs/operators'; import { Observable, throwError } from 'rxjs'; import { HelpService } from '@app/core/model/configuration-models/help-service.model'; import { LoginProviders } from '@app/core/model/configuration-models/login-providers.model'; import { Logging } from '@app/core/model/configuration-models/logging.model'; import { HttpClient } from '@angular/common/http'; @Injectable({ providedIn: 'root', }) export class ConfigurationService extends BaseComponent { constructor(private http: HttpClient) { super(); } private _server: string; get server(): string { return this._server; } private _app: string; get app(): string { return this._app; } private _helpService: HelpService; get helpService(): HelpService { return this._helpService; } private _defaultCulture: string; get defaultCulture(): string { return this._defaultCulture; } private _loginProviders: LoginProviders; get loginProviders(): LoginProviders { return this._loginProviders; } private _logging: Logging; get logging(): Logging { return this._logging; } private _lockInterval: number; get lockInterval(): number { return this._lockInterval; } private _guideAssets: string; get guideAssets():string { return this._guideAssets; } private _allowOrganizationCreator: boolean; get allowOrganizationCreator():boolean { return this._allowOrganizationCreator; } private _doiLink: string; get doiLink(): string { return this._doiLink; } public loadConfiguration(): Promise { return new Promise((r, e) => { // We need to exclude all interceptors here, for the initial configuration request. const params = new BaseHttpParams(); params.interceptorContext = { excludedInterceptors: [InterceptorType.AuthToken, InterceptorType.JSONContentType, InterceptorType.Locale, InterceptorType.ProgressIndication, InterceptorType.RequestTiming, InterceptorType.UnauthorizedResponse] }; this.http.get('./assets/config/config.json', { params: params }).pipe(catchError((err: any, caught: Observable) => throwError(err))) .pipe(takeUntil(this._destroyed)) .subscribe( (content: ConfigurationService) => { this.parseResponse(content); r(this); }, reason => e(reason)); }); } private parseResponse(config: any) { this._server = config.Server; this._app = config.App; this._helpService = HelpService.parseValue(config.HelpService); this._defaultCulture = config.defaultCulture; this._loginProviders = LoginProviders.parseValue(config.loginProviders); this._logging = Logging.parseValue(config.logging); this._lockInterval = config.lockInterval; this._guideAssets = config.guideAssets; this._allowOrganizationCreator = config.allowOrganizationCreator; this._doiLink = config.doiLink; } }