env-typed-config
Version:
Intuitive, type-safe configuration library for Node.js
114 lines (109 loc) • 3.88 kB
TypeScript
import { TypedConfigOptions } from './types.js';
import { OptionsSync } from 'cosmiconfig';
import 'class-transformer';
import 'class-validator';
interface FileLoaderOptions extends Partial<OptionsSync> {
/**
* basename of config file, defaults to `.env`.
*
* In other words, `.env.yaml`, `.env.yml`, `.env.json`, `.env.toml`, `.env.js`
* will be searched by default.
*/
basename?: string;
/**
* Use given file directly, instead of recursively searching in directory tree.
*/
absolutePath?: string;
/**
* The directory to search from, defaults to `process.cwd()`. See: https://github.com/davidtheclark/cosmiconfig#explorersearch
*/
searchFrom?: string;
/**
* If "true", ignore environment variable substitution.
* Default: true
*/
ignoreEnvironmentVariableSubstitution?: boolean;
/**
* If "true", disallow undefined environment variables.
* Default: true
*/
disallowUndefinedEnvironmentVariables?: boolean;
}
/**
* File loader loads configuration with `cosmiconfig` from file system.
*
* It is designed to be easy to use by default:
* 1. Searching for configuration file starts at `process.cwd()`, and continues
* to search up the directory tree until it finds some acceptable configuration.
* 2. Various extensions are supported, such as `.json`, `.yaml`, `.toml`, `.js` and `.cjs`.
* 3. Configuration base name defaults to .env (so the full name is `.env.json` or `.env.yaml`),
* separate file for each environment is also supported. For example, if current `NODE_ENV` is
* development, `.env.development.json` has higher priority over `.env.json`.
*
* @see https://github.com/davidtheclark/cosmiconfig
* @param options cosmiconfig initialize options. See: https://github.com/davidtheclark/cosmiconfig#cosmiconfigoptions
*/
declare const fileLoader: (options?: FileLoaderOptions) => (() => Record<string, any>);
interface DotenvLoaderOptions {
/**
* If set, use the separator to parse environment variables to objects.
*
* @example
*
* ```bash
* app__port=8080
* db__host=127.0.0.1
* db__port=3000
* ```
*
* if `separator` is set to `__`, environment variables above will be parsed as:
*
* ```json
* {
* "app": {
* "port": 8080
* },
* "db": {
* "host": "127.0.0.1",
* "port": 3000
* }
* }
* ```
*/
separator?: string;
/**
* If "true", environment files (`.env`) will be ignored.
*/
ignoreEnvFile?: boolean;
/**
* If "true", predefined environment variables will not be validated.
*/
ignoreEnvVars?: boolean;
/**
* Path to the environment file(s) to be loaded.
*/
envFilePath?: string | string[];
/**
* A boolean value indicating the use of expanded variables.
* If .env contains expanded variables, they'll only be parsed if
* this property is set to true.
*
* Internally, dotenv-expand is used to expand variables.
*/
expandVariables?: boolean;
}
/**
* Dotenv loader loads configuration with `dotenv`.
*
*/
declare const dotenvLoader: (options?: DotenvLoaderOptions) => () => Record<string, any>;
declare class TypedConfig<T extends object> {
init(options: TypedConfigOptions<T>): Promise<T>;
private processConfig;
private getRawConfigAsync;
private validateWithClassValidator;
private getConfigErrorMessage;
private formatValidationError;
}
declare function defineConfig<T extends object>(options: TypedConfigOptions<T>): Promise<T>;
export { DotenvLoaderOptions, FileLoaderOptions, TypedConfig, defineConfig, dotenvLoader, fileLoader };