Giter VIP home page Giter VIP logo

getenv.ts's Introduction

getenv.ts

Helper to get, validate, cast and provide type safety for environment variables with TypeScript.

Installation

pnpm add getenv.ts
# or
npm install getenv.ts

Usage

Set environment variables in a suitable file, and load them in using a library like dotenv/dotenv-safe, or similar strategy:

export HTTP_HOST="localhost"
export HTTP_PORT=8080
export HTTP_SECURE=true
export AB_TEST_RATIO=0.5

Alternatively, pass the variables along with the execution command:

HTTP_HOST="localhost" HTTP_PORT=8080 node main.ts

Basically, all variables you intend to use must be loaded into process.env in order to be available for getting.

Then you just have to import the package and use the variables:

import getenv from "getenv.ts";

// Get values cast to desired type.
const host: string = getenv.string("HTTP_HOST");
const port: number = getenv.int("HTTP_PORT");
const secure: boolean = getenv.bool("HTTP_SECURE");
const abTestRatio: number = getenv.float("AB_TEST_RATIO");

// Use default value if variable isn't defined.
const sudoUser = getenv.string("SUDO_USER", "root");

// Throws `ReferenceError` when variable isn't defined.
try {
	const invalid = getenv.string("DOES_NOT_EXIST");
} catch (error) {
	// Will throw...
}

// Throws `TypeError` when variable isn't of the expected type.
try {
	const invalid = getenv.int("HTTP_HOST");
} catch (error) {
	// Will throw...
}

getenv.ts's People

Contributors

semantic-release-bot avatar tdolsen avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

getenv.ts's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.