UNPKG

tma-helper

Version:

Telegram Mini Apps helper library for Vue 3 projects

197 lines (196 loc) 5.49 kB
var s = Object.defineProperty; var o = (i, e, t) => e in i ? s(i, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[e] = t; var p = (i, e, t) => o(i, typeof e != "symbol" ? e + "" : e, t); class a { constructor() { p(this, "webApp"); var t; if (typeof window > "u") { console.error("[TMA Helper] Window object not found (non-browser environment)"); return; } const e = (t = window.Telegram) == null ? void 0 : t.WebApp; if (!e) { console.error( `[TMA Helper] Telegram WebApp SDK not detected. Please include: <script src="https://telegram.org/js/telegram-web-app.js"><\/script>` ); return; } this.webApp = e, console.log("[TMA Helper] ✅ Telegram SDK initialized."); } /** Telegram WebApp initData (string, signed) */ get initData() { var e; return (e = this.webApp) == null ? void 0 : e.initData; } /** Unsafe initData object (not verified) */ get initDataUnsafe() { var e; return (e = this.webApp) == null ? void 0 : e.initDataUnsafe; } /** Telegram WebApp instance */ get instance() { return this.webApp; } /** Checks if Telegram WebApp is available */ get isAvailable() { return !!this.webApp; } get safeAreaInset() { var e; return (e = this.webApp) == null ? void 0 : e.safeAreaInset; } /** Content safe area insets */ get contentSafeAreaInset() { var e; return (e = this.webApp) == null ? void 0 : e.contentSafeAreaInset; } /** Close the WebApp */ close() { var e; (e = this.webApp) == null || e.close(); } /** Hide the WebApp */ hide() { var e; (e = this.webApp) == null || e.hide(); } /** Expand the WebApp to full height */ expand() { var e; (e = this.webApp) == null || e.expand(); } /** Disable vertical swipes for expanding/closing */ disableVerticalSwipes() { var e; (e = this.webApp) == null || e.disableVerticalSwipes(); } /** Enable vertical swipes for expanding/closing */ enableVerticalSwipes() { var e; (e = this.webApp) == null || e.enableVerticalSwipes(); } /** Enable confirmation dialog on WebApp close */ enableClosingConfirmation() { var e; (e = this.webApp) == null || e.enableClosingConfirmation(); } /** Disable confirmation dialog on WebApp close */ disableClosingConfirmation() { var e; (e = this.webApp) == null || e.disableClosingConfirmation(); } /** Lock orientation */ lockOrientation() { var e; (e = this.webApp) == null || e.lockOrientation(); } /** Lock orientation */ unlockOrientation() { var e; (e = this.webApp) == null || e.unlockOrientation(); } /** Hide the back button */ hideBackButton() { var e, t; (t = (e = this.webApp) == null ? void 0 : e.BackButton) == null || t.hide(); } /** Show the back button */ showBackButton() { var e, t; (t = (e = this.webApp) == null ? void 0 : e.BackButton) == null || t.show(); } /** * Set a callback function for back button click * @param callback - function to call on back button click */ onBackButtonClick(e) { var t, n; (n = (t = this.webApp) == null ? void 0 : t.BackButton) == null || n.onClick(e); } /** * Trigger haptic feedback * @param style - impact style ("light", "medium", "heavy") */ hapticFeedback(e) { var t, n; (n = (t = this.webApp) == null ? void 0 : t.HapticFeedback) == null || n.impactOccurred(e); } /** * Open a link in Telegram * @param url - Telegram link (t.me/...) */ openTelegramLink(e) { var t; (t = this.webApp) == null || t.openTelegramLink(e); } /** * Open a link. * Uses openTelegramLink if the URL is a Telegram link (t.me/...), otherwise openLink. * @param url - URL to open */ openLink(e) { if (!this.webApp || !e) return; /^https?:\/\/t\.me|^t\.me/i.test(e) ? this.webApp.openTelegramLink(e) : this.webApp.openLink(e); } /** Request fullscreen mode */ requestFullscreen() { var e; (e = this.webApp) == null || e.requestFullscreen(); } /** Exit fullscreen mode */ exitFullscreen() { var e; (e = this.webApp) == null || e.exitFullscreen(); } /** * Open an invoice * @param url - invoice URL * @param callback - callback with status */ openInvoice(e, t) { var n; (n = this.webApp) == null || n.openInvoice(e, (r) => { t && t(r); }); } /** * Set the header color * @param color - color in #RRGGBB format */ setHeaderColor(e) { var t; (t = this.webApp) == null || t.setHeaderColor(e); } isVersionAtLeast(e) { var t; return (t = this.webApp) == null ? void 0 : t.isVersionAtLeast(e); } } const w = new a(); class l { constructor() { p(this, "webApp"); this.checkEnvironment(); } checkEnvironment() { if (typeof window > "u") { console.error("[TMA Helper] Window object not found. Are you in a browser?"); return; } window.Telegram && window.Telegram.WebApp ? (this.webApp = window.Telegram.WebApp, console.log("[TMA Helper] ✅ Telegram WebApp detected and ready.")) : console.error( `[TMA Helper] ❌ Telegram WebApp not detected. Make sure the Telegram SDK script is loaded: Add <script src="https://telegram.org/js/telegram-web-app.js"><\/script> before your app runs.` ); } getWebApp() { return this.webApp; } } const b = new l(); export { l as TMAHelper, w as Telegram, b as tmaHelper };