UNPKG

ts-app-env

Version:
77 lines (76 loc) 2.83 kB
declare type LogFn = (msg: string) => void; declare type Logger = { debug?: LogFn; info: LogFn; warn: LogFn; error: LogFn; }; /** * Creates a new config object, described by the {@code ConfigSpec} instance, * after populating/validating it against the {@code env} environment. * * The {@code ConfigSpec} should be an object literal that looks like: * * ```typescript * const AppEnv = { * prop_a: string(), * } * ``` * * And calling `newConfig(AppEnv, env)` will return a new object literal * that has the same type as `MyEnv`, but with `prop_a` resolved to the * `PROP_A` (or as appropriate) env value. */ export interface ConfigOptions { ignoreErrors?: boolean; doNotLogErrors?: boolean; /** The NODE_ENV e.g. production or development; used for properties that use `notNeededIn`. */ nodeEnv?: string; /** * Pass a custom logger (e.g., a Pino instance). * * @default - console */ logger?: Logger; } export declare function newConfig<S>(spec: S, env: Environment, options?: ConfigOptions): S; /** * The environment to pull setting names out of. * * This doesn't have to be Node's `process.env`, but that is the general * assumption and primary use case. */ export interface Environment { [name: string]: string | undefined; } export declare class ConfigError extends Error { } /** The settings that can be defined for each configuration option. */ export interface ConfigOptionSettings<V> { /** The environment variable name. */ env?: string; /** The default value to use if the environment variable is not set. */ default?: V; /** If this is optional, in which case the type should be {@code V | undefined}. */ optional?: boolean; /** A list of NODE_ENVs where this setting is not needed in. */ notNeededIn?: string | string[]; } /** Construct a config option that is a number. */ export declare function number(options: ConfigOptionSettings<number> & { optional: true; }): number | undefined; export declare function number(options?: ConfigOptionSettings<number>): number; /** Construct a config option that is a string. */ export declare function string(options: ConfigOptionSettings<string> & { optional: true; }): string | undefined; export declare function string(options?: ConfigOptionSettings<string>): string; /** Construct a config option that is a boolean, only the exact string value 'true' is treated as true, everything else is false. */ export declare function boolean(options: ConfigOptionSettings<boolean> & { optional: true; }): boolean | undefined; export declare function boolean(options?: ConfigOptionSettings<boolean>): boolean; /** Construct a generic config option. */ export declare function option<V>(options: ConfigOptionSettings<V>, parser: (s: string) => V): V; export {};