Skip to content

jakobkroenun/typenvy

 
 

Repository files navigation

typenvy

uses npm uses typescript uses github

"typenvy" is a environment managment library

table of contents

Features

General

  • Set default (typed) environment varables
  • Define variables types
  • Defined required variables
  • Load and parse variables from process.env

Getting started

1. install typenvy

npm i typenvy

2. env file

Create a example environment file at "./src/env/env.ts":

import * as typenvy from "typenvy"
export const defaultEnv = {
    PRODUCTION: (process.env.NODE_ENV === "production") as boolean,
    VERBOSE: false as boolean,

    PORT: 8080 as number,
    API_KEY: undefined as string,
    API_URL: undefined as string,
}
export const variablesTypes: typenvy.VariablesTypes = {
    PRODUCTION: [typenvy.TC_BOOLEAN],
    VERBOSE: [typenvy.TC_BOOLEAN],

    PORT: [typenvy.TC_NUMBER],
    API_KEY: [typenvy.TC_STRING],
    API_URL: [typenvy.TC_STRING],
}

3. env parser

Create a example environment parser file at "./src/env/envParser.ts":

import { parseEnv } from "typenvy"
import { defaultEnv, variablesTypes } from "./env"

export const env = parseEnv(defaultEnv, variablesTypes)
  .setProcessEnv()
  .errExit()
  .env
export default env

if (!env.PRODUCTION) {
    process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = "0"
}

4. load and print env in "./src/index.ts"

import env from "./env/envParser"

console.log("parser env: ", {
  prod: env.PRODUCTION,
  v: env.VERBOSE,
  port: env.PORT,
  key: env.API_KEY,
  url: env.API_URL,
})

console.log("process env: ", {
  prod: process.env.PRODUCTION,
  v: process.env.VERBOSE,
  port: process.env.PORT,
  key: process.env.API_KEY,
  url: process.env.API_URL,
})

5. start

If you run the index.js after compile the app throws an error. This is because in the "env.ts" there is no default value provided for "API_KEY" and "API_URL".

There are 3 options to remove this error:

1. Set environment variables

export API_KEY="qwertzui"
export API_URL="https://api.github.io/v2/repo/majo418/testrepo"

2. Allow undefined as value

Allow undefined as environment variable value in env.ts

export const variablesTypes: typenvy.VariablesTypes = {
    PRODUCTION: [typenvy.TC_BOOLEAN],
    VERBOSE: [typenvy.TC_BOOLEAN],

    PORT: [typenvy.TC_NUMBER],
    API_KEY: [typenvy.TC_STRING, typenvy.TC_UNDEFINED], // <---
    API_URL: [typenvy.TC_STRING, typenvy.TC_UNDEFINED], // <---
}

3. Set a default value

Allow undefined as environment variable value in env.ts

export const defaultEnv = {
    PRODUCTION: (process.env.NODE_ENV === "production") as boolean,
    VERBOSE: false as boolean,

    PORT: 8080 as number,
    API_KEY: "myDEfaultAPIkey" as string,
    API_URL: "https://api.cloudflare.com/v1/dns" as string,
}

npm scripts

The npm scripts are made for linux but can also work on mac and windows.

use

You can run npm scripts in the project folder like this:

npm run <scriptname>

Here is an example:

npm run test

base scripts

You can find all npm scripts in the package.json file. This is a list of the most important npm scripts:

  • test // test the app
  • build // build the app
  • exec // run the app
  • start // build and run the app

watch mode

Like this example you can run all npm scripts in watch mode:

npm run start:watch

contribution

    1. fork the project
    1. implement your idea
    1. create a pull/merge request
// please create seperated forks for different kind of featues/ideas/structure changes/implementations

cya ;3
by majo418

About

A js/ty environment manager

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 100.0%