File-based configuration provider

Loads configuration from JSON and YAML files with support for:

  • Multiple files with merge
  • Hot reload with file watchers
  • Validation with schemas
  • Environment-specific overrides
const config = new FileConfigProvider({
files: [
'./config/default.json',
'./config/production.json',
],
watch: true,
});

// Watch for changes
config.watch?.((changes) => {
console.log('Config changed:', changes);
});

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

    Parameters

    • callback: WatchCallback

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