Socket
Socket
Sign inDemoInstall

guard-env

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    guard-env

Guard-env is a Node.js module for type-checking and guarding environment variables based on specified configurations.


Version published
Weekly downloads
4
decreased by-20%
Maintainers
1
Install size
10.8 kB
Created
Weekly downloads
 

Changelog

Source

0.0.2

Patch Changes

  • Fixed issue where guardEnv incorrectly assigned types for environment variables based on the first object in the setConfig parameter.

Readme

Source

guard-env

Guard-env is a Node.js module for type-checking and guarding environment variables based on specified configurations.

Installation

You can install guard-env via npm:

npm install guard-env

Usage

import { guardEnv } from 'guard-env';
import 'dotenv/config'; // Don't forget to load environment variables

// Example configuration
const config = {
    PORT: Number,
    ENABLED: Boolean,
    API_KEY: String
};

// Guarding environment variables
const env = guardEnv(process.env, config);

// Accessing type-checked environment variables
console.log(env.PORT); // 3000
console.log(env.ENABLED); // true
console.log(env.API_KEY); // 'secret

// Accessing non-existent variable will throw an error
console.log(env.NON_EXISTENT); // Error: 'NON_EXISTENT' is not defined in 'guard-env' config

API

guardEnv

Guards the environment variables based on the provided configuration and returns a proxy object with type-checked values.

  • env: The Node.js process environment variables.
  • setConfig: The configuration object specifying the expected types for each environment variable.

Returns a proxy object with type-checked values.

Throws an Error if:

  • Configuration is empty.

    const env = guardEnv(process.env, {});
    
    console.log(env);
    // Error: Configuration is empty
    
  • A variable is not defined in the environment.

    PORT=3000
    
    const config = {
        PORT: Number,
        ENABLED: Boolean
    };
    const env = guardEnv(process.env, config);
    
    console.log(env.ENABLED);
    // Error: 'ENABLED' is not defined in 'guard-env' config
    
  • An unsupported type is specified in the configuration. Throws a TypeError if a variable is not of the expected type (number, boolean, or string).

    PORT=THREE_THOUSAND
    ENABLED=ON
    
    const config = {
        PORT: Number,
        ENABLED: Boolean
    };
    const env = guardEnv(process.env, config);
    
    console.log(env.PORT);
    // TypeError: 'PORT' is not of type number
    console.log(env.ENABLED);
    // TypeError: 'ENABLED' is not of type boolean
    
  • An unsupported type is specified in the configuration.

    const config = {
        DATE: Date
    };
    const env = guardEnv(process.env, config);
    
    console.log(env.DATE);
    // Error: 'Date' type is not supported
    

Examples

Check out the examples directory for more usage examples.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Keywords

FAQs

Last updated on 15 Feb 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc