Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@humanwhocodes/env

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@humanwhocodes/env - npm Package Compare versions

Comparing version 0.4.0 to 1.0.0

65

dist/env.cjs.js

@@ -43,2 +43,12 @@ 'use strict';

/**
* Throws an error saying that the key was an empty string.
* @param {string} key The key to report as an empty string.
* @returns {void}
* @throws {Error} Always.
*/
function emptyString(key) {
throw new Error(`Required environment variable '${key}' is an empty string.`);
}
//-----------------------------------------------------------------------------

@@ -75,2 +85,12 @@ // Main

/**
* Determines if a given environment variable exists.
* @param {string} key The environment variable name to check.
* @returns {boolean} True if the environment variable exists,
* false if not.
*/
has(key) {
return key in this.source;
}
/**
* Retrieves the first environment variable found in a list of environment

@@ -102,6 +122,7 @@ * variable names.

* Retrieves an environment variable. If the environment variable does
* not exist, then it throws an error.
* not exist or is an empty string, then it throws an error.
* @param {string} key The environment variable name to retrieve.
* @returns {string?} The environment variable value.
* @throws {Error} When the environment variable doesn't exist.
* @throws {Error} When the environment variable doesn't exist or is an
* empty string.
*/

@@ -112,2 +133,4 @@ require(key) {

keyNotFound(key);
} else if (value === "") {
throw emptyString(key);
} else {

@@ -123,7 +146,39 @@ return value;

*/
get exists() {
const existsProxy = new Proxy(this.source, {
get(target, key) {
if (key in target) {
return target[key];
}
keyNotFound(key);
}
});
// redefine this property as a data attribute
Object.defineProperty(this, "exists", {
value: existsProxy,
writable: false,
enumerable: false,
configurable: false
});
return existsProxy;
}
/**
* Lazy-loading property containing a proxy that can be used to
* automatically throw errors when an undefined or empty string
* environment variable is accessed.
*/
get required() {
const proxy = new Proxy(this.source, {
const requiredProxy = new Proxy(this.source, {
get(target, key) {
if (key in target) {
if (target[key] === "") {
emptyString(key);
}
return target[key];

@@ -138,3 +193,3 @@ }

Object.defineProperty(this, "required", {
value: proxy,
value: requiredProxy,
writable: false,

@@ -145,3 +200,3 @@ enumerable: false,

return proxy;
return requiredProxy;
}

@@ -148,0 +203,0 @@

@@ -39,2 +39,12 @@ /**

/**
* Throws an error saying that the key was an empty string.
* @param {string} key The key to report as an empty string.
* @returns {void}
* @throws {Error} Always.
*/
function emptyString(key) {
throw new Error(`Required environment variable '${key}' is an empty string.`);
}
//-----------------------------------------------------------------------------

@@ -71,2 +81,12 @@ // Main

/**
* Determines if a given environment variable exists.
* @param {string} key The environment variable name to check.
* @returns {boolean} True if the environment variable exists,
* false if not.
*/
has(key) {
return key in this.source;
}
/**
* Retrieves the first environment variable found in a list of environment

@@ -98,6 +118,7 @@ * variable names.

* Retrieves an environment variable. If the environment variable does
* not exist, then it throws an error.
* not exist or is an empty string, then it throws an error.
* @param {string} key The environment variable name to retrieve.
* @returns {string?} The environment variable value.
* @throws {Error} When the environment variable doesn't exist.
* @throws {Error} When the environment variable doesn't exist or is an
* empty string.
*/

@@ -108,2 +129,4 @@ require(key) {

keyNotFound(key);
} else if (value === "") {
throw emptyString(key);
} else {

@@ -119,7 +142,39 @@ return value;

*/
get exists() {
const existsProxy = new Proxy(this.source, {
get(target, key) {
if (key in target) {
return target[key];
}
keyNotFound(key);
}
});
// redefine this property as a data attribute
Object.defineProperty(this, "exists", {
value: existsProxy,
writable: false,
enumerable: false,
configurable: false
});
return existsProxy;
}
/**
* Lazy-loading property containing a proxy that can be used to
* automatically throw errors when an undefined or empty string
* environment variable is accessed.
*/
get required() {
const proxy = new Proxy(this.source, {
const requiredProxy = new Proxy(this.source, {
get(target, key) {
if (key in target) {
if (target[key] === "") {
emptyString(key);
}
return target[key];

@@ -134,3 +189,3 @@ }

Object.defineProperty(this, "required", {
value: proxy,
value: requiredProxy,
writable: false,

@@ -141,3 +196,3 @@ enumerable: false,

return proxy;
return requiredProxy;
}

@@ -144,0 +199,0 @@

2

dist/env.min.js

@@ -1,1 +0,1 @@

const defaultEnvSource=(()=>"object"==typeof process?process.env:"undefined"==typeof Deno?{}:Deno.env())();function keyNotFound(a){throw new Error(`Required environment variable '${a}' not found.`)}class Env{constructor(a=defaultEnvSource){this.source=a}get(a,b=null){return a in this.source?this.source[a]:b}first(a,b=null){if(!Array.isArray(a)||2>a.length)throw new TypeError("First argument must be an array of two or more strings.");for(const c of a)if(c in this.source)return this.source[c];return b}require(a){const b=this.get(a);return null===b?void keyNotFound(a):b}get required(){const a=new Proxy(this.source,{get(a,b){return b in a?a[b]:void keyNotFound(b)}});return Object.defineProperty(this,"required",{value:a,writable:!1,enumerable:!1,configurable:!1}),a}}export{Env};
const defaultEnvSource=(()=>"object"==typeof process?process.env:"undefined"==typeof Deno?{}:Deno.env())();function keyNotFound(a){throw new Error(`Required environment variable '${a}' not found.`)}function emptyString(a){throw new Error(`Required environment variable '${a}' is an empty string.`)}class Env{constructor(a=defaultEnvSource){this.source=a}get(a,b=null){return a in this.source?this.source[a]:b}has(a){return a in this.source}first(a,b=null){if(!Array.isArray(a)||2>a.length)throw new TypeError("First argument must be an array of two or more strings.");for(const c of a)if(c in this.source)return this.source[c];return b}require(a){const b=this.get(a);if(null===b)keyNotFound(a);else if(""===b)throw emptyString(a);else return b}get exists(){const a=new Proxy(this.source,{get(a,b){return b in a?a[b]:void keyNotFound(b)}});return Object.defineProperty(this,"exists",{value:a,writable:!1,enumerable:!1,configurable:!1}),a}get required(){const a=new Proxy(this.source,{get(a,b){return b in a?(""===a[b]&&emptyString(b),a[b]):void keyNotFound(b)}});return Object.defineProperty(this,"required",{value:a,writable:!1,enumerable:!1,configurable:!1}),a}}export{Env};
{
"name": "@humanwhocodes/env",
"version": "0.4.0",
"version": "1.0.0",
"description": "A utility to verify that environment variables exist.",

@@ -5,0 +5,0 @@ "main": "dist/env.cjs.js",

@@ -76,2 +76,5 @@ # Env utility

// determine if a variable exists
const username = env.has("USERNAME");
// read the first found variable and use a default is empty

@@ -81,2 +84,3 @@ const username = env.first(["USERNAME", "USERNAME2"], "humanwhocodes");

// read a variable and throw an error if it doesn't exist
// or is an empty string
const username = env.require("USERNAME");

@@ -90,2 +94,3 @@ ```

// throws if variables are undefined or an empty string
const {

@@ -97,4 +102,16 @@ CLIENT_ID,

In this example, an error is thrown if either `CLIENT_ID` or `CLIENT_SECRET` is missing. The `required` property is a proxy object that throws an error whenever you attempt to access a property that doesn't exist.
In this example, an error is thrown if either `CLIENT_ID` or `CLIENT_SECRET` is missing or an empty string. The `required` property is a proxy object that throws an error whenever you attempt to access a property that doesn't exist.
If you don't want to throw an error for environment variables containing an empty string, use the `exists` property:
```js
const env = new Env();
// throws only if variables are not defined
const {
CLIENT_ID,
CLIENT_SECRET
} = env.exists;
```
You can also specify an alternate object to read variables from. This can be useful for testing or in the browser (where there is no environment variable to read from by default):

@@ -101,0 +118,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc