@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
108 lines (107 loc) • 4.29 kB
TypeScript
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 declare enum ConfigLoadingStrategy {
DEFAULT = "default",
FIFO = "fifo"
}
export declare enum ConfigLoadMethod {
FROM_URLS = "fromUrls",
FROM_LOCAL_STORAGE = "fromLocalStorage",
FROM_URL_PARAM = "fromUrlParam",
FROM_CID = "fromCid"
}
export interface ConfigLoadOptions {
fromUrlParam?: string | boolean;
fromUrls?: 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;
fetchCidContent?: (cid: string, path?: string) => Promise<Blob | null>;
ipfsGatewayServers?: Array<(cid: string) => string>;
dnsServers?: string[];
strategy?: ConfigLoadingStrategy;
/**
* Defines the order and subset of config sources to load.
* If not specified, defaults to: [FROM_URLS, FROM_LOCAL_STORAGE, FROM_URL_PARAM, FROM_CID]
*
* Note: static config is always merged first, and Overwrites are always merged last.
* Note: fromDns is not a source itself - it resolves the CID for FROM_CID.
*/
order?: ConfigLoadMethod[];
}
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 isSourceEnabled;
private static loadConfigFromMethod;
private static getMethodName;
private static loadConfigFromUrlParam;
private static loadConfigFromLocalStorage;
private static loadConfigFromUrls;
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>>;
}