Coder Social home page Coder Social logo

joienv's Introduction

Joienv

Loads and validate your environement variables using Joi

TL;DR

import JoiEnv from "joienv"
import Joi from "joi"

type Config = {
  misc: {
    baseUrl: string,
    env: string,
    port: number,
  },
  mongo: {
    connectionString: string,
    db?: string
  },
}

const Schema = {
  misc: {
    port: Joi.number().port(), // will read `process.env.port ?? process.env.PORT`
                               // and validate it against this rule
    baseUrl: "BASE_URL", // will validate against `process.env.BASE_URL,
                         // `Joi.string().required().tag("BASE_URL")
    env: /^(dev|staging|prod)$/, // will validate `process.env.env ?? process.env.ENV`
                                 // against this regex
  },
  mongo: {
    cs: Joi.string().required()
           .uri({ scheme: ["mongodb", "mongodb+srv"] }) // validate agains this rule
           .tag("MONGODB_URI"), // check for this env var `process.env.MONGODB_URI`
    db: "?MONGODB_DB" // Will convert in `Joi.string().optional().tag("MONGODB_DB")
  }
}

const config = JoiEnv<Config>(Schema)

NB

  • JoiEnv will use the env var as a label, unless specified otherwise
  • JoiEnv will first test for the tag OR then the key, then key uppercase and finnaly key lowercase: tag ? process.env[tag] : process.env[key] ?? process.env[key.toUpperCase()] ?? process.env[key.toLowerCase()]
  • You can wrap nested objects in Joi.object() for better control

API

loadenv<T>(config: Config): T

Loads & validate your environment, and returns it as the object you want

Config is defined as follow:

type Config = {
  [key: string]:
      string // Takes the environment variable
    | RegExp // Validation object by joi
    | Config // Nested configutaion object
    | Joi.Schema // Joi schema object
}

NB

  • when using a string, you can prefix it with a ? to make it optional (e.g. ?FOO)

Q & A

Why is this different than dotenv ?

dotenv is used to inject a "configuration file" into process.env.
Loadenv makes sure your environment is properly configured and is valid.

The two libs can work hand in hands because they do not serve the same purpose.

joienv's People

Contributors

qntfrd avatar renovate-bot avatar renovate[bot] avatar

Watchers

 avatar

joienv's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/bump.yml
  • actions/checkout v2
.github/workflows/test.yml
  • actions/checkout v2
  • actions/setup-node v2
npm
package.json
  • @types/chai 4.3.3
  • @types/joi 17.2.3
  • @types/mocha 9.1.1
  • @types/node 18.0.3
  • chai 4.3.6
  • mocha 10.0.0
  • nyc 15.1.0
  • ts-node 10.9.1
  • typescript 4.7.4
  • joi ^17.6.0

  • Check this box to trigger a request for Renovate to run again on this repository

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.