/************************************************************************* * Copyright 2024 Adobe * All Rights Reserved. * * NOTICE: Adobe permits you to use, modify, and distribute this file in * accordance with the terms of the Adobe license agreement accompanying * it. If you have received this file from a source other than Adobe, * then your use, modification, or distribution of it requires the prior * written permission of Adobe. **************************************************************************/ export declare enum FrameTarget { APP = "app", RAIL = "rail" } export declare const InterFrameTarget: typeof FrameTarget; export declare enum InterFrameResponseStatus { IN_PROGRESS = "in_progress", COMPLETED = "completed", FAILED = "failed", ACKNOWLEDGED = "acknowledged" } export declare enum InterFrameMessageType { INTER_FRAME_ACKNOWLEDGE = "INTER_FRAME_ACKNOWLEDGE", INTER_FRAME_REQUEST = "INTER_FRAME_REQUEST", INTER_FRAME_RESPONSE = "INTER_FRAME_RESPONSE" } export declare enum InterFrameError { NO_HANDLER = "NO_HANDLER", TARGET_NOT_FOUND = "TARGET_NOT_FOUND", UNKNOWN_ERROR = "ERROR" } /** * Inter-frame request message. * Used when one frame (app or rail) needs to send a message to another frame. */ export interface InterFrameRequest { type: InterFrameMessageType.INTER_FRAME_REQUEST; /** * Action is the event name that will be sent and emitted. In the future, we * may restrict this type to an enum of valid actions. */ action: string; /** * Data accompanying the action to be sent and emitted. */ data: any; requestId: string; source: FrameTarget; target: FrameTarget; } /** * Inter-frame response message. * Sent back to the requesting frame with results or errors. */ export interface InterFrameResponse { data?: any; error?: { code: string; details?: any; message: string; }; requestId: string; status: InterFrameResponseStatus; source: FrameTarget; target: FrameTarget; type: InterFrameMessageType.INTER_FRAME_RESPONSE; } /** * Inter-frame acknowledgment message. * Sent immediately upon receiving a request to confirm receipt. */ export interface InterFrameAcknowledge { type: InterFrameMessageType.INTER_FRAME_ACKNOWLEDGE; requestId: string; source: FrameTarget; target: FrameTarget; status: InterFrameResponseStatus.ACKNOWLEDGED; } /** * Union type for all inter-frame messages. * These messages enable communication between the app and rail frames. */ export type InterFrameMessage = InterFrameRequest | InterFrameResponse | InterFrameAcknowledge; /** * Callbacks provided to the send() method for handling responses. */ export interface SendCallbacks { /** * Called when the operation completes successfully. */ onComplete?: (data?: any) => void; /** * Called when the operation fails. */ onError?: (data?: any) => void; /** * Called when the operation progresses. */ onProgress?: (data?: any) => void; } /** * Functions provided to request handlers for sending responses. */ export interface ResponseCallbacks { /** * Send a successful completion response. */ sendCompleted: (data?: any) => void; /** * Send an error response. */ sendError: (error: { code: string; message: string; details?: any; } | any) => void; /** * Send a progress update. */ sendProgress: (data: any) => void; } /** * Payload received by request handlers registered via interFrame.on(). */ export interface RequestPayload { /** * The request data. */ data: any; /** * Callbacks for sending responses back to the requester. */ callbacks: ResponseCallbacks; } /** * APIs for inter-frame communication between app and rail iframes. */ export interface InterFrameApi { /** * Invoke all handlers for the given action. * @param action The action type to invoke. * @param event The RequestPayload to pass to handlers. */ emit(action: string, event?: RequestPayload): void; /** * Remove an event handler for the given action. * @param action The action to unregister handler from. * @param handler Handler function to remove. */ off(action: string, handler: (event?: RequestPayload) => void): void; /** * Register an event handler for the given action. * @param action The action to listen for. * @param handler Function to call in response to the action. */ on(action: string, handler: (event?: RequestPayload) => void): void; /** * Send a message between the app and rail iframes. * Automatically handles routing between app and rail frames. When the target receives * the message via the "on" event listener, it can optionally send responses to the * error, progress, and completion callbacks. * * ***Example:*** * * ```typescript * interFrame.send('CREATE', { * onComplete: (data) => console.log('Created:', data), * onError: (error) => console.error('Failed:', error), * onProgress: (data) => console.log('Progress:', data) * }, {type: 'event', name: 'New Event'}); * ``` * * @param action - The action to perform (CREATE, UPDATE, etc.) * @param callbacks - Optional callbacks for handling responses * @param data - The optional payload data * @param target - The target frame to send the message to. This is determined by the current frame identity if none is provided. */ send: (action: string, callbacks?: SendCallbacks, data?: any, target?: FrameTarget) => void; } declare const interFrame: InterFrameApi; export default interFrame;