diff --git a/apps/docs/packages/typescript/tma-js-sdk-solid.md b/apps/docs/packages/typescript/tma-js-sdk-solid.md index d3671c8b2..8f7cb2325 100644 --- a/apps/docs/packages/typescript/tma-js-sdk-solid.md +++ b/apps/docs/packages/typescript/tma-js-sdk-solid.md @@ -163,7 +163,12 @@ import { Match, type ParentProps, } from 'solid-js'; -import { SDKProvider, useSDKContext } from '@tma.js/sdk-solid'; +import { + SDKProvider, + useSDKContext, + useBackButton, + useMiniApp, +} from '@tma.js/sdk-solid'; /** * Part of the application which doesn't know anything about SDK diff --git a/packages/sdk/src/init/init.ts b/packages/sdk/src/init/init.ts index faea7a6c8..5668ff5cc 100644 --- a/packages/sdk/src/init/init.ts +++ b/packages/sdk/src/init/init.ts @@ -30,96 +30,103 @@ export function init(options: O): ComputedInitResult { acceptCustomStyles = false, } = options; - // Retrieve launch data. - const { - launchParams: { - initData, - initDataRaw, - version, - platform, - themeParams, - botInline = false, - }, - isPageReload, - } = retrieveLaunchData(); + try { + // Retrieve launch data. + const { + launchParams: { + initData, + initDataRaw, + version, + platform, + themeParams, + botInline = false, + }, + isPageReload, + } = retrieveLaunchData(); - const createRequestId = createRequestIdGenerator(); - const postEvent = createPostEvent(version); + const createRequestId = createRequestIdGenerator(); + const postEvent = createPostEvent(version); - // In Telegram web version we should listen to special event sent from the Telegram application - // to know, when we should reload the Mini App. - if (isIframe()) { - if (acceptCustomStyles) { - catchCustomStyles(); - } + // In Telegram web version we should listen to special event sent from the Telegram application + // to know, when we should reload the Mini App. + if (isIframe()) { + if (acceptCustomStyles) { + catchCustomStyles(); + } - // Notify Telegram, iframe is ready. This will result in sending style tag html from native - // application which is used in catchCustomStyles function. We should call this method also - // to start receiving "reload_iframe" events from the Telegram application. - postEvent('iframe_ready', { reload_supported: true }); - on('reload_iframe', () => window.location.reload()); - } + // Notify Telegram, iframe is ready. This will result in sending style tag html from native + // application which is used in catchCustomStyles function. We should call this method also + // to start receiving "reload_iframe" events from the Telegram application. + postEvent('iframe_ready', { reload_supported: true }); + on('reload_iframe', () => window.location.reload()); + } - const result: Omit = { - backButton: createBackButton(isPageReload, version, postEvent), - closingBehavior: createClosingBehavior(isPageReload, postEvent), - cloudStorage: new CloudStorage(version, createRequestId, postEvent), - createRequestId, - hapticFeedback: new HapticFeedback(version, postEvent), - invoice: new Invoice(version, postEvent), - mainButton: createMainButton( - isPageReload, - themeParams.buttonColor || '#000000', - themeParams.buttonTextColor || '#ffffff', + const result: Omit = { + backButton: createBackButton(isPageReload, version, postEvent), + closingBehavior: createClosingBehavior(isPageReload, postEvent), + cloudStorage: new CloudStorage(version, createRequestId, postEvent), + createRequestId, + hapticFeedback: new HapticFeedback(version, postEvent), + invoice: new Invoice(version, postEvent), + mainButton: createMainButton( + isPageReload, + themeParams.buttonColor || '#000000', + themeParams.buttonTextColor || '#ffffff', + postEvent, + ), + miniApp: createMiniApp( + isPageReload, + themeParams.backgroundColor || '#ffffff', + version, + botInline, + postEvent, + ), + popup: new Popup(version, postEvent), postEvent, - ), - miniApp: createMiniApp( - isPageReload, - themeParams.backgroundColor || '#ffffff', - version, - botInline, - postEvent, - ), - popup: new Popup(version, postEvent), - postEvent, - qrScanner: new QRScanner(version, postEvent), - themeParams: createThemeParams(themeParams), - utils: new Utils(version, createRequestId, postEvent), - ...(initData - // Init data could be missing in case, application was launched via InlineKeyboardButton. - ? { - initData: new InitData(initData), - initDataRaw, - } - : {}), - }; + qrScanner: new QRScanner(version, postEvent), + themeParams: createThemeParams(themeParams), + utils: new Utils(version, createRequestId, postEvent), + ...(initData + // Init data could be missing in case, application was launched via InlineKeyboardButton. + ? { + initData: new InitData(initData), + initDataRaw, + } + : {}), + }; - const viewport = async - ? createViewportAsync(isPageReload, platform, postEvent) - : createViewportSync(isPageReload, platform, postEvent); + const viewport = async + ? createViewportAsync(isPageReload, platform, postEvent) + : createViewportSync(isPageReload, platform, postEvent); - if (viewport instanceof Promise) { - return viewport.then((vp) => { - processCSSVars( - cssVars, - result.miniApp, - result.themeParams, - vp, - ); + if (viewport instanceof Promise) { + return viewport.then((vp) => { + processCSSVars( + cssVars, + result.miniApp, + result.themeParams, + vp, + ); - return { - ...result, - viewport: vp, - }; - }) as ComputedInitResult; - } + return { + ...result, + viewport: vp, + }; + }) as ComputedInitResult; + } - processCSSVars( - cssVars, - result.miniApp, - result.themeParams, - viewport, - ); + processCSSVars( + cssVars, + result.miniApp, + result.themeParams, + viewport, + ); - return { ...result, viewport } as ComputedInitResult; + return { ...result, viewport } as ComputedInitResult; + } catch (e) { + if (async) { + return Promise.reject(e) as unknown as ComputedInitResult; + } + throw e; + } }