From f61769e7fcf2ad5ac916deddcf356d467b7af757 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Thu, 18 Nov 2021 11:40:57 -0300 Subject: [PATCH] feat(env): add support to template string on env function feat #3 --- README.md | 9 ++++++++- package.json | 2 +- src/Env.ts | 17 ++++++++++++++++- tests/env.spec.ts | 8 ++++++++ tests/global.spec.ts | 13 +++++++++++++ 5 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 tests/global.spec.ts diff --git a/README.md b/README.md index 4dfc163..98c7863 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ > Very simple Env get function for NodeJS [![GitHub followers](https://img.shields.io/github/followers/jlenon7.svg?style=social&label=Follow&maxAge=2592000)](https://github.com/jlenon7?tab=followers) -[![GitHub stars](https://img.shields.io/github/stars/secjs/config.svg?style=social&label=Star&maxAge=2592000)](https://github.com/secjs/env/stargazers/) +[![GitHub stars](https://img.shields.io/github/stars/secjs/env.svg?style=social&label=Star&maxAge=2592000)](https://github.com/secjs/env/stargazers/)

Buy Me A Coffee @@ -65,14 +65,21 @@ Env('DB_DATABASE', 'my-database') import { Env } from '@secjs/env' // Simulating .env file +HOST='127.0.0.1' +PORT=3333 DB_PORT=5432 DB_DEBUG=false DB_DATABASE='database' +APP_URL='http://${HOST}:${PORT}' // The response value will be the value of DB_DATABASE variable or my-database by default const db = Env('DB_DATABASE', 'my-database') console.log(db) // 'database' +// Template string support +const appUrl = Env('APP_URL', 'http://localhost:3000') +console.log(appUrl) // 'http://127.0.0.1:3333' + const dbPort = Env('DB_PORT', '5432') console.log(dbPort) // '5432' diff --git a/package.json b/package.json index 8ffd6a4..2988a7b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@secjs/env", - "version": "1.2.4", + "version": "1.2.5", "license": "MIT", "author": "João Lenon", "repository": "https://github.com/SecJS/Env.git", diff --git a/src/Env.ts b/src/Env.ts index c526182..788580d 100644 --- a/src/Env.ts +++ b/src/Env.ts @@ -19,7 +19,7 @@ export function Env( env: string | IEnv, defaultValue: string | number | boolean, ): string | number | boolean | any { - const environment = process.env[`${typeof env === 'string' ? env : env.name}`] + let environment = process.env[`${typeof env === 'string' ? env : env.name}`] if (!environment) { logger.debug(`Variable ${env} not found`) @@ -38,5 +38,20 @@ export function Env( return defaultValue } + const matches = environment.match(/\${([^}]+)}/g) + + if (matches) { + for (let match of matches) { + const key = match.replace(/[!${}]+/g, '') + + match = match.replace('$', '\\$') + + environment = environment.replace( + new RegExp(match, 'g'), + process.env[key] || null, + ) + } + } + return environment } diff --git a/tests/env.spec.ts b/tests/env.spec.ts index 6f293be..76d342f 100644 --- a/tests/env.spec.ts +++ b/tests/env.spec.ts @@ -5,6 +5,7 @@ describe('\n Env 🔁', () => { let DB_PASSWORD = '' beforeAll(() => { + process.env.HOST = '127.0.0.1' process.env.PORT = '3333' process.env.DB_DEBUG = 'false' process.env.DB_USERNAME = 'user' @@ -31,6 +32,13 @@ describe('\n Env 🔁', () => { expect(DB_PORT).toBe(portDefaultValue) }) + it('should replace template strings on environment values', () => { + // eslint-disable-next-line no-template-curly-in-string + process.env.APP_URL = 'http://${HOST}:${PORT}' + + expect(Env('APP_URL', '')).toBe('http://127.0.0.1:3333') + }) + it('should get an environment variable type using a object with env name and type to Env function', () => { const PORT = Env({ name: 'PORT', type: 'number' }, '') const DB_DEBUG = Env({ name: 'DB_DEBUG', type: 'boolean' }, '') diff --git a/tests/global.spec.ts b/tests/global.spec.ts new file mode 100644 index 0000000..7419a66 --- /dev/null +++ b/tests/global.spec.ts @@ -0,0 +1,13 @@ +import '../src/utils/global' + +describe('\n Global 🔁', () => { + beforeAll(() => { + process.env.DB_USERNAME = 'user' + process.env.DB_PASSWORD = 'pass' + }) + + it('should get an environment variable from name using global Env', () => { + expect(Env('DB_USERNAME', '')).toBe('user') + expect(Env('DB_PASSWORD', '')).toBe('pass') + }) +})