UNPKG

runtime-config-loader

Version:

Most applications require certain configuration values that can be changed at runtime of the app. The `environment.ts` files in an Angular application technically work for setting configuration values in an app, but those are buildtime configuration value

109 lines (103 loc) 4.52 kB
import * as i1 from '@angular/common/http'; import { HttpClientModule } from '@angular/common/http'; import * as i0 from '@angular/core'; import { Injectable, Optional, APP_INITIALIZER, NgModule } from '@angular/core'; import { Subject, forkJoin, of } from 'rxjs'; import { tap, catchError, take } from 'rxjs/operators'; class RuntimeConfig { constructor(obj = {}) { this.configUrl = obj.configUrl || './assets/config.json'; } } class RuntimeConfigLoaderService { constructor(_http, config) { this._http = _http; this.configUrl = './assets/config.json'; this.configObject = null; this.configSubject = new Subject(); if (config) { this.configUrl = config.configUrl; } } loadConfig() { const urls = Array.isArray(this.configUrl) ? this.configUrl : [this.configUrl]; const observables = urls.map((url) => this.makeHttpCall(url)); return forkJoin(observables).pipe(tap((configDataArray) => { this.configObject = configDataArray.reduce((acc, configData) => { return { ...acc, ...configData }; }, {}); this.configSubject.next(this.configObject); }), catchError((err) => { console.error('Error loading config: ', err); this.configObject = null; this.configSubject.next(this.configObject); return of(null); })); } makeHttpCall(url) { return this._http.get(url).pipe(take(1)); } getConfig() { return this.configObject; } getConfigObjectKey(key) { return this.configObject ? this.configObject[key] : null; } } RuntimeConfigLoaderService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.0", ngImport: i0, type: RuntimeConfigLoaderService, deps: [{ token: i1.HttpClient }, { token: RuntimeConfig, optional: true }], target: i0.ɵɵFactoryTarget.Injectable }); RuntimeConfigLoaderService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.2.0", ngImport: i0, type: RuntimeConfigLoaderService }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.0", ngImport: i0, type: RuntimeConfigLoaderService, decorators: [{ type: Injectable }], ctorParameters: function () { return [{ type: i1.HttpClient }, { type: RuntimeConfig, decorators: [{ type: Optional }] }]; } }); function initConfig(configSvc) { return () => configSvc.loadConfig(); } class RuntimeConfigLoaderModule { static forRoot(config) { return { ngModule: RuntimeConfigLoaderModule, providers: [ { provide: RuntimeConfig, useValue: config, }, RuntimeConfigLoaderService, ], }; } } RuntimeConfigLoaderModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.0", ngImport: i0, type: RuntimeConfigLoaderModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); RuntimeConfigLoaderModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.2.0", ngImport: i0, type: RuntimeConfigLoaderModule, imports: [HttpClientModule] }); RuntimeConfigLoaderModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.2.0", ngImport: i0, type: RuntimeConfigLoaderModule, providers: [ RuntimeConfigLoaderService, { provide: APP_INITIALIZER, useFactory: initConfig, deps: [RuntimeConfigLoaderService], multi: true, }, ], imports: [HttpClientModule] }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.0", ngImport: i0, type: RuntimeConfigLoaderModule, decorators: [{ type: NgModule, args: [{ imports: [HttpClientModule], providers: [ RuntimeConfigLoaderService, { provide: APP_INITIALIZER, useFactory: initConfig, deps: [RuntimeConfigLoaderService], multi: true, }, ], }] }] }); /** * Generated bundle index. Do not edit. */ export { RuntimeConfig, RuntimeConfigLoaderModule, RuntimeConfigLoaderService, initConfig }; //# sourceMappingURL=runtime-config-loader.mjs.map