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

1import { ConfigLoader } from '@signalwire/sdk';
2
3const loader = new ConfigLoader();
4const config = {
5 port: '${PORT|8080}',
6 debug: '${DEBUG|false}',
7 name: '${SERVICE_NAME|my-service}',
8};
9
10const resolved = loader.substituteVars(config);
11console.log(resolved);
12// { port: 8080, debug: false, name: "my-service" } (types coerced)