guardz-event
Version:
Type-safe event handling with runtime validation using guardz for unsafe data from 3rd parties
35 lines • 1.46 kB
TypeScript
import type { TypeGuardFn } from 'guardz';
import { Status } from '../../domain/event/Status';
import type { ErrorContext } from '../../utils/safe-event-never-throws';
/**
* Configuration for PostMessage event listeners
*/
export interface PostMessageListenerConfig<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 PostMessage event listener with callback-based API
* Usage: window.addEventListener('message', safePostMessageListener(isUserMessage, { tolerance: true, onTypeMismatch: console.warn }));
*/
export declare function safePostMessageListener<T>(guard: TypeGuardFn<T>, config: PostMessageListenerConfig<T> & {
onSuccess: (data: T) => void;
onError?: (result: {
status: Status.ERROR;
code: number;
message: string;
}) => void;
}): (event: MessageEvent) => void;
//# sourceMappingURL=PostMessageListener.d.ts.map