0.0.1 • Published 3 years ago
config-env2 v0.0.1
CONFIG-ENV2 (WIP)
About
config-env2
stands for config enVironment Validator. A tiny library to validate environment variables at runtime and extract content.
It's backed by fp-ts for functional prog utilities and io-ts for runtime validation and define custom types.
For those who are unfamiliar with functional programming No Worries you need to only import npm i io-ts
and follow example
supplied
Usage
- Basic usage =>
npm i io-ts config-env2
- FP usage =>
npm i io-ts fp-ts config-env2
Example
- Define your environment schema
import * as io from "io-ts"
import { NonEmptyString, NumberString, URL } from 'config-env2'
const MyEnvironment = io.strict({
SQL_SERVER: NonEmptyString,
SQL_DATABASE: NonEmptyString,
SQL_USER: NonEmptyString,
SQL_PASSWORD: NonEmptyString,
SQL_CONNECTION_TIMEOUT: NumberString,
SQL_REQUEST_TIMEOUT: NumberString,
APP_HOST: URL
})
type MyEnvironment = io.TypeOf<typeof MyEnvironment>
- validate and extract content
import { load } from "config-env2"
import { MyEnvironment } from "./src/environment"
const myConf = load(MyEnvironment)
const { SQL_SERVER: server, SQL_DATABASE: database, SQL_PASSWORD: password } = myConf
type SqlConf = {
server: string
database: string
password: string
}
const connect = (_: SqlConf) => console.log("connected")
const sqlConf: SqlConf = { server, database, password }
connect(sqlConf)
If your env file is located in other place => load(MyEnvironment,customPath)
Contribution
TODO
License
The MIT License (MIT)
0.0.1
3 years ago