
Security News
Vite+ Joins the Push to Consolidate JavaScript Tooling
Evan You announces Vite+, a commercial, Rust-powered toolchain built on the Vite ecosystem to unify JavaScript development and fund open source.
@tsigel/get_env_prop
Advanced tools
A lightweight utility for safely accessing and processing environment variables in Node.js applications.
npm install @tsigel/get_env_prop
# or
yarn add @tsigel/get_env_prop
# or
pnpm add @tsigel/get_env_prop
import { get_env_prop, strict, list } from '@tsigel/get_env_prop';
// Basic usage - get an environment variable
const apiUrl = get_env_prop('API_URL');
// With strict validation (throws if undefined)
const port = get_env_prop('PORT', strict);
// With custom processing
const dbTimeout = get_env_prop('DB_TIMEOUT', (value) => value ? parseInt(value) : 30000);
// Parse comma-separated list
const allowedOrigins = get_env_prop('ALLOWED_ORIGINS', list());
// Parse list with custom separator
const tags = get_env_prop('TAGS', list('|'));
// Chain processors
const requiredList = get_env_prop('REQUIRED_VALUES', (value) => list()(strict(value)));
get_env_prop(key: string): string | undefined
Retrieves an environment variable by key.
undefined
if not setget_env_prop<T>(key: string, processor: (value: string | undefined) => T): T
Retrieves and processes an environment variable with a custom processor function.
strict(value: string | undefined): string
Ensures that an environment variable is defined.
undefined
or null
list(separator?: string)
Creates a processor that splits a string value into an array.
,
)undefined
if input is undefined
import { get_env_prop, strict } from '@tsigel/get_env_prop';
// Application configuration
const config = {
port: get_env_prop('PORT', (value) => parseInt(value || '3000')),
nodeEnv: get_env_prop('NODE_ENV', (value) => value || 'development'),
apiKey: get_env_prop('API_KEY', strict),
dbUrl: get_env_prop('DATABASE_URL', strict)
};
import { get_env_prop } from '@tsigel/get_env_prop';
const isProduction = get_env_prop('NODE_ENV', (value) => value === 'production');
const debugMode = get_env_prop('DEBUG', (value) => value === 'true');
import { get_env_prop } from '@tsigel/get_env_prop';
const cacheTimeout = get_env_prop('CACHE_TIMEOUT', (value) => {
if (!value) return 3600; // default 1 hour
return parseInt(value);
});
import { get_env_prop, list, strict } from '@tsigel/get_env_prop';
// Optional list
const supportedLanguages = get_env_prop('SUPPORTED_LANGUAGES', list());
// ["en", "fr", "de"] if SUPPORTED_LANGUAGES="en,fr,de"
// undefined if SUPPORTED_LANGUAGES is not defined
// Required list
const requiredFeatures = get_env_prop('REQUIRED_FEATURES', (value) => list()(strict(value)));
// Will throw if REQUIRED_FEATURES is not defined
import { get_env_prop, strict } from '@tsigel/get_env_prop';
const serverConfig = get_env_prop('SERVER_CONFIG', (value) => {
if (!value) return { port: 3000, host: 'localhost' };
try {
return JSON.parse(value);
} catch (e) {
throw new Error(`Invalid JSON in SERVER_CONFIG: ${e.message}`);
}
});
The library provides helpful error messages if something goes wrong when accessing environment variables:
try {
const requiredValue = get_env_prop('REQUIRED_VALUE', strict);
} catch (error) {
console.error(error.message);
// Outputs: "Error get REQUIRED_VALUE from env! Env value can't be empty!"
}
The library is fully typed and provides accurate type inference:
// value is string | undefined
const value = get_env_prop('SOME_VAR');
// requiredValue is string (never undefined)
const requiredValue = get_env_prop('REQUIRED', strict);
// numberValue is number
const numberValue = get_env_prop('NUMBER', (val) => parseInt(val || '0'));
// items is string[] | undefined
const items = get_env_prop('ITEMS', list());
MIT
FAQs
Get required property from env with exeption. Get typed property.
We found that @tsigel/get_env_prop demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Evan You announces Vite+, a commercial, Rust-powered toolchain built on the Vite ecosystem to unify JavaScript development and fund open source.
Security News
Ruby Central’s incident report on the RubyGems.org access dispute sparks backlash from former maintainers and renewed debate over project governance.
Research
/Security News
Socket researchers uncover how threat actors weaponize Discord across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.