@forestadmin/context
Advanced tools
Comparing version 1.39.3 to 1.40.0
{ | ||
"name": "@forestadmin/context", | ||
"version": "1.39.3", | ||
"version": "1.40.0", | ||
"description": "Minimal context management helper for applications and libraries.", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -64,70 +64,94 @@ const Metadata = require('./metadata'); | ||
addReplacement(path, name, value, options) { | ||
this._metadata.add(path, name, 'replacement', value, options); | ||
this._setNewValue(name, value, options); | ||
return this; | ||
try { | ||
this._metadata.add(path, name, 'replacement', value, options); | ||
this._setNewValue(name, value, options); | ||
return this; | ||
} catch (cause) { | ||
throw new Error(`Adding replacement on path "${this._metadata.getCurrentPath()}": ${cause.message}`, { cause }); | ||
} | ||
} | ||
addValue(path, name, value, options) { | ||
this._metadata.add(path, name, 'value', value, options); | ||
this._setNewValue( | ||
name, | ||
(typeof value === 'function') ? value(this.get()) : value, | ||
options, | ||
); | ||
return this; | ||
try { | ||
this._metadata.add(path, name, 'value', value, options); | ||
this._setNewValue( | ||
name, | ||
(typeof value === 'function') ? value(this.get()) : value, | ||
options, | ||
); | ||
return this; | ||
} catch (cause) { | ||
throw new Error(`Adding value on path "${this._metadata.getCurrentPath()}": ${cause.message}`, { cause }); | ||
} | ||
} | ||
addRawValue(path, name, value, options) { | ||
this._metadata.add(path, name, 'value', value, options); | ||
this._setNewValue(name, value, options); | ||
return this; | ||
try { | ||
this._metadata.add(path, name, 'value', value, options); | ||
this._setNewValue(name, value, options); | ||
return this; | ||
} catch (cause) { | ||
throw new Error(`Adding raw value on path "${this._metadata.getCurrentPath()}": ${cause.message}`, { cause }); | ||
} | ||
} | ||
addNumber(path, name, value, options = {}) { | ||
this._metadata.add(path, name, 'number', value, options); | ||
const { | ||
min = Number.NEGATIVE_INFINITY, | ||
default: defaultValue, | ||
max = Number.POSITIVE_INFINITY, | ||
nullable, | ||
} = options; | ||
const rawValue = (typeof value === 'function') ? value(this.get()) : value; | ||
if (rawValue === null) { | ||
if (!nullable) throw new Error(`Adding number on path "${this._metadata.getCurrentPath()}": Specified value is null`); | ||
this._setNewValue(name, rawValue, options); | ||
return this; | ||
} | ||
if (rawValue === undefined) { | ||
if (defaultValue === undefined) throw new Error(`Adding number on path "${this._metadata.getCurrentPath()}": No specified value and no default value`); | ||
this._setNewValue(name, defaultValue, options); | ||
return this; | ||
} | ||
try { | ||
this._metadata.add(path, name, 'number', value, options); | ||
const { | ||
min = Number.NEGATIVE_INFINITY, | ||
default: defaultValue, | ||
max = Number.POSITIVE_INFINITY, | ||
nullable, | ||
} = options; | ||
const rawValue = (typeof value === 'function') ? value(this.get()) : value; | ||
if (rawValue === null) { | ||
if (!nullable) throw new Error('Specified value is null'); | ||
this._setNewValue(name, rawValue, options); | ||
return this; | ||
} | ||
if (rawValue === undefined) { | ||
if (defaultValue === undefined) throw new Error('No specified value and no default value'); | ||
this._setNewValue(name, defaultValue, options); | ||
return this; | ||
} | ||
const expectedNumber = Number(rawValue); | ||
if (Number.isNaN(expectedNumber)) { | ||
if (!defaultValue) throw new Error(`Adding number on path "${this._metadata.getCurrentPath()}": Specified value is not a number: "${rawValue}"`); | ||
this._setNewValue(name, defaultValue, options); | ||
const expectedNumber = Number(rawValue); | ||
if (Number.isNaN(expectedNumber)) { | ||
if (!defaultValue) throw new Error(`Specified value is not a number: "${rawValue}"`); | ||
this._setNewValue(name, defaultValue, options); | ||
return this; | ||
} | ||
if (expectedNumber < min) throw new Error(`Specified value is below min: "${expectedNumber}" (min=${min})`); | ||
if (max < expectedNumber) throw new Error(`Specified value is above max: "${expectedNumber}" (max=${max})`); | ||
this._setNewValue(name, expectedNumber, options); | ||
return this; | ||
} catch (cause) { | ||
throw new Error(`Adding number on path "${this._metadata.getCurrentPath()}": ${cause.message}`, { cause }); | ||
} | ||
if (expectedNumber < min) throw new Error(`Adding number on path "${this._metadata.getCurrentPath()}": Specified value is below min: "${expectedNumber}" (min=${min})`); | ||
if (max < expectedNumber) throw new Error(`Adding number on path "${this._metadata.getCurrentPath()}": Specified value is above max: "${expectedNumber}" (max=${max})`); | ||
this._setNewValue(name, expectedNumber, options); | ||
return this; | ||
} | ||
addInstance(path, name, instance, options) { | ||
this._metadata.add(path, name, 'instance', instance, options); | ||
this._setNewValue( | ||
name, | ||
(typeof instance === 'function') ? instance(this.get()) : instance, | ||
options, | ||
); | ||
return this; | ||
try { | ||
this._metadata.add(path, name, 'instance', instance, options); | ||
this._setNewValue( | ||
name, | ||
(typeof instance === 'function') ? instance(this.get()) : instance, | ||
options, | ||
); | ||
return this; | ||
} catch (cause) { | ||
throw new Error(`Adding instance on path "${this._metadata.getCurrentPath()}": ${cause.message}`, { cause }); | ||
} | ||
} | ||
addFunction(path, name, theFunction, options) { | ||
this._metadata.add(path, name, 'function', theFunction, options); | ||
this._setNewValue(name, theFunction, options); | ||
return this; | ||
try { | ||
this._metadata.add(path, name, 'function', theFunction, options); | ||
this._setNewValue(name, theFunction, options); | ||
return this; | ||
} catch (cause) { | ||
throw new Error(`Adding function on path "${this._metadata.getCurrentPath()}": ${cause.message}`, { cause }); | ||
} | ||
} | ||
@@ -148,33 +172,49 @@ | ||
addUsingFunctionStack(path, name, factoryFunctionList, options) { | ||
this._metadata.add(path, name, 'function**', factoryFunctionList, options); | ||
this._checkKeyAvailable(name); | ||
factoryFunctionList.forEach((factoryFunction) => { | ||
const bag = this.get(); | ||
const value = factoryFunction(bag); | ||
this._setValue(name, value, options); | ||
}); | ||
try { | ||
this._metadata.add(path, name, 'function**', factoryFunctionList, options); | ||
this._checkKeyAvailable(name); | ||
factoryFunctionList.forEach((factoryFunction) => { | ||
const bag = this.get(); | ||
const value = factoryFunction(bag); | ||
this._setValue(name, value, options); | ||
}); | ||
return this; | ||
return this; | ||
} catch (cause) { | ||
throw new Error(`Using factory function stack on path "${this._metadata.getCurrentPath()}": ${cause.message}`, { cause }); | ||
} | ||
} | ||
addUsingClass(path, name, Class, options) { | ||
this._metadata.add(path, name, 'class', Class, options); | ||
const instance = this._instanciate(path, name, Class, options); | ||
this._setNewValue(name, instance, options); | ||
return this; | ||
try { | ||
this._metadata.add(path, name, 'class', Class, options); | ||
const instance = this._instanciate(path, name, Class, options); | ||
this._setNewValue(name, instance, options); | ||
return this; | ||
} catch (cause) { | ||
throw new Error(`Using class on path "${this._metadata.getCurrentPath()}": ${cause.message}`, { cause }); | ||
} | ||
} | ||
addModule(path, name, module, options) { | ||
this._metadata.add(path, name, 'module', module, options); | ||
this._setNewValue( | ||
name, | ||
(typeof module === 'function') ? module() : module, | ||
options, | ||
); | ||
return this; | ||
try { | ||
this._metadata.add(path, name, 'module', module, options); | ||
this._setNewValue( | ||
name, | ||
(typeof module === 'function') ? module() : module, | ||
options, | ||
); | ||
return this; | ||
} catch (cause) { | ||
throw new Error(`Using module on path "${this._metadata.getCurrentPath()}": ${cause.message}`, { cause }); | ||
} | ||
} | ||
with(name, work) { | ||
work(this._lookup(name)); | ||
return this; | ||
try { | ||
work(this._lookup(name)); | ||
return this; | ||
} catch (cause) { | ||
throw new Error(`Using with on path "${this._metadata.getCurrentPath()}": ${cause.message}`, { cause }); | ||
} | ||
} | ||
@@ -181,0 +221,0 @@ |
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
63656
745