env-verifier
Advanced tools
Comparing version 1.1.2 to 1.1.3
{ | ||
"name": "env-verifier", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "\"Make sure you have all your env variables!\"", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -27,29 +27,53 @@ # env-verifier | ||
```javascript | ||
const { strictVerify } = require('env-verifier') | ||
//throws on one or more env misses | ||
module.exports = strictVerify({ | ||
database: { | ||
name: 'DB_NAME', | ||
host: 'DB_HOST', | ||
password: 'DB_PASSWORD' | ||
}, | ||
baseUrl: 'BASE_URL' | ||
}) | ||
``` | ||
This package exposes two verification functions - `verify` and `strictVerify`. Use `verify` (as seen below) when you want to handle reporting missing values, and `strictVerify` (as seen above) when you want, when any `env` misses are encountered, us to throw a descriptive error containing all `env` misses. | ||
Use example for `verify`: | ||
```javascript | ||
const { verify } = require('env-verifier') | ||
const config = { | ||
const { config, errors } = verify({ | ||
database: { | ||
name: DB_NAME | ||
host: DB_HOST | ||
password: DB_PASSWORD | ||
name: 'DB_NAME' | ||
host: 'DB_HOST' | ||
password: 'DB_PASSWORD' | ||
}, | ||
baseUrl: BASE_URL | ||
} | ||
baseUrl: 'BASE_URL' | ||
}) | ||
const { config: builtConfig, errors } = verify(config) | ||
// do custom error logging, possibly throw your own errors | ||
errors.forEach(console.error) | ||
errors.foreach(error => console.error(error)) | ||
module.exports = builtConfig | ||
module.exports = config | ||
``` | ||
This package exposes two verification functions - `verify` and `strictVerify`. Use `verify` (as seen above) when you want to handle reporting missing values, and `strictVerify` when you want us to throw a descriptive error. | ||
You can pass in your own `env` object as a parameter as long as its an object that is non-nested and has key value pairs with `undefined` or `string` as their value type. | ||
Function signatures: | ||
--- | ||
## Usage Notes | ||
- [Function Signatures](#functionSignatures) | ||
- [Arbitrary Value Insertion](#arbitraryValueInsertion) | ||
- [Error Generation and Reporting](#errorGenerationAndReporting) | ||
- [Variable Transformation (TransformTuple)](#variableTransformation) | ||
#### <a name="functionSignatures"><a/> Function signatures | ||
```typescript | ||
export interface TransformFn { | ||
interface TransformFn { | ||
(envValue: string): any | ||
@@ -60,3 +84,3 @@ } | ||
// [envKeyName, TransformFn] | ||
export type TransformTuple = [string, TransformFn] | ||
type TransformTuple = [string, TransformFn] | ||
@@ -71,3 +95,3 @@ interface ConfigWithEnvKeys { | ||
export interface VerifiedConfig { | ||
interface VerifiedConfig { | ||
[key: string]: any | string | VerifiedConfig | ||
@@ -80,25 +104,11 @@ } | ||
function insert(value: any): InsertValue //see inserting arbitrary values below | ||
function insert(value: any): InsertValue | ||
function verify(config: Config, env: Env = process.env): { config: MappedConfig, errors: string[] } | ||
function verify(config: ConfigWithEnvKeys, env: Env = process.env): { config: MappedConfig, errors: string[] } | ||
function strictVerify(config: Config, env: Env = process.env): VerifiedConfig //See Errors section | ||
function strictVerify(config: ConfigWithEnvKeys, env: Env = process.env): VerifiedConfig | ||
``` | ||
Use example for `strictVerify`: | ||
#### <a name="arbitraryValueInsertion"><a/> Arbitrary Value Insertion | ||
```javascript | ||
//will throw on undefined or empty string env variables | ||
module.exports = strictVerify({ | ||
database: { | ||
name: 'DB_NAME' | ||
host: 'DB_HOST' | ||
password: 'DB_PASSWORD' | ||
}, | ||
baseUrl: 'BASE_URL' | ||
}) | ||
``` | ||
#### Arbitrary value insertion | ||
You may have values that aren't present on your `env` object, but that you would like to live in your config object, this can be achieved by using the `insert()` function. | ||
@@ -112,3 +122,3 @@ | ||
... // other env key names | ||
}) | ||
}).config | ||
@@ -122,3 +132,3 @@ //exports: | ||
#### Error generation and reporting | ||
#### <a name="errorGenerationAndReporting"><a/> Error Generation and Reporting | ||
@@ -129,6 +139,8 @@ Error reports are generated when an `env` variable is missing. An `env` variable is considered missing under the following circumstances: | ||
- an empty string, `''`, is returned from the `env` object. | ||
`verify` will always return the errors array, but it will be an empty array if there are no `env` misses. | ||
`strictVerify` will not throw an error on the first encountered missing `env` value. Instead it will continue in order to report all missing `env` variables. | ||
#### Variable Transformation (TransformTuple) | ||
#### <a name="variableTransformation"><a/> Variable Transformation (TransformTuple) | ||
@@ -135,0 +147,0 @@ Since process.env only returns strings, sometimes its necessary to transform those strings into something else (IE: transform the string `"true"` to a boolean `true`) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
49156
198