UNPKG

@grzpab/ts-envvar

Version:

A library for managing environment variables in TypeScript projects

45 lines (44 loc) 1.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.extractBooleanEnvVar = exports.extractIntegerEnvVar = exports.extractNumberEnvVar = exports.extractStringEnvVar = exports.assertNonNullable = void 0; function assertNonNullable(name, value) { if (value === null || value === undefined) { const message = `Variable "${name}" cannot be "${String(value)}".`; throw new Error(message); } } exports.assertNonNullable = assertNonNullable; function extractStringEnvVar(key) { const value = process.env[key]; if (value === undefined) { const message = `The environment variable "${key}" cannot be "undefined".`; throw new Error(message); } return value; } exports.extractStringEnvVar = extractStringEnvVar; function extractNumberEnvVar(key) { const stringValue = extractStringEnvVar(key); const numberValue = parseFloat(stringValue); if (Number.isNaN(numberValue)) { const message = `The environment variable "${key}" has to hold a stringified number value - not ${stringValue}`; throw new Error(message); } return numberValue; } exports.extractNumberEnvVar = extractNumberEnvVar; function extractIntegerEnvVar(key) { const stringValue = extractStringEnvVar(key); const numberValue = parseInt(stringValue, 10); if (Number.isNaN(numberValue)) { const message = `The environment variable "${key}" has to hold a stringified integer value - not ${stringValue}`; throw new Error(message); } return numberValue; } exports.extractIntegerEnvVar = extractIntegerEnvVar; function extractBooleanEnvVar(key) { const value = extractIntegerEnvVar(key); return Boolean(value); } exports.extractBooleanEnvVar = extractBooleanEnvVar;