@mocks-server/config
Advanced tools
Comparing version 2.0.0-beta.2 to 2.0.0-beta.3
@@ -59,2 +59,18 @@ "use strict"; | ||
} | ||
read(namespaces, { allowUnknownOption }) { | ||
const config = {}; | ||
// Create commander options | ||
const commanderOptionsData = {}; | ||
const program = new commander_1.Command(); | ||
this._createNamespaceOptions(namespaces, program, commanderOptionsData); | ||
// Get commander results | ||
if (allowUnknownOption) { | ||
program.allowUnknownOption(); | ||
} | ||
program.parse(); | ||
const results = program.opts(); | ||
// Convert commander results into object with namespaces levels | ||
this._config = this._commanderResultsToConfigObject(results, config, commanderOptionsData); | ||
return this._config; | ||
} | ||
_createNamespaceInterfaceOptions(namespace, command, optionsData) { | ||
@@ -97,19 +113,3 @@ namespace.options.forEach((option) => { | ||
} | ||
read(namespaces, { allowUnknownOption }) { | ||
const config = {}; | ||
// Create commander options | ||
const commanderOptionsData = {}; | ||
const program = new commander_1.Command(); | ||
this._createNamespaceOptions(namespaces, program, commanderOptionsData); | ||
// Get commander results | ||
if (allowUnknownOption) { | ||
program.allowUnknownOption(); | ||
} | ||
program.parse(); | ||
const results = program.opts(); | ||
// Convert commander results into object with namespaces levels | ||
this._config = this._commanderResultsToConfigObject(results, config, commanderOptionsData); | ||
return this._config; | ||
} | ||
}; | ||
exports.CommandLineArguments = CommandLineArguments; |
@@ -28,2 +28,14 @@ "use strict"; | ||
constructor({ moduleName, mergeArrays = true } = { moduleName: "", mergeArrays: true }) { | ||
Object.defineProperty(this, "addOption", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: void 0 | ||
}); | ||
Object.defineProperty(this, "addOptions", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: void 0 | ||
}); | ||
Object.defineProperty(this, "_initializated", { | ||
@@ -149,14 +161,2 @@ enumerable: true, | ||
}); | ||
Object.defineProperty(this, "addOption", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: void 0 | ||
}); | ||
Object.defineProperty(this, "addOptions", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: void 0 | ||
}); | ||
this._initializated = false; | ||
@@ -237,2 +237,85 @@ this._deepMergeOptions = !mergeArrays | ||
} | ||
get programmaticLoadedValues() { | ||
return Object.assign({}, this._programmaticConfig); | ||
} | ||
get fileLoadedValues() { | ||
return Object.assign({}, this._fileConfig); | ||
} | ||
get envLoadedValues() { | ||
return Object.assign({}, this._envConfig); | ||
} | ||
get argsLoadedValues() { | ||
return Object.assign({}, this._argsConfig); | ||
} | ||
get loadedFile() { | ||
return this._files.loadedFile; | ||
} | ||
get namespaces() { | ||
return this._namespaces.filter((namespace) => !namespace.isRoot); | ||
} | ||
get options() { | ||
return this._rootNamespace.options; | ||
} | ||
get root() { | ||
return this; | ||
} | ||
get value() { | ||
return (0, ConfigNamespaceHelpers_1.getNamespacesValues)(this._namespaces); | ||
} | ||
set value(configuration) { | ||
this.set(configuration); | ||
} | ||
validate(config, { allowAdditionalProperties = false } = {}) { | ||
return (0, Validation_1.validateConfig)(config, { | ||
namespaces: this._namespaces, | ||
allowAdditionalProperties, | ||
}); | ||
} | ||
getValidationSchema({ allowAdditionalProperties = false, } = {}) { | ||
return (0, Validation_1.getValidationSchema)({ | ||
namespaces: this._namespaces, | ||
allowAdditionalProperties, | ||
removeCustomProperties: true, | ||
}); | ||
} | ||
init(programmaticConfig = {}) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this._programmaticConfig = programmaticConfig; | ||
yield this._load({ allowUnknown: true }); | ||
}); | ||
} | ||
load(programmaticConfig) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this._initializated) { | ||
yield this.init(programmaticConfig); | ||
} | ||
yield this._load(); | ||
this._startNamespacesEvents(); | ||
}); | ||
} | ||
addNamespace(name) { | ||
(0, ConfigNamespaceHelpers_1.checkNamespaceName)(name, { | ||
namespaces: this._namespaces, | ||
options: this._rootNamespace && this._rootNamespace.options, | ||
}); | ||
const namespace = new ConfigNamespace_1.ConfigNamespace(name, { brothers: this._namespaces, root: this }); | ||
this._namespaces.push(namespace); | ||
return namespace; | ||
} | ||
namespace(name) { | ||
return (0, ConfigNamespaceHelpers_1.findObjectWithName)(this._namespaces, name); | ||
} | ||
option(name) { | ||
return (0, ConfigNamespaceHelpers_1.findObjectWithName)(this._rootNamespace.options, name); | ||
} | ||
set(configuration = {}, options = {}) { | ||
this._namespaces.forEach((namespace) => { | ||
if (!namespace.isRoot) { | ||
namespace.set(configuration[namespace.name] || {}, options); | ||
} | ||
else { | ||
namespace.set(configuration, options); | ||
} | ||
}); | ||
} | ||
_loadFromFile() { | ||
@@ -271,15 +354,2 @@ return __awaiter(this, void 0, void 0, function* () { | ||
} | ||
validate(config, { allowAdditionalProperties = false } = {}) { | ||
return (0, Validation_1.validateConfig)(config, { | ||
namespaces: this._namespaces, | ||
allowAdditionalProperties, | ||
}); | ||
} | ||
getValidationSchema({ allowAdditionalProperties = false, } = {}) { | ||
return (0, Validation_1.getValidationSchema)({ | ||
namespaces: this._namespaces, | ||
allowAdditionalProperties, | ||
removeCustomProperties: true, | ||
}); | ||
} | ||
_validateAndThrow({ allowAdditionalProperties }) { | ||
@@ -320,73 +390,3 @@ (0, Validation_1.validateConfigAndThrow)(this._config, { | ||
} | ||
init(programmaticConfig = {}) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this._programmaticConfig = programmaticConfig; | ||
yield this._load({ allowUnknown: true }); | ||
}); | ||
} | ||
load(programmaticConfig) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this._initializated) { | ||
yield this.init(programmaticConfig); | ||
} | ||
yield this._load(); | ||
this._startNamespacesEvents(); | ||
}); | ||
} | ||
addNamespace(name) { | ||
(0, ConfigNamespaceHelpers_1.checkNamespaceName)(name, { | ||
namespaces: this._namespaces, | ||
options: this._rootNamespace && this._rootNamespace.options, | ||
}); | ||
const namespace = new ConfigNamespace_1.ConfigNamespace(name, { brothers: this._namespaces, root: this }); | ||
this._namespaces.push(namespace); | ||
return namespace; | ||
} | ||
namespace(name) { | ||
return (0, ConfigNamespaceHelpers_1.findObjectWithName)(this._namespaces, name); | ||
} | ||
option(name) { | ||
return (0, ConfigNamespaceHelpers_1.findObjectWithName)(this._rootNamespace.options, name); | ||
} | ||
get value() { | ||
return (0, ConfigNamespaceHelpers_1.getNamespacesValues)(this._namespaces); | ||
} | ||
set value(configuration) { | ||
this.set(configuration); | ||
} | ||
get programmaticLoadedValues() { | ||
return Object.assign({}, this._programmaticConfig); | ||
} | ||
get fileLoadedValues() { | ||
return Object.assign({}, this._fileConfig); | ||
} | ||
get envLoadedValues() { | ||
return Object.assign({}, this._envConfig); | ||
} | ||
get argsLoadedValues() { | ||
return Object.assign({}, this._argsConfig); | ||
} | ||
get loadedFile() { | ||
return this._files.loadedFile; | ||
} | ||
get namespaces() { | ||
return this._namespaces.filter((namespace) => !namespace.isRoot); | ||
} | ||
get options() { | ||
return this._rootNamespace.options; | ||
} | ||
get root() { | ||
return this; | ||
} | ||
set(configuration = {}, options = {}) { | ||
this._namespaces.forEach((namespace) => { | ||
if (!namespace.isRoot) { | ||
namespace.set(configuration[namespace.name] || {}, options); | ||
} | ||
else { | ||
namespace.set(configuration, options); | ||
} | ||
}); | ||
} | ||
}; | ||
exports.Config = Config; |
@@ -33,2 +33,20 @@ import type { JSONSchema7 } from "json-schema"; | ||
export interface ConfigInterface { | ||
/** Returns current options values and values from all child namespaces */ | ||
value: ConfigurationObject; | ||
/** Returns values assigned in programmatic configuration */ | ||
programmaticLoadedValues: ConfigurationObject; | ||
/** Returns values assigned in configuration files */ | ||
fileLoadedValues: ConfigurationObject; | ||
/** Returns values assigned in environment variables */ | ||
envLoadedValues: ConfigurationObject; | ||
/** Returns values assigned in arguments */ | ||
argsLoadedValues: ConfigurationObject; | ||
/** Returns the path of the configuration file loaded, or null in case no file was loaded */ | ||
loadedFile: string | null; | ||
/** Returns an array containing all current namespaces */ | ||
namespaces: ConfigNamespaceInterface[]; | ||
/** Returns an array containing all current options */ | ||
options: OptionInterfaceGeneric[]; | ||
/** Returns the root config interface */ | ||
root: ConfigInterface; | ||
/** | ||
@@ -104,20 +122,2 @@ * Validates a provided object based on current configuration and namespaces options | ||
]; | ||
/** Returns current options values and values from all child namespaces */ | ||
value: ConfigurationObject; | ||
/** Returns values assigned in programmatic configuration */ | ||
programmaticLoadedValues: ConfigurationObject; | ||
/** Returns values assigned in configuration files */ | ||
fileLoadedValues: ConfigurationObject; | ||
/** Returns values assigned in environment variables */ | ||
envLoadedValues: ConfigurationObject; | ||
/** Returns values assigned in arguments */ | ||
argsLoadedValues: ConfigurationObject; | ||
/** Returns the path of the configuration file loaded, or null in case no file was loaded */ | ||
loadedFile: string | null; | ||
/** Returns an array containing all current namespaces */ | ||
namespaces: ConfigNamespaceInterface[]; | ||
/** Returns an array containing all current options */ | ||
options: OptionInterfaceGeneric[]; | ||
/** Returns the root config interface */ | ||
root: ConfigInterface; | ||
/** | ||
@@ -124,0 +124,0 @@ * Set the value of the options, including child namespaces, using the values in the provided configuration object |
@@ -76,2 +76,26 @@ "use strict"; | ||
} | ||
get isRoot() { | ||
return Boolean(this._isRoot); | ||
} | ||
get name() { | ||
return this._name; | ||
} | ||
get parents() { | ||
return [...this._parents]; | ||
} | ||
get root() { | ||
return this._root; | ||
} | ||
get namespaces() { | ||
return [...this._namespaces]; | ||
} | ||
get options() { | ||
return [...this._options]; | ||
} | ||
get value() { | ||
return Object.assign(Object.assign({}, (0, ConfigNamespaceHelpers_1.getNamespacesValues)(this._namespaces)), (0, ConfigNamespaceHelpers_1.getOptionsValues)(this._options)); | ||
} | ||
set value(configuration) { | ||
this.set(configuration); | ||
} | ||
addOption(optionProperties) { | ||
@@ -89,8 +113,2 @@ (0, ConfigNamespaceHelpers_1.checkOptionName)(optionProperties.name, { | ||
} | ||
_setOptions(configuration, options) { | ||
this._options.forEach((option) => { | ||
const optionValue = configuration[option.name]; | ||
option.set(optionValue, options); | ||
}); | ||
} | ||
set(configuration = {}, options = {}) { | ||
@@ -118,26 +136,2 @@ this._setOptions(configuration, options); | ||
} | ||
get isRoot() { | ||
return Boolean(this._isRoot); | ||
} | ||
get name() { | ||
return this._name; | ||
} | ||
get parents() { | ||
return [...this._parents]; | ||
} | ||
get root() { | ||
return this._root; | ||
} | ||
get namespaces() { | ||
return [...this._namespaces]; | ||
} | ||
get options() { | ||
return [...this._options]; | ||
} | ||
get value() { | ||
return Object.assign(Object.assign({}, (0, ConfigNamespaceHelpers_1.getNamespacesValues)(this._namespaces)), (0, ConfigNamespaceHelpers_1.getOptionsValues)(this._options)); | ||
} | ||
set value(configuration) { | ||
this.set(configuration); | ||
} | ||
namespace(name) { | ||
@@ -149,3 +143,9 @@ return (0, ConfigNamespaceHelpers_1.findObjectWithName)(this._namespaces, name); | ||
} | ||
_setOptions(configuration, options) { | ||
this._options.forEach((option) => { | ||
const optionValue = configuration[option.name]; | ||
option.set(optionValue, options); | ||
}); | ||
} | ||
}; | ||
exports.ConfigNamespace = ConfigNamespace; |
@@ -30,2 +30,6 @@ "use strict"; | ||
} | ||
read(namespaces) { | ||
this._config = this._readNamespaces(namespaces); | ||
return this._config; | ||
} | ||
_loadFromEnv(namespace, optionName) { | ||
@@ -60,7 +64,3 @@ return process.env[envVarName(this._moduleName, namespace, optionName)]; | ||
} | ||
read(namespaces) { | ||
this._config = this._readNamespaces(namespaces); | ||
return this._config; | ||
} | ||
}; | ||
exports.Environment = Environment; |
@@ -5,2 +5,2 @@ /// <reference types="node" /> | ||
import type { EventListener, EventListenerRemover } from "./Events.types"; | ||
export declare function addEventListener(listener: EventListener, eventName: string, eventEmitter: EventEmitter): EventListenerRemover; | ||
export declare function addEventListener<DataType>(listener: EventListener<DataType>, eventName: string, eventEmitter: EventEmitter): EventListenerRemover; |
/** Function to execute when the event is triggered */ | ||
export interface EventListener { | ||
export interface EventListener<DataType> { | ||
/** | ||
* Function executed when the event is triggered | ||
*/ | ||
(data?: unknown): void; | ||
(data: DataType): void; | ||
} | ||
@@ -8,0 +8,0 @@ /** Function that removes the event listener */ |
@@ -39,10 +39,4 @@ "use strict"; | ||
} | ||
_transformConfig(config, initConfig) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if ((0, lodash_1.isFunction)(config)) { | ||
return config(initConfig); | ||
} | ||
console.log(config); | ||
return config; | ||
}); | ||
get loadedFile() { | ||
return this._loadedFrom; | ||
} | ||
@@ -67,6 +61,11 @@ read(initConfig, { searchPlaces, searchFrom, searchStop }) { | ||
} | ||
get loadedFile() { | ||
return this._loadedFrom; | ||
_transformConfig(config, initConfig) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if ((0, lodash_1.isFunction)(config)) { | ||
return config(initConfig); | ||
} | ||
return config; | ||
}); | ||
} | ||
}; | ||
exports.Files = Files; |
@@ -11,4 +11,10 @@ import type { ModuleName, ConfigurationObject } from "./Common.types"; | ||
export interface FilesInterface { | ||
/** Path to the config file that has been loaded */ | ||
loadedFile: string | null; | ||
/** Read config from file | ||
* @param initialConfig - Initial config object | ||
* @param options - Options for reading config | ||
* @returns Config object from file | ||
**/ | ||
read(initialConfig: ConfigurationObject, options: FilesReadOptions): Promise<ConfigurationObject>; | ||
loadedFile: string | null; | ||
} |
@@ -21,15 +21,15 @@ import type { UnknownObject } from "./Common.types"; | ||
get default(): GetOptionValueTypeFromDefinition<T, TypeOfValue>; | ||
get value(): GetOptionValueTypeFromDefinition<T, TypeOfValue>; | ||
set value(value: GetOptionValueTypeFromDefinition<T, TypeOfValue>); | ||
get nullable(): boolean; | ||
get extraData(): UnknownObject | undefined; | ||
get itemsType(): T["itemsType"] | undefined; | ||
get hasBeenSet(): boolean; | ||
get value(): GetOptionValueTypeFromDefinition<T, TypeOfValue>; | ||
set value(value: GetOptionValueTypeFromDefinition<T, TypeOfValue>); | ||
set(value: GetOptionValueTypeFromDefinition<T, TypeOfValue>, { merge }?: SetMethodOptions): void; | ||
startEvents(): void; | ||
onChange(listener: EventListener<GetOptionValueTypeFromDefinition<T, TypeOfValue>>): EventListenerRemover; | ||
private _clone; | ||
private _validateAndThrow; | ||
private _emitChange; | ||
onChange(listener: EventListener): EventListenerRemover; | ||
private _merge; | ||
set(value: GetOptionValueTypeFromDefinition<T, TypeOfValue>, { merge }?: SetMethodOptions): void; | ||
startEvents(): void; | ||
get hasBeenSet(): boolean; | ||
} |
@@ -106,8 +106,2 @@ "use strict"; | ||
} | ||
get value() { | ||
return this._clone(this._value); | ||
} | ||
set value(value) { | ||
this.set(value); | ||
} | ||
get nullable() { | ||
@@ -122,2 +116,31 @@ return this._nullable; | ||
} | ||
get hasBeenSet() { | ||
return this._hasBeenSet; | ||
} | ||
get value() { | ||
return this._clone(this._value); | ||
} | ||
set value(value) { | ||
this.set(value); | ||
} | ||
set(value, { merge = false } = {}) { | ||
if (!(0, lodash_1.isUndefined)(value)) { | ||
this._hasBeenSet = true; | ||
if (merge && (0, Typing_1.typeIsObject)(this.type)) { | ||
this._merge(value); | ||
} | ||
else { | ||
const previousValue = this._value; | ||
this._validateAndThrow(value); | ||
this._value = this._clone(value); | ||
this._emitChange(previousValue, this._value); | ||
} | ||
} | ||
} | ||
startEvents() { | ||
this._eventsStarted = true; | ||
} | ||
onChange(listener) { | ||
return (0, Events_1.addEventListener)(listener, Events_1.CHANGE, this._eventEmitter); | ||
} | ||
_clone(value) { | ||
@@ -145,5 +168,2 @@ if ((0, lodash_1.isUndefined)(value) || (this._nullable === true && (0, lodash_1.isNull)(value))) { | ||
} | ||
onChange(listener) { | ||
return (0, Events_1.addEventListener)(listener, Events_1.CHANGE, this._eventEmitter); | ||
} | ||
_merge(value) { | ||
@@ -157,23 +177,3 @@ const previousValue = this._value; | ||
} | ||
set(value, { merge = false } = {}) { | ||
if (!(0, lodash_1.isUndefined)(value)) { | ||
this._hasBeenSet = true; | ||
if (merge && (0, Typing_1.typeIsObject)(this.type)) { | ||
this._merge(value); | ||
} | ||
else { | ||
const previousValue = this._value; | ||
this._validateAndThrow(value); | ||
this._value = this._clone(value); | ||
this._emitChange(previousValue, this._value); | ||
} | ||
} | ||
} | ||
startEvents() { | ||
this._eventsStarted = true; | ||
} | ||
get hasBeenSet() { | ||
return this._hasBeenSet; | ||
} | ||
} | ||
exports.Option = Option; |
@@ -187,3 +187,3 @@ import type { ObjectWithName, UnknownObject } from "./Common.types"; | ||
*/ | ||
onChange(eventListener: EventListener): EventListenerRemover; | ||
onChange(eventListener: EventListener<GetOptionValueTypeFromDefinition<T, TypeOfValue>>): EventListenerRemover; | ||
/** | ||
@@ -190,0 +190,0 @@ * Set the value of the option |
@@ -10,1 +10,2 @@ export * from "./CommandLineArgument.types"; | ||
export * from "./Validation.types"; | ||
export * from "./Options.types"; |
@@ -26,1 +26,2 @@ "use strict"; | ||
__exportStar(require("./Validation.types"), exports); | ||
__exportStar(require("./Options.types"), exports); |
{ | ||
"name": "@mocks-server/config", | ||
"version": "2.0.0-beta.2", | ||
"version": "2.0.0-beta.3", | ||
"description": "Modular configuration provider. Read it from file, environment and arguments", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
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
156588
49
2220