@w0s/env-value-type
v1.0.1
Published
Get the value of `process.env` with specified types
Readme
Get the value of process.env with specified types
Examples
# .env
SAMPLE_STRING = foo
SAMPLE_STRINGS = foo bar baz
SAMPLE_NUMBER = 1
SAMPLE_NUMBERS = 1,2,3
SAMPLE_BOOLEAN_TRUE = true
SAMPLE_BOOLEAN_FALSE = falseimport { env } from '@w0s/env-value-type';
env('SAMPLE_STRING'); // 'foo'
env('SAMPLE_STRING', 'string'); // 'foo'
env('SAMPLE_STRINGS', 'string[]'); // ['foo', 'bar', 'baz']
env('SAMPLE_STRINGS', 'string[]', { separator: ' ' }); // ['foo', 'bar', 'baz']
env('SAMPLE_NUMBER'); // '1'
env('SAMPLE_NUMBER', 'number'); // 1
env('SAMPLE_NUMBERS', 'number[]', { separator: ',' }); // [1, 2, 3]
env('SAMPLE_BOOLEAN_TRUE'); // 'true'
env('SAMPLE_BOOLEAN_TRUE', 'boolean'); // true
env('SAMPLE_BOOLEAN_FALSE', 'boolean'); // false
try {
env('XXX'); // Error
} catch {
}Functions
function getValue(key: string, type?: 'string', option?: Readonly<Option>): stringfunction getValue(key: string, type: 'string[]', option?: Readonly<Option>): string[]function getValue(key: string, type: 'number', option?: Readonly<Option>): numberfunction getValue(key: string, type: 'number[]', option?: Readonly<Option>): number[]function getValue(key: string, type: 'boolean', option?: Readonly<Option>): boolean
Option
interface Option {
separator: string; // Separator text. The default is ` ` (U+0020).
}