npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

configurable-environment-loader

v1.0.0

Published

A utility to load environment configurations easily.

Downloads

13

Readme

Environment Loader

A utility to load environment configurations easily. This package helps you load and validate environment variables with ease, providing mechanisms to handle default values, type checking, and error reporting.

Installation

You can install the environment-loader package via npm:

npm install environment-loader

Usage

Importing the Module

First, import the necessary functions and types from the environment-loader package:


import { loadEnvironment, EnvironmentLoaderOptions, convertToNumeric, valueIsNumeric, valueIsString } from 'environment-loader';

Defining Environment Variables

Define the environment variables you expect using the EnvironmentLoaderOptions type. You can specify the variable name, expected type, whether it is required, default values, and custom messages for validation errors.

Example Usage

Here’s an example of how to use the environment-loader package to load and validate environment variables:


import { loadEnvironment, EnvironmentLoaderOptions, convertToNumeric, valueIsNumeric, valueIsString } from 'environment-loader';

// Define the options for loading environment variables
let options: EnvironmentLoaderOptions = {
    logFunction: console.log,
    throwExceptionOnInvalid: false,
    expectedEnvironmentVariables: [
        {
            variableName: "SuppliedValue",
            expectedType: valueIsString,
            isRequired: true,
            messageWhenInvalid: "Supplied value SuppliedValue is not valid",
            messageWhenMissing: "SuppliedValue parameter is not specified"
        },
        {
            variableName: "Count",
            expectedType: valueIsNumeric,
            conversionFunction: convertToNumeric,
            isRequired: true,
        }
    ],
    suppliedEnvironmentVariables: {
        "SuppliedValue": process.env.SUPPLIED_VALUE,
        "Count": process.env.COUNT
    }
};

// Load and validate the environment variables
let actualEnvironmentVariablesLoaded = loadEnvironment(options);

console.log(actualEnvironmentVariablesLoaded);

Handling Errors

You can configure the loader to throw exceptions on invalid values by setting throwExceptionOnInvalid to true. Otherwise, it will log the errors and continue.


let options: EnvironmentLoaderOptions = {
    logFunction: console.log,
    throwExceptionOnInvalid: true,
    expectedEnvironmentVariables: [
        {
            variableName: "InvalidNumber",
            expectedType: valueIsNumeric,
            isRequired: true,
            messageWhenInvalid: "InvalidNumber is not valid"
        }
    ],
    suppliedEnvironmentVariables: {
        "InvalidNumber": "stringshouldbenumber"
    }
};

try {
    loadEnvironment(options);
} catch (error) {
    console.error(error);
}

Providing Default Values

You can specify default values for optional environment variables:


let options: EnvironmentLoaderOptions = {
    logFunction: console.log,
    throwExceptionOnInvalid: false,
    expectedEnvironmentVariables: [
        {
            variableName: "OptionalValue",
            expectedType: valueIsString,
            isRequired: false,
            defaultValue: "defaultvalue",
            messageWhenInvalid: "OptionalValue is not valid"
        }
    ],
    suppliedEnvironmentVariables: {}
};

let actualEnvironmentVariablesLoaded = loadEnvironment(options);
console.log(actualEnvironmentVariablesLoaded);

Conversion Functions

You can specify conversion functions to convert the values to the desired type after validation:


let options: EnvironmentLoaderOptions = {
    logFunction: console.log,
    throwExceptionOnInvalid: false,
    expectedEnvironmentVariables: [
        {
            variableName: "Count",
            expectedType: valueIsNumeric,
            conversionFunction: convertToNumeric,
            isRequired: true
        }
    ],
    suppliedEnvironmentVariables: { "Count": "123" }
};

let actualEnvironmentVariablesLoaded = loadEnvironment(options);
console.log(actualEnvironmentVariablesLoaded);

Building the source

Clone the repository, install the necessary build packages.

Clone the repository

git clone https://github.com/iitwa/environment-loader

Install NodeJS

This was built using NodeJS v18. This can be found at [https://nodejs.org] (https://nodejs.org). Install NodeJS and make the node programs path accsesible.

Install development packages

From the cloned directory, install the packages using the npm command as follows:

npm install --save-dev .

From here, development activities can be driven by the Gulp file using commands to compile, test and package them.

These gulp targets are as follows: | Task | Purpose | |-----------------:|-------------------------------------:| | compile | Does Typescript transpilation. | | test | Executes unit tests on compiled code | | npm | Gathers files into one place so that | | | NPM packaging can be done