@vegajs/config
Version:
A flexible and easy-to-use configuration management service for TypeScript/JavaScript applications, allowing initialization, setting, and retrieval of configuration parameters.
1 lines • 1.07 kB
JavaScript
function i(i,n="Assertion failed: value is not defined"){if(null===i||void 0===i)throw new Error(n)}class n{constructor(i){this.defaultConfig=i,this.#i=this.defaultConfig}#i;#n=null;#t=new Map;init(i){this.config=i}async initAsync(i){if(this.#n)throw new Error("Initialization already in progress");try{return this.#n=i().then((i=>this.init(i))).finally((()=>{this.#n=null})),await this.#n}catch(n){throw this.#n=null,n}}get config(){return i(this.#i,"ConfigService is not initialized"),this.#i}toJSON(){return i(this.#i,"ConfigService is not initialized"),JSON.stringify(this.#i)}set config(i){this.#i=Object.freeze(i)}get(n){if(i(this.#i,"ConfigService is not initialized"),!this.#i){if(this.#n)throw new Error("Config is being loaded. Use await initAsync() first");throw new Error("ConfigService is not initialized")}if(this.#t.has(n))return this.#t.get(n);const t=n.split(".");let e=this.#i;for(const i of t){if("object"!==typeof e||null===e||!(i in e))throw new Error(`Key "${n}" not found in config`);e=e[i]}return this.#t.set(n,e),e}}export{n as ConfigService};