UNPKG

@rxap/config

Version:

Provides a configuration service for Angular applications, allowing you to load and manage application settings from various sources such as static files, URLs, local storage, and URL parameters. It supports schema validation and provides utilities for ac

79 lines (78 loc) 3.18 kB
import { Environment } from '@rxap/environment'; import { ReplaySubject } from 'rxjs'; import { NoInferType } from './types'; import * as i0 from "@angular/core"; export type AnySchema = { validateAsync: (...args: any[]) => any; }; export interface ConfigLoadOptions { fromUrlParam?: string | boolean; fromLocalStorage?: boolean; schema?: AnySchema; url?: string | string[] | ((environment: Environment) => string | string[]); /** * static config values */ static?: Record<string, any>; /** * Load additional configuration based on a CID found in a DNS TXT record. * If true, uses `location.hostname`. If a string, uses that domain. */ fromDns?: string | boolean; /** * Load additional configuration directly from the given Content Identifier (CID). * This takes precedence over `fromDns` if both are provided. */ fromCid?: string; } export declare class ConfigService<Config extends Record<string, any> = Record<string, any>> { static onError: ReplaySubject<unknown>; static onRequestError: ReplaySubject<Response>; static onErrorFnc: Array<(error: any) => void>; static onRequestErrorFnc: Array<(response: Response) => void>; static Config: any; /** * Static default values for the config object. * Will be overwritten by an dynamic config file specified in * the Urls array. */ static Defaults: any; /** * Any value definition in the Overwrites object will overwrite any * value form the Defaults values or dynamic config files */ static Overwrites: any; static LocalStorageKey: string; /** * @deprecated instead use the url property of the ConfigLoadOptions */ static Urls: never[]; readonly config: Config; constructor(config?: any | null); /** * Used to load the app config from a remote resource. * * Promise.all([ ConfigService.Load() ]) * .then(() => platformBrowserDynamic().bootstrapModule(AppModule)) * .catch(err => console.error(err)) * */ static Load(options?: ConfigLoadOptions, environment?: Environment): Promise<void>; private static loadConfigFromCid; private static loadConfigFromDns; private static handleError; private static handleRequestError; private static loadConfig; private static LoadConfigDefaultFromUrlParam; static SideLoad(url: string, propertyPath: string, required?: boolean, schema?: AnySchema): Promise<void>; static Get<T = any, K extends Record<string, any> = Record<string, any>>(path: string, defaultValue: T | undefined, config: Record<string, any>): T; private static showError; setLocalConfig(config: Config): void; clearLocalConfig(): void; get<T = any>(propertyPath: string): T | undefined; get<T = any>(propertyPath: string, defaultValue: NoInferType<T>): T; getOrThrow<T = any>(propertyPath: string): T; getOrThrow<T = any>(propertyPath: string, defaultValue: NoInferType<T>): T; static ɵfac: i0.ɵɵFactoryDeclaration<ConfigService<any>, [{ optional: true; }]>; static ɵprov: i0.ɵɵInjectableDeclaration<ConfigService<any>>; }