Environment-based configuration provider

Reads configuration from environment variables with support for:

  • .env files (using dotenv)
  • Automatic type transformation
  • Custom transformers
  • Nested objects using double underscore notation (__)
  • Validation with schemas
// Environment:
// APP_PORT=3000
// APP_DATABASE__URL=postgres://localhost/db
// APP_FEATURES__CACHE=true

const config = new EnvConfigProvider({
prefix: 'APP_',
autoTransform: true,
});

const port = await config.getRequired<number>('port'); // 3000
const dbUrl = await config.get<string>('database.url');
const cacheEnabled = await config.get<boolean>('features.cache');

Implements

  • ConfigProvider

Constructors

Methods

  • Get a configuration value with optional type and default

    Type Parameters

    • T = unknown

    Parameters

    • key: string

      The configuration key (supports dot notation: 'database.url')

    • OptionaldefaultValue: T

      Default value if key is not found

    Returns Promise<undefined | T>

    The configuration value or default

    const port = await config.get<number>('server.port', 3000);
    const timeout = await config.get<number>('api.timeout', 5000);
  • Get a required configuration value

    Type Parameters

    • T = unknown

    Parameters

    • key: string

      The configuration key

    Returns Promise<T>

    The configuration value

    ConfigNotFoundError if the value is not found

    const apiKey = await config.getRequired<string>('api.key');
    
  • Get all configuration as an object

    Type Parameters

    • T = Record<string, unknown>

    Returns Promise<T>

    The complete configuration object

    const allConfig = await config.getAll<AppConfig>();
    
  • Get configuration for a specific namespace

    Type Parameters

    • T = Record<string, unknown>

    Parameters

    • namespace: string

      The namespace key (e.g., 'database', 'server')

    Returns Promise<T>

    Configuration object for the namespace

    const dbConfig = await config.getNamespace('database');
    // Returns: { url: '...', poolSize: 10, ... }
  • Check if a configuration key exists

    Parameters

    • key: string

      The configuration key

    Returns Promise<boolean>

    True if the key exists, false otherwise

    if (await config.has('features.newUI')) {
    // Feature flag exists
    }
  • Reload configuration (if supported by implementation)

    Returns Promise<void>

    await config.reload();
    console.log('Configuration reloaded');