UNPKG

@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.49 kB
"use strict";var i,e=Object.defineProperty,t=Object.getOwnPropertyDescriptor,n=Object.getOwnPropertyNames,o=Object.prototype.hasOwnProperty,r={};function s(i,e="Assertion failed: value is not defined"){if(null===i||void 0===i)throw new Error(e)}((i,t)=>{for(var n in t)e(i,n,{get:t[n],enumerable:!0})})(r,{ConfigService:()=>c}),module.exports=(i=r,((i,r,s,c)=>{if(r&&"object"===typeof r||"function"===typeof r)for(let f of n(r))o.call(i,f)||f===s||e(i,f,{get:()=>r[f],enumerable:!(c=t(r,f))||c.enumerable});return i})(e({},"__esModule",{value:!0}),i));class c{constructor(i){this.defaultConfig=i,this.#i=this.defaultConfig}#i;#e=null;#t=new Map;init(i){this.config=i}async initAsync(i){if(this.#e)throw new Error("Initialization already in progress");try{return this.#e=i().then((i=>this.init(i))).finally((()=>{this.#e=null})),await this.#e}catch(e){throw this.#e=null,e}}get config(){return s(this.#i,"ConfigService is not initialized"),this.#i}toJSON(){return s(this.#i,"ConfigService is not initialized"),JSON.stringify(this.#i)}set config(i){this.#i=Object.freeze(i)}get(i){if(s(this.#i,"ConfigService is not initialized"),!this.#i){if(this.#e)throw new Error("Config is being loaded. Use await initAsync() first");throw new Error("ConfigService is not initialized")}if(this.#t.has(i))return this.#t.get(i);const e=i.split(".");let t=this.#i;for(const n of e){if("object"!==typeof t||null===t||!(n in t))throw new Error(`Key "${i}" not found in config`);t=t[n]}return this.#t.set(i,t),t}}