Utility functions for configuration processing including array handling, log level determination, and configuration merging.
function arrayOf<T>(value: T | T[] | undefined): T[];Converts a configuration value that can be either an array, a single value, or undefined into an array containing all values.
Parameters:
value: T | T[] | undefined - Value to wrap in an arrayReturns:
T[] - Array containing all the valuesBehavior:
undefined → [] (empty array)[value] (wrapped in array)[...value] (spread to new array)Examples:
import { arrayOf } from "@autorest/configuration";
// Single values
console.log(arrayOf("single")); // ["single"]
console.log(arrayOf(42)); // [42]
console.log(arrayOf(true)); // [true]
// Arrays
console.log(arrayOf(["a", "b", "c"])); // ["a", "b", "c"]
console.log(arrayOf([1, 2, 3])); // [1, 2, 3]
// Undefined
console.log(arrayOf(undefined)); // []
// Iterables (but not strings)
console.log(arrayOf(new Set(["x", "y"]))); // ["x", "y"]function isIterable(target: any): target is Iterable<any>;Type guard function that checks if a value implements the iterable protocol.
Parameters:
target: any - Value to check for iterabilityReturns:
target is Iterable<any> - Type guard indicating if value is iterableExamples:
import { isIterable } from "@autorest/configuration";
console.log(isIterable([1, 2, 3])); // true
console.log(isIterable("string")); // true
console.log(isIterable(new Set())); // true
console.log(isIterable(new Map())); // true
console.log(isIterable(42)); // false
console.log(isIterable({})); // false
console.log(isIterable(null)); // falseinterface LogOptions {
debug?: boolean;
verbose?: boolean;
level?: LogLevel | string;
}Configuration options for determining log levels.
Properties:
debug?: boolean - Enable debug loggingverbose?: boolean - Enable verbose logginglevel?: LogLevel | string - Explicit log level settingfunction getLogLevel(config: LogOptions): LogLevel;
type LogLevel = "debug" | "verbose" | "information" | "warning" | "error";Determines the appropriate log level from configuration options.
Parameters:
config: LogOptions - Configuration with logging optionsReturns:
LogLevel - Determined log levelLogic:
debug is true → "debug"verbose is true → "verbose"level is set and valid → level"information"Examples:
import { getLogLevel } from "@autorest/configuration";
console.log(getLogLevel({ debug: true })); // "debug"
console.log(getLogLevel({ verbose: true })); // "verbose"
console.log(getLogLevel({ level: "warning" })); // "warning"
console.log(getLogLevel({})); // "information"
// Debug takes precedence over verbose
console.log(getLogLevel({ debug: true, verbose: true })); // "debug"function isValidLogLevel(level: string): level is LogLevel;Type guard that validates if a string is a valid log level.
Parameters:
level: string - String to validate as log levelReturns:
level is LogLevel - Type guard indicating valid log levelValid Log Levels:
"debug" - Debug level logging"verbose" - Verbose level logging"information" - Information level logging"warning" - Warning level logging"error" - Error level loggingExamples:
import { isValidLogLevel } from "@autorest/configuration";
console.log(isValidLogLevel("debug")); // true
console.log(isValidLogLevel("info")); // false
console.log(isValidLogLevel("ERROR")); // false (case sensitive)
console.log(isValidLogLevel("information")); // truefunction mergeConfigurations<T>(configs: Array<T>, options?: MergeOptions): T;Merges multiple configuration objects together using deep merge with configurable options.
Parameters:
configs: Array<T> - Array of configuration objects to mergeoptions?: MergeOptions - Optional merge configurationReturns:
T - Merged configuration objectMerge Behavior:
Examples:
import { mergeConfigurations } from "@autorest/configuration";
const config1 = {
"input-file": ["swagger1.yaml"],
"output-folder": "./generated",
debug: false
};
const config2 = {
"input-file": ["swagger2.yaml"],
verbose: true,
csharp: true
};
const config3 = {
debug: true,
"namespace": "MyApi"
};
const merged = mergeConfigurations([config1, config2, config3]);
console.log(merged);
// Result:
// {
// "input-file": ["swagger2.yaml"], // Replaced by config2
// "output-folder": "./generated", // From config1
// debug: true, // Overridden by config3
// verbose: true, // From config2
// csharp: true, // From config2
// "namespace": "MyApi" // From config3
// }interface MergeOptions {
arrayMergeStrategy?: "replace" | "merge" | "low-pri-first";
// Additional merge configuration options
}Configuration options for controlling merge behavior.
Properties:
arrayMergeStrategy?: string - Strategy for merging arrays:
"replace" - Replace arrays completely"merge" - Merge arrays together"low-pri-first" - Low priority items firstimport {
arrayOf,
getLogLevel,
mergeConfigurations,
isValidLogLevel
} from "@autorest/configuration";
// Process CLI arguments
function processCliArgs(args: any) {
return {
"input-file": arrayOf(args["input-file"]),
"exclude-file": arrayOf(args["exclude-file"]),
debug: args.debug || false,
verbose: args.verbose || false,
level: isValidLogLevel(args.level) ? args.level : undefined
};
}
// Determine logging configuration
function setupLogging(config: any) {
const logLevel = getLogLevel({
debug: config.debug,
verbose: config.verbose,
level: config.level
});
console.log(`Log level: ${logLevel}`);
return logLevel;
}
// Merge multiple configuration sources
function buildFinalConfig(cliConfig: any, fileConfig: any, defaults: any) {
return mergeConfigurations([
defaults, // Lowest priority
fileConfig, // Medium priority
cliConfig // Highest priority
], {
arrayMergeStrategy: "low-pri-first"
});
}// Normalize configuration arrays
function normalizeConfig(rawConfig: any) {
return {
...rawConfig,
"input-file": arrayOf(rawConfig["input-file"]),
"exclude-file": arrayOf(rawConfig["exclude-file"]),
directive: arrayOf(rawConfig.directive),
use: arrayOf(rawConfig.use),
require: arrayOf(rawConfig.require),
"try-require": arrayOf(rawConfig["try-require"])
};
}
// Validate logging configuration
function validateLoggingConfig(config: any) {
if (config.level && !isValidLogLevel(config.level)) {
throw new Error(`Invalid log level: ${config.level}`);
}
return {
...config,
logLevel: getLogLevel(config)
};
}