UNPKG

@layerfig/config

Version:

Layer and runtime-validate type-safe configs for JavaScript apps.

64 lines 2.31 kB
//#region src/types.d.ts interface ResultSuccess<TSuccess = undefined> { ok: true; data: TSuccess; } interface ResultError<TError = undefined> { ok: false; error?: TError; } type Result<TSuccess = undefined, TError = undefined> = ResultSuccess<TSuccess> | ResultError<TError>; /** * Utility type to resolves complex types (specially the ones with intersection types) * to a more readable format. */ type Prettify<T> = { [K in keyof T]: T[K] } & {}; type PartialDeepUnknown<T> = { [K in keyof T]?: T[K] extends Record<string, unknown> ? T[K] extends unknown[] ? unknown : T[K] extends Date ? unknown : T[K] extends Function ? unknown : PartialDeepUnknown<T[K]> : unknown }; //#endregion //#region src/parser/define-config-parser.d.ts type ParserFunction = (fileContent: string) => Result<Record<string, unknown>, Error>; interface ConfigParserOptions { acceptedFileExtensions: string[]; parse: ParserFunction; } interface ConfigParser { acceptsExtension(fileExtension: string): boolean; load: ConfigParserOptions["parse"]; acceptedFileExtensions: ConfigParserOptions["acceptedFileExtensions"]; } declare function defineConfigParser({ acceptedFileExtensions, parse: load }: ConfigParserOptions): ConfigParser; //#endregion //#region src/sources/source.d.ts interface RuntimeEnv { [key: string]: string | undefined; } interface LoadSourceOptions { parser: ConfigParser; relativeConfigFolderPath: string; runtimeEnv: RuntimeEnv; slotPrefix: string; } interface MaybeReplaceSlotFromValueOptions extends Pick<LoadSourceOptions, "runtimeEnv" | "slotPrefix"> { value: string; } declare abstract class Source<T = Record<string, unknown>> { #private; /** * An abstract method that must be implemented by any subclass. * It defines the contract for loading a source. * @param loadSourceOptions - The options for loading the source. * @returns A record representing the loaded source data. */ abstract loadSource(loadSourceOptions: LoadSourceOptions): Prettify<T>; maybeReplaceSlotFromValue({ value, slotPrefix, runtimeEnv }: MaybeReplaceSlotFromValueOptions): string; } //#endregion export { ConfigParser, LoadSourceOptions, PartialDeepUnknown, Prettify, Source as Source$1, defineConfigParser }; //# sourceMappingURL=source-CJDueGqx.d.ts.map