Configuration Provider Interface

Defines the contract for retrieving application configuration from various sources with type safety and validation.

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

const port = await config.getRequired<number>('port');
const host = await config.get<string>('host', 'localhost');
const dbConfig = await config.getNamespace('database');
interface ConfigProvider {
    get<T = unknown>(key: string, defaultValue?: T): Promise<undefined | T>;
    getRequired<T = unknown>(key: string): Promise<T>;
    getAll<T = Record<string, unknown>>(): Promise<T>;
    getNamespace<T = Record<string, unknown>>(namespace: string): Promise<T>;
    has(key: string): Promise<boolean>;
    reload(): Promise<void>;
    watch(callback: (changes: ConfigChange[]) => void): () => void;
}

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');
  • 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?.();