@mx7/tenv
Version:
type-safe environment variable parser
2 lines • 2.68 kB
JavaScript
"use strict";var a=Object.defineProperty;var o=Object.getOwnPropertyDescriptor;var u=Object.getOwnPropertyNames;var v=Object.prototype.hasOwnProperty;var h=(t,e)=>{for(var s in e)a(t,s,{get:e[s],enumerable:!0})},l=(t,e,s,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of u(e))!v.call(t,n)&&n!==s&&a(t,n,{get:()=>e[n],enumerable:!(r=o(e,n))||r.enumerable});return t};var d=t=>l(a({},"__esModule",{value:!0}),t);var g={};h(g,{default:()=>i});module.exports=d(g);var i=class{constructor(e={}){this.parsed={};this.parsed=e,this.value=void 0}get isProduction(){let e=String(this.parsed.NODE_ENV).toLowerCase();return e==="production"||e==="prod"}get isDevelopment(){let e=String(this.parsed.NODE_ENV).toLowerCase();return e==="development"||e==="dev"}get isStaging(){return String(this.parsed.NODE_ENV).toLowerCase()==="staging"}get isTest(){return String(this.parsed.NODE_ENV).toLowerCase()==="test"}get(){if(!(typeof this.value=="string"&&this.value.length===0))return this.value}key(e,s=!1){if(this.value=this.parsed[e],typeof s!="boolean"&&typeof s<"u")return this.value=s,this;if(s&&!this.value)throw new Error(`Environment variable ${e} is required!`);return s===!0?this:this}checkBoolean(e){if(!["TRUE","FALSE","true","false"].includes(e))throw new Error("Invalid Boolean value!")}bool(){return this.checkBoolean(this.value),this.value=this.value.toLowerCase()==="true",this}email(){if(!this.value||!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(this.value))throw new Error("Invalid email address!");return this.value=this.value,this}url(e={}){if(e.ipv6){let r=/^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$|^::1$|^(?:[0-9a-fA-F]{1,4}:){0,6}(?::[0-9a-fA-F]{1,4})?$/;if(!this.value||!r.test(this.value))throw new Error("Invalid IPv6 address!");return this.value=this.value,this}let s=/^(?:(?:[0-9]{1,3}\.){3}[0-9]{1,3})|(?:(?:(?:[a-fA-F0-9]{1,4}:){6}(?:[a-fA-F0-9]{1,4}:)?(?:[a-fA-F0-9]{1,4})?)|(?:[a-zA-Z0-9_-]+\.docker)|(?:[a-zA-Z0-9_-]+\.[a-zA-Z]{2,})|(?:[a-zA-Z]+:\/\/[^\s:$]+)(?::[0-9]{1,5})?)$/;if(!this.value||!s.test(this.value))throw new Error("Invalid URL address!");return this.value=this.value,this}checkNumber(e){if(!/^-?\d+(\.\d+)?$/.test(e))throw new Error("Invalid numbers!")}integer(){return this.checkNumber(this.value),this.value=parseInt(this.value,10),this}float(){return this.checkNumber(this.value),this.value=parseFloat(this.value),this}unsigned(){if(this.checkNumber(this.value),Number(this.value)<0)throw new Error("Excepted an unsigned number!");return this.value=this.value,this}signed(){if(this.checkNumber(this.value),Number(this.value)>=0)throw new Error("Excepted signed number!");return this.value=this.value,this}};
//# sourceMappingURL=index.js.map