substituteVars

View as MarkdownOpen in Claude

Recursively substitute ${VAR|default} environment-variable references in any value. Walks strings, objects, and arrays. After substitution, resulting strings are coerced to:

  • true / false when they equal "true" / "false" (case-insensitive)
  • an integer when they match /^\d+$/
  • a float when they match /^\d+\.\d+$/
  • otherwise left as a string

For raw string interpolation without type coercion, use interpolateEnvVars() instead.

Parameters

value
unknownRequired

The value to process. Can be a string, object, array, or primitive.

maxDepth
numberDefaults to 10

Maximum recursion depth. Throws Error("Maximum variable substitution depth exceeded") when the limit is reached.

Returns

The input value with all environment variables substituted. Type depends on the input and the coercion rules above.

Example

import { ConfigLoader } from '@signalwire/sdk';
const loader = new ConfigLoader();
const config = {
port: '${PORT|8080}',
debug: '${DEBUG|false}',
name: '${SERVICE_NAME|my-service}',
};
const resolved = loader.substituteVars(config);
console.log(resolved);
// { port: 8080, debug: false, name: "my-service" } (types coerced)