@daiso-tech/core
Version:
The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.
50 lines (49 loc) • 1.61 kB
TypeScript
/**
* @module ConfigAccessor
*/
import type { Get, Paths } from "type-fest";
import { type OneOrArray, type UndefinedToNull } from "../../utilities/_module.js";
/**
* @group Contracts
*/
export type FieldConfigValue = string | number | boolean;
/**
* @group Contracts
*/
export type BaseConfig = Partial<Record<string, OneOrArray<FieldConfigValue | Partial<Record<string, FieldConfigValue>>>>>;
/**
* @group Contracts
*/
export declare function defineConfig<TConfig extends BaseConfig>(config: TConfig): TConfig;
/**
* @group contracts
*/
export type RestrictedPaths<TConfig> = Paths<TConfig, {
maxRecursionDepth: 1;
}>;
/**
* @group contracts
*/
export type PathValue<TConfig, TPath extends string> = UndefinedToNull<Get<TConfig, TPath>>;
/**
* `IConfigAccessor` provides type-safe access to configuration objects.
*
* @group Contracts
*/
export type IConfigAccessor<TConfig extends BaseConfig = BaseConfig> = {
/**
* Get a value from the config, or null if missing/undefined.
*
* @param path The config value path to retrieve.
* @returns The value or null if not present.
*/
get<TPath extends RestrictedPaths<TConfig>>(path: TPath): PathValue<TConfig, TPath>;
/**
* Get a value from the config, or return default value.
*
* @param path The config value path to retrieve.
* @param defaultValue The default value when not found.
* @returns The value or default value.
*/
getOr<TPath extends RestrictedPaths<TConfig>>(path: TPath, defaultValue: NonNullable<Get<TConfig, TPath>>): NonNullable<PathValue<TConfig, TPath>>;
};