Node Scripting Patterns
Environment Config Record
Environment variables arrive as strings, then scripts convert them into typed config.
Environment Config Record
env.ts
type EnvConfig = {
debug: boolean;
retries: number;
};
function readConfig(env: Record<string, string>): EnvConfig {
return {
debug: env.DEBUG === "true",
retries: Number(env.RETRIES)
};
}
const debugText: string = ;
const env: Record<string, string> = {
DEBUG: debugText,
RETRIES: "3"
};
const config: EnvConfig = readConfig(env);
console.log(`${config.debug ? "debug" : "normal"}:${config.retries}`);
type EnvConfig = {
debug: boolean;
retries: number;
};
function readConfig(env: Record<string, string>): EnvConfig {
return {
debug: env.DEBUG === "true",
retries: Number(env.RETRIES)
};
}
const debugText: string = ;
const env: Record<string, string> = {
DEBUG: debugText,
RETRIES: "3"
};
const config: EnvConfig = readConfig(env);
console.log(`${config.debug ? "debug" : "normal"}:${config.retries}`);
environment config
A typed config object keeps string environment input at the edge of a script.