UNPKG

@ghii/yaml-loader

Version:

A Funny yaml loader for ghii configuration manager

65 lines 2.64 kB
import type { Loader } from '@ghii/ghii-v2'; /** * Creates a YAML file loader function for use with GHII configuration manager. * * This function generates a loader that reads and parses a YAML file from the filesystem. * The loader supports customizable error handling and logging, making it suitable for * various deployment scenarios where configuration files may or may not be present. * * @template T - The expected shape of the configuration object. Must extend `Record<string, unknown>`. * Defaults to `Record<string, unknown>` if not specified. * * @param options - Configuration options for the loader. * @param options.throwOnError - If `true`, throws errors when file operations fail. * If `false` (default), returns an empty object `{}` on errors. * @param options.logger - Custom logging function called when errors occur. * Receives the error object and a message string. * Defaults to `console.log(message, err)`. * @param filePathToken - Variable number of path segments that will be joined together * to form the absolute file path using `path.join()`. * Example: `yamlLoader(options, '/path/to', 'config.yaml')` * * @returns A `Loader` function that returns a Promise resolving to the parsed YAML content. * The loader will: * - Return the parsed YAML as a typed object `T` on success * - Throw an error if `throwOnError: true` and an error occurs * - Return `{}` (typed as `T`) if `throwOnError: false` and an error occurs * * @example * ```typescript * // Basic usage with error throwing * const loader = yamlLoader( * { throwOnError: true }, * __dirname, * 'config.yaml' * ); * const config = await loader(); * * // Usage with custom logger * const loader = yamlLoader( * { * throwOnError: false, * logger: (err, msg) => console.error(msg, err), * }, * __dirname, * 'config.yaml' * ); * * // Type-safe usage * interface AppConfig { * database: { host: string; port: number }; * } * const loader = yamlLoader<AppConfig>( * { throwOnError: true }, * __dirname, * 'config.yaml' * ); * const config = await loader(); // typed as AppConfig * ``` */ export default function yamlLoader<T extends Record<string, unknown> = Record<string, unknown>>({ throwOnError, logger, }: { throwOnError?: boolean; logger?: (err: unknown, message: string) => void; }, ...filePathToken: string[]): Loader; export { yamlLoader }; //# sourceMappingURL=yaml-loader.d.ts.map