/** * An error thrown when configuration could not be loaded. */ declare class ConfigError> extends Error { /** * A collection of the individual errors, keyed by property name. */ errors: ErrorMap; /** * Create a ConfigError. * @param errors - The errors that this ConfigError represent. */ constructor(errors: ErrorMap); /** * Convert this ConfigError to JSON. * * @remarks * This is here so the ConfigError can be stringified a little cleaner. Throwing it'll still just show the base message. * @returns An object containing the message of this error and the messages of the errors it represents. */ toJSON(): { errors: { [name: string]: string; }; message: string; }; } /** * Configuration for a property that can be formatted. */ interface BaseConfig { /** * The default value of this property. */ defaultValue?: Value; /** * A function to format the raw value. */ format?: PropertyFormatter; /** * Whether this property is required. * @defaultValue true */ required?: boolean; } /** * Configuration for a property loaded from an environment variable. */ interface EnvironmentConfig extends BaseConfig { /** * The name of the environment variable. */ variableName: string; } /** * Configuration for a property loaded from a secret. */ interface FileConfig extends BaseConfig { /* eslint-disable unicorn/text-encoding-identifier-case -- Values that would upset this rule are still valid. */ /** * The encoding of the file containing the secret. * @defaultValue 'utf8' */ encoding?: "ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "base64url" | "latin1" | "binary" | "hex"; /* eslint-enable unicorn/text-encoding-identifier-case */ /** * The path to the file containing the secret. */ filePath: string; } /** * The configuration for loading a specific property. */ type PropertyConfig = string | EnvironmentConfig | FileConfig; /** * A function to format a string property. */ type PropertyFormatter = (raw: string) => Value; /** * Unwrap a config map to its resultant config. * * @typeParam ConfigMap - Type of the config map that will be passed to `loadConfig` or `loadConfigSync`. * * @remarks * * This type is exported for consumer use (e.g. a function that calls `load-config-sync` and returns the result), but is not actually used in this library. * While the implementation is identical to the return type of `load-config-sync` (or `load-config` but without the Promise), this type is not used there. * Currently, TypeScript, at least in VSCode, does not evaluate user-defined type aliases on hover, leading to a less clear user experience. * If this behavior is changed, this type should replace the inline implementations. */ type UnwrapConfigMap>> = { [key in keyof ConfigMap]: UnwrapPropertyConfig; }; /** * Unwrap a property config to its resultant value. */ type UnwrapPropertyConfig> = PConfig extends string ? string : PConfig extends { required: false; } ? UnwrapResult | undefined : UnwrapResult; /** * Unwrap a result to its value. */ type UnwrapResult> = PConfig extends BaseConfig ? Value : string; /** * Load configuration asynchronously. * * @example * ``` * const config = await loadConfig({ * postgresPassword: { * filePath: '/secrets/password', * }, * postgresUrl: { * format: (value) => new URL(value), * variableName: 'POSTGRES_URL' * }, * postgresUsername: { * defaultValue: 'postgres', * filePath: '/secrets/username', * }, * }); * * assert(typeof config.postgresPassword === 'string'); * assert(config.postgresUrl instanceof URL); * assert(typeof config.postgresUsername === 'string'); * ``` * * @param configMap - An object map of configuration properties and how they should be loaded. * @returns A Promise that resolves to the loaded configuration object. * * @throws {@link ConfigError} * Rejects if one or more configuration properties was not able to load properly. */ declare function loadConfig>>(configMap: ConfigMap): Promise<{ [key in keyof ConfigMap]: UnwrapPropertyConfig; }>; /** * Load configuration synchronously. * * @example * ``` * const config = loadConfigSync({ * postgresPassword: { * filePath: '/secrets/password', * }, * postgresUrl: { * format: (value) => new URL(value), * variableName: 'POSTGRES_URL' * }, * postgresUsername: { * defaultValue: 'postgres', * filePath: '/secrets/username', * }, * }); * * assert(typeof config.postgresPassword === 'string'); * assert(config.postgresUrl instanceof URL); * assert(typeof config.postgresUsername === 'string'); * ``` * * @param configMap - An object map of configuration properties and how they should be loaded. * @returns The loaded configuration object. * * @throws {@link ConfigError} * Thrown if one or more configuration properties was not able to load properly. */ declare function loadConfigSync>>(configMap: ConfigMap): { [key in keyof ConfigMap]: UnwrapPropertyConfig; }; export { ConfigError, loadConfig, loadConfigSync }; export type { UnwrapConfigMap };