Composite configuration provider

Combines multiple configuration providers with configurable merge strategies.

const config = new CompositeConfigProvider({
providers: [
new EnvConfigProvider({ prefix: 'APP_' }), // Highest priority
new FileConfigProvider({ files: ['config.json'] }), // Fallback
],
strategy: 'first-wins',
});

// Searches ENV first, then file
const port = await config.get<number>('port');

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
    }
  • Watch for configuration changes (if supported)

    Parameters

    • callback: (changes: ConfigChange[]) => void

      Function called when configuration changes

    Returns () => void

    Function to unsubscribe from changes

    const unwatch = config.watch?.((changes) => {
    for (const change of changes) {
    console.log(`${change.key} changed from ${change.oldValue} to ${change.newValue}`);
    }
    });

    // Later: stop watching
    unwatch?.();