diff --git a/seasoned_api/src/config/configuration.js b/seasoned_api/src/config/configuration.js index bd9d4c3..b9eede4 100644 --- a/seasoned_api/src/config/configuration.js +++ b/seasoned_api/src/config/configuration.js @@ -4,40 +4,40 @@ const Field = require('./field.js'); let instance = null; class Config { - constructor() { - this.location = Config.determineLocation(); - this.fields = require(`${this.location}`); - } + constructor() { + this.location = Config.determineLocation(); + this.fields = require(`${this.location}`); + } - static getInstance() { - if (instance == null) { - instance = new Config(); - } - return instance; - } + static getInstance() { + if (instance == null) { + instance = new Config(); + } + return instance; + } - static determineLocation() { - return path.join(__dirname, '..', '..', process.env.SEASONED_CONFIG); - } + static determineLocation() { + return path.join(__dirname, '..', '..', process.env.SEASONED_CONFIG); + } - get(section, option) { - if (this.fields[section] === undefined || this.fields[section][option] === undefined) { - throw new Error(`Filed "${section} => ${option}" does not exist.`); - } + get(section, option) { + if (this.fields[section] === undefined || this.fields[section][option] === undefined) { + throw new Error(`Filed "${section} => ${option}" does not exist.`); + } - const field = new Field(this.fields[section][option]) + const field = new Field(this.fields[section][option]); - if (field.value === '') { - const envField = process.env[[section.toUpperCase(), option.toUpperCase()].join('_')] - if (envField !== undefined && envField.length !== 0) { return envField } - } + if (field.value === '') { + const envField = process.env[[section.toUpperCase(), option.toUpperCase()].join('_')]; + if (envField !== undefined && envField.length !== 0) { return envField; } + } - if (field.value === undefined) { - throw new Error(`${section} => ${option} is empty.`); - } + if (field.value === undefined) { + throw new Error(`${section} => ${option} is empty.`); + } - return field.value; - } + return field.value; + } } -module.exports = Config; \ No newline at end of file +module.exports = Config; diff --git a/seasoned_api/src/config/environmentVariables.js b/seasoned_api/src/config/environmentVariables.js index d879fe7..b7fc3f2 100644 --- a/seasoned_api/src/config/environmentVariables.js +++ b/seasoned_api/src/config/environmentVariables.js @@ -1,16 +1,15 @@ class EnvironmentVariables { + constructor(variables) { + this.variables = variables || process.env; + } - constructor(variables) { - this.variables = variables || process.env; - } + get(variable) { + return this.variables[variable]; + } - get(variable) { - return this.variables[variable]; - } - - has(variable) { - return this.get(variable) !== undefined; - } + has(variable) { + return this.get(variable) !== undefined; + } } module.exports = EnvironmentVariables; diff --git a/seasoned_api/src/config/field.js b/seasoned_api/src/config/field.js index 1a8b79a..42eb8cd 100644 --- a/seasoned_api/src/config/field.js +++ b/seasoned_api/src/config/field.js @@ -2,49 +2,48 @@ const Filters = require('./filters.js'); const EnvironmentVariables = require('./environmentVariables.js'); class Field { + constructor(rawValue, environmentVariables) { + this.rawValue = rawValue; + this.filters = new Filters(rawValue); + this.valueWithoutFilters = this.filters.removeFiltersFromValue(); + this.environmentVariables = new EnvironmentVariables(environmentVariables); + } - constructor(rawValue, environmentVariables) { - this.rawValue = rawValue; - this.filters = new Filters(rawValue); - this.valueWithoutFilters = this.filters.removeFiltersFromValue(); - this.environmentVariables = new EnvironmentVariables(environmentVariables); - } + get value() { + if (this.filters.isEmpty()) { + return this.valueWithoutFilters; + } - get value() { - if (this.filters.isEmpty()) { - return this.valueWithoutFilters; - } + if (this.filters.has('base64') && !this.filters.has('env')) { + return Field.base64Decode(this.valueWithoutFilters); + } - if (this.filters.has('base64') && !this.filters.has('env')) { - return Field.base64Decode(this.valueWithoutFilters); - } + if (this.environmentVariables.has(this.valueWithoutFilters) && + this.environmentVariables.get(this.valueWithoutFilters) === '') { + return undefined; + } - if (this.environmentVariables.has(this.valueWithoutFilters) && - this.environmentVariables.get(this.valueWithoutFilters) === '') { - return undefined; - } + if (!this.filters.has('base64') && this.filters.has('env')) { + if (this.environmentVariables.has(this.valueWithoutFilters)) { + return this.environmentVariables.get(this.valueWithoutFilters); + } + return undefined; + } - if (!this.filters.has('base64') && this.filters.has('env')) { - if (this.environmentVariables.has(this.valueWithoutFilters)) { - return this.environmentVariables.get(this.valueWithoutFilters); - } - return undefined; - } + if (this.filters.has('env') && this.filters.has('base64')) { + if (this.environmentVariables.has(this.valueWithoutFilters)) { + const encodedEnvironmentVariable = this.environmentVariables.get(this.valueWithoutFilters); + return Field.base64Decode(encodedEnvironmentVariable); + } + return undefined; + } - if (this.filters.has('env') && this.filters.has('base64')) { - if (this.environmentVariables.has(this.valueWithoutFilters)) { - const encodedEnvironmentVariable = this.environmentVariables.get(this.valueWithoutFilters); - return Field.base64Decode(encodedEnvironmentVariable); - } - return undefined; - } + return this.valueWithoutFilters; + } - return this.valueWithoutFilters; - } - - static base64Decode(string) { - return new Buffer(string, 'base64').toString('utf-8'); - } + static base64Decode(string) { + return new Buffer(string, 'base64').toString('utf-8'); + } } -module.exports = Field; \ No newline at end of file +module.exports = Field; diff --git a/seasoned_api/src/config/filters.js b/seasoned_api/src/config/filters.js index fbeab07..b4ec359 100644 --- a/seasoned_api/src/config/filters.js +++ b/seasoned_api/src/config/filters.js @@ -1,35 +1,34 @@ class Filters { + constructor(value) { + this.value = value; + this.delimiter = '|'; + } - constructor(value) { - this.value = value; - this.delimiter = '|'; - } + get filters() { + return this.value.split(this.delimiter).slice(0, -1); + } - get filters() { - return this.value.split(this.delimiter).slice(0, -1); - } + isEmpty() { + return !this.hasValidType() || this.value.length === 0; + } - isEmpty() { - return !this.hasValidType() || this.value.length === 0; - } + has(filter) { + return this.filters.includes(filter); + } - has(filter) { - return this.filters.includes(filter); - } + hasValidType() { + return (typeof this.value === 'string'); + } - hasValidType() { - return (typeof this.value === 'string'); - } + removeFiltersFromValue() { + if (this.hasValidType() === false) { + return this.value; + } - removeFiltersFromValue() { - if (this.hasValidType() === false) { - return this.value; - } - - let filtersCombined = this.filters.join(this.delimiter); - filtersCombined += this.filters.length >= 1 ? this.delimiter : ''; - return this.value.replace(filtersCombined, ''); - } + let filtersCombined = this.filters.join(this.delimiter); + filtersCombined += this.filters.length >= 1 ? this.delimiter : ''; + return this.value.replace(filtersCombined, ''); + } } module.exports = Filters;