guardz-event
Version:
Type-safe event handling with runtime validation using guardz for unsafe data from 3rd parties
35 lines • 1.4 kB
TypeScript
import type { TypeGuardFn } from 'guardz';
import { Status } from '../../domain/event/Status';
import type { ErrorContext } from '../../utils/safe-event-never-throws';
/**
* Configuration for DOM event listeners
*/
export interface DOMEventListenerConfig<T> {
/** Type guard function to validate event data */
guard: TypeGuardFn<T>;
/** Enable tolerance mode (default: false) */
tolerance?: boolean;
/** Allowed origins for security validation */
allowedOrigins?: string[];
/** Allowed sources for security validation */
allowedSources?: string[];
/** Callback for type mismatch errors */
onTypeMismatch?: (errorMessage: string) => void;
/** Callback for security violations */
onSecurityViolation?: (origin: string, message: string) => void;
/** Callback for other errors */
onError?: (error: string, context: ErrorContext) => void;
}
/**
* Safe DOM event listener with callback-based API
* Usage: element.addEventListener('click', safeDOMEventListener(isClickData, { onSuccess: handleClick }));
*/
export declare function safeDOMEventListener<T>(guard: TypeGuardFn<T>, config: DOMEventListenerConfig<T> & {
onSuccess: (data: T) => void;
onError?: (result: {
status: Status.ERROR;
code: number;
message: string;
}) => void;
}): (event: Event) => void;
//# sourceMappingURL=DOMEventListener.d.ts.map