Comparing version 1.0.3 to 1.1.0
{ | ||
"name": "envboss", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "package for environment variables", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -16,23 +16,3 @@ [![Known Vulnerabilities](https://snyk.io/test/github/PayU/envboss/badge.svg?targetFile=package.json)](https://snyk.io/test/github/PayU/envboss?targetFile=package.json) | ||
## How does it work? | ||
It goes over the configuration you provide, and returns an object where the `key` is the `<ENV_PARAM_NAME>` and the value is `process.env[<ENV_PARAM_NAME>]` after it was validated and sanitized. | ||
### configuration | ||
`mandatory` - sets this param as mandatory. | ||
`default` - lets you define a default value. | ||
`validationFunction` - will help you validate the values. | ||
`validValues`- lets you describe what values are valid. | ||
`wrappingFunction` - converts the envparam value to the given value. By default, | ||
if `default` value is provided, `process.env[<ENV_PARAM_NAME>]` value will be converted to the type default's value type | ||
If you wouldn't like to validate envparams (e.g. in tests) pass false to | ||
```createEnvObject(ENV_VARS_CONFIG,false)``` | ||
***Installation*** | ||
@@ -48,9 +28,11 @@ ```bash | ||
import { createEnvObject, mandatory } from 'envboss'; | ||
import { createEnvObject, mandatory, Types } from 'envboss'; | ||
const ENV_VARS_CONFIG = { | ||
CLUSTER: { mandatory }, | ||
CLUSTER: { mandatory: true }, | ||
ENVIRONMENT: { mandatory, validValues: ['live', 'sandbox']}, | ||
STORAGE_PORT: { mandatory, wrappingFunction: Number }, | ||
IS_MASTER: { mandatory }, | ||
IS_MASTER: { mandatory, type: Types.Boolean }, | ||
IP_ADDRESSES_ARR: { mandatory, type: Types.Array }, //resolves as array of ip addresses | ||
STORAGE_PORT: { mandatory, wrappingFunction: someFunc }, | ||
// optional | ||
@@ -71,1 +53,24 @@ PORT: { default: 8082 }, | ||
``` | ||
## How does it work? | ||
It goes over the configuration you provide, and returns an object where the `key` is the `<ENV_PARAM_NAME>` and the value is `process.env[<ENV_PARAM_NAME>]` after it was validated and sanitized. | ||
### configuration | ||
`mandatory` - when set to true, sets this param as mandatory. Can be used as `mandatory` only when required from 'envboss'; | ||
`default` - lets you define a default value. | ||
`type` - converts the given envParam value from 'string' to given Type. Supported types: `Number, Boolean, String, Array`. By default, | ||
if `default` config is provided, `process.env[<ENV_PARAM_NAME>]` value will be converted to the type default's value type | ||
`validationFunction` - will help you validate the values. | ||
`validValues`- lets you describe what values are valid. | ||
`wrappingFunction` - converts the envparam value to the given value. | ||
If you wouldn't like to validate envparams (e.g. in tests) pass false to | ||
```createEnvObject(ENV_VARS_CONFIG,false)``` | ||
@@ -17,4 +17,11 @@ declare module 'envboss' | ||
} | ||
export enum Types{ | ||
Number= 'number', | ||
Boolean= 'boolean', | ||
String= 'string', | ||
Array= 'Array' | ||
} | ||
export declare const mandatory:boolean | ||
export declare function createEnvObject(paramsConfig:ParamsConfiguration, shouldValidateEnvParams?:boolean): EnvironmentVariables; | ||
@@ -1,4 +0,4 @@ | ||
const isEnvParamEmpty = ([paramName]) => !process.env[paramName] || process.env[paramName].trim() === ''; | ||
const isMandatory = ([, envParamConfig]) => envParamConfig.mandatory; | ||
// eslint-disable-next-line no-unused-vars | ||
const isMandatory = ([name, envParamConfig]) => envParamConfig.mandatory; | ||
@@ -30,21 +30,35 @@ function validateMandatoryEnvParams(paramsConfig) { | ||
} | ||
function getWrappingFunctionByDefaultValue(value) { | ||
let wrappingFunction; | ||
switch (typeof value) { | ||
case 'number': | ||
wrappingFunction = Number; | ||
const wrappingFunction = getConvertingFunctionByType(typeof value); | ||
return wrappingFunction; | ||
} | ||
function getConvertingFunctionByType(type) { | ||
let convertingFunc; | ||
switch (type) { | ||
case Types.Number: | ||
convertingFunc = Number; | ||
break; | ||
case 'boolean': | ||
wrappingFunction = Boolean; | ||
case Types.Boolean: | ||
convertingFunc = (v) => v === 'true'; | ||
break; | ||
case 'string': | ||
wrappingFunction = String; | ||
case Types.String: | ||
convertingFunc = String; | ||
break; | ||
case Types.Array: | ||
convertingFunc = (v) => v.split(','); | ||
break; | ||
default: | ||
wrappingFunction = (v)=>v; | ||
convertingFunc = (v) => v; | ||
} | ||
return wrappingFunction; | ||
return convertingFunc; | ||
} | ||
function convertValueToRequiredType(type, value) { | ||
const convertingFunction = getConvertingFunctionByType(type); | ||
return convertingFunction(value); | ||
} | ||
function createEnvObject(paramsConfig, shouldValidateEnvParams = true) { | ||
@@ -55,26 +69,27 @@ if (shouldValidateEnvParams) { | ||
const result = {}; | ||
Object.entries(paramsConfig).forEach(([paramName, config]) => { | ||
let value = process.env[paramName] || config.default; | ||
Object.entries(paramsConfig) | ||
.forEach(([paramName, config]) => { | ||
let value = process.env[paramName] || config.default; | ||
if (config.wrappingFunction) { | ||
value = applyWrappingFunction(paramName, config.wrappingFunction, value); | ||
} else { | ||
const wrappingFunction = getWrappingFunctionByDefaultValue(config.default); | ||
value = applyWrappingFunction(paramName, wrappingFunction, value); | ||
} | ||
if (shouldValidateEnvParams) { | ||
if (config.validationFunction) { | ||
applyValidationFunction(paramName, config, value); | ||
if (config.type) { | ||
value = convertValueToRequiredType(config.type, value); | ||
} else if (config.wrappingFunction) { | ||
value = applyWrappingFunction(paramName, config.wrappingFunction, value); | ||
} else { | ||
const wrappingFunction = getWrappingFunctionByDefaultValue(config.default); | ||
value = applyWrappingFunction(paramName, wrappingFunction, value); | ||
} | ||
if (config.validValues) { | ||
if (!config.validValues.includes(value)) { | ||
if (shouldValidateEnvParams) { | ||
if (config.validationFunction) { | ||
applyValidationFunction(paramName, config, value); | ||
} | ||
if (config.validValues && !config.validValues.includes(value)) { | ||
throw new Error(`value '${value}' of env param '${paramName}' is not valid, check paramsConfig to see valid values`); | ||
} | ||
} | ||
} | ||
result[paramName] = value; | ||
}); | ||
result[paramName] = value; | ||
}); | ||
@@ -84,5 +99,13 @@ return result; | ||
const Types = { | ||
Number: 'number', | ||
Boolean: 'boolean', | ||
String: 'string', | ||
Array: 'Array', | ||
}; | ||
module.exports = { | ||
createEnvObject, | ||
mandatory: true, | ||
Types, | ||
}; |
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
19346
114
74