@igo2/core
Version:
174 lines (166 loc) • 6.35 kB
JavaScript
import * as i0 from '@angular/core';
import { inject, Injectable, InjectionToken, makeEnvironmentProviders, provideAppInitializer, NgModule } from '@angular/core';
import { HttpBackend, HttpClient } from '@angular/common/http';
import { ObjectUtils } from '@igo2/utils';
import { BehaviorSubject, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
const CONFIG_DEPRECATED = {
showMenuButton: {
alternativeKey: 'menu.button.visible',
mayBeRemoveIn: new Date('2024-06-06')
},
menuButtonReverseColor: {
alternativeKey: 'menu.button.useThemeColor',
mayBeRemoveIn: new Date('2024-06-06')
},
importWithStyle: {
alternativeKey: 'importExport.importWithStyle',
mayBeRemoveIn: new Date('2024-06-06')
},
hasGeolocateButton: {
alternativeKey: 'geolocate.button.visible',
mayBeRemoveIn: new Date('2024-06-06')
}
};
const ALTERNATE_CONFIG_FROM_DEPRECATION = new Map(Object.entries(CONFIG_DEPRECATED)
.filter(([_, options]) => options.alternativeKey)
.map(([key, options]) => [
options.alternativeKey,
{
deprecatedKey: key
}
]));
const version = {
lib: '20.1.1',
releaseDate: 1779216550996
};
class ConfigService {
config;
httpClient;
configDeprecated = new Map(Object.entries(CONFIG_DEPRECATED));
_isLoaded$ = new BehaviorSubject(null);
isLoaded$ = this._isLoaded$.asObservable();
constructor() {
const handler = inject(HttpBackend);
this.httpClient = new HttpClient(handler);
}
/**
* Use to get the all config file (merge from environnement.ts and config.json)
*/
getConfigs() {
Array.from(this.configDeprecated.keys()).map((deprecatedKey) => {
const deprecatedValue = ObjectUtils.resolve(this.config, deprecatedKey);
if (deprecatedValue !== undefined) {
this.handleDeprecatedConfig(deprecatedKey);
}
});
return this.config;
}
/**
* Use to get the data found in config file
*/
getConfig(key, defaultValue) {
let value = ObjectUtils.resolve(this.config, key);
const isDeprecated = this.configDeprecated.get(key);
if (isDeprecated && value !== undefined) {
this.handleDeprecatedConfig(key);
}
else if (value === undefined) {
value = this.handleDeprecationPossibility(key);
}
return value ?? defaultValue;
}
handleDeprecatedConfig(key) {
const options = this.configDeprecated.get(key);
let message = `This config (${key}) is deprecated and will be removed shortly`;
if (options.alternativeKey) {
message += ` You should use this key (${options.alternativeKey}) as an alternate solution`;
}
const currentDate = new Date();
currentDate >= options.mayBeRemoveIn
? console.error(message)
: console.warn(message);
}
handleDeprecationPossibility(key) {
const options = ALTERNATE_CONFIG_FROM_DEPRECATION.get(key);
if (!options) {
return;
}
return this.getConfig(options.deprecatedKey);
}
/**
* This method loads "[path]" to get all config's variables
*/
load(options) {
const baseConfig = options.default;
if (!options.path) {
this.config = baseConfig;
this._isLoaded$.next(true);
return;
}
return new Promise((resolve) => {
this.httpClient
.get(options.path)
.pipe(catchError((error) => {
console.log(`Configuration file ${options.path} could not be read`);
this._isLoaded$.next(false);
resolve(true);
return throwError(error.error || 'Server error');
}))
.subscribe((configResponse) => {
this.config = ObjectUtils.mergeDeep(ObjectUtils.mergeDeep({ version }, baseConfig), configResponse);
this._isLoaded$.next(true);
resolve(true);
});
});
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: ConfigService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: ConfigService, providedIn: 'root' });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: ConfigService, decorators: [{
type: Injectable,
args: [{
providedIn: 'root'
}]
}], ctorParameters: () => [] });
const CONFIG_OPTIONS = new InjectionToken('configOptions');
function provideConfig(options) {
return makeEnvironmentProviders([
{
provide: CONFIG_OPTIONS,
useValue: options
},
provideAppInitializer(async () => {
const configService = inject(ConfigService);
const options = inject(CONFIG_OPTIONS);
return configService.load(options);
})
]);
}
/**
* @deprecated import the provideConfig directly
*/
class IgoConfigModule {
static forRoot() {
return {
ngModule: IgoConfigModule,
providers: [provideConfig({})]
};
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: IgoConfigModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "20.3.19", ngImport: i0, type: IgoConfigModule });
static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: IgoConfigModule });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: IgoConfigModule, decorators: [{
type: NgModule,
args: [{
imports: [],
declarations: [],
exports: []
}]
}] });
/**
* Generated bundle index. Do not edit.
*/
export { CONFIG_OPTIONS, ConfigService, IgoConfigModule, provideConfig, version };
//# sourceMappingURL=igo2-core-config.mjs.map