import type { Builder, Ref } from '../core/index.js'; import type { ClassType } from '../core/index.js'; import { ReflectionKind, Type } from '../reflection/type.js'; import type { SerializerBuildContext } from './context.js'; /** * Type handler function signature. * Handlers receive the type, input ref, builder, and build context, * and return a Ref representing the transformed value. * * The Ctx type parameter allows handlers to be typed for specific build contexts * (e.g., JsonBuildContext for JSON serialization, BsonBuildContext for BSON). * * @example * ```typescript * const stringHandler: TypeHandler = (type, input, b, ctx) => { * return b.ternary( * b.isType(input, 'string'), * input, * b.call(String, input) * ); * }; * ``` */ export type TypeHandler = (type: T, input: Ref, b: Builder, ctx: Ctx) => Ref; /** * Hook function for wrapping type handlers. * Can modify behavior before/after the main handler executes. * * The Ctx type parameter allows hooks to be typed for specific build contexts. * * @example * ```typescript * const validationHook: TypeHook = (type, input, b, ctx, next) => { * const result = next(); * // Add validation after type conversion * runValidators(type, result, b, ctx); * return result; * }; * ``` */ export type TypeHook = (type: Type, input: Ref, b: Builder, ctx: Ctx, next: () => Ref) => Ref; /** * Registry for type handlers, organized by ReflectionKind, class type, and annotations. * * The Ctx type parameter specifies the build context type this registry works with. * This allows @runtyped/bson to create registries typed for BsonBuildContext while * @runtyped/type uses JsonBuildContext. * * Handlers are executed in order: * 1. Pre-hooks (in order added) * 2. Annotation handlers (first matching predicate wins) * 3. Class handlers (for class types with specific classType) * 4. Kind handlers (by ReflectionKind) * 5. Post-hooks (in order added) */ export declare class HandlerRegistry { private static nextId; /** Unique ID that changes when handlers are modified. Used for cache invalidation. */ id: number; readonly direction: 'serialize' | 'deserialize'; /** Pre-formatted cache key for default naming strategy. Lazily computed. */ _fastCacheKey?: string; private kindHandlers; private classHandlers; private annotationHandlers; private preHooks; private postHooks; constructor(direction?: 'serialize' | 'deserialize'); /** Increment ID to invalidate cached functions. */ private invalidateCache; /** * Register a handler for a ReflectionKind. * Multiple handlers can be registered for the same kind. */ register(kind: ReflectionKind, handler: TypeHandler): this; /** * Prepend a handler for a ReflectionKind (runs before existing handlers). */ prepend(kind: ReflectionKind, handler: TypeHandler): this; /** * Append a handler for a ReflectionKind (alias for register). */ append(kind: ReflectionKind, handler: TypeHandler): this; /** * Register a handler for a specific class type (Date, Set, Map, etc.). */ registerClass(classType: ClassType, handler: TypeHandler): this; /** * Register a handler for all binary types (ArrayBuffer, TypedArrays). */ registerBinary(handler: TypeHandler): this; /** * Register a handler based on an annotation/decorator predicate. * First matching predicate wins. */ addDecorator(predicate: (type: Type) => boolean, handler: TypeHandler): this; /** * Add a pre-hook that runs before main handlers. */ addPreHook(hook: TypeHook): this; /** * Add a post-hook that runs after main handlers. */ addPostHook(hook: TypeHook): this; /** * Get handlers for a ReflectionKind. */ getKindHandlers(kind: ReflectionKind): TypeHandler[]; /** * Get handlers for a class type. */ getClassHandlers(classType: ClassType): TypeHandler[]; /** * Check if any handlers exist for a type. */ has(type: Type): boolean; /** * Build the output for a type using registered handlers. * Executes pre-hooks, handlers, and post-hooks in order. */ build(type: Type, input: Ref, b: Builder, ctx: Ctx): Ref; /** * Clear all registered handlers and hooks. */ clear(): void; } /** * Registry for type guards organized by specificality level. * * @deprecated Use HandlerRegistry instead. The Serializer now uses a single * unified HandlerRegistry for type guards with behavior controlled by * BuildState flags (collectErrors, rejectUnknownKeys). * * Specificality levels determine when guards activate: * - Negative values: Loose mode only (string coercion) * - 1: Exact/strict mode (typeof checks) * - > 1: Fallback modes (late resolution) * * @example * ```typescript * const guards = new TypeGuardRegistry(); * * // Exact match (specificality 1) * guards.register(1, ReflectionKind.number, (type, input, b) => * b.isType(input, 'number') * ); * * // Loose from string (specificality -0.5) * guards.register(-0.5, ReflectionKind.number, (type, input, b) => * b.and(b.isType(input, 'string'), b.call(isNumeric, input)) * ); * ``` */ export declare class TypeGuardRegistry { private levels; /** * Get or create a HandlerRegistry for a specificality level. */ getRegistry(specificality: number): HandlerRegistry; /** * Register a guard for a ReflectionKind at a specificality level. */ register(specificality: number, kind: ReflectionKind, handler: TypeHandler): this; /** * Register a guard for a class type at a specificality level. */ registerClass(specificality: number, classType: ClassType, handler: TypeHandler): this; /** * Register a guard for binary types at a specificality level. */ registerBinary(specificality: number, handler: TypeHandler): this; /** * Register a guard based on an annotation predicate. */ addDecorator(specificality: number, predicate: (type: Type) => boolean, handler: TypeHandler): this; /** * Get all specificality levels sorted ascending (lowest first). * Lower specificality = more loose, tried first. */ getSortedLevels(): Array<[number, HandlerRegistry]>; /** * Get levels within a specificality range, sorted ascending. */ getLevelsInRange(min: number, max: number): Array<[number, HandlerRegistry]>; /** * Check if any guards exist for a type at any specificality level. */ has(type: Type): boolean; /** * Clear all guards at all specificality levels. */ clear(): void; } export declare type __ΩTypeHandler = any[]; export declare type __ΩTypeHook = any[];