Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cbyad/config-env2
A tiny lib to validate env file at runtime and extract content
https://github.com/cbyad/config-env2
Last synced: about 1 month ago
JSON representation
A tiny lib to validate env file at runtime and extract content
- Host: GitHub
- URL: https://github.com/cbyad/config-env2
- Owner: cbyad
- License: mit
- Created: 2021-04-26T23:03:13.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-05-07T11:31:37.000Z (over 3 years ago)
- Last Synced: 2024-11-09T18:03:27.950Z (about 2 months ago)
- Language: TypeScript
- Homepage:
- Size: 518 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# CONFIG-ENV2 (WIP)
## About
`config-env2` stands for config en**V**ironment **V**alidator. A tiny library to validate environment variables at runtime and extract content.It's backed by [fp-ts](https://github.com/gcanti/fp-ts) for functional prog utilities and [io-ts](https://github.com/gcanti/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
1. Define your environment schema```ts
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
```2. validate and extract content
```ts
import { load } from "config-env2"
import { MyEnvironment } from "./src/environment"const myConf = load(MyEnvironment)
const { SQL_SERVER: server, SQL_DATABASE: database, SQL_PASSWORD: password } = myConftype 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)