This package has been deprecated

Author message:

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

typenvy
TypeScript icon, indicating that this package has built-in type declarations

1.5.15 • Public • Published

typenvy

CI/CD MIT typescript npm github

table of contents

About

typenvy is a environment managment library.

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 envDefaults = {
    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 envTypes: 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 { envDefaults, envTypes } from "./env"

export const env = parseEnv(
  envDefaults,
  envTypes
)
  .setProcessEnv()
  .errExit()
  .env
export default env

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

import env from "./env/envParser"

console.log("parser env: ", env)
console.log("process env: ", process.env)

5. start and error

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

Define the variables in your shell:

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 default environment values in envParser.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,
}

other functions

By using the parseEnv() function tou get a EnvResult<T>. Here are all function of the Environment Result:

export interface EnvResult<T> {
  // Overwrite default values
  overwriteEnv(
    env: { [key: string]: any }
  ): EnvResult<T>
  // Set value if its missing in default values
  setMissingEnv(
    env: { [key: string]: any }
  ): EnvResult<T>
  // Put all env value as strings into process.env
  setProcessEnv(): EnvResult<T>
  // Clear all env values from process.env
  clearProcessEnv(
    justEqualValues: boolean = true
  ): EnvResult<T>
  // Print env errors to console
  errPrint(): EnvResult<T> 
  // Throw env errors
  errThrow(): EnvResult<T>
  // Exit on error
  errExit(
    exitCode: number = 1
  ): EnvResult<T> | never
}

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

Package Sidebar

Install

npm i typenvy

Weekly Downloads

6

Version

1.5.15

License

MIT

Unpacked Size

38.7 kB

Total Files

7

Last publish

Collaborators

  • majo418
  • npm-support