From 8256f16a07387f01d1fcc953cb73f28241a29fd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Thu, 27 Oct 2022 09:52:56 -0300 Subject: [PATCH 1/2] feat(config): get all configuration values without any key --- package-lock.json | 4 ++-- package.json | 2 +- src/Helpers/EnvHelper.js | 52 +++++++++++++++++++++++----------------- src/index.d.ts | 6 ++--- src/index.js | 14 +++++++++-- tests/Stubs/.env | 12 ++++++---- tests/Unit/EnvTest.js | 13 ++++++---- 7 files changed, 65 insertions(+), 38 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7d11e62..e4f3b23 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@athenna/config", - "version": "1.1.7", + "version": "1.1.9", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@athenna/config", - "version": "1.1.7", + "version": "1.1.9", "license": "MIT", "dependencies": { "@athenna/common": "1.0.0", diff --git a/package.json b/package.json index 2194d21..ae9ef5f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/config", - "version": "1.1.8", + "version": "1.1.9", "description": "Cache and handle environment variables and config files of Athenna.", "license": "MIT", "author": "João Lenon ", diff --git a/src/Helpers/EnvHelper.js b/src/Helpers/EnvHelper.js index 55beb41..27c59ec 100644 --- a/src/Helpers/EnvHelper.js +++ b/src/Helpers/EnvHelper.js @@ -24,10 +24,6 @@ export class EnvHelper { * @return {string} */ static setEnvInEnv(environment, autoCast) { - if (!environment) { - return undefined - } - if (!Is.String(environment)) { return environment } @@ -56,9 +52,12 @@ export class EnvHelper { } /** - * Cast the environment variable if matches - * the cast reserved vars -> (). If the var - * doesnt match, returns the same variable value. + * Cast the environment variable if it values matches a + * number string, boolean string or json string. Also, + * if the variable has the reserved vars -> () enclosed + * it will be removed and returned as string. So the value + * (false) in .env is equals to 'false' but the value 10 is + * equals to number 10. * * @param {string} environment * @return {any} @@ -67,15 +66,19 @@ export class EnvHelper { if (environment.match(/\((.*?)\)/)) { environment = environment.slice(1, -1) - /** - * Is.Json(environment) will be true if values - * are boolean string and also number string. - * - * JSON.parse also can cast this type of values. - */ - if (Is.Json(environment)) { - return JSON.parse(environment) - } + return environment + } + + if (/^-?\d+$/.test(environment)) { + return +environment + } + + if (Is.Json(environment)) { + return JSON.parse(environment) + } + + if (environment === 'true' || environment === 'false') { + return environment === 'true' } return environment @@ -108,11 +111,16 @@ export class EnvHelper { * @return {""|boolean} */ static isToOverrideEnvs() { - return ( - process.env.OVERRIDE_ENV && - (process.env.OVERRIDE_ENV === true || - process.env.OVERRIDE_ENV === 'true' || - process.env.OVERRIDE_ENV === '(true)') - ) + return this.isEnvTrue(process.env.OVERRIDE_ENV) + } + + /** + * Verify if the env variable is true, 'true' or '(true)'. + * + * @param {string} env + * @return {boolean} + */ + static isEnvTrue(env) { + return env && (env === true || env === 'true' || env === '(true)') } } diff --git a/src/index.d.ts b/src/index.d.ts index 00d7928..0769c60 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -51,11 +51,11 @@ export class Config { * Get the value from config file by key. If not * found, defaultValue will be used. * - * @param {string} key - * @param {any,undefined} defaultValue + * @param {string} [key] + * @param {any,undefined} [defaultValue] * @return {any} */ - static get(key: string, defaultValue?: any): any + static get(key?: string, defaultValue?: any): any /** * Load all configuration files in path. diff --git a/src/index.js b/src/index.js index cd0df7d..0c884e8 100644 --- a/src/index.js +++ b/src/index.js @@ -29,11 +29,21 @@ export class Config { * Get the value from config file by key. If not * found, defaultValue will be used. * - * @param {string} key - * @param {any,undefined} defaultValue + * @param {string} [key] + * @param {any,undefined} [defaultValue] * @return {any} */ static get(key, defaultValue = undefined) { + if (!key) { + const config = {} + + for (const [key, value] of this.configs.entries()) { + config[key] = value + } + + return config + } + const [mainKey, ...keys] = key.split('.') const config = this.configs.get(mainKey) diff --git a/tests/Stubs/.env b/tests/Stubs/.env index e953a43..4aadc69 100644 --- a/tests/Stubs/.env +++ b/tests/Stubs/.env @@ -1,8 +1,12 @@ ENV="production" -NUMBER_ENV="(10)" -BOOLEAN_ENV="(true)" -OBJECT_ENV="({"name":"Paulo"})" +NUMBER_ENV="10" +STRING_NUMBER_ENV="(10)" +BOOLEAN_ENV="true" +STRING_BOOLEAN_ENV="(true)" +OBJECT_ENV="{"name":"Paulo"}" +STRING_OBJECT_ENV="({"name":"Paulo"})" ENV_IN_ENV="${NUMBER_ENV}-${BOOLEAN_ENV}" -ENV_IN_ENV_JSON="({ "maintainers": ${OBJECT_ENV} })" +ENV_IN_ENV_JSON="{ "maintainers": ${OBJECT_ENV} }" +STRING_ENV_IN_ENV_JSON="({ "maintainers": ${OBJECT_ENV} })" TEST_ENV="(true)" PRESET="false" diff --git a/tests/Unit/EnvTest.js b/tests/Unit/EnvTest.js index 887e709..b3a7eba 100644 --- a/tests/Unit/EnvTest.js +++ b/tests/Unit/EnvTest.js @@ -31,7 +31,7 @@ test.group('EnvTest', group => { process.env.NODE_ENV = 'testing' EnvHelper.resolveFile() - assert.equal(Env('PRESET'), 'true') + assert.equal(Env('PRESET'), true) }) test('should override pre set environment variables', async ({ assert }) => { @@ -43,7 +43,7 @@ test.group('EnvTest', group => { process.env.NODE_ENV = 'testing' EnvHelper.resolveFile() - assert.equal(Env('PRESET'), 'false') + assert.equal(Env('PRESET'), false) }) test('should be able to resolve more than one env files at same time', async ({ assert }) => { @@ -62,12 +62,15 @@ test.group('EnvTest', group => { EnvHelper.resolveFile() assert.strictEqual(Env('NUMBER_ENV'), 10) + assert.strictEqual(Env('STRING_NUMBER_ENV'), '10') assert.strictEqual(Env('BOOLEAN_ENV'), true) + assert.strictEqual(Env('STRING_BOOLEAN_ENV'), 'true') assert.deepEqual(Env('OBJECT_ENV'), { name: 'Paulo' }) + assert.deepEqual(Env('STRING_OBJECT_ENV'), `{"name":"Paulo"}`) }) test('should be able to fallback to default values when the env does not exist', async ({ assert }) => { - assert.equal(Env('NO_EXIST', 'Hello World'), 'Hello World') + assert.deepEqual(Env('NO_EXIST', false), false) }) test('should be able to set env values inside of other env values', async ({ assert }) => { @@ -76,13 +79,15 @@ test.group('EnvTest', group => { assert.equal(Env('ENV_IN_ENV'), '10-true') assert.deepEqual(Env('ENV_IN_ENV_JSON'), { maintainers: { name: 'Paulo' } }) + assert.deepEqual(Env('STRING_ENV_IN_ENV_JSON'), `{ "maintainers": {"name":"Paulo"} }`) }) test('should be able to turn off the auto cast for specifics envs when needed', async ({ assert }) => { process.env.NODE_ENV = '' EnvHelper.resolveFile() - assert.equal(Env('NUMBER_ENV', '10', false), '(10)') + assert.equal(Env('NUMBER_ENV', '10', false), '10') + assert.equal(Env('STRING_NUMBER_ENV', '(10)', false), '(10)') }) test('should be able to use Env without any .env file', async ({ assert }) => { From d027cea4b811dc128d95ca3aa64f92c67a968853 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Thu, 27 Oct 2022 09:53:40 -0300 Subject: [PATCH 2/2] refactor(env): change behavior of env casting --- tests/Unit/ConfigTest.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/Unit/ConfigTest.js b/tests/Unit/ConfigTest.js index eef6ef6..39fadf7 100644 --- a/tests/Unit/ConfigTest.js +++ b/tests/Unit/ConfigTest.js @@ -27,7 +27,14 @@ test.group('ConfigTest', group => { await new Folder(Path.config()).remove() }) - test('should be able to get full configurations values from Config class', ({ assert }) => { + test('should be able to get all configurations values from Config class', ({ assert }) => { + const configs = Config.get() + + assert.deepEqual(configs.app, { name: 'Athenna', env: 'test' }) + assert.deepEqual(configs.database, { username: 'Athenna' }) + }) + + test('should be able to get full configurations values of one file from Config class', ({ assert }) => { const app = Config.get('app') assert.deepEqual(app, {