/** * 配置段类型 * @category 配置管理 */ export interface ISectionConfig { /** * RESTful接口协议 */ schema?: string; /** * RESTful接口主机 */ host?: string; /** * RESTful接口端口 */ port?: string | number; /** * 所有RESTful接口path前置路径。例如 /basePath/restful/api */ basePath?: string; /** * 定义RESTful接口列表。key将作为后续取值键名,值为具体的RESTful接口path */ api?: Record; [configKey: string | number]: any; } /** * 含有多个配置段的配置对象类型 */ type IConfig = Record; /** * 用来管理多个环境配置,且具备配置继承特性的配置类 * @category 配置管理 */ export declare class Config { private readonly _conf; /** * 存放配置段名称列表 */ private readonly _sections; /** * 初始化配置段 * * 建议首次初始化传入的配置就包含所有环境信息,以确定环境信息顺序关系, * 后续合并新的配置段就不会出现配置对象键名顺序问题。 * * 用例 * * ```ts * import { Config } from 'sunny-js' * // 创建一个Config实例,传入多个环境的配置信息 * const _exampleConf = new Config({ * local: { * key1: 'value1', * }, * development: { * key1: 'value2', * key2: 'value2', * }, * staging: { * key1: 'value3', * key3: 'value3', * }, * production: { * key1: 'value4', * key4: 'value4', * }, * }) * // 运行时切换配置段 * _exampleConf.section('production') * // 读值 * _exampleConf.get('key1') * // => 'value4' * // 读不到配置段staging key3的值 * _exampleConf.get('key3') * // => undefined * * // 运行时切换配置段,配置段继承模式 * _exampleConf.section('production', 'inherit') * // 可以读取配置段staging key3的值 * _exampleConf.get('key3') * // => 'value3' * * // combine的用法解决配置对象合并的问题 * _exampleConf.combine({ * production: { * key5: 'value5', * }, * }) * * // 访问配置段level,这种属性在输出调试代码时比较有用 * if (_exampleConf.level <= _exampleConf.toLevel('staging')) { * console.debug('开发环境、测试环境都能输出这个调试信息') * } * * // 通过api()访问接口地址 * _exampleConf.combine({ * production: { * api: { * api1: 'api/path', * }, * }, * }) * _exampleConf.api('api1') * ``` * */ constructor(conf: IConfig); /** * 添加新的配置对象 */ combine(...args: IConfig[]): Config; /** * 合并新的配置对象 * @param replace 是否以替换方式的合并。如果为否,则已经定义的属性为只读模式,不可修改 * @param args */ private _combine; /** * 当前配置段section所处配置对象Config所在等级序数 */ private _level; get level(): number; /** * section转level * 这里排序是受原生Object key值排序规则影响的 */ toLevel(section: keyof IConfig): number; /** * section是各个环境的抽象名称代指当前环境配置 */ private readonly _section; /** * 返回当前env环境的config * @param section 要切换的配置段 * @param [mode] 默认扁平化合并方式,和none配置合并;如果是inherit则是继承模式,右下往上合并配置 */ section(section: keyof IConfig, mode?: 'inherit'): Config; /** * 访问配置 * @param [name] 如果不传,则返回所有配置信息 */ get(name?: keyof ISectionConfig | TName): TName extends keyof ISectionConfig ? ISectionConfig[TName] : ISectionConfig; /** * 根据 {@link ISectionConfig.schema | schema}, {@link ISectionConfig.host | host}, * {@link ISectionConfig.port | port}, {@link ISectionConfig.basePath | basePath} * 合成一个base url */ baseURL(): string; /** * 读取web service api */ api: (api: string) => string; private static _instance; /** * 用单例模式使用类 * * 用此方法Config实例不会重复创建 */ static Singleton(...args: ConstructorParameters): Config; } export {};