@axah/env-utils
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -10,10 +10,5 @@ "use strict"; | ||
function parseBooleanFromEnv(options) { | ||
const { | ||
key, | ||
throwError | ||
} = options; | ||
function parseBooleanFromEnv(key, valueInDev) { | ||
if (!(0, _utils.isValueFilled)(key)) { | ||
return (0, _utils.getDevValueOrHandleError)(options); | ||
return (0, _utils.getDevValueOrThrowError)(key, valueInDev); | ||
} | ||
@@ -35,4 +30,3 @@ | ||
(0, _utils.handleError)(`Could not read the env-variable ${key}, because the value "${value}" cannot be parsed to a boolean.`, throwError); | ||
return null; | ||
throw new Error(`Could not read the env-variable ${key}, because the value "${value}" cannot be parsed to a boolean.`); | ||
} |
@@ -10,10 +10,5 @@ "use strict"; | ||
function parseDateFromEnv(options) { | ||
const { | ||
key, | ||
throwError | ||
} = options; | ||
function parseDateFromEnv(key, valueInDev) { | ||
if (!(0, _utils.isValueFilled)(key)) { | ||
return (0, _utils.getDevValueOrHandleError)(options); | ||
return (0, _utils.getDevValueOrThrowError)(key, valueInDev); | ||
} | ||
@@ -27,4 +22,3 @@ | ||
// $FlowFixMe | ||
(0, _utils.handleError)(`Could not read the env-variable ${key}, because the value "${value}" cannot be parsed to a date.`, throwError); | ||
return null; | ||
throw new Error(`Could not read the env-variable ${key}, because the value "${value}" cannot be parsed to a date.`); | ||
} | ||
@@ -31,0 +25,0 @@ |
@@ -10,10 +10,5 @@ "use strict"; | ||
function parseFloatFromEnv(options) { | ||
const { | ||
key, | ||
throwError | ||
} = options; | ||
function parseFloatFromEnv(key, valueInDev) { | ||
if (!(0, _utils.isValueFilled)(key)) { | ||
return (0, _utils.getDevValueOrHandleError)(options); | ||
return (0, _utils.getDevValueOrThrowError)(key, valueInDev); | ||
} | ||
@@ -27,4 +22,3 @@ | ||
// $FlowFixMe | ||
(0, _utils.handleError)(`Could not read the env-variable ${key}, because the value "${value}" cannot be parsed to a float.`, throwError); | ||
return null; | ||
throw new Error(`Could not read the env-variable ${key}, because the value "${value}" cannot be parsed to a float.`); | ||
} | ||
@@ -31,0 +25,0 @@ |
@@ -10,10 +10,5 @@ "use strict"; | ||
function parseIntegerFromEnv(options) { | ||
const { | ||
key, | ||
throwError | ||
} = options; | ||
function parseIntegerFromEnv(key, valueInDev) { | ||
if (!(0, _utils.isValueFilled)(key)) { | ||
return (0, _utils.getDevValueOrHandleError)(options); | ||
return (0, _utils.getDevValueOrThrowError)(key, valueInDev); | ||
} | ||
@@ -27,4 +22,3 @@ | ||
// $FlowFixMe | ||
(0, _utils.handleError)(`Could not read the env-variable ${key}, because the value "${value}" cannot be parsed to an integer.`, throwError); | ||
return null; | ||
throw new Error(`Could not read the env-variable ${key}, because the value "${value}" cannot be parsed to an integer.`); | ||
} | ||
@@ -31,0 +25,0 @@ |
@@ -10,9 +10,5 @@ "use strict"; | ||
function parseStringFromEnv(options) { | ||
const { | ||
key | ||
} = options; | ||
function parseStringFromEnv(key, valueInDev) { | ||
if (!(0, _utils.isValueFilled)(key)) { | ||
return (0, _utils.getDevValueOrHandleError)(options); | ||
return (0, _utils.getDevValueOrThrowError)(key, valueInDev); | ||
} | ||
@@ -19,0 +15,0 @@ |
@@ -6,3 +6,3 @@ "use strict"; | ||
}); | ||
exports.getDevValueOrHandleError = exports.handleError = exports.isValueFilled = void 0; | ||
exports.getDevValueOrThrowError = exports.isValueFilled = void 0; | ||
@@ -15,10 +15,2 @@ /** | ||
const isValueFilled = key => Object.prototype.hasOwnProperty.call(process.env, key) && process.env[key] !== null && process.env[key] !== undefined && process.env[key] !== ''; | ||
exports.isValueFilled = isValueFilled; | ||
const handleError = (message, throwError = true) => { | ||
if (throwError) { | ||
throw new Error(message); | ||
} | ||
}; | ||
/** | ||
@@ -30,19 +22,11 @@ * Convenience function which returns the valueInDev, if the environment ist development (or test), | ||
exports.handleError = handleError; | ||
exports.isValueFilled = isValueFilled; | ||
const getDevValueOrHandleError = options => { | ||
const { | ||
key, | ||
valueInDev, | ||
throwError | ||
} = options; | ||
const getDevValueOrThrowError = (key, valueInDev) => { | ||
if (process.env.NODE_ENV !== 'development') { | ||
handleError(`The environment variable ${key} has not been defined or is empty.`, throwError); | ||
return null; | ||
throw new Error(`The environment variable ${key} has not been defined or is empty.`); | ||
} | ||
if (valueInDev === undefined) { | ||
handleError(`The dev-environment variable ${key} has not been defined.`, throwError); | ||
return null; | ||
throw new Error(`The dev-environment variable ${key} has not been defined.`); | ||
} | ||
@@ -53,2 +37,2 @@ | ||
exports.getDevValueOrHandleError = getDevValueOrHandleError; | ||
exports.getDevValueOrThrowError = getDevValueOrThrowError; |
{ | ||
"name": "@axah/env-utils", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"main": "lib/main.js", | ||
@@ -5,0 +5,0 @@ "license": "UNLICENSED", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
11744
146