@tma.js/sdk 0.13.3 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (426) hide show
  1. package/dist/dts/{components/BackButton → back-button}/BackButton.d.ts +12 -9
  2. package/dist/dts/back-button/index.d.ts +2 -0
  3. package/dist/dts/{components/BackButton → back-button}/types.d.ts +3 -2
  4. package/dist/dts/bridge/env/hasExternalNotify.d.ts +12 -0
  5. package/dist/dts/bridge/env/hasWebviewProxy.d.ts +12 -0
  6. package/dist/dts/bridge/env/index.d.ts +3 -0
  7. package/dist/dts/bridge/env/isIframe.d.ts +5 -0
  8. package/dist/dts/bridge/errors/MethodUnsupportedError.d.ts +8 -0
  9. package/dist/dts/bridge/errors/ParameterUnsupportedError.d.ts +8 -0
  10. package/dist/dts/bridge/errors/index.d.ts +2 -0
  11. package/dist/dts/bridge/events/createEmitter.d.ts +6 -0
  12. package/dist/dts/bridge/events/events.d.ts +130 -0
  13. package/dist/dts/bridge/events/index.d.ts +9 -0
  14. package/dist/dts/bridge/events/off.d.ts +7 -0
  15. package/dist/dts/bridge/events/on.d.ts +10 -0
  16. package/dist/dts/bridge/events/onTelegramEvent.d.ts +7 -0
  17. package/dist/dts/bridge/events/once.d.ts +9 -0
  18. package/dist/dts/bridge/events/parsers/clipboardTextReceived.d.ts +13 -0
  19. package/dist/dts/bridge/events/parsers/customMethodInvoked.d.ts +16 -0
  20. package/dist/dts/bridge/events/parsers/index.d.ts +9 -0
  21. package/dist/dts/bridge/events/parsers/invoiceClosed.d.ts +12 -0
  22. package/dist/dts/bridge/events/parsers/phoneRequested.d.ts +8 -0
  23. package/dist/dts/bridge/events/parsers/popupClosed.d.ts +8 -0
  24. package/dist/dts/bridge/events/parsers/qrTextReceived.d.ts +7 -0
  25. package/dist/dts/bridge/events/parsers/theme-changed.d.ts +42 -0
  26. package/dist/dts/bridge/events/parsers/viewportChanged.d.ts +19 -0
  27. package/dist/dts/bridge/events/parsers/writeAccessRequested.d.ts +8 -0
  28. package/dist/dts/bridge/events/singletonEmitter.d.ts +6 -0
  29. package/dist/dts/bridge/events/subscribe.d.ts +9 -0
  30. package/dist/dts/bridge/events/unsubscribe.d.ts +6 -0
  31. package/dist/dts/bridge/index.d.ts +5 -0
  32. package/dist/dts/bridge/methods/createPostEvent.d.ts +10 -0
  33. package/dist/dts/bridge/methods/haptic.d.ts +40 -0
  34. package/dist/dts/bridge/methods/index.d.ts +6 -0
  35. package/dist/dts/bridge/methods/invoke-custom-method.d.ts +24 -0
  36. package/dist/dts/bridge/methods/methods.d.ts +329 -0
  37. package/dist/dts/bridge/methods/popup.d.ts +49 -0
  38. package/dist/dts/bridge/methods/postEvent.d.ts +29 -0
  39. package/dist/dts/bridge/request.d.ts +65 -0
  40. package/dist/dts/classnames/classNames.d.ts +10 -0
  41. package/dist/dts/classnames/index.d.ts +2 -0
  42. package/dist/dts/classnames/mergeClassNames.d.ts +32 -0
  43. package/dist/dts/{components/ClosingBehaviour/ClosingBehaviour.d.ts → closing-behavior/ClosingBehavior.d.ts} +5 -4
  44. package/dist/dts/closing-behavior/index.d.ts +2 -0
  45. package/dist/dts/closing-behavior/types.d.ts +7 -0
  46. package/dist/dts/{components/CloudStorage → cloud-storage}/CloudStorage.d.ts +19 -11
  47. package/dist/dts/colors/index.d.ts +5 -0
  48. package/dist/dts/colors/isColorDark.d.ts +6 -0
  49. package/dist/dts/colors/isRGB.d.ts +6 -0
  50. package/dist/dts/colors/isRGBShort.d.ts +6 -0
  51. package/dist/dts/colors/toRGB.d.ts +11 -0
  52. package/dist/dts/colors/types.d.ts +8 -0
  53. package/dist/dts/event-emitter/EventEmitter.d.ts +64 -0
  54. package/dist/dts/event-emitter/index.d.ts +2 -0
  55. package/dist/dts/event-emitter/types.d.ts +44 -0
  56. package/dist/dts/globals.d.ts +22 -0
  57. package/dist/dts/{components/HapticFeedback → haptic-feedback}/HapticFeedback.d.ts +3 -3
  58. package/dist/dts/index.d.ts +24 -6
  59. package/dist/dts/init/catchCustomStyles.d.ts +4 -0
  60. package/dist/dts/init/creators/createBackButton.d.ts +2 -2
  61. package/dist/dts/init/creators/createClosingBehavior.d.ts +3 -3
  62. package/dist/dts/init/creators/createMainButton.d.ts +3 -3
  63. package/dist/dts/init/creators/createMiniApp.d.ts +14 -0
  64. package/dist/dts/init/creators/createRequestIdGenerator.d.ts +1 -1
  65. package/dist/dts/init/creators/createThemeParams.d.ts +2 -3
  66. package/dist/dts/init/creators/createViewport.d.ts +16 -9
  67. package/dist/dts/init/creators/index.d.ts +1 -1
  68. package/dist/dts/init/css/bindMiniAppCSSVars.d.ts +16 -0
  69. package/dist/dts/init/css/bindThemeCSSVars.d.ts +12 -0
  70. package/dist/dts/init/css/bindViewportCSSVars.d.ts +20 -0
  71. package/dist/dts/init/css/index.d.ts +1 -0
  72. package/dist/dts/init/css/processCSSVarsOption.d.ts +12 -0
  73. package/dist/dts/init/css/setCSSVar.d.ts +6 -0
  74. package/dist/dts/init/init.d.ts +5 -5
  75. package/dist/dts/init/types.d.ts +42 -69
  76. package/dist/dts/init-data/InitData.d.ts +53 -0
  77. package/dist/dts/init-data/chatParser.d.ts +5 -0
  78. package/dist/dts/init-data/index.d.ts +6 -0
  79. package/dist/dts/init-data/initDataParser.d.ts +5 -0
  80. package/dist/dts/init-data/parseInitData.d.ts +6 -0
  81. package/dist/dts/init-data/types.d.ts +133 -0
  82. package/dist/dts/init-data/userParser.d.ts +5 -0
  83. package/dist/dts/invoice/Invoice.d.ts +43 -0
  84. package/dist/dts/invoice/index.d.ts +2 -0
  85. package/dist/dts/invoice/types.d.ts +7 -0
  86. package/dist/dts/launch-params/computeLaunchData.d.ts +6 -0
  87. package/dist/dts/launch-params/computePageReload.d.ts +6 -0
  88. package/dist/dts/launch-params/getFirstNavigationEntry.d.ts +6 -0
  89. package/dist/dts/launch-params/index.d.ts +12 -0
  90. package/dist/dts/launch-params/launchParamsParser.d.ts +5 -0
  91. package/dist/dts/launch-params/parseLaunchParams.d.ts +6 -0
  92. package/dist/dts/launch-params/retrieveCurrent.d.ts +5 -0
  93. package/dist/dts/launch-params/retrieveFromLocation.d.ts +6 -0
  94. package/dist/dts/launch-params/retrieveFromPerformance.d.ts +8 -0
  95. package/dist/dts/launch-params/retrieveLaunchData.d.ts +6 -0
  96. package/dist/dts/launch-params/serializeLaunchParams.d.ts +6 -0
  97. package/dist/dts/launch-params/storage.d.ts +13 -0
  98. package/dist/dts/launch-params/types.d.ts +48 -0
  99. package/dist/dts/logger/Logger.d.ts +38 -0
  100. package/dist/dts/logger/index.d.ts +1 -0
  101. package/dist/dts/main-button/MainButton.d.ts +104 -0
  102. package/dist/dts/main-button/index.d.ts +2 -0
  103. package/dist/dts/main-button/types.d.ts +20 -0
  104. package/dist/dts/mini-app/MiniApp.d.ts +99 -0
  105. package/dist/dts/mini-app/index.d.ts +2 -0
  106. package/dist/dts/mini-app/types.d.ts +19 -0
  107. package/dist/dts/misc/index.d.ts +2 -0
  108. package/dist/dts/misc/isRecord.d.ts +5 -0
  109. package/dist/dts/misc/isTMA.d.ts +4 -0
  110. package/dist/dts/navigation/HashNavigator/HashNavigator.d.ts +43 -0
  111. package/dist/dts/navigation/HashNavigator/drop.d.ts +4 -0
  112. package/dist/dts/navigation/HashNavigator/go.d.ts +5 -0
  113. package/dist/dts/navigation/HashNavigator/index.d.ts +2 -0
  114. package/dist/dts/navigation/HashNavigator/types.d.ts +34 -0
  115. package/dist/dts/navigation/Navigator/Navigator.d.ts +104 -0
  116. package/dist/dts/navigation/Navigator/index.d.ts +2 -0
  117. package/dist/dts/navigation/Navigator/types.d.ts +44 -0
  118. package/dist/dts/navigation/ensurePrefix.d.ts +7 -0
  119. package/dist/dts/navigation/getHash.d.ts +14 -0
  120. package/dist/dts/navigation/index.d.ts +4 -0
  121. package/dist/dts/parsing/ArrayValueParser.d.ts +18 -0
  122. package/dist/dts/parsing/ParseError.d.ts +22 -0
  123. package/dist/dts/parsing/ParseSchemaFieldError.d.ts +17 -0
  124. package/dist/dts/parsing/ValueParser.d.ts +36 -0
  125. package/dist/dts/parsing/createValueParserGenerator.d.ts +9 -0
  126. package/dist/dts/parsing/index.d.ts +10 -0
  127. package/dist/dts/parsing/parseBySchema.d.ts +8 -0
  128. package/dist/dts/parsing/parsers/array.d.ts +6 -0
  129. package/dist/dts/parsing/parsers/boolean.d.ts +4 -0
  130. package/dist/dts/parsing/parsers/date.d.ts +4 -0
  131. package/dist/dts/parsing/parsers/index.d.ts +8 -0
  132. package/dist/dts/parsing/parsers/json.d.ts +8 -0
  133. package/dist/dts/parsing/parsers/number.d.ts +4 -0
  134. package/dist/dts/parsing/parsers/rgb.d.ts +4 -0
  135. package/dist/dts/parsing/parsers/searchParams.d.ts +8 -0
  136. package/dist/dts/parsing/parsers/string.d.ts +4 -0
  137. package/dist/dts/parsing/toRecord.d.ts +7 -0
  138. package/dist/dts/parsing/types.d.ts +30 -0
  139. package/dist/dts/parsing/unexpectedTypeError.d.ts +4 -0
  140. package/dist/dts/{components/Popup → popup}/Popup.d.ts +10 -9
  141. package/dist/dts/popup/index.d.ts +2 -0
  142. package/dist/dts/popup/preparePopupParams.d.ts +7 -0
  143. package/dist/dts/{components/Popup → popup}/types.d.ts +4 -4
  144. package/dist/dts/{components/QRScanner → qr-scanner}/QRScanner.d.ts +6 -5
  145. package/dist/dts/qr-scanner/index.d.ts +2 -0
  146. package/dist/dts/{components/QRScanner → qr-scanner}/types.d.ts +1 -1
  147. package/dist/dts/state/State.d.ts +19 -7
  148. package/dist/dts/state/types.d.ts +4 -7
  149. package/dist/dts/storage.d.ts +4 -4
  150. package/dist/dts/supports/createSupportsFunc.d.ts +10 -0
  151. package/dist/dts/supports/createSupportsParamFunc.d.ts +15 -0
  152. package/dist/dts/supports/index.d.ts +4 -0
  153. package/dist/dts/supports/supports.d.ts +15 -0
  154. package/dist/dts/supports/types.d.ts +1 -0
  155. package/dist/dts/theme-params/ThemeParams.d.ts +62 -0
  156. package/dist/dts/theme-params/index.d.ts +6 -0
  157. package/dist/dts/theme-params/keys.d.ts +11 -0
  158. package/dist/dts/theme-params/parseThemeParams.d.ts +6 -0
  159. package/dist/dts/theme-params/requestThemeParams.d.ts +7 -0
  160. package/dist/dts/theme-params/serializeThemeParams.d.ts +5 -0
  161. package/dist/dts/theme-params/themeParamsParser.d.ts +2 -0
  162. package/dist/dts/theme-params/types.d.ts +14 -0
  163. package/dist/dts/timeout/TimeoutError.d.ts +3 -0
  164. package/dist/dts/timeout/index.d.ts +3 -0
  165. package/dist/dts/timeout/isTimeoutError.d.ts +6 -0
  166. package/dist/dts/timeout/withTimeout.d.ts +14 -0
  167. package/dist/dts/types/index.d.ts +3 -0
  168. package/dist/dts/types/platform.d.ts +4 -0
  169. package/dist/dts/types/request-id.d.ts +9 -0
  170. package/dist/dts/types/utils.d.ts +36 -0
  171. package/dist/dts/utils/Utils.d.ts +45 -0
  172. package/dist/dts/utils/index.d.ts +1 -0
  173. package/dist/dts/version/compareVersions.d.ts +8 -0
  174. package/dist/dts/version/index.d.ts +2 -0
  175. package/dist/dts/version/types.d.ts +4 -0
  176. package/dist/dts/{components/Viewport → viewport}/Viewport.d.ts +16 -38
  177. package/dist/dts/viewport/index.d.ts +3 -0
  178. package/dist/dts/viewport/requestViewport.d.ts +12 -0
  179. package/dist/dts/{components/Viewport → viewport}/types.d.ts +9 -1
  180. package/dist/dts/viewport/utils.d.ts +5 -0
  181. package/dist/index.cjs +1 -1
  182. package/dist/index.cjs.map +1 -1
  183. package/dist/index.iife.js +1 -1
  184. package/dist/index.iife.js.map +1 -1
  185. package/dist/index.mjs +2254 -1027
  186. package/dist/index.mjs.map +1 -1
  187. package/package.json +7 -16
  188. package/src/{components/BackButton → back-button}/BackButton.ts +23 -21
  189. package/src/back-button/index.ts +2 -0
  190. package/src/{components/BackButton → back-button}/types.ts +3 -2
  191. package/src/bridge/env/hasExternalNotify.ts +19 -0
  192. package/src/bridge/env/hasWebviewProxy.ts +19 -0
  193. package/src/bridge/env/index.ts +3 -0
  194. package/src/bridge/env/isIframe.ts +11 -0
  195. package/src/bridge/errors/MethodUnsupportedError.ts +13 -0
  196. package/src/bridge/errors/ParameterUnsupportedError.ts +13 -0
  197. package/src/bridge/errors/index.ts +2 -0
  198. package/src/bridge/events/createEmitter.ts +108 -0
  199. package/src/bridge/events/events.ts +170 -0
  200. package/src/bridge/events/index.ts +9 -0
  201. package/src/bridge/events/off.ts +14 -0
  202. package/src/bridge/events/on.ts +19 -0
  203. package/src/bridge/events/onTelegramEvent.ts +83 -0
  204. package/src/bridge/events/once.ts +18 -0
  205. package/src/bridge/events/parsers/clipboardTextReceived.ts +26 -0
  206. package/src/bridge/events/parsers/customMethodInvoked.ts +25 -0
  207. package/src/bridge/events/parsers/index.ts +9 -0
  208. package/src/bridge/events/parsers/invoiceClosed.ts +26 -0
  209. package/src/bridge/events/parsers/phoneRequested.ts +14 -0
  210. package/src/bridge/events/parsers/popupClosed.ts +19 -0
  211. package/src/bridge/events/parsers/qrTextReceived.ts +14 -0
  212. package/src/bridge/events/parsers/theme-changed.ts +58 -0
  213. package/src/bridge/events/parsers/viewportChanged.ts +33 -0
  214. package/src/bridge/events/parsers/writeAccessRequested.ts +14 -0
  215. package/src/bridge/events/singletonEmitter.ts +19 -0
  216. package/src/bridge/events/subscribe.ts +15 -0
  217. package/src/bridge/events/unsubscribe.ts +10 -0
  218. package/src/bridge/index.ts +5 -0
  219. package/src/bridge/methods/createPostEvent.ts +40 -0
  220. package/src/bridge/methods/haptic.ts +52 -0
  221. package/src/bridge/methods/index.ts +6 -0
  222. package/src/bridge/methods/invoke-custom-method.ts +25 -0
  223. package/src/bridge/methods/methods.ts +372 -0
  224. package/src/bridge/methods/popup.ts +53 -0
  225. package/src/bridge/methods/postEvent.ts +101 -0
  226. package/src/bridge/request.ts +185 -0
  227. package/src/classnames/classNames.ts +34 -0
  228. package/src/classnames/index.ts +2 -0
  229. package/src/classnames/mergeClassNames.ts +68 -0
  230. package/src/{components/ClosingBehaviour/ClosingBehaviour.ts → closing-behavior/ClosingBehavior.ts} +12 -10
  231. package/src/closing-behavior/index.ts +2 -0
  232. package/src/closing-behavior/types.ts +12 -0
  233. package/src/{components/CloudStorage → cloud-storage}/CloudStorage.ts +50 -28
  234. package/src/colors/index.ts +5 -0
  235. package/src/colors/isColorDark.ts +22 -0
  236. package/src/colors/isRGB.ts +9 -0
  237. package/src/colors/isRGBShort.ts +9 -0
  238. package/src/colors/toRGB.ts +49 -0
  239. package/src/colors/types.ts +9 -0
  240. package/src/event-emitter/EventEmitter.ts +146 -0
  241. package/src/event-emitter/index.ts +2 -0
  242. package/src/event-emitter/types.ts +60 -0
  243. package/src/globals.ts +38 -0
  244. package/src/{components/HapticFeedback → haptic-feedback}/HapticFeedback.ts +16 -8
  245. package/src/index.ts +171 -6
  246. package/src/init/catchCustomStyles.ts +17 -0
  247. package/src/init/creators/createBackButton.ts +4 -6
  248. package/src/init/creators/createClosingBehavior.ts +6 -8
  249. package/src/init/creators/createMainButton.ts +13 -20
  250. package/src/init/creators/createMiniApp.ts +44 -0
  251. package/src/init/creators/createRequestIdGenerator.ts +1 -1
  252. package/src/init/creators/createThemeParams.ts +3 -6
  253. package/src/init/creators/createViewport.ts +92 -42
  254. package/src/init/creators/index.ts +1 -1
  255. package/src/init/css/bindMiniAppCSSVars.ts +48 -0
  256. package/src/init/css/bindThemeCSSVars.ts +31 -0
  257. package/src/init/css/bindViewportCSSVars.ts +36 -0
  258. package/src/init/css/index.ts +1 -0
  259. package/src/init/css/processCSSVarsOption.ts +57 -0
  260. package/src/init/css/setCSSVar.ts +8 -0
  261. package/src/init/init.ts +119 -154
  262. package/src/init/types.ts +42 -87
  263. package/src/init-data/InitData.ts +96 -0
  264. package/src/init-data/chatParser.ts +19 -0
  265. package/src/init-data/index.ts +6 -0
  266. package/src/init-data/initDataParser.ts +41 -0
  267. package/src/init-data/parseInitData.ts +10 -0
  268. package/src/init-data/types.ts +164 -0
  269. package/src/init-data/userParser.ts +45 -0
  270. package/src/invoice/Invoice.ts +123 -0
  271. package/src/invoice/index.ts +2 -0
  272. package/src/invoice/types.ts +11 -0
  273. package/src/launch-params/computeLaunchData.ts +81 -0
  274. package/src/launch-params/computePageReload.ts +13 -0
  275. package/src/launch-params/getFirstNavigationEntry.ts +10 -0
  276. package/src/launch-params/index.ts +12 -0
  277. package/src/launch-params/launchParamsParser.ts +41 -0
  278. package/src/launch-params/parseLaunchParams.ts +10 -0
  279. package/src/launch-params/retrieveCurrent.ts +27 -0
  280. package/src/launch-params/retrieveFromLocation.ts +10 -0
  281. package/src/launch-params/retrieveFromPerformance.ts +23 -0
  282. package/src/launch-params/retrieveLaunchData.ts +30 -0
  283. package/src/launch-params/serializeLaunchParams.ts +37 -0
  284. package/src/launch-params/storage.ts +33 -0
  285. package/src/launch-params/types.ts +57 -0
  286. package/src/logger/Logger.ts +72 -0
  287. package/src/logger/index.ts +1 -0
  288. package/src/main-button/MainButton.ts +239 -0
  289. package/src/main-button/index.ts +2 -0
  290. package/src/main-button/types.ts +26 -0
  291. package/src/mini-app/MiniApp.ts +237 -0
  292. package/src/mini-app/index.ts +2 -0
  293. package/src/mini-app/types.ts +25 -0
  294. package/src/misc/index.ts +2 -0
  295. package/src/misc/isRecord.ts +7 -0
  296. package/src/misc/isTMA.ts +13 -0
  297. package/src/navigation/HashNavigator/HashNavigator.ts +220 -0
  298. package/src/navigation/HashNavigator/drop.ts +36 -0
  299. package/src/navigation/HashNavigator/go.ts +28 -0
  300. package/src/navigation/HashNavigator/index.ts +2 -0
  301. package/src/navigation/HashNavigator/types.ts +41 -0
  302. package/src/navigation/Navigator/Navigator.ts +282 -0
  303. package/src/navigation/Navigator/index.ts +2 -0
  304. package/src/navigation/Navigator/types.ts +55 -0
  305. package/src/navigation/ensurePrefix.ts +9 -0
  306. package/src/navigation/getHash.ts +17 -0
  307. package/src/navigation/index.ts +4 -0
  308. package/src/parsing/ArrayValueParser.ts +79 -0
  309. package/src/parsing/ParseError.ts +27 -0
  310. package/src/parsing/ParseSchemaFieldError.ts +21 -0
  311. package/src/parsing/ValueParser.ts +71 -0
  312. package/src/parsing/createValueParserGenerator.ts +16 -0
  313. package/src/parsing/index.ts +10 -0
  314. package/src/parsing/parseBySchema.ts +65 -0
  315. package/src/parsing/parsers/array.ts +9 -0
  316. package/src/parsing/parsers/boolean.ts +22 -0
  317. package/src/parsing/parsers/date.ts +13 -0
  318. package/src/parsing/parsers/index.ts +8 -0
  319. package/src/parsing/parsers/json.ts +17 -0
  320. package/src/parsing/parsers/number.ts +21 -0
  321. package/src/parsing/parsers/rgb.ts +12 -0
  322. package/src/parsing/parsers/searchParams.ts +24 -0
  323. package/src/parsing/parsers/string.ts +12 -0
  324. package/src/parsing/toRecord.ts +27 -0
  325. package/src/parsing/types.ts +32 -0
  326. package/src/parsing/unexpectedTypeError.ts +6 -0
  327. package/src/{components/Popup → popup}/Popup.ts +38 -28
  328. package/src/popup/index.ts +2 -0
  329. package/src/{components/Popup/utils.ts → popup/preparePopupParams.ts} +3 -3
  330. package/src/{components/Popup → popup}/types.ts +4 -4
  331. package/src/{components/QRScanner → qr-scanner}/QRScanner.ts +18 -10
  332. package/src/qr-scanner/index.ts +2 -0
  333. package/src/{components/QRScanner → qr-scanner}/types.ts +1 -1
  334. package/src/state/State.ts +31 -21
  335. package/src/state/types.ts +5 -8
  336. package/src/storage.ts +6 -4
  337. package/src/supports/createSupportsFunc.ts +18 -0
  338. package/src/supports/createSupportsParamFunc.ts +27 -0
  339. package/src/supports/index.ts +4 -0
  340. package/src/supports/supports.ts +84 -0
  341. package/src/supports/types.ts +1 -0
  342. package/src/theme-params/ThemeParams.ts +131 -0
  343. package/src/theme-params/index.ts +6 -0
  344. package/src/theme-params/keys.ts +24 -0
  345. package/src/theme-params/parseThemeParams.ts +10 -0
  346. package/src/theme-params/requestThemeParams.ts +13 -0
  347. package/src/theme-params/serializeThemeParams.ts +20 -0
  348. package/src/theme-params/themeParamsParser.ts +20 -0
  349. package/src/theme-params/types.ts +33 -0
  350. package/src/timeout/TimeoutError.ts +6 -0
  351. package/src/timeout/index.ts +3 -0
  352. package/src/timeout/isTimeoutError.ts +9 -0
  353. package/src/timeout/withTimeout.ts +36 -0
  354. package/src/types/index.ts +3 -0
  355. package/src/types/platform.ts +14 -0
  356. package/src/types/request-id.ts +10 -0
  357. package/src/types/utils.ts +50 -0
  358. package/src/utils/Utils.ts +107 -0
  359. package/src/utils/index.ts +1 -0
  360. package/src/version/compareVersions.ts +28 -0
  361. package/src/version/index.ts +2 -0
  362. package/src/version/types.ts +4 -0
  363. package/src/{components/Viewport → viewport}/Viewport.ts +72 -98
  364. package/src/viewport/index.ts +3 -0
  365. package/src/viewport/requestViewport.ts +23 -0
  366. package/src/{components/Viewport → viewport}/types.ts +10 -1
  367. package/src/viewport/utils.ts +7 -0
  368. package/dist/dts/components/BackButton/index.d.ts +0 -2
  369. package/dist/dts/components/ClosingBehaviour/index.d.ts +0 -2
  370. package/dist/dts/components/ClosingBehaviour/types.d.ts +0 -7
  371. package/dist/dts/components/InitData/InitData.d.ts +0 -60
  372. package/dist/dts/components/InitData/index.d.ts +0 -1
  373. package/dist/dts/components/MainButton/MainButton.d.ts +0 -114
  374. package/dist/dts/components/MainButton/index.d.ts +0 -2
  375. package/dist/dts/components/MainButton/types.d.ts +0 -15
  376. package/dist/dts/components/Popup/index.d.ts +0 -2
  377. package/dist/dts/components/Popup/utils.d.ts +0 -7
  378. package/dist/dts/components/QRScanner/index.d.ts +0 -2
  379. package/dist/dts/components/ThemeParams/ThemeParams.d.ts +0 -85
  380. package/dist/dts/components/ThemeParams/index.d.ts +0 -2
  381. package/dist/dts/components/ThemeParams/types.d.ts +0 -6
  382. package/dist/dts/components/Viewport/index.d.ts +0 -2
  383. package/dist/dts/components/WebApp/WebApp.d.ts +0 -146
  384. package/dist/dts/components/WebApp/index.d.ts +0 -2
  385. package/dist/dts/components/WebApp/types.d.ts +0 -11
  386. package/dist/dts/components/index.d.ts +0 -11
  387. package/dist/dts/env.d.ts +0 -10
  388. package/dist/dts/errors/MethodNotSupportedError.d.ts +0 -6
  389. package/dist/dts/errors/ParameterNotSupportedError.d.ts +0 -6
  390. package/dist/dts/errors/index.d.ts +0 -2
  391. package/dist/dts/init/creators/createWebApp.d.ts +0 -16
  392. package/dist/dts/init/css.d.ts +0 -50
  393. package/dist/dts/supports.d.ts +0 -22
  394. package/dist/dts/types.d.ts +0 -10
  395. package/dist/dts/url.d.ts +0 -7
  396. package/src/components/BackButton/index.ts +0 -2
  397. package/src/components/ClosingBehaviour/index.ts +0 -6
  398. package/src/components/ClosingBehaviour/types.ts +0 -12
  399. package/src/components/InitData/InitData.ts +0 -139
  400. package/src/components/InitData/index.ts +0 -1
  401. package/src/components/MainButton/MainButton.ts +0 -242
  402. package/src/components/MainButton/index.ts +0 -2
  403. package/src/components/MainButton/types.ts +0 -20
  404. package/src/components/Popup/index.ts +0 -8
  405. package/src/components/QRScanner/index.ts +0 -2
  406. package/src/components/ThemeParams/ThemeParams.ts +0 -168
  407. package/src/components/ThemeParams/index.ts +0 -2
  408. package/src/components/ThemeParams/types.ts +0 -11
  409. package/src/components/Viewport/index.ts +0 -2
  410. package/src/components/WebApp/WebApp.ts +0 -311
  411. package/src/components/WebApp/index.ts +0 -2
  412. package/src/components/WebApp/types.ts +0 -17
  413. package/src/components/index.ts +0 -11
  414. package/src/env.ts +0 -22
  415. package/src/errors/MethodNotSupportedError.ts +0 -9
  416. package/src/errors/ParameterNotSupportedError.ts +0 -9
  417. package/src/errors/index.ts +0 -2
  418. package/src/init/creators/createWebApp.ts +0 -52
  419. package/src/init/css.ts +0 -142
  420. package/src/supports.ts +0 -44
  421. package/src/types.ts +0 -13
  422. package/src/url.ts +0 -23
  423. /package/dist/dts/{components/CloudStorage → cloud-storage}/index.d.ts +0 -0
  424. /package/dist/dts/{components/HapticFeedback → haptic-feedback}/index.d.ts +0 -0
  425. /package/src/{components/CloudStorage → cloud-storage}/index.ts +0 -0
  426. /package/src/{components/HapticFeedback → haptic-feedback}/index.ts +0 -0
@@ -0,0 +1,79 @@
1
+ import type { AnyParser, Parser } from './types.js';
2
+ import { unexpectedTypeError } from './unexpectedTypeError.js';
3
+ import type { ParseResult, ValueParserOverrides } from './ValueParser.js';
4
+ import { ValueParser } from './ValueParser.js';
5
+
6
+ export type OfResult<BaseClass, ItemType, IsOptional extends boolean> = ArrayParserType<
7
+ BaseClass,
8
+ ItemType,
9
+ IsOptional
10
+ >;
11
+
12
+ export interface ArrayParserOverrides<
13
+ BaseClass,
14
+ ItemType,
15
+ IsOptional extends boolean,
16
+ > extends ValueParserOverrides<BaseClass, ItemType[], IsOptional> {
17
+ /**
18
+ * Specifies parser for each array item.
19
+ * @param parser - item parser.
20
+ */
21
+ of<Item>(parser: AnyParser<Item>): OfResult<BaseClass, Item, IsOptional>;
22
+ }
23
+
24
+ export type ArrayParserType<BaseClass, ItemType, IsOptional extends boolean> =
25
+ Omit<BaseClass, keyof ArrayParserOverrides<any, any, any>>
26
+ & ArrayParserOverrides<BaseClass, ItemType, IsOptional>;
27
+
28
+ /**
29
+ * Parses incoming value as array.
30
+ * @param value - value to parse.
31
+ */
32
+ function parseArray(value: unknown): unknown[] {
33
+ if (Array.isArray(value)) {
34
+ return value;
35
+ }
36
+
37
+ if (typeof value === 'string') {
38
+ try {
39
+ const json = JSON.parse(value);
40
+
41
+ if (Array.isArray(json)) {
42
+ return json;
43
+ }
44
+ // eslint-disable-next-line no-empty
45
+ } catch (e) {
46
+ }
47
+ }
48
+ throw unexpectedTypeError();
49
+ }
50
+
51
+ export class ArrayValueParser<ItemType, IsOptional extends boolean>
52
+ extends ValueParser<unknown[], IsOptional> {
53
+ private itemParser: Parser<any>;
54
+
55
+ constructor(
56
+ itemParser: AnyParser<ItemType>,
57
+ isOptional: IsOptional,
58
+ type?: string,
59
+ ) {
60
+ super(parseArray, isOptional, type);
61
+
62
+ this.itemParser = typeof itemParser === 'function'
63
+ ? itemParser
64
+ : itemParser.parse.bind(itemParser);
65
+ }
66
+
67
+ override parse(value: unknown): ParseResult<ItemType[], IsOptional> {
68
+ const arr = super.parse(value);
69
+ return arr === undefined ? arr : arr.map(this.itemParser);
70
+ }
71
+
72
+ of<Item>(itemParser: AnyParser<Item>): OfResult<this, Item, IsOptional> {
73
+ this.itemParser = typeof itemParser === 'function'
74
+ ? itemParser
75
+ : itemParser.parse.bind(itemParser);
76
+
77
+ return this as OfResult<this, Item, IsOptional>;
78
+ }
79
+ }
@@ -0,0 +1,27 @@
1
+ interface Options {
2
+ /**
3
+ * Type name.
4
+ */
5
+ type?: string;
6
+
7
+ /**
8
+ * Original occurred error.
9
+ */
10
+ cause?: unknown;
11
+ }
12
+
13
+ /**
14
+ * Error thrown in case, there was an error during parsing.
15
+ */
16
+ export class ParseError extends Error {
17
+ /**
18
+ * Parser name.
19
+ */
20
+ public readonly type?: string;
21
+
22
+ constructor(public readonly value: unknown, { cause, type }: Options = {}) {
23
+ super(`Unable to parse value${type ? ` as ${type}` : ''}`, { cause });
24
+ Object.setPrototypeOf(this, ParseError.prototype);
25
+ this.type = type;
26
+ }
27
+ }
@@ -0,0 +1,21 @@
1
+ interface Options {
2
+ /**
3
+ * Type name.
4
+ */
5
+ type?: string;
6
+
7
+ /**
8
+ * Original occurred error.
9
+ */
10
+ cause?: unknown;
11
+ }
12
+
13
+ /**
14
+ * Error thrown in case, there was an error during parse.
15
+ */
16
+ export class ParseSchemaFieldError extends Error {
17
+ constructor(field: string, { cause, type }: Options = {}) {
18
+ super(`Unable to parse field "${field}"${type ? ` as ${type}` : ''}`, { cause });
19
+ Object.setPrototypeOf(this, ParseSchemaFieldError.prototype);
20
+ }
21
+ }
@@ -0,0 +1,71 @@
1
+ import type { If } from '~/types/index.js';
2
+
3
+ import { ParseError } from './ParseError.js';
4
+ import type { Parser } from './types.js';
5
+
6
+ /**
7
+ * Result of "parse" function.
8
+ */
9
+ export type ParseResult<ResultType, IsOptional extends boolean> =
10
+ | ResultType
11
+ | If<IsOptional, undefined, never>;
12
+
13
+ /**
14
+ * Result of "optional" function in ValueParser.
15
+ */
16
+ export type OptionalResult<BaseClass, ResultType> = ValueParserType<
17
+ BaseClass,
18
+ ResultType,
19
+ true
20
+ >;
21
+
22
+ export interface ValueParserOverrides<BaseClass, ResultType, IsOptional extends boolean> {
23
+ /**
24
+ * Parses incoming value applying parsing function passed via constructor.
25
+ * @param value - value to parse.
26
+ */
27
+ parse(value: unknown): ParseResult<ResultType, IsOptional>;
28
+
29
+ /**
30
+ * Marks this parser result as optional. This makes the parser to check if passed value
31
+ * is empty. If so, parser will return undefined value instead of passing it to the actual
32
+ * parser.
33
+ */
34
+ optional(): OptionalResult<BaseClass, ResultType>;
35
+ }
36
+
37
+ /**
38
+ * Describes generated ValueParser interface. Shortly saying, this type overrides BaseClass
39
+ * properties defined in ValueParser.
40
+ */
41
+ export type ValueParserType<BaseClass, ResultType, IsOptional extends boolean> =
42
+ Omit<BaseClass, keyof ValueParserOverrides<any, any, any>>
43
+ & ValueParserOverrides<BaseClass, ResultType, IsOptional>;
44
+
45
+ export class ValueParser<ResultType, IsOptional extends boolean> {
46
+ constructor(
47
+ protected parser: Parser<ResultType>,
48
+ protected isOptional: IsOptional,
49
+ protected type?: string,
50
+ ) {
51
+ }
52
+
53
+ parse(value: unknown): ParseResult<ResultType, IsOptional> {
54
+ // In case, parsing result is specified as optional, and passed value is considered as empty,
55
+ // we can return undefined. Otherwise, pass to parser.
56
+ if (this.isOptional && value === undefined) {
57
+ return undefined as ParseResult<ResultType, IsOptional>;
58
+ }
59
+
60
+ try {
61
+ return this.parser(value) as ParseResult<ResultType, IsOptional>;
62
+ } catch (cause) {
63
+ throw new ParseError(value, { type: this.type, cause });
64
+ }
65
+ }
66
+
67
+ optional(): OptionalResult<this, ResultType> {
68
+ this.isOptional = true as IsOptional;
69
+ return this as OptionalResult<this, ResultType>;
70
+ }
71
+ }
@@ -0,0 +1,16 @@
1
+ import type { Parser } from './types.js';
2
+ import { ValueParser } from './ValueParser.js';
3
+
4
+ export type ValueParserGenerator<T> = () => ValueParser<T, false>;
5
+
6
+ /**
7
+ * Creates function which generates new scalar value parser based on the specified one.
8
+ * @param parser - parser to use as basic.
9
+ * @param type - type name.
10
+ */
11
+ export function createValueParserGenerator<T>(
12
+ parser: Parser<T>,
13
+ type?: string,
14
+ ): ValueParserGenerator<T> {
15
+ return () => new ValueParser(parser, false, type);
16
+ }
@@ -0,0 +1,10 @@
1
+ export * from './ArrayValueParser.js';
2
+ export * from './createValueParserGenerator.js';
3
+ export * from './parseBySchema.js';
4
+ export * from './ParseError.js';
5
+ export * from './parsers/index.js';
6
+ export * from './ParseSchemaFieldError.js';
7
+ export * from './toRecord.js';
8
+ export * from './types.js';
9
+ export * from './unexpectedTypeError.js';
10
+ export * from './ValueParser.js';
@@ -0,0 +1,65 @@
1
+ import { ParseError } from './ParseError.js';
2
+ import { ParseSchemaFieldError } from './ParseSchemaFieldError.js';
3
+ import type { Parser, Schema } from './types.js';
4
+
5
+ /**
6
+ * Parses external value by specified schema. Functions iterates over each schema field
7
+ * and uses getField function to get its value from the external source.
8
+ * @param schema - object schema.
9
+ * @param getField - function which gets external value by its field name.
10
+ */
11
+ export function parseBySchema<T>(
12
+ schema: Schema<T>,
13
+ getField: (field: string) => unknown,
14
+ ): T {
15
+ const result = {} as T;
16
+
17
+ // eslint-disable-next-line guard-for-in,no-restricted-syntax
18
+ for (const field in schema) {
19
+ const definition = schema[field];
20
+ if (!definition) {
21
+ continue;
22
+ }
23
+
24
+ let from: string;
25
+ let parser: Parser<any>;
26
+
27
+ // In case, definition has "type" property, then SchemaFieldDetailed was passed.
28
+ if (typeof definition === 'function' || 'parse' in definition) {
29
+ // Otherwise we are working with either parser function or instance.
30
+ from = field;
31
+ parser = typeof definition === 'function' ? definition : definition.parse.bind(definition);
32
+ } else {
33
+ const { type } = definition;
34
+
35
+ from = definition.from || field;
36
+ parser = typeof type === 'function' ? type : type.parse.bind(type);
37
+ }
38
+
39
+ let parsedValue: unknown;
40
+ const originalValue = getField(from);
41
+
42
+ try {
43
+ parsedValue = parser(originalValue);
44
+ } catch (error) {
45
+ // If error is not instance of ParseError, we have nothing additional to do with the error.
46
+ if (!(error instanceof ParseError)) {
47
+ throw new ParseSchemaFieldError(from, { cause: error });
48
+ }
49
+
50
+ // Otherwise, we are going to rethrow the error with extended data.
51
+ throw new ParseSchemaFieldError(from, {
52
+ type: error.type,
53
+ cause: error,
54
+ });
55
+ }
56
+
57
+ if (parsedValue === undefined) {
58
+ continue;
59
+ }
60
+
61
+ (result as any)[field] = parsedValue;
62
+ }
63
+
64
+ return result;
65
+ }
@@ -0,0 +1,9 @@
1
+ import { ArrayValueParser } from '../ArrayValueParser.js';
2
+
3
+ /**
4
+ * Parses incoming value as an array.
5
+ * @param type - parser type name.
6
+ */
7
+ export function array(type?: string): ArrayValueParser<unknown, false> {
8
+ return new ArrayValueParser((value) => value, false, type);
9
+ }
@@ -0,0 +1,22 @@
1
+ import { createValueParserGenerator } from '../createValueParserGenerator.js';
2
+ import { unexpectedTypeError } from '../unexpectedTypeError.js';
3
+
4
+ /**
5
+ * Returns parser to parse value as boolean.
6
+ */
7
+ export const boolean = createValueParserGenerator<boolean>((value) => {
8
+ if (typeof value === 'boolean') {
9
+ return value;
10
+ }
11
+ const asString = String(value);
12
+
13
+ if (asString === '1' || asString === 'true') {
14
+ return true;
15
+ }
16
+
17
+ if (asString === '0' || asString === 'false') {
18
+ return false;
19
+ }
20
+
21
+ throw unexpectedTypeError();
22
+ }, 'boolean');
@@ -0,0 +1,13 @@
1
+ import { number } from './number.js';
2
+ import { createValueParserGenerator } from '../createValueParserGenerator.js';
3
+
4
+ const num = number();
5
+
6
+ /**
7
+ * Returns parser to parse value as Date.
8
+ */
9
+ export const date = createValueParserGenerator<Date>((value) => (
10
+ value instanceof Date
11
+ ? value
12
+ : new Date(num.parse(value) * 1000)
13
+ ), 'Date');
@@ -0,0 +1,8 @@
1
+ export * from './array.js';
2
+ export * from './boolean.js';
3
+ export * from './date.js';
4
+ export * from './json.js';
5
+ export * from './number.js';
6
+ export * from './rgb.js';
7
+ export * from './searchParams.js';
8
+ export * from './string.js';
@@ -0,0 +1,17 @@
1
+ import { parseBySchema } from '../parseBySchema.js';
2
+ import { toRecord } from '../toRecord.js';
3
+ import type { Schema } from '../types.js';
4
+ import { ValueParser } from '../ValueParser.js';
5
+
6
+ /**
7
+ * Creates new Json parser according to passed schema.
8
+ * @param schema - object schema.
9
+ * @param type - parser type name.
10
+ */
11
+ export function json<T>(schema: Schema<T>, type?: string): ValueParser<T, false> {
12
+ return new ValueParser((value) => {
13
+ const record = toRecord(value);
14
+
15
+ return parseBySchema(schema, (field) => record[field]);
16
+ }, false, type);
17
+ }
@@ -0,0 +1,21 @@
1
+ import { createValueParserGenerator } from '../createValueParserGenerator.js';
2
+ import { unexpectedTypeError } from '../unexpectedTypeError.js';
3
+
4
+ /**
5
+ * Returns parser to parse value as number.
6
+ */
7
+ export const number = createValueParserGenerator<number>((value) => {
8
+ if (typeof value === 'number') {
9
+ return value;
10
+ }
11
+
12
+ if (typeof value === 'string') {
13
+ const num = Number(value);
14
+
15
+ if (!Number.isNaN(num)) {
16
+ return num;
17
+ }
18
+ }
19
+
20
+ throw unexpectedTypeError();
21
+ }, 'number');
@@ -0,0 +1,12 @@
1
+ import { toRGB } from '~/colors/index.js';
2
+ import type { RGB } from '~/colors/index.js';
3
+
4
+ import { string } from './string.js';
5
+ import { createValueParserGenerator } from '../createValueParserGenerator.js';
6
+
7
+ const str = string();
8
+
9
+ /**
10
+ * Returns parser to parse value as RGB color.
11
+ */
12
+ export const rgb = createValueParserGenerator<RGB>((value) => toRGB(str.parse(value)), 'rgb');
@@ -0,0 +1,24 @@
1
+ import { parseBySchema } from '../parseBySchema.js';
2
+ import type { Schema } from '../types.js';
3
+ import { unexpectedTypeError } from '../unexpectedTypeError.js';
4
+ import { ValueParser } from '../ValueParser.js';
5
+
6
+ /**
7
+ * Creates new search params parser according to passed schema.
8
+ * @param schema - object schema.
9
+ * @param type - parser type name.
10
+ */
11
+ export function searchParams<T>(schema: Schema<T>, type?: string): ValueParser<T, false> {
12
+ return new ValueParser((value) => {
13
+ if (typeof value !== 'string' && !(value instanceof URLSearchParams)) {
14
+ throw unexpectedTypeError();
15
+ }
16
+
17
+ const params = typeof value === 'string' ? new URLSearchParams(value) : value;
18
+
19
+ return parseBySchema(schema, (field) => {
20
+ const paramValue = params.get(field);
21
+ return paramValue === null ? undefined : paramValue;
22
+ });
23
+ }, false, type);
24
+ }
@@ -0,0 +1,12 @@
1
+ import { createValueParserGenerator } from '../createValueParserGenerator.js';
2
+ import { unexpectedTypeError } from '../unexpectedTypeError.js';
3
+
4
+ /**
5
+ * Returns parser to parse value as string.
6
+ */
7
+ export const string = createValueParserGenerator<string>((value) => {
8
+ if (typeof value === 'string' || typeof value === 'number') {
9
+ return value.toString();
10
+ }
11
+ throw unexpectedTypeError();
12
+ }, 'string');
@@ -0,0 +1,27 @@
1
+ import { unexpectedTypeError } from './unexpectedTypeError.js';
2
+
3
+ /**
4
+ * Converts value to record.
5
+ * @param value - value to convert.
6
+ * @throws {Error} Value passed as a string does not represent JSON object.
7
+ * @throws {Error} Value is not convertable.
8
+ */
9
+ export function toRecord(value: unknown): Record<string, unknown> {
10
+ let formattedValue: any = value;
11
+
12
+ // Convert value to JSON in case, it is string. We expect value to be JSON string.
13
+ if (typeof formattedValue === 'string') {
14
+ formattedValue = JSON.parse(formattedValue);
15
+ }
16
+
17
+ // We expect json to be usual object.
18
+ if (
19
+ typeof formattedValue !== 'object'
20
+ || formattedValue === null
21
+ || Array.isArray(formattedValue)
22
+ ) {
23
+ throw unexpectedTypeError();
24
+ }
25
+
26
+ return formattedValue;
27
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Represents any known parser.
3
+ */
4
+ export type AnyParser<T> = Parser<T> | { parse: Parser<T> };
5
+
6
+ /**
7
+ * Detailed schema field options.
8
+ */
9
+ export interface SchemaFieldDetailed<T> {
10
+ /**
11
+ * Source property name.
12
+ * @default Original property name in schema.
13
+ */
14
+ from?: string;
15
+
16
+ /**
17
+ * Function which should parse incoming value and return expected type.
18
+ */
19
+ type: AnyParser<T>;
20
+ }
21
+
22
+ /**
23
+ * Function which parses incoming value.
24
+ */
25
+ export type Parser<T> = (value: unknown) => T;
26
+
27
+ /**
28
+ * Object schema definition.
29
+ */
30
+ export type Schema<T> = {
31
+ [K in keyof T]: AnyParser<T[K]> | SchemaFieldDetailed<T[K]>;
32
+ };
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Creates instance of TypeError stating, that value has unexpected type.
3
+ */
4
+ export function unexpectedTypeError(): TypeError {
5
+ return new TypeError('Value has unexpected type');
6
+ }
@@ -1,13 +1,18 @@
1
- import { EventEmitter } from '@tma.js/event-emitter';
2
- import { postEvent as defaultPostEvent, request, type PostEvent } from '@tma.js/bridge';
1
+ import {
2
+ type PostEvent,
3
+ postEvent as defaultPostEvent,
4
+ request,
5
+ } from '~/bridge/index.js';
6
+ import { EventEmitter } from '~/event-emitter/index.js';
7
+ import { State } from '~/state/index.js';
8
+ import {
9
+ createSupportsFunc,
10
+ type SupportsFunc,
11
+ } from '~/supports/index.js';
12
+ import type { Version } from '~/version/index.js';
3
13
 
4
- import type { Version } from '@tma.js/utils';
5
-
6
- import { preparePopupParams } from './utils.js';
7
- import { createSupportsFunc, type SupportsFunc } from '../../supports.js';
8
- import { State } from '../../state/index.js';
9
-
10
- import type { PopupParams, PopupEvents, PopupState } from './types.js';
14
+ import { preparePopupParams } from './preparePopupParams.js';
15
+ import type { OpenPopupOptions, PopupEvents, PopupState } from './types.js';
11
16
 
12
17
  /**
13
18
  * Controls currently displayed application popup. It allows developers to
@@ -18,13 +23,20 @@ export class Popup {
18
23
 
19
24
  private readonly state: State<PopupState>;
20
25
 
21
- constructor(version: Version, private readonly postEvent: PostEvent = defaultPostEvent) {
26
+ constructor(
27
+ version: Version,
28
+ private readonly postEvent: PostEvent = defaultPostEvent,
29
+ ) {
22
30
  this.state = new State({ isOpened: false }, this.ee);
23
31
  this.supports = createSupportsFunc(version, { open: 'web_app_open_popup' });
24
32
  }
25
33
 
34
+ private set isOpened(value) {
35
+ this.state.set('isOpened', value);
36
+ }
37
+
26
38
  /**
27
- * Shows whether popup is currently opened.
39
+ * True if popup is currently opened.
28
40
  */
29
41
  get isOpened(): boolean {
30
42
  return this.state.get('isOpened');
@@ -33,12 +45,12 @@ export class Popup {
33
45
  /**
34
46
  * Adds new event listener.
35
47
  */
36
- on: typeof this.ee.on = this.ee.on.bind(this.ee);
48
+ on = this.ee.on.bind(this.ee);
37
49
 
38
50
  /**
39
51
  * Removes event listener.
40
52
  */
41
- off: typeof this.ee.off = this.ee.off.bind(this.ee);
53
+ off = this.ee.off.bind(this.ee);
42
54
 
43
55
  /**
44
56
  * A method that shows a native popup described by the `params` argument.
@@ -50,28 +62,26 @@ export class Popup {
50
62
  *
51
63
  * FIXME: In desktop, this function may work incorrectly.
52
64
  * Issue: https://github.com/Telegram-Mini-Apps/tma.js/issues/7
53
- * @param params - popup parameters.
65
+ * @param options - popup parameters.
54
66
  * @throws {Error} Popup is already opened.
55
67
  */
56
- async open(params: PopupParams): Promise<string | null> {
68
+ open(options: OpenPopupOptions): Promise<string | null> {
57
69
  if (this.isOpened) {
58
70
  throw new Error('Popup is already opened.');
59
71
  }
60
72
 
61
- this.state.set('isOpened', true);
62
-
63
- try {
64
- const { button_id: buttonId = null } = await request(
65
- 'web_app_open_popup',
66
- preparePopupParams(params),
67
- 'popup_closed',
68
- { postEvent: this.postEvent },
69
- );
73
+ this.isOpened = true;
70
74
 
71
- return buttonId;
72
- } finally {
73
- this.state.set('isOpened', false);
74
- }
75
+ return request(
76
+ 'web_app_open_popup',
77
+ preparePopupParams(options),
78
+ 'popup_closed',
79
+ { postEvent: this.postEvent },
80
+ )
81
+ .then(({ button_id = null }) => button_id)
82
+ .finally(() => {
83
+ this.isOpened = false;
84
+ });
75
85
  }
76
86
 
77
87
  /**
@@ -0,0 +1,2 @@
1
+ export * from './Popup.js';
2
+ export * from './types.js';
@@ -1,12 +1,12 @@
1
- import type { PopupButton, PopupParams as BridgePopupParams } from '@tma.js/bridge';
1
+ import type { PopupButton, PopupParams as BridgePopupParams } from '~/bridge/index.js';
2
2
 
3
- import type { PopupParams } from './types.js';
3
+ import type { OpenPopupOptions } from './types.js';
4
4
 
5
5
  /**
6
6
  * Prepares popup parameters before sending them to native app.
7
7
  * @param params - popup parameters.
8
8
  */
9
- export function preparePopupParams(params: PopupParams): BridgePopupParams {
9
+ export function preparePopupParams(params: OpenPopupOptions): BridgePopupParams {
10
10
  const message = params.message.trim();
11
11
  const title = (params.title || '').trim();
12
12
  const buttons = params.buttons || [];
@@ -1,4 +1,4 @@
1
- import type { StateEvents } from '../../state/index.js';
1
+ import type { StateEvents } from '~/state/index.js';
2
2
 
3
3
  export interface PopupState {
4
4
  isOpened: boolean;
@@ -14,7 +14,7 @@ export type PopupEventListener<E extends PopupEventName> = PopupEvents[E];
14
14
  * This object describes the native popup.
15
15
  * @see https://core.telegram.org/bots/webapps#popupparams
16
16
  */
17
- export interface PopupParams {
17
+ export interface OpenPopupOptions {
18
18
  /**
19
19
  * The text to be displayed in the popup title, 0-64 characters.
20
20
  * @default ""
@@ -30,14 +30,14 @@ export interface PopupParams {
30
30
  * List of buttons to be displayed in the popup, 1-3 buttons.
31
31
  * @default [{type: 'close'}]
32
32
  */
33
- buttons?: PopupButton[]
33
+ buttons?: OpenPopupOptionsButton[];
34
34
  }
35
35
 
36
36
  /**
37
37
  * This object describes the native popup button.
38
38
  * @see https://core.telegram.org/bots/webapps#popupbutton
39
39
  */
40
- export type PopupButton = {
40
+ export type OpenPopupOptionsButton = {
41
41
  /**
42
42
  * Identifier of the button, 0-64 characters.
43
43
  * @default ""