guardz-event
Version:
Type-safe event handling with runtime validation using guardz for unsafe data from 3rd parties
35 lines • 1.43 kB
TypeScript
import type { TypeGuardFn } from 'guardz';
import { Status } from '../../domain/event/Status';
import type { ErrorContext } from '../../utils/safe-event-never-throws';
/**
* Configuration for CustomEvent listeners
*/
export interface CustomEventListenerConfig<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 CustomEvent listener with callback-based API
* Usage: element.addEventListener('custom-event', safeCustomEventListener(isCustomData, { onSuccess: handleCustomData }));
*/
export declare function safeCustomEventListener<T>(guard: TypeGuardFn<T>, config: CustomEventListenerConfig<T> & {
onSuccess: (data: T) => void;
onError?: (result: {
status: Status.ERROR;
code: number;
message: string;
}) => void;
}): (event: CustomEvent) => void;
//# sourceMappingURL=CustomEventListener.d.ts.map