You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Centralized configuration management with .env support and type-safe dot-notation access.
Installation
npm install @atlex/config
# or
yarn add @atlex/config
Quick Start
import{config,env,loadEnv}from'@atlex/config'// Load environment variables from .env fileloadEnv()// Access configuration with dot notationconstappName=config('app.name')constdbHost=config('database.host')// Get environment variablesconstnodeEnv=env('NODE_ENV','development')// Check if configuration existsif(config.has('api.secret')){constsecret=config('api.secret')}// Get all configurationconstallConfig=config.all()
Features
Dot-Notation Access: Access nested configuration using familiar dot notation syntax
.env File Support: Automatically load environment variables from .env files
Type-Safe Getters: Retrieve configuration with type coercion and default values
Wildcard Queries: Query configuration hierarchies with wildcard patterns
Configuration Caching: Cache configuration in memory for improved performance
Environment Casting: Automatic type casting for environment variables
File-Based Loaders: Load configuration from various file formats
Configuration Loading
Loading Environment Variables
import{loadEnv}from'@atlex/config'// Load from .env file in the root directoryloadEnv()// Load from a specific .env fileloadEnv('.env.local')// Load from multiple files with precedenceloadEnv('.env')loadEnv('.env.local')// Overrides .env values
Checking Environment Variables
import{hasEnv,envs}from'@atlex/config'// Check if an environment variable existsif(hasEnv('DATABASE_URL')){console.log('Database is configured')}// Get all environment variables as an objectconstallEnvs=envs()console.log(allEnvs)
ConfigRepository API
Dot-Notation Access
import{config}from'@atlex/config'// Get single value with dot notationconstappName: string=config('app.name')constdbHost: string=config('database.host')constdbPort: number=config('database.port')// Get with default valueconstapiTimeout=config('api.timeout',30000)// Get with type coercionconstdebugMode=config.string('app.debug')// Converts to stringconstmaxConnections=config.integer('database.maxConnections')constenableCache=config.boolean('cache.enabled')
Checking Configuration
import{config}from'@atlex/config'// Check if a configuration key existsif(config.has('api.key')){console.log('API key is configured')}// Check multiple keysconsthasAllRequired=config.has('database.host','database.name')
Wildcard Queries
import{config}from'@atlex/config'// Get all database configurationconstdbConfig=config('database.*')// Returns: { host: 'localhost', port: 5432, name: 'myapp' }// Get all values at any depthconstallMailSettings=config('mail.**')
Setting Configuration
import{config}from'@atlex/config'// Set a configuration value at runtimeconfig.set('api.timeout',60000)// Set nested valuesconfig.set('database',{host: 'localhost',port: 5432,name: 'production_db',})// Set multiple valuesconfig.set({'app.name': 'My App','app.version': '1.0.0',debug: false,})
Getting All Configuration
import{config}from'@atlex/config'// Get entire configuration objectconstallConfig=config.all()// Merge with additional valuesconstmerged={
...config.all(),runtime: {startTime: Date.now(),},}
Environment Casting
import{EnvCaster}from'@atlex/config'constcaster=newEnvCaster()// Cast to stringconstappName=caster.asString('APP_NAME')// Cast to integerconstport=caster.asInteger('PORT',3000)// Cast to booleanconstdebug=caster.asBoolean('DEBUG')// Cast to array (comma-separated or custom)constallowedHosts=caster.asArray('ALLOWED_HOSTS','localhost,127.0.0.1')// Cast to JSONconstdbPool=caster.asJson('DB_POOL',{min: 2,max: 10})
File-Based Configuration
Using FileConfigLoader
import{FileConfigLoader}from'@atlex/config'importpathfrom'path'constloader=newFileConfigLoader(path.join(__dirname,'../config'))// Load all .js or .ts files from config directoryconstappConfig=loader.load('app')// Loads app.js/app.tsconstdatabaseConfig=loader.load('database')// Loads database.js/database.tsconstmailConfig=loader.load('mail')// Loads mail.js/mail.ts// Example config file structure:// config/app.tsexportdefault{name: process.env.APP_NAME||'Atlex App',environment: process.env.NODE_ENV||'development',debug: process.env.APP_DEBUG==='true',url: process.env.APP_URL||'http://localhost:3000',}
Configuration Caching
import{readCachedConfigSync,writeConfigCacheSync,clearConfigCacheSync}from'@atlex/config'// Read from cache (returns null if not cached)constcached=readCachedConfigSync()if(cached){console.log('Using cached configuration')}// Write configuration to cacheimport{config}from'@atlex/config'writeConfigCacheSync(config.all())// Clear cacheclearConfigCacheSync()
Complete Example
import{config,env,loadEnv}from'@atlex/config'import{FileConfigLoader}from'@atlex/config'importpathfrom'path'// 1. Load environment variablesloadEnv()// 2. Load configuration filesconstloader=newFileConfigLoader(path.join(__dirname,'../config'))// 3. Set up configuration structureconfig.set({app: {name: loader.load('app').name,environment: env('NODE_ENV','development'),debug: env('APP_DEBUG')==='true',},database: {host: env('DB_HOST','localhost'),port: env('DB_PORT',5432),name: env('DB_NAME','atlex'),user: env('DB_USER'),password: env('DB_PASSWORD'),},api: {timeout: env.integer('API_TIMEOUT',30000),retries: env.integer('API_RETRIES',3),},})// 4. Use configuration throughout your appconsole.log(`Running ${config('app.name')} in ${config('app.environment')} mode`)if(config('app.debug')){console.log('Debug mode is enabled')}constdbPool={host: config('database.host'),port: config('database.port'),database: config('database.name'),user: config('database.user'),password: config('database.password'),}console.log('Database configured:',dbPool)
API Overview
ConfigRepository
Method
Description
get(key, defaultValue?)
Get configuration value with dot notation
set(key, value)
Set configuration value(s)
has(...keys)
Check if configuration key(s) exist
all()
Get entire configuration object
string(key, defaultValue?)
Get value as string
integer(key, defaultValue?)
Get value as integer
boolean(key, defaultValue?)
Get value as boolean
array(key, defaultValue?)
Get value as array
EnvCaster
Method
Description
asString(key, defaultValue?)
Cast environment variable to string
asInteger(key, defaultValue?)
Cast environment variable to integer
asBoolean(key, defaultValue?)
Cast environment variable to boolean
asArray(key, defaultValue?)
Cast environment variable to array
asJson(key, defaultValue?)
Cast environment variable to JSON object
Helper Functions
Function
Description
config(key, defaultValue?)
Get configuration value (same as ConfigRepository.get)