or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

async-config.mdconfig-utilities.mdcore-config.mddeferred-config.mdenvironment-integration.mdindex.mdraw-config.md
tile.json

config-utilities.mddocs/

Configuration Utilities

Comprehensive utilities for configuration management, including module defaults, immutability control, file loading, and object manipulation. All utilities are accessed via the config.util namespace.

Capabilities

Module Defaults Management

Set default configurations for modules to enable configurable library development.

/**
 * Set default configurations for a module
 * @param moduleName - Name of the module (can include dots for nesting)
 * @param defaultProperties - Default configuration object for the module
 * @returns Module configuration object with Config prototype attached
 */
config.util.setModuleDefaults(moduleName: string, defaultProperties: object): object;

Usage Examples:

const config = require('config');

// Set module defaults
config.util.setModuleDefaults('MyModule', {
  templateName: 't-50',
  colorScheme: 'green',
  timeout: 5000
});

// Access module configuration
console.log('Template:', config.MyModule.templateName);

// Nested module configuration
config.util.setModuleDefaults('Services.Database', {
  host: 'localhost',
  port: 5432,
  poolSize: 10
});

// Access nested module configuration
const dbConfig = config.Services.Database;
console.log(`Database: ${dbConfig.host}:${dbConfig.port}`);

Immutability Control

Make configuration objects and properties immutable to prevent runtime modifications.

/**
 * Make configuration properties immutable
 * @param object - Object to make immutable
 * @param property - Property name(s) to make immutable (optional)
 * @param value - Value to set before making immutable (optional)
 * @returns Original object for chaining
 */
config.util.makeImmutable(object: object, property?: string | string[], value?: any): object;

/**
 * Make configuration property hidden from enumeration
 * @param object - Object to modify
 * @param property - Property name to hide
 * @param value - Value to set (optional)
 * @returns Original object for chaining
 */
config.util.makeHidden(object: object, property: string, value?: any): object;

Usage Examples:

const config = require('config');

// Make entire config immutable
config.util.makeImmutable(config);

// Make specific property immutable
const myObject = { apiKey: 'secret', timeout: 5000 };
config.util.makeImmutable(myObject, 'apiKey');

// Make multiple properties immutable
config.util.makeImmutable(myObject, ['apiKey', 'timeout']);

// Hide sensitive properties
config.util.makeHidden(config.database, 'password');
config.util.makeHidden(config.api, 'secretKey', 'hidden-value');

// Hidden properties won't appear in enumeration
console.log(Object.keys(config.database)); // password won't be listed
console.log(config.database.password); // but still accessible

Configuration Sources

Access information about loaded configuration files and sources.

/**
 * Get array of configuration sources loaded
 * @returns Array of configuration source objects
 */
config.util.getConfigSources(): ConfigSource[];

interface ConfigSource {
  name: string;      // Source name (usually filename)
  parsed: object;    // Parsed configuration object  
  original?: string; // Original unparsed content
}

Usage Examples:

const config = require('config');

// Get configuration sources
const sources = config.util.getConfigSources();

sources.forEach(source => {
  console.log(`Loaded: ${source.name}`);
  console.log('Config keys:', Object.keys(source.parsed));
});

// Check which files were loaded
const fileNames = sources.map(source => source.name);
console.log('Configuration files loaded:', fileNames);

// Access original file content
sources.forEach(source => {
  if (source.original) {
    console.log(`Original content of ${source.name}:`, source.original);
  }
});

File Loading

Load configuration files from custom directories or with specific options.

/**
 * Load configuration files from specified directory
 * @param configDir - Configuration directory path (optional)
 * @param options - Loading options (optional)
 * @returns Configuration object
 */
config.util.loadFileConfigs(configDir?: string, options?: LoadOptions): object;

interface LoadOptions {
  configDir?: string;        // Configuration directory
  nodeEnv?: string[];        // Environment names
  hostName?: string;         // Hostname for host-specific configs
  appInstance?: string;      // Application instance ID
  skipConfigSources?: boolean; // Skip source tracking
  gitCrypt?: boolean;        // Allow gitcrypt files
}

Usage Examples:

const config = require('config');

// Load from custom directory
const customConfig = config.util.loadFileConfigs('./custom-config');

// Load with specific options
const testConfig = config.util.loadFileConfigs('./config', {
  nodeEnv: ['test'],
  hostName: 'test-server',
  skipConfigSources: true
});

// Load from multiple environments
const multiEnvConfig = config.util.loadFileConfigs('./config', {
  nodeEnv: ['development', 'local']
});

Object Manipulation Utilities

Comprehensive object manipulation utilities for deep operations.

/**
 * Create deep copy of configuration object
 * @param config - Configuration object to copy (defaults to main config)
 * @returns Plain object copy without Config prototype
 */
config.util.toObject(config?: object): object;

/**
 * Attach Config prototype methods to object recursively
 * @param toObject - Object to attach methods to
 * @param depth - Maximum recursion depth (default: 20)
 * @returns Original object with Config methods attached
 */
config.util.attachProtoDeep(toObject: object, depth?: number): object;

Usage Examples:

const config = require('config');

// Create plain object copy
const configCopy = config.util.toObject();
console.log(typeof configCopy.get); // undefined (no Config methods)

// Create copy of sub-configuration
const dbConfigCopy = config.util.toObject(config.database);

// Attach Config methods to plain object
const plainObject = { host: 'localhost', port: 3000 };
const configObject = config.util.attachProtoDeep(plainObject);
console.log(typeof configObject.get); // function
console.log(configObject.get('host')); // 'localhost'

// Methods are attached recursively
const nestedObject = {
  server: { host: 'localhost', port: 3000 },
  database: { host: 'db.local', port: 5432 }
};
config.util.attachProtoDeep(nestedObject);
console.log(nestedObject.server.get('host')); // 'localhost'

Deprecated Utilities

Legacy utilities maintained for backward compatibility.

// File parsing utilities (deprecated)
config.util.parseFile(fullFilename: string, options?: object): object;
config.util.parseString(content: string, format: string): object;

// Object manipulation utilities (deprecated)
config.util.cloneDeep(parent: object, depth?: number, circular?: boolean, prototype?: object): object;
config.util.extendDeep(mergeInto: object, ...mergeFrom: object[]): object;
config.util.equalsDeep(object1: object, object2: object, depth?: number): boolean;
config.util.diffDeep(object1: object, object2: object, depth?: number): object;
config.util.setPath(object: object, path: string[], value: any): void;

// Environment and parameter utilities (deprecated)
config.util.initParam(paramName: string, defaultValue?: any): any;
config.util.getCmdLineArg(searchFor: string): any;
config.util.getEnv(varName: string): string;

// Object type checking utilities (deprecated)
config.util.isObject(obj: any): boolean;
config.util.isPromise(obj: any): boolean;
config.util.getOption(options: object, optionName: string, defaultValue: any): any;

Usage Examples:

const config = require('config');

// Parse configuration file directly
const fileConfig = config.util.parseFile('./config/custom.json');

// Parse configuration string
const jsonConfig = config.util.parseString('{"key": "value"}', 'json');

// Deep object operations
const original = { a: { b: { c: 1 } } };
const copy = config.util.cloneDeep(original);
const extended = config.util.extendDeep({}, original, { a: { b: { d: 2 } } });

// Environment variable access
const nodeEnv = config.util.getEnv('NODE_ENV');
const customParam = config.util.initParam('CUSTOM_PARAM', 'default-value');