/** * Configuration for a property that can be formatted. */ export 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. */ export interface EnvironmentConfig extends BaseConfig { /** * The name of the environment variable. */ variableName: string; } /** * Configuration for a property loaded from a secret. */ export 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. */ export type PropertyConfig = | string | EnvironmentConfig | FileConfig; /** * A function to format a string property. */ export type PropertyFormatter = (raw: string) => Value; /** * The result loading a property. * @internal */ export interface PropertyResult { /** * The error that was thrown, or false if there is none. */ error: false | Error; /** * The value that was loaded, or undefined if it could not be loaded. */ value: undefined | 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. */ export type UnwrapConfigMap< ConfigMap extends Record>, > = {[key in keyof ConfigMap]: UnwrapPropertyConfig}; /** * Unwrap a property config to its resultant value. */ export 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;