3.0.0 • Published 8 months ago
@doctor_internet/interenv v3.0.0
InterEnv
A simple environment wrapper.
InterEnv is designed to be a thin wrapper around process.env, or any string keyed, string valued object.
Usage
const {Environment} = require('@doctor_internet/interenv')
// By default, wrap process.env
const env = new Environment()
console.log(env.environment())
console.log(env.get('TEST_VAR'))
console.log(env.int('PORT'))
user@localhost:~$ NODE_ENV=local TEST_VAR="Hello, world!" PORT=80 node examples/readme-1.cjs
> local
> Hello, world!
> 80
API
Standard Interaction
The functions get, int, float and list are used to pull raw values, integers, floats and arrays out of the environment.
import {Environment} from "@doctor_internet/interenv"
let env = new Environment({
NODE_ENV: "test",
TEST_INT: "1",
TEST_FLOAT: "5.7",
TEST_LIST: "this,is,a,csv,list"
})
let value
value = env.get("NODE_ENV")
console.log(typeof value, '->', value)
value = env.int("TEST_INT")
console.log(typeof value, '->', value)
value = env.float("TEST_FLOAT")
console.log(typeof value, '->', value)
value = env.list("TEST_LIST")
console.log(Array.isArray(value) ? 'array' : typeof value, '->', value)
user@localhost:~$ node examples/readme-2.mjs
> string -> test
> number -> 1
> number -> 5.7
> array -> ['this', 'is', 'a', 'csv', 'list']
Prefixing / Namespace'd Vars
For specific use cases, such as fetching only DB vars, prefixing can be used.
import {Environment} from "@doctor_internet/interenv"
const env = new Environment({
NODE_ENV: "test",
DB_HOST: "example.com",
DB_USER: "testuser",
DB_PASS: "testpass",
DB_MAIN_DB: "test_database_eu",
DB_BACKUP_DB: "test_database_us"
})
const sub = env.prefixed("DB_")
console.log("DB_ / NODE_ENV?", sub.has("NODE_ENV"))
console.log("DB_ / HOST:", sub.raw("HOST"))
const main = sub.prefixed("MAIN_")
console.log("DB_ / MAIN_ / DB:", main.raw("DB"))
const backup = sub.prefixed("BACKUP_")
console.log("DB_ / BACKUP_ / DB:", backup.raw("DB"))
user@localhost:~$ ts-node examples/readme-1.ts
> DB_ / NODE_ENV? false
> DB_ / HOST: example.com
> DB_ / MAIN_ / DB: test_database_eu
> DB_ / BACKUP_ / DB: test_database_us
API Reference
-- todo
Install
With npm installed, run
user@localhost:~$ npm install @doctor_internet/interenv
See Also
- noffle/common-readme :: Readme Generation
- mocha :: Testing Suite
License
MIT
2.3.0-alpha.3
8 months ago
3.0.0
8 months ago
2.2.1
8 months ago
3.0.0-alpha.1
8 months ago
3.0.0-alpha.3
8 months ago
3.0.0-alpha.2
8 months ago
3.0.0-alpha.5
8 months ago
2.2.1-beta.3
8 months ago
3.0.0-alpha.4
8 months ago
2.2.1-beta.2
8 months ago
2.3.0-alpha.4
8 months ago
2.2.0
4 years ago
2.1.1
5 years ago
2.1.0
5 years ago
2.0.1
5 years ago
2.0.0
5 years ago
1.0.0
5 years ago
0.2.3
5 years ago
0.2.2
5 years ago
0.2.1
5 years ago
0.2.0
5 years ago