import { OpenYoloError , OpenYoloExposedErrorData } from './errors' ; import { DataValidator , isNonEmptyString , isValidError } from './validators' ; /**
 * Top-level message envelope types defined for OpenYOLO:
 *
 * - ack: sent by the recipientof a message to its original sender to
 *     acknowledge it has been received.
 * - verifyPing: sent by the provider frame to every ancestor frame for origin
 *     verification.
 * - verifyAck: sent by ancestor frames to the provider farme in response to a
 *     ping.
 * - readyForConnect: sent by the provider frame to the parent frame to
 *     indicate it is ready to accept requests.
 * - channelError: sent when establishing the message channel fails.
 */ export const enum PostMessageType { ack = 'ack' , verifyPing = 'verifyPing' , verifyAck = 'verifyAck' , readyForConnect = 'readyForConnect' , channelConnect = 'channelConnect' , channelReady = 'channelReady' , channelError = 'channelError' } /**
 * A map of message types to their data payload types.
 */ export type PostMessageDataTypes = { 'ack' : string ; 'readyForConnect' : string ; 'verifyPing' : string ; 'verifyAck' : string ; 'channelReady' : string ; 'channelConnect' : string ; 'channelError' : OpenYoloExposedErrorData ; } ; export type PostMessageData < T extends PostMessageType > = PostMessageDataTypes [ T ] ; export interface PostMessage < T extends PostMessageType > { type : T ; data : PostMessageDataTypes [ T ] ; } export const POST_MESSAGE_DATA_VALIDATORS = { 'ack' : isNonEmptyString , 'verifyPing' : isNonEmptyString , 'verifyAck' : isNonEmptyString , 'readyForConnect' : isNonEmptyString , 'channelConnect' : isNonEmptyString , 'channelReady' : isNonEmptyString , 'channelError' : isValidError } ; export function postMessage < T extends PostMessageType > ( type , data ) { return { type , data } ; } export function ackMessage ( id ) { return postMessage ( PostMessageType . ack , id ) ; } export function verifyPingMessage ( nonce ) { return postMessage ( PostMessageType . verifyPing , nonce ) ; } export function verifyAckMessage ( nonce ) { return postMessage ( PostMessageType . verifyAck , nonce ) ; } export function channelReadyMessage ( nonce ) { return postMessage ( PostMessageType . channelReady , nonce ) ; } export function channelErrorMessage ( error ) { return postMessage ( PostMessageType . channelError , error . toData ( ) ) ; } export function readyForConnectMessage ( nonce ) { return postMessage ( PostMessageType . readyForConnect , nonce ) ; } export function channelConnectMessage ( nonce ) { return postMessage ( PostMessageType . channelConnect , nonce ) ; }