Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)

<p>
<a href="https://www.buymeacoffee.com/secjs" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>
Expand Down Expand Up @@ -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'

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
17 changes: 16 additions & 1 deletion src/Env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand All @@ -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
}
8 changes: 8 additions & 0 deletions tests/env.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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' }, '')
Expand Down
13 changes: 13 additions & 0 deletions tests/global.spec.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})