"typenvy" is a environment managment library
- Set default (typed) environment varables
- Define variables types
- Defined required variables
- Load and parse variables from process.env
npm i typenvy
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],
}
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"
}
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,
})
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:
export API_KEY="qwertzui"
export API_URL="https://api.github.io/v2/repo/majo418/testrepo"
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], // <---
}
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,
}
The npm scripts are made for linux but can also work on mac and windows.
You can run npm scripts in the project folder like this:
npm run <scriptname>
Here is an example:
npm run test
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
Like this example you can run all npm scripts in watch mode:
npm run start:watch
-
- fork the project
-
- implement your idea
-
- create a pull/merge request
// please create seperated forks for different kind of featues/ideas/structure changes/implementations
cya ;3
by majo418