Class ConfigurationManager

Configuration Manager for the Mosaia SDK

Provides a single source of truth for configuration across the entire SDK. This prevents configuration copies and mutations, ensuring consistency.

The ConfigurationManager implements the singleton pattern to ensure that only one configuration instance exists throughout the application lifecycle.

import { ConfigurationManager } from '@mosaia/mosaia-node-sdk';

// Get the singleton instance
const configManager = ConfigurationManager.getInstance();

// Initialize with user configuration
configManager.initialize({
apiKey: 'your-api-key',
apiURL: 'https://api.mosaia.ai'
});

// Access configuration anywhere in the SDK
const config = configManager.getConfig();
// Update specific configuration values
configManager.updateConfig('apiKey', 'new-api-key');
configManager.updateConfig('version', '2');

// Get read-only configuration
const readOnlyConfig = configManager.getReadOnlyConfig();

Methods

  • Get the singleton instance of ConfigurationManager

    Creates a new instance if one doesn't exist, otherwise returns the existing instance.

    Returns ConfigurationManager

    The singleton ConfigurationManager instance

    const configManager = ConfigurationManager.getInstance();
    
  • Initialize the configuration manager with user settings

    Merges user-provided configuration with default values to create a complete configuration object. This method should be called before using any other ConfigurationManager methods.

    Parameters

    • userConfig: Partial<MosaiaConfig>

      User-provided configuration options

    Returns void

    const configManager = ConfigurationManager.getInstance();

    configManager.initialize({
    apiKey: 'your-api-key',
    apiURL: 'https://api.mosaia.ai',
    version: '1',
    clientId: 'your-client-id'
    });
  • Get the current configuration

    Returns the current configuration object. Throws an error if configuration hasn't been initialized.

    Returns MosaiaConfig

    The current configuration object

    When configuration hasn't been initialized

    const config = configManager.getConfig();
    console.log(config.apiKey); // 'your-api-key'
    console.log(config.apiURL); // 'https://api.mosaia.ai'
  • Set the current configuration

    Replaces the entire configuration object with a new one. This method completely overwrites the existing configuration.

    Parameters

    Returns void

    configManager.setConfig({
    apiKey: 'new-api-key',
    apiURL: 'https://api-staging.mosaia.ai',
    version: '2'
    });
  • Get a read-only copy of the configuration

    Returns a frozen copy of the configuration object to prevent accidental modifications.

    Returns Readonly<MosaiaConfig>

    A frozen copy of the configuration

    const readOnlyConfig = configManager.getReadOnlyConfig();

    // This will throw an error in strict mode
    // readOnlyConfig.apiKey = 'new-key'; // Error!
  • Update a specific configuration value

    Updates a single configuration property while preserving all other configuration values.

    Type Parameters

    Parameters

    • key: K

      The configuration key to update

    • value: MosaiaConfig[K]

      The new value for the configuration key

    Returns void

    When configuration hasn't been initialized

    // Update API key
    configManager.updateConfig('apiKey', 'new-api-key');

    // Update API URL
    configManager.updateConfig('apiURL', 'https://api-staging.mosaia.ai');

    // Update version
    configManager.updateConfig('version', '2');
  • Get the API base URL with version

    Constructs the full API URL by combining the base URL and version. Falls back to default values if configuration is not set.

    Returns string

    The full API URL with version

    const apiUrl = configManager.getApiUrl();
    console.log(apiUrl); // 'https://api.mosaia.ai/v1'
  • Get the API key

    Returns the current API key used for authentication.

    Returns undefined | string

    The API key, or undefined if not set

    const apiKey = configManager.getApiKey();
    if (apiKey) {
    console.log('API key is configured');
    } else {
    console.log('No API key configured');
    }
  • Check if configuration is initialized

    Returns true if the configuration has been initialized with user settings, false otherwise.

    Returns boolean

    True if configuration has been initialized

    if (!configManager.isInitialized()) {
    configManager.initialize({
    apiKey: 'your-api-key'
    });
    }
  • Reset configuration to default

    Clears the current configuration and resets the manager to its uninitialized state.

    Returns void

    // Clear all configuration
    configManager.reset();

    // Configuration must be re-initialized
    configManager.initialize({
    apiKey: 'new-api-key'
    });