import { BaseSDK } from './sdk'; import { Router, StandardResponse, ResponseType, NavigateToNativePageType } from '@jolibox/types'; const NavigateToNativePageTypeTypes = [ 'openHistory', 'openDiscover', 'openGame', 'openDrama', 'openFeed', 'openTopup', 'openRewards' ]; /** * @private * Provides routing and navigation functionalities within the Jolibox SDK. * This includes opening schemas, web pages, and navigating to native application pages. * @implements {Router} */ export class RouterSDK extends BaseSDK implements Router { /** * @public * Opens a given schema URL. * This is often used for deep linking or triggering specific actions within the host application or other apps. * @param schema - The schema URL string to open (e.g., 'yourapp://action?param=value'). * @returns A promise that resolves with a StandardResponse, or an error object if the capability is not available. */ async openSchema( schema: string ): Promise | { code: ResponseType; message: string }> { const errMsg = this.canIUseIfThrow('router.openSchema'); if (errMsg) { return errMsg; } return await this.commands.executeCommand('RouterSDK.openSchema', schema); } /** * @public * Opens a web page in a new view (e.g., an in-app browser or a new tab). * @param url - The URL of the web page to open. * @returns A promise that resolves with a StandardResponse containing the webviewId, or an error object if the capability is not available. */ async openPage( url: string ): Promise | { code: ResponseType; message: string }> { const errMsg = this.canIUseIfThrow('router.openPage'); if (errMsg) { return errMsg; } return await this.commands.executeCommand('RouterSDK.openPage', url); } /** * @public * Closes a previously opened web page/view, identified by its webviewId. * @param webviewId - The ID of the webview to close. * @returns A promise that resolves with a StandardResponse, or an error object if the capability is not available. */ async closePage( webviewId: number ): Promise | { code: ResponseType; message: string }> { const errMsg = this.canIUseIfThrow('router.closePage'); if (errMsg) { return errMsg; } return await this.commands.executeCommand('RouterSDK.closePage', webviewId); } /** * @public * Intercepts the system's back button or exit gesture. * @param intercept - A boolean indicating whether to intercept the exit (true) or allow default behavior (false). * @returns A promise that resolves with a StandardResponse, or an error object if the capability is not available. */ async interceptSystemExit( intercept: boolean ): Promise | { code: ResponseType; message: string }> { const errMsg = this.canIUseIfThrow('router.interceptSystemExit'); if (errMsg) { return errMsg; } return await this.commands.executeCommand('RouterSDK.interceptSystemExit', intercept); } /** * @public * Navigates to a specific native page within the host application. * @param path - The identifier for the native page to navigate to. Currently, only 'openHistory' is explicitly supported by this client-side check. * @param params - A record of parameters to pass to the native page. * @returns A promise that resolves with a StandardResponse, or an error object if the capability is not available or the path is invalid. */ async navigateToNativePage( path: string, params: Record ): Promise | { code: ResponseType; message: string }> { const errMsg = this.canIUseIfThrow('router.navigateToNativePage'); if (errMsg) { return errMsg; } if (!NavigateToNativePageTypeTypes.includes(path)) { return { code: 'FAILURE' as ResponseType, message: 'invalid path' }; } await this.commands.executeCommand( 'RouterSDK.navigateToNativePage', path as NavigateToNativePageType, params ); return { code: 'SUCCESS' as ResponseType, message: `navigate to ${path} success` }; } }