Comparing version 3.0.1 to 3.0.2
@@ -0,3 +1,5 @@ | ||
## 3.0.2 (19/10/17) | ||
* Restore support for use in browser based applications | ||
## 3.0.1 (13/10/17) | ||
## 3.0.1 (19/10/17) | ||
* Fix bug that caused default values to be ignored | ||
@@ -4,0 +6,0 @@ |
'use strict' | ||
const EnvVarError = require('./env-error') | ||
const readdirSync = require('fs').readdirSync | ||
const join = require('path').join | ||
const extname = require('path').extname | ||
const basename = require('path').basename | ||
const camel = require('camelcase') | ||
@@ -28,5 +23,3 @@ /** | ||
*/ | ||
function generateAccessor (container, varName, defValue, accessorPath) { | ||
let accessor = require(accessorPath) | ||
function generateAccessor (container, varName, defValue, accessor) { | ||
return function () { | ||
@@ -60,23 +53,2 @@ let value = container[varName] | ||
/** | ||
* Loads any accessor stored in the accessors folder and makes it available via | ||
* a function attache to the Ojbect returned from this function. | ||
* @return {Object} | ||
*/ | ||
function getAccessors (container, varName, defValue) { | ||
return readdirSync(join(__dirname, './accessors')) | ||
.filter((name) => extname(name) === '.js') | ||
.reduce((data, accessor) => { | ||
// Create the "asThing" naming convention | ||
const exportName = camel(`as-${basename(accessor, '.js')}`) | ||
// Generates the path for requiring the accessor | ||
const reqPath = join(__dirname, './accessors', accessor) | ||
return Object.assign(data, { | ||
[exportName]: generateAccessor(container, varName, defValue, reqPath) | ||
}) | ||
}, {}) | ||
} | ||
/** | ||
* Returns an Object that contains functions to read and specify the format of | ||
@@ -87,14 +59,37 @@ * the variable you wish to have returned | ||
module.exports = function getVariableAccessors (container, varName, defValue) { | ||
const accessors = getAccessors(container, varName, defValue) | ||
// Cannot dynamically load accessors if we want to support browsers | ||
const accessors = { | ||
asArray: generateAccessor(container, varName, defValue, require('./accessors/array')), | ||
/** | ||
* Ensures a variable is set in the given environment container. Throws an | ||
* EnvVarError if the variable is not set or a default is not provided | ||
*/ | ||
accessors.required = () => { | ||
if (typeof container[varName] === 'undefined' && typeof defValue === 'undefined') { | ||
throw new EnvVarError(`"${varName}" is a required variable, but it was not set`) | ||
asBoolStrict: generateAccessor(container, varName, defValue, require('./accessors/bool-strict')), | ||
asBool: generateAccessor(container, varName, defValue, require('./accessors/bool')), | ||
asFloatNegative: generateAccessor(container, varName, defValue, require('./accessors/float-negative')), | ||
asFloatPositive: generateAccessor(container, varName, defValue, require('./accessors/float-positive')), | ||
asFloat: generateAccessor(container, varName, defValue, require('./accessors/float')), | ||
asIntNegative: generateAccessor(container, varName, defValue, require('./accessors/int-negative')), | ||
asIntPositive: generateAccessor(container, varName, defValue, require('./accessors/int-positive')), | ||
asInt: generateAccessor(container, varName, defValue, require('./accessors/int')), | ||
asJsonArray: generateAccessor(container, varName, defValue, require('./accessors/json-array')), | ||
asJsonObject: generateAccessor(container, varName, defValue, require('./accessors/json-object')), | ||
asJson: generateAccessor(container, varName, defValue, require('./accessors/json')), | ||
asString: generateAccessor(container, varName, defValue, require('./accessors/string')), | ||
asUrlObject: generateAccessor(container, varName, defValue, require('./accessors/url-object')), | ||
asUrlString: generateAccessor(container, varName, defValue, require('./accessors/url-string')), | ||
/** | ||
* Ensures a variable is set in the given environment container. Throws an | ||
* EnvVarError if the variable is not set or a default is not provided | ||
*/ | ||
required: function () { | ||
if (typeof container[varName] === 'undefined' && typeof defValue === 'undefined') { | ||
throw new EnvVarError(`"${varName}" is a required variable, but it was not set`) | ||
} | ||
return accessors | ||
} | ||
return accessors | ||
} | ||
@@ -101,0 +96,0 @@ |
{ | ||
"name": "env-var", | ||
"version": "3.0.1", | ||
"version": "3.0.2", | ||
"description": "Solution for loading and sanatizing environment variables in node.js with correct typings", | ||
@@ -5,0 +5,0 @@ "main": "env-var.js", |
@@ -21,3 +21,3 @@ # env-var | ||
```js | ||
var PARALLEL_LIMIT = env.get('PARALLEL_LIMIT').required().asIntPositive(); | ||
const LIMIT = env.get('LIMIT').required().asIntPositive(); | ||
``` | ||
@@ -27,3 +27,3 @@ | ||
1. If *PARALLEL_LIMIT* is not set _required()_ will raise an exception. | ||
1. If *LIMIT* is not set _required()_ will raise an exception. | ||
2. If it is set, but not a positive integer _asIntPositive()_ will raise an | ||
@@ -36,3 +36,3 @@ exception. | ||
## TypeScript | ||
To use with TypeScript, just import it like this: | ||
To use with TypeScript, just import and use the same as JavaScript: | ||
@@ -42,3 +42,3 @@ ```ts | ||
const stringVar = env.get('STRING').required().asString(); | ||
const LIMIT = env.get('LIMIT').required().asIntPositive(); | ||
``` | ||
@@ -48,5 +48,3 @@ | ||
Over time it became apparent that parsing environment variables is a | ||
repetitive task, and testing code that relies on them is cumbersome unless | ||
using an inversion of control system for declaring modules so we can inject a | ||
fake *process.env*. | ||
repetitive task, and testing code that relies on can be cumbersome. | ||
@@ -140,3 +138,4 @@ Take this example: | ||
// Read PORT variable and ensure it's a positive integer. If it is not a | ||
// positive integer or is not set the process will exit with an error | ||
// positive integer or is not set the process will exit with an error (unless | ||
// you catch it using a try/catch or "uncaughtException" handler) | ||
const PORT = env.get('PORT').required().asIntPositive() | ||
@@ -327,2 +326,5 @@ | ||
module.exports = function numberZero (raiseError, environmentValue) { | ||
// Your custom code should go here...below code is an example | ||
const val = parseInt(environmentValue) | ||
@@ -338,8 +340,16 @@ | ||
The `env-var` module will auto load this new file and add it to the public | ||
exports with the name in camelcase format `asNumberZero` | ||
Next update the `accessors` Object in `getVariableAccessors()` in | ||
`lib/variable.js` to include your new module. The naming convention should be of | ||
the format "asTypeSubtype", so for our `number-zero` example it would be done | ||
like so: | ||
```js | ||
asNumberZero: generateAccessor(container, varName, defValue, require('./accessors/number-zero')), | ||
``` | ||
Once you've done that, add some unit tests and use it like so: | ||
```js | ||
// Uses your new function to ensure the SOME_NUMBER is the integer 0 | ||
env.get('SOME_NUMBER').asNumberZero() | ||
``` |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
29836
348
1
426