UNPKG

dome-embedded-app-sdk

Version:

Use this SDK to build plugins for Dome. There are two plugins supported:

256 lines (255 loc) 9.03 kB
export declare enum ViewerMessageType { CONNECTION_SUCCESS = "CONNECTION_SUCCESS", INIT = "INIT",// Indicates the app is initialized REQUEST_SAVE = "REQUEST_SAVE",// Request to save data in the parent REQUEST_CLOSE = "REQUEST_CLOSE",// Request to save data in the parent REQUEST_INITIAL_DATA = "REQUEST_INITIAL_DATA",// Request to load initial data from the parent SET_DIRTY = "SET_DIRTY",// Indicates the app has unsaved changes SEND_CLOSE = "SEND_CLOSE",// Signals the app is ready to close SEND_EXCEPTION = "SEND_EXCEPTION" } declare enum ClientMessageType { CONNECT = "CONNECT", REQUEST_CLOSE = "REQUEST_CLOSE",// Requests the app to close REQUEST_SAVE = "REQUEST_SAVE",// Requests the app for data to be saved SAVE_ERROR = "SAVE_ERROR",// Sends status of the save event SAVE_SUCCESS = "SAVE_SUCCESS",// Sends status of the save event DATA_CHANGE = "DATA_CHANGE", FILE_DATA = "FILE_DATA", WRITE_FILE_ACK = "WRITE_FILE_ACK", READ_FILE_ACK = "READ_FILE_ACK", INIT_ACK = "INIT_ACK", ERROR = "ERROR", REFRESH = "REFRESH" } interface SaveStatusData { status: 'success' | 'error'; message: string; } export interface ViewerEventHandler { onInitialData: (data: { doc: Document; ui: UiProps; isNewFile: boolean; perms: any; config?: Record<string, any>; }) => void; onDataChange?: (data: { doc: Document; perms: any; userConsent: "override" | null; }) => void; onCloseRequest: () => void; onSaveRequest: () => void; } export interface Document { data: any; name: string; type: string; } export interface UiProps { theme: string; } /** * DomeEmbeddedAppSdk: * Base SDK class providing methods to send messages to the parent application. */ declare class DomeEmbeddedAppSdk { private readonly targetOrigin; protected isAppReady: boolean; protected port2: MessagePort | null; private platform; constructor(); /** * Detects the platform (iOS, Android, or Web) and saves it. */ private detectPlatform; /** * Method to send messages to the parent application. * Ensures the parent window exists and sends a structured message with type and data. * @param type - The type of message being sent * @param data - (Optional) payload data for the message */ protected sendMessage(type: ViewerMessageType | CardMessageType, data?: any): void; /** * Notifies the parent application that the app is ready, if it hasn’t already. */ protected sendAppInit(): void; /** * Safely invokes a function from the handler object if it exists. * and logs a warning if the handler is not provided for the given message type. * * @param eventName - Name of the event method to be invoked from the handler. * @param handlerObj - The handler object that contains the message handling methods. * @param data - (Optional) The data to be passed to the handler function if invoked. */ protected safeInvoke<T extends ViewerEventHandler | CardEventHandler>(eventName: keyof T, handlerObj: T, data?: any): void; protected setupParentConnection(): Promise<void>; private notifyConnectionSuccess; private handlePortMessage; protected handleMessage(type: string, data: any): void; } /** * ViewerSdk: * A subclass of DomeEmbeddedAppSdk specifically for document viewer applications. * It includes additional methods and properties to manage app interactions. */ export declare class ViewerSdk extends DomeEmbeddedAppSdk { private static instance; private static initialized; private handler; private pendingRequests; private pendingInitAck; private constructor(); /** * Static initialization method to get or create the singleton instance of ViewerSdk. * Allows setting the handler during initialization. * @param handler - (Optional) Custom handler for different message types * @returns The singleton ViewerSdk instance */ static init(handler?: ViewerEventHandler): ViewerSdk; /** * Method to set or update the handler object. * @param handler - Custom handler for different message types */ setHandler(handler: ViewerEventHandler): void; /** * Checks if the given permissions string allows reading. * @param perms - The permissions string. * @returns - True if the permission string includes read access. */ canRead(perms?: string): boolean; /** * Checks if the given permissions string allows writing. * @param perms - The permissions string. * @returns - True if the permission string includes write access. */ canWrite(perms?: string): boolean; initializeViewerSdk(): void; /** * Sends a request to the parent application to retrieve initial data. */ requestInitialData(): void; /** * Sends a request to the parent application to save data. * @param doc - payload data to be saved * @param isDataDirty - Boolean indicating indicating modified data */ requestSave(doc: Document, isDataDirty: boolean): Promise<SaveStatusData>; private handleOnSave; /** * Sets the viewer's "dirty" state, indicating modified data. * @param isDirty - Boolean indicating whether the viewer has modified data */ setDirty(isDirty: boolean): void; /** * Sends a close request to the parent, with information on whether the data is dirty. * @param doc - Latest document data * @param isDataDirty - Boolean indicating indicating modified data */ sendClose(doc: Document, isDataDirty: boolean): void; /** * Sends an exception to parent. * @param error - An error object with name and message or an error string */ sendException(error: { name?: string; message: string; } | string): void; protected handleMessage(type: ClientMessageType, data: any): void; } export declare enum CardMessageType { APP_READY = "APP_READY",// Indicates the app is ready to be interacted with INIT = "INIT",// Event to send the init key to the viewer READ_FILE = "READ_FILE", WRITE_FILE = "WRITE_FILE", FILE_DIRTY = "FILE_DIRTY" } export interface CardEventHandler { onInit: (data: any) => void; onError: (data: { error_code: string | number; message: string; data: any; }) => void; onFileChange: (data: { iuid: string; data: { data: { type: string; value: any; }; link: { url: string; }; meta: any; perms: any; from_cache: boolean; }; }) => void; onRefreshRequest: (data: any) => void; } /** * Use CardSdk to create webapp cards */ export declare class CardSdk extends DomeEmbeddedAppSdk { private static instance; private handler; private cryptoA01; dataStore: any; private accessToken; cardFS: { readFile: (card_iuid: string, name: string) => Promise<any>; writeFile: (card_iuid: string, name: string, data: any) => Promise<void>; }; private fileReadResolvers; private fileWriteResolvers; private pendingAcks; private constructor(); /** * Static initialization method to get or create the singleton instance of CardSdk. * @param secret - The card developer secret key * @param handler - (Optional) Handler for different events emitted by the SDK * @returns The singleton CardSdk instance */ static init(secret: string, handler?: CardEventHandler, options?: { devMode?: boolean; }): Promise<CardSdk>; /** * Method to set or update the handler object. * @param handler - Custom handler for different message types */ setHandler(handler: CardEventHandler): void; private initializeCardSdk; private sendInit; private sendMessageWithAck; protected handleMessage: (type: ClientMessageType, data: any) => void; private _readFile; private _writeFile; /** * Converts a JavaScript object to a JSON file. */ private convertToJsonFile; /** * Converts data to a file. */ private convertToFile; /** * Get document associated with the current card */ getDocumentAttachedToCard(card_iuid: string, document_name?: string, ts_m?: number): Promise<any>; /** * Save document attached to current card */ postDocumentAttachedToCard(card_iuid: string, document: any): Promise<any>; getAllDocumentsAttachedToCard(card_iuid: string): Promise<Response>; postUploadFile(name: string, fileObj: any): Promise<any>; shareDocumentWithCard(card_iuid: string, documents: any): Promise<any>; private sendEventError; setFileDirty(data: { iuid: string; ts_m: number; }): void; getUsername(userObj: any): string; } export {};