UNPKG

@hapiness/config

Version:

Configuration Library to use it inside Hapiness framework

64 lines 1.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const c = require("config"); const clone = require("clone"); class Config { static ensureConfigData() { if (!this._data) { this.load(); } } /** * Load the config */ static load(payload) { if (payload) { // This method will make the payload object have // .get (chainable) .has and .util method // Allowing custom object to be feed in the config module const _payload = clone(payload); this._data = this.attachProtoDeep(_payload); return _payload; } this._data = c; return this._data; } static attachProtoDeep(payload) { return this._data.util['attachProtoDeep'](payload); } /** * Return config data */ static getData() { this.ensureConfigData(); return this._data; } /** * Check if the settings exists * * @param {string} key * @returns boolean */ static has(key) { this.ensureConfigData(); return this._data.has(key); } /** * Return the config value * from a key * * @param {string} key * @param {any} defaultValue * @returns any */ static get(key, defaultValue) { this.ensureConfigData(); if (!this._data) { throw new Error('Empty config data'); } return this._data.has(key) ? this._data.get(key) : defaultValue !== undefined ? defaultValue : undefined; } } exports.Config = Config; //# sourceMappingURL=config.js.map