configly-js
Version:
The dead simple place to put and retrieve static/config data into your Node.js application / JS
3 lines (2 loc) • 3.03 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0});var e=require("axios"),t=require("qs"),i=require("remove-trailing-slash"),n=require("../package.json").version,r=function(){function r(){this.cache={},this.cacheTtl={},this.apiKey="",this.options={host:"https://api.config.ly",timeout:3e3,enableCache:!0}}r.init=function(e,t){if(!e||!e.length||0==e.length)throw new Error("You must supply your API Key. You can find it by logging in to Config.ly");if(r.instance)throw new Error("configly.init() is called multiple times. It can only be called once.");var n=new r;return n.options.host=i((t=t||{}).host||n.options.host),n.options.enableCache=void 0===t.enableCache?n.options.enableCache:t.enableCache,n.apiKey=e,n.options.timeout=t.timeout||n.options.timeout,r.instance=n},r.getInstance=function(){if(!r.isInitialized())throw new Error("Configly.getInstance() is called before Configly.init(); you must call init.");return r.instance},r.isInitialized=function(){return!!r.instance},r.getUnixTimestampSecs=function(){return Math.round(Date.now()/1e3)};var a=r.prototype;return a._isCached=function(e){return!(!this.cache[e]||this.cacheTtl[e]<r.getUnixTimestampSecs())},a._cacheGet=function(e){return this.cache[e]},a.get=function(i,o){var a=this;if("string"!=typeof i)return Promise.reject(new TypeError("key must be a string"));if(!i||0==i.length)return Promise.reject(new Error("key must be a non-empty string"));o=o||{};var s={Accept:"application/json"};s["X-Lib-Version"]=["configly-node",n].join("/");var c=!0;return void 0!==o.enableCache?c=o.enableCache:this.options.enableCache||(c=!1),c&&this._isCached(i)?Promise.resolve(this._cacheGet(i)):e.get(this.options.host+"/api/v1/value",{auth:{username:this.apiKey},headers:s,params:{keys:[i]},paramsSerializer:function(e){return t.stringify(e,{arrayFormat:"brackets"})},timeout:o.timeout||this.options.timeout||3e3}).then((function(e){var t=e.data.data[i]||{},n=t.value,o=t.ttl;return o=o||60,c&&void 0!==n&&(a.cacheTtl[i]=r.getUnixTimestampSecs()+o,a.cache[i]=n),n})).catch(r.handleGetError)},r.makeError=function(e,t,i){return{status:e,message:t,originalError:i}},r.handleGetError=function(e){var t=o.OTHER,i=["Something went wrong. Have you upgraded to the latest client?","Take a look at 'originalError' inside the error object for more details."].join("");return e.response?(t=401===e.response.status?o.INVALID_API_KEY:o.OTHER,i=(e.response.data||"").substring(0,1e3)):"ECONNREFUSED"===e.code&&(t=o.CONNECTION_ERROR,i=["Configly didn't receive an HTTP response.","This could be because of a network disruption with the server or a bad supplied hostname.","If you've supplied a host parameter, please ensure it is correct.","Otherwise, try again."].join(" ")),Promise.reject(r.makeError(t,i,e))},a.destroy=function(){r.instance=void 0},r}(),o={OTHER:"OTHER",CONNECTION_ERROR:"CONNECTION_ERROR",INVALID_API_KEY:"INVALID_API_KEY"};Object.freeze(o),exports.Configly=r,exports.ERRORS=o,exports.default=r;
//# sourceMappingURL=configly-js.cjs.production.min.js.map