v9.1.0
legacyFunctions

Envapt

Call Signature

function Envapt(key, options?): EnvaptFieldDecorator<string | undefined>;

A custom converter function with a fallback (both required), or a fallback only.

Parameters

ParameterTypeDescription
keyEnvKeyInputEnvironment variable name(s) to load
options?{ converter?: undefined; fallback: undefined; }Configuration options
options.converter?undefined-
options.fallbackundefined-

Returns

EnvaptFieldDecorator<string | undefined>

Example

class Config extends Envapter {
  \@Envapt('ADMIN_EMAILS', { fallback: new Set<string>(), converter: (raw) => new Set(raw.split(',')) })
  static readonly adminEmails: Set<string>;

  \@Envapt('LOG_FILE', { fallback: '/var/log/app.log' })
  static readonly logFile: string;
}

Call Signature

function Envapt<TFallback>(key, options): EnvaptFieldDecorator<TFallback>;

A custom converter function with a fallback (both required), or a fallback only.

Type Parameters

Type Parameter
TFallback

Parameters

ParameterTypeDescription
keyEnvKeyInputEnvironment variable name(s) to load
options{ converter: ConverterFunction<TFallback | null | undefined>; fallback: TFallback; }Configuration options
options.converterConverterFunction<TFallback | null | undefined>-
options.fallbackTFallback-

Returns

EnvaptFieldDecorator<TFallback>

Example

class Config extends Envapter {
  \@Envapt('ADMIN_EMAILS', { fallback: new Set<string>(), converter: (raw) => new Set(raw.split(',')) })
  static readonly adminEmails: Set<string>;

  \@Envapt('LOG_FILE', { fallback: '/var/log/app.log' })
  static readonly logFile: string;
}

Call Signature

function Envapt<TFallback>(key, options): EnvaptFieldDecorator<FallbackOnlyReturn<TFallback>>;

A custom converter function with a fallback (both required), or a fallback only.

Type Parameters

Type Parameter
TFallback extends FallbackOnlyValue | undefined

Parameters

ParameterTypeDescription
keyEnvKeyInputEnvironment variable name(s) to load
options{ converter?: undefined; fallback: TFallback; }Configuration options
options.converter?undefined-
options.fallbackTFallback-

Returns

EnvaptFieldDecorator<FallbackOnlyReturn<TFallback>>

Example

class Config extends Envapter {
  \@Envapt('ADMIN_EMAILS', { fallback: new Set<string>(), converter: (raw) => new Set(raw.split(',')) })
  static readonly adminEmails: Set<string>;

  \@Envapt('LOG_FILE', { fallback: '/var/log/app.log' })
  static readonly logFile: string;
}

Call Signature

function Envapt<TReturnType>(key, options): EnvaptFieldDecorator<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

Type Parameter
TReturnType

Parameters

ParameterTypeDescription
keyEnvKeyInputEnvironment variable name(s) to load
options{ converter: ConverterFunction<TReturnType>; required?: false; }Configuration options
options.converterConverterFunction<TReturnType>-
options.required?false-

Returns

EnvaptFieldDecorator<NonNullable<TReturnType> | undefined>

Example

class Config extends Envapter {
  \@Envapt('MAINTENANCE_UNTIL', { converter: (raw) => new Date(raw) })
  static readonly maintenanceUntil: Date | undefined;

  \@Envapt('JWT_SECRET', { converter: (raw) => Buffer.from(raw, 'base64'), required: true })
  static readonly jwtSecret: Buffer;
}

Call Signature

function Envapt<TReturnType>(key, options): EnvaptFieldDecorator<NonNullable<TReturnType>>;

A custom converter function with a fallback (both required), or a fallback only.

Type Parameters

Type Parameter
TReturnType

Parameters

ParameterTypeDescription
keyEnvKeyInputEnvironment variable name(s) to load
options{ converter: ConverterFunction<TReturnType>; required: true; }Configuration options
options.converterConverterFunction<TReturnType>-
options.requiredtrue-

Returns

EnvaptFieldDecorator<NonNullable<TReturnType>>

Example

class Config extends Envapter {
  \@Envapt('ADMIN_EMAILS', { fallback: new Set<string>(), converter: (raw) => new Set(raw.split(',')) })
  static readonly adminEmails: Set<string>;

  \@Envapt('LOG_FILE', { fallback: '/var/log/app.log' })
  static readonly logFile: string;
}

Call Signature

function Envapt<TConverter>(key, options): EnvaptFieldDecorator<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

Type Parameter
TConverter extends ConverterToken | ArrayOf<ArrayElement>

Parameters

ParameterTypeDescription
keyEnvKeyInputEnvironment variable name(s) to load
options| { converter: TConverter; fallback: InferConverterFallbackType<TConverter>; required?: false; } | { converter: TConverter; required: true; }Configuration options

Returns

EnvaptFieldDecorator<InferConverterReturnType<TConverter>>

Example

import { Converters } from 'envapt';

class Config extends Envapter {
  \@Envapt('APP_PORT', { converter: Converters.Port, fallback: 3000 })
  static readonly port: number;

  \@Envapt('APP_URL', { converter: Converters.Url, fallback: new URL('http://localhost:3000') })
  static readonly url: URL;

  // reads CANARY_URL when present, otherwise APP_URL
  \@Envapt(['CANARY_URL', 'APP_URL'], { converter: Converters.Url })
  static readonly canaryUrl: URL | undefined;

  // Time takes a number (milliseconds) or a time-string fallback (`<number><unit>`)
  \@Envapt('REQUEST_TIMEOUT', { converter: Converters.Time, fallback: '10s' })
  static readonly requestTimeout: number;

  \@Envapt('ALLOWED_ORIGINS', {
    converter: Converters.array({ of: Converters.String }),
    fallback: ['https://example.com']
  })
  static readonly allowedOrigins: string[];

  \@Envapt('DATABASE_URL', { converter: Converters.Url, required: true })
  static readonly databaseUrl: URL;
}

Call Signature

function Envapt<TConverter>(key, options): EnvaptFieldDecorator<InferConverterReturnType<TConverter> | undefined>;

A custom converter function with a fallback (both required), or a fallback only.

Type Parameters

Type Parameter
TConverter extends ConverterToken | ArrayOf<ArrayElement>

Parameters

ParameterTypeDescription
keyEnvKeyInputEnvironment variable name(s) to load
options{ converter: TConverter; fallback?: undefined; required?: false; }Configuration options
options.converterTConverter-
options.fallback?undefined-
options.required?false-

Returns

EnvaptFieldDecorator<InferConverterReturnType<TConverter> | undefined>

Example

class Config extends Envapter {
  \@Envapt('ADMIN_EMAILS', { fallback: new Set<string>(), converter: (raw) => new Set(raw.split(',')) })
  static readonly adminEmails: Set<string>;

  \@Envapt('LOG_FILE', { fallback: '/var/log/app.log' })
  static readonly logFile: string;
}

Call Signature

function Envapt(key, options): EnvaptFieldDecorator<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

ParameterTypeDescription
keyEnvKeyInputEnvironment variable name(s) to load
options{ required: true; }{ required: true }
options.requiredtrue-

Returns

EnvaptFieldDecorator<string>

Example

class Config extends Envapter {
  \@Envapt('API_KEY', { required: true })
  static readonly apiKey: string;
}

Call Signature

function Envapt<Schema>(key, options): EnvaptFieldDecorator<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

ParameterType
keyEnvKeyInput
options{ fallback?: undefined; required?: false; schema: SchemaConstraint<Schema>; }
options.fallback?undefined
options.required?false
options.schemaSchemaConstraint<Schema>

Returns

EnvaptFieldDecorator<InferSchemaOutput<Schema> | undefined>

Call Signature

function Envapt<Schema>(key, options): EnvaptFieldDecorator<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

ParameterTypeDescription
keyEnvKeyInputEnvironment variable name(s) to load
options| { fallback: InferSchemaOutput<Schema>; required?: false; schema: SchemaConstraint<Schema>; } | { required: true; schema: SchemaConstraint<Schema>; }Configuration options

Returns

EnvaptFieldDecorator<InferSchemaOutput<Schema>>

Example

class Config extends Envapter {
  \@Envapt('ADMIN_EMAILS', { fallback: new Set<string>(), converter: (raw) => new Set(raw.split(',')) })
  static readonly adminEmails: Set<string>;

  \@Envapt('LOG_FILE', { fallback: '/var/log/app.log' })
  static readonly logFile: string;
}

On this page