New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@fine-js/env

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fine-js/env - npm Package Compare versions

Comparing version

to
1.0.0

env.js

24

package.json
{
"name": "@fine-js/env",
"version": "0.0.2",
"version": "1.0.0",
"description": "Read env vars in a robust way.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Aleksei Zabrodskii <elmigranto@orotonmail.com> (https://elmigranto.me/)",
"license": "MIT",
"keywords": [

@@ -16,7 +14,15 @@ "process.env",

],
"author": "Alexey Zabrodsky <elmigranto@gmail.com> (https://elmigranto.me/)",
"license": "MIT",
"dependencies": {
"@fine-js/read-only-map": "0.0.1"
"main": "env.js",
"files": [
"env.js"
],
"scripts": {
"test": "mocha env.test.js",
"testw": "nodemon --exec yarn test"
},
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^7.1.1",
"nodemon": "^2.0.3"
}
}
# env
Read [environment variables](https://nodejs.org/dist/latest/docs/api/process.html#process_process_env)
in a robust way and finally stop getting `undefined` everywhere across your config.
in a robust way and finally stop getting `undefined`s everywhere across your config.
``` sh
```
npm install @fine-js/env

@@ -12,12 +12,18 @@ ```

``` js
const env = require('@fine-js/env');
```js
const env = require('@fine-js/env')
env.has('PORT'); // => false
env.get('PORT'); // => Error: env var PORT: missing
env.get('PORT', {required: false, defaultsTo: 3000}); // => 3000
env.has('PORT') // => false
env.get('PORT') // => Error: env var "PORT" missing
env.get('PORT', {required: false, defaultsTo: 3000, parseFn: Number}) // => 3000
console.log(env) // Prints a nice represantation sorted alphabetically by key
```
# `#get(name, options)`
# API
## `get(name, options)`
Returns the value of variable `name` or throws an `Error` based on following options:
- **`required`** <sup>`bool` `[true]`</sup>

@@ -29,3 +35,4 @@

For non-required env vars, this will be a default value.
For non-required env vars, this value will be returned directly
without parsing with `parseFn`.

@@ -35,3 +42,3 @@ - **`parseFn`** <sup>`function` `[(x) => x]`</sup>

Values that come from `process.env` will be passed to this function for parsing,
e.g. for port numbers, you'd want `parseInt`;
e.g. for port numbers, you'd want `Number`;
feel free to do any validations here, but note that `defaultsTo` is returned directly.

@@ -52,18 +59,74 @@

- **`oneOf`** <sup>`null|Array` `[null]`</sup>
## `has(name)`
You can pass in an array of allowed values, and `#get()` will throw
if `oneOf.includes()` returns `false`.
Returns `true` when the variable named `name` is set to _any_ value.
# `class Env`
## `createEnv()`
Default export is `new Env(process.env)`. It inherits from [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map),
and you can use those APIs (including iteration with [`for...of`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of)),
except for [mutating methods](https://www.npmjs.com/package/@fine-js/read-only-map),
so **modifying `Env` instance will throw an `Error`**.
Default export is an environment object based with the contents of `process.env`
at the time of the module being required. You can get an updated version
or create new environment from any other object:
In case you want to access constructor function, use this:
```js
const {createEnv} = require('@fine-js/env')
``` js
const {constructor: Env} = require('@fine-js/env');
// Defaults to using process.env at the moment of calling.
createEnv()
// Or you can pass any other object.
createEnv({
my: 'custom',
env: '42',
})
```
## Changes
### `1.0.0`
I had no need to change this package for years now,
seems like it's time for the first stable version
with tests, overhauled API and 0 dependencies 🎉
`Env` is no longer a class, but a plain object with 4 exports:
- `get` is a main function for reading env
- `has` will tell you if environment has a certain key
- `keys` creates an iterator for all the environment variable names present
- `createEnv` allows to create a new environment
Not being a class means no longer inherting from `Map`,
which makes it non-iterable except via `keys()`
(there is rarely a need to iterate over **values**).
Another bonus is being nicely printable with `console` methods
and serializable to JSON.
`oneOf` option is removed as being out of scope.
You'll get better validations and more options by putting
those kinds of things under `parseFn`.
```js
env.get('PORT', {
required: false,
defaultsTo: 3000,
parseFn: (val) => {
const port = Number(val)
assert(Number.isSafeInteger(port) && port >= 0 && port <= 65535)
return port
}
})
```
### `0.0.2`
**Fixed**
- Small codebase issues and typos.
### `0.0.1`
**Added**
- Initial realease.