Envapt
Call Signature
function Envapt(key, options?): EnvaptAccessorDecorator<string | undefined>;A custom converter function with a fallback (both required), or a fallback only.
Parameters
| Parameter | Type | Description |
|---|---|---|
key | EnvKeyInput | Environment variable name(s) to load |
options? | { converter?: undefined; fallback: undefined; } | Configuration options |
options.converter? | undefined | - |
options.fallback | undefined | - |
Returns
EnvaptAccessorDecorator<string | undefined>
Example
class Config {
\@Envapt('ADMIN_EMAILS', { fallback: new Set<string>(), converter: (raw) => new Set(raw.split(',')) })
static accessor adminEmails: Set<string>;
\@Envapt('LOG_FILE', { fallback: '/var/log/app.log' })
static accessor logFile: string;
}Call Signature
function Envapt<TFallback>(key, options): EnvaptAccessorDecorator<TFallback>;A custom converter function with a fallback (both required), or a fallback only.
Type Parameters
Parameters
| Parameter | Type | Description |
|---|---|---|
key | EnvKeyInput | Environment variable name(s) to load |
options | { converter: ConverterFunction<TFallback | null | undefined>; fallback: TFallback; } | Configuration options |
options.converter | ConverterFunction<TFallback | null | undefined> | - |
options.fallback | TFallback | - |
Returns
EnvaptAccessorDecorator<TFallback>
Example
class Config {
\@Envapt('ADMIN_EMAILS', { fallback: new Set<string>(), converter: (raw) => new Set(raw.split(',')) })
static accessor adminEmails: Set<string>;
\@Envapt('LOG_FILE', { fallback: '/var/log/app.log' })
static accessor logFile: string;
}Call Signature
function Envapt<TFallback>(key, options): EnvaptAccessorDecorator<FallbackOnlyReturn<TFallback>>;A custom converter function with a fallback (both required), or a fallback only.
Type Parameters
Parameters
| Parameter | Type | Description |
|---|---|---|
key | EnvKeyInput | Environment variable name(s) to load |
options | { converter?: undefined; fallback: TFallback; } | Configuration options |
options.converter? | undefined | - |
options.fallback | TFallback | - |
Returns
EnvaptAccessorDecorator<FallbackOnlyReturn<TFallback>>
Example
class Config {
\@Envapt('ADMIN_EMAILS', { fallback: new Set<string>(), converter: (raw) => new Set(raw.split(',')) })
static accessor adminEmails: Set<string>;
\@Envapt('LOG_FILE', { fallback: '/var/log/app.log' })
static accessor logFile: string;
}Call Signature
function Envapt<TReturnType>(key, options): EnvaptAccessorDecorator<NonNullable<TReturnType> | undefined>;A custom converter function without a fallback. Omit required to get undefined for a missing
value, or pass required: true to throw MissingEnvValue on missing or empty values.
Type Parameters
Parameters
| Parameter | Type | Description |
|---|---|---|
key | EnvKeyInput | Environment variable name(s) to load |
options | { converter: ConverterFunction<TReturnType>; required?: false; } | Configuration options |
options.converter | ConverterFunction<TReturnType> | - |
options.required? | false | - |
Returns
EnvaptAccessorDecorator<NonNullable<TReturnType> | undefined>
Example
class Config {
\@Envapt('MAINTENANCE_UNTIL', { converter: (raw) => new Date(raw) })
static accessor maintenanceUntil: Date | undefined;
\@Envapt('JWT_SECRET', { converter: (raw) => Buffer.from(raw, 'base64'), required: true })
static accessor jwtSecret: Buffer;
}Call Signature
function Envapt<TReturnType>(key, options): EnvaptAccessorDecorator<NonNullable<TReturnType>>;A custom converter function with a fallback (both required), or a fallback only.
Type Parameters
Parameters
| Parameter | Type | Description |
|---|---|---|
key | EnvKeyInput | Environment variable name(s) to load |
options | { converter: ConverterFunction<TReturnType>; required: true; } | Configuration options |
options.converter | ConverterFunction<TReturnType> | - |
options.required | true | - |
Returns
EnvaptAccessorDecorator<NonNullable<TReturnType>>
Example
class Config {
\@Envapt('ADMIN_EMAILS', { fallback: new Set<string>(), converter: (raw) => new Set(raw.split(',')) })
static accessor adminEmails: Set<string>;
\@Envapt('LOG_FILE', { fallback: '/var/log/app.log' })
static accessor logFile: string;
}Call Signature
function Envapt<TConverter>(key, options): EnvaptAccessorDecorator<InferConverterReturnType<TConverter>>;A built-in or array converter with a fallback, or required: true. The converter sets the
fallback type, for example a number or time-string for Converters.Time and a URL instance
for Converters.Url.
Type Parameters
Parameters
| Parameter | Type | Description |
|---|---|---|
key | EnvKeyInput | Environment variable name(s) to load |
options | | { converter: TConverter; fallback: InferConverterFallbackType<TConverter>; required?: false; } | { converter: TConverter; required: true; } | Configuration options |
Returns
EnvaptAccessorDecorator<InferConverterReturnType<TConverter>>
Example
import { Converters } from 'envapt';
class Config {
\@Envapt('APP_PORT', { converter: Converters.Port, fallback: 3000 })
static accessor port: number;
\@Envapt('APP_URL', { converter: Converters.Url, fallback: new URL('http://localhost:3000') })
static accessor url: URL;
// reads CANARY_URL when present, otherwise APP_URL
\@Envapt(['CANARY_URL', 'APP_URL'], { converter: Converters.Url })
static accessor canaryUrl: URL | undefined;
// Time takes a number (milliseconds) or a time-string fallback (`<number><unit>`)
\@Envapt('REQUEST_TIMEOUT', { converter: Converters.Time, fallback: '10s' })
static accessor requestTimeout: number;
\@Envapt('ALLOWED_ORIGINS', {
converter: Converters.array({ of: Converters.String }),
fallback: ['https://example.com']
})
static accessor allowedOrigins: string[];
\@Envapt('DATABASE_URL', { converter: Converters.Url, required: true })
static accessor databaseUrl: URL;
}Call Signature
function Envapt<TConverter>(key, options): EnvaptAccessorDecorator<InferConverterReturnType<TConverter> | undefined>;A custom converter function with a fallback (both required), or a fallback only.
Type Parameters
Parameters
| Parameter | Type | Description |
|---|---|---|
key | EnvKeyInput | Environment variable name(s) to load |
options | { converter: TConverter; fallback?: undefined; required?: false; } | Configuration options |
options.converter | TConverter | - |
options.fallback? | undefined | - |
options.required? | false | - |
Returns
EnvaptAccessorDecorator<InferConverterReturnType<TConverter> | undefined>
Example
class Config {
\@Envapt('ADMIN_EMAILS', { fallback: new Set<string>(), converter: (raw) => new Set(raw.split(',')) })
static accessor adminEmails: Set<string>;
\@Envapt('LOG_FILE', { fallback: '/var/log/app.log' })
static accessor logFile: string;
}Call Signature
function Envapt(key, options): EnvaptAccessorDecorator<string>;Required, no converter (raw string). Throws MissingEnvValue on first access when the env
value is missing or empty. Pairing required: true with fallback matches no overload at
compile time. A call that bypasses the types throws InvalidUserDefinedConfig at runtime.
Parameters
| Parameter | Type | Description |
|---|---|---|
key | EnvKeyInput | Environment variable name(s) to load |
options | { required: true; } | { required: true } |
options.required | true | - |
Returns
EnvaptAccessorDecorator<string>
Example
class Config {
\@Envapt('API_KEY', { required: true })
static accessor apiKey: string;
}Call Signature
function Envapt<Schema>(key, options): EnvaptAccessorDecorator<InferSchemaOutput<Schema> | undefined>;A Standard Schema v1 schema (zod, valibot, arktype, or hand-written). Synchronous schemas only.
A validate that returns a Promise throws InvalidUserDefinedConfig at runtime. Pairing
schema with converter matches no overload at compile time. A call that bypasses the types
throws InvalidUserDefinedConfig at runtime.
Type Parameters
| Type Parameter |
|---|
Schema extends StandardSchemaV1<unknown, unknown> |
Parameters
| Parameter | Type |
|---|---|
key | EnvKeyInput |
options | { fallback?: undefined; required?: false; schema: SchemaConstraint<Schema>; } |
options.fallback? | undefined |
options.required? | false |
options.schema | SchemaConstraint<Schema> |
Returns
EnvaptAccessorDecorator<InferSchemaOutput<Schema> | undefined>
Call Signature
function Envapt<Schema>(key, options): EnvaptAccessorDecorator<InferSchemaOutput<Schema>>;A custom converter function with a fallback (both required), or a fallback only.
Type Parameters
| Type Parameter |
|---|
Schema extends StandardSchemaV1<unknown, unknown> |
Parameters
| Parameter | Type | Description |
|---|---|---|
key | EnvKeyInput | Environment variable name(s) to load |
options | | { fallback: InferSchemaOutput<Schema>; required?: false; schema: SchemaConstraint<Schema>; } | { required: true; schema: SchemaConstraint<Schema>; } | Configuration options |
Returns
EnvaptAccessorDecorator<InferSchemaOutput<Schema>>
Example
class Config {
\@Envapt('ADMIN_EMAILS', { fallback: new Set<string>(), converter: (raw) => new Set(raw.split(',')) })
static accessor adminEmails: Set<string>;
\@Envapt('LOG_FILE', { fallback: '/var/log/app.log' })
static accessor logFile: string;
}