import { type GetterOrValue } from '../getter/getter'; import { type Maybe, type MaybeSo } from '../value/maybe.type'; import { type ApplyMapFunctionWithOptions, type MapFunction } from '../value/map'; import { type MergeReplace, type ReplaceType } from '../type'; import { type XOR } from 'ts-essentials'; /** * Type used to declare a sister-type to the generic object. */ export type MappedModelData = MergeReplace; export type TypedMappedModelData = ReplaceType; export type ModelMapFunction = ApplyMapFunctionWithOptions, O, ModelConversionOptions>; export type ModelMapFromFunction = ModelMapFunction; export type ModelMapToFunction = ModelMapFunction; export interface ModelMapFunctions { from: ModelMapFromFunction; to: ModelMapToFunction; } /** * Creates bidirectional map functions (`from` and `to`) from a set of {@link ModelFieldConversions}. * * The `to` function converts from the model (V) to data (D), while `from` converts back from data (D) to model (V). * * @param fields - Field conversion definitions for each key in the model * @returns Object with `from` and `to` mapping functions */ export declare function makeModelMapFunctions(fields: ModelFieldConversions): ModelMapFunctions; export type ModelConversionFieldTuple = [keyof I, ModelFieldMapFunction]; export type ModelConversionFieldValuesConfig = ModelConversionFieldTuple[]; export interface ModelConversionOptions { /** * Fields to include. */ fields?: (keyof I)[]; /** * Whether or not to only convert fields that are defined. Fields with a null value are still converted. */ definedOnly?: boolean; } /** * A model conversion function. Performs a conversion on all non-null values. */ export type ModelConversionFieldValuesFunction = ApplyMapFunctionWithOptions, O, ModelConversionOptions>; /** * Creates a conversion function that maps fields from an input object to an output object using per-field conversion functions. * * Supports optional filtering by field names and skipping undefined values via {@link ModelConversionOptions}. * * @param fields - Array of `[key, convertFn]` tuples defining how each field is converted * @returns A function that converts an input object to an output object */ export declare function makeModelConversionFieldValuesFunction(fields: ModelConversionFieldValuesConfig): ModelConversionFieldValuesFunction; /** * An object map containing a ModelFieldMapFunctions entry for each key (required and optional) from the generic object. */ export type ModelFieldConversions = Required<{ [K in keyof V]: ModelFieldMapFunctions[K]>; }>; /** * An object map containing a ModelFieldMapFunctionsConfig for each key (required and optional) from the generic object. */ export type ModelFieldConversionsConfig = Required<{ [K in keyof V]: ModelFieldMapFunctionsConfig[K]>; }>; /** * Converts a {@link ModelFieldConversionsConfig} (with raw config per field) into resolved {@link ModelFieldConversions} (with compiled map functions per field). * * @param config - Configuration object with a conversion config for each model field * @returns Resolved field conversions with compiled `from` and `to` functions */ export declare function modelFieldConversions(config: ModelFieldConversionsConfig): ModelFieldConversions; export type ModelFieldMapFunctions = { readonly from: ModelFieldMapFromFunction; readonly to: ModelFieldMapToFunction; }; export type ModelFieldMapFunctionsConfig = { readonly from: ModelFieldMapFromConfig; readonly to: ModelFieldMapToConfig; }; export type ModelFieldMapFunctionsWithDefaultsConfig = { readonly from: ModelFieldMapFromWithDefaultConfig; readonly to: ModelFieldMapToWithDefaultConfig; }; /** * Compiles a {@link ModelFieldMapFunctionsConfig} into resolved {@link ModelFieldMapFunctions} with `from` and `to` mapping functions. * * @param config - Configuration with `from` and `to` field map configs * @returns Compiled field map functions */ export declare function modelFieldMapFunctions(config: ModelFieldMapFunctionsConfig): ModelFieldMapFunctions; /** * ModelFieldMapFunction configuration that can convert a MaybeValue to the target value. */ export type ModelFieldMapMaybeTooConfig = { convertMaybe: ModelFieldMapConvertMaybeFunction; }; export type ModelFieldMapMaybeWithDefaultValueConfig = { defaultInput: GetterOrValue; convert: ModelFieldMapConvertFunction; }; export type ModelFieldMapMaybeWithDefaultDataConfig = { convert: ModelFieldMapConvertFunction; default: GetterOrValue; }; export type ModelFieldMapConvertMaybeFunction = MapFunction, O>; export type ModelFieldMapConvertFunction = MapFunction, O>; /** * ModelFieldMapFunction configuration that handles the MaybeNot case with undefined. */ export type ModelFieldMapMaybeWithDefaultConfig = ModelFieldMapMaybeWithDefaultValueConfig | ModelFieldMapMaybeWithDefaultDataConfig; /** * Configuration is either a ModelFieldMapMaybeTooConfig or a ModelFieldMapMaybeWithDefaultConfig */ export type ModelFieldMapConfig = XOR, ModelFieldMapMaybeWithDefaultConfig>; export type ModelFieldMapFromConfig = ModelFieldMapConfig; export type ModelFieldMapToConfig = ModelFieldMapConfig; export type ModelFieldMapFromWithDefaultConfig = ModelFieldMapMaybeWithDefaultConfig; export type ModelFieldMapToWithDefaultConfig = ModelFieldMapMaybeWithDefaultConfig; export type ModelFieldMapFunction = MapFunction, O>; export type ModelFieldMapFromFunction = ModelFieldMapFunction; export type ModelFieldMapToFunction = ModelFieldMapFunction; /** * Creates a {@link ModelFieldMapFunction} from a config that handles Maybe input values. * * The function handles three cases: * - Non-null input: uses `convert` to transform the value * - Null/undefined input with `convertMaybe`: delegates to that function * - Null/undefined input with `default` or `defaultInput`: uses the appropriate fallback * * @param config - Configuration specifying how to convert values and handle null/undefined * @returns A function that maps Maybe input values to output values */ export declare function modelFieldMapFunction(config: ModelFieldMapConfig): ModelFieldMapFunction; export type ModelFieldConversionsConfigRef = { readonly fields: ModelFieldConversionsConfig; }; export type ModelFieldConversionsRef = { readonly fieldConversions: ModelFieldConversions; }; export type ToModelFieldConversionsInput = ModelFieldConversionsConfigRef | ModelFieldConversionsRef; /** * Resolves a {@link ToModelFieldConversionsInput} to a {@link ModelFieldConversions} value. * * Accepts either a pre-built `fieldConversions` reference or a `fields` config that will be compiled. * * @param input - Either a config ref or a pre-built conversions ref * @returns Resolved field conversions */ export declare function toModelFieldConversions(input: ToModelFieldConversionsInput): Required<{ [K in keyof T]: ModelFieldMapFunctions[K]>; }>; export type ModelMapFunctionsRef = { readonly mapFunctions: ModelMapFunctions; }; export type ToModelMapFunctionsInput = ToModelFieldConversionsInput | ModelMapFunctionsRef; /** * Resolves a {@link ToModelMapFunctionsInput} to {@link ModelMapFunctions}. * * Accepts a pre-built `mapFunctions` reference, a `fieldConversions` ref, or a `fields` config. * * @param input - Input that can be resolved to model map functions * @returns Bidirectional model map functions */ export declare function toModelMapFunctions(input: ToModelMapFunctionsInput): ModelMapFunctions;