@tma.js/sdk 1.4.4 → 1.4.7

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 (298) hide show
  1. package/README.md +2 -2
  2. package/dist/dts/bridge/env/index.d.ts +0 -1
  3. package/dist/dts/bridge/events/events.d.ts +16 -16
  4. package/dist/dts/bridge/methods/methods.d.ts +27 -27
  5. package/dist/dts/bridge/methods/popup.d.ts +1 -1
  6. package/dist/dts/index.d.ts +3 -3
  7. package/dist/dts/init-data/types.d.ts +3 -3
  8. package/dist/dts/launch-params/index.d.ts +1 -4
  9. package/dist/dts/launch-params/retrieveFromLocation.d.ts +2 -2
  10. package/dist/dts/launch-params/retrieveFromPerformance.d.ts +3 -4
  11. package/dist/dts/launch-params/retrieveFromUrl.d.ts +3 -2
  12. package/dist/dts/launch-params/retrieveLaunchData.d.ts +1 -0
  13. package/dist/dts/launch-params/retrieveLaunchParams.d.ts +6 -0
  14. package/dist/dts/launch-params/storage.d.ts +3 -5
  15. package/dist/dts/launch-params/types.d.ts +1 -1
  16. package/dist/dts/{launch-params → misc}/getFirstNavigationEntry.d.ts +1 -1
  17. package/dist/dts/misc/index.d.ts +3 -0
  18. package/dist/dts/{bridge/env → misc}/isIframe.d.ts +1 -1
  19. package/dist/dts/misc/isPageReload.d.ts +5 -0
  20. package/dist/dts/theme-params/types.d.ts +1 -1
  21. package/dist/index.cjs +1 -2
  22. package/dist/index.iife.js +1 -2
  23. package/dist/index.mjs +841 -866
  24. package/package.json +2 -3
  25. package/dist/dts/launch-params/computeLaunchData.d.ts +0 -6
  26. package/dist/dts/launch-params/computePageReload.d.ts +0 -6
  27. package/dist/dts/launch-params/retrieveCurrent.d.ts +0 -5
  28. package/dist/index.cjs.map +0 -1
  29. package/dist/index.iife.js.map +0 -1
  30. package/dist/index.mjs.map +0 -1
  31. package/src/__tests__/globals.ts +0 -39
  32. package/src/back-button/BackButton.ts +0 -90
  33. package/src/back-button/__tests__/BackButton.ts +0 -129
  34. package/src/back-button/index.ts +0 -2
  35. package/src/back-button/types.ts +0 -14
  36. package/src/bridge/__tests__/parseMessage.ts +0 -23
  37. package/src/bridge/__tests__/request.ts +0 -236
  38. package/src/bridge/env/__tests__/hasExternalNotify.ts +0 -15
  39. package/src/bridge/env/__tests__/hasWebviewProxy.ts +0 -15
  40. package/src/bridge/env/__tests__/isIframe.ts +0 -30
  41. package/src/bridge/env/hasExternalNotify.ts +0 -19
  42. package/src/bridge/env/hasWebviewProxy.ts +0 -19
  43. package/src/bridge/env/index.ts +0 -3
  44. package/src/bridge/env/isIframe.ts +0 -11
  45. package/src/bridge/errors/MethodUnsupportedError.ts +0 -13
  46. package/src/bridge/errors/ParameterUnsupportedError.ts +0 -13
  47. package/src/bridge/errors/index.ts +0 -2
  48. package/src/bridge/events/__tests__/createEmitter.ts +0 -143
  49. package/src/bridge/events/__tests__/off.ts +0 -34
  50. package/src/bridge/events/__tests__/on.ts +0 -49
  51. package/src/bridge/events/__tests__/onTelegramEvent.ts +0 -49
  52. package/src/bridge/events/__tests__/once.ts +0 -64
  53. package/src/bridge/events/__tests__/singletonEmitter.ts +0 -22
  54. package/src/bridge/events/__tests__/subscribe.ts +0 -49
  55. package/src/bridge/events/__tests__/unsubscribe.ts +0 -34
  56. package/src/bridge/events/createEmitter.ts +0 -108
  57. package/src/bridge/events/events.ts +0 -170
  58. package/src/bridge/events/index.ts +0 -9
  59. package/src/bridge/events/off.ts +0 -14
  60. package/src/bridge/events/on.ts +0 -19
  61. package/src/bridge/events/onTelegramEvent.ts +0 -81
  62. package/src/bridge/events/once.ts +0 -18
  63. package/src/bridge/events/parsers/__tests__/clipboardTextReceived.ts +0 -21
  64. package/src/bridge/events/parsers/__tests__/invoiceClosed.ts +0 -12
  65. package/src/bridge/events/parsers/__tests__/popupClosed.ts +0 -10
  66. package/src/bridge/events/parsers/__tests__/qrTextReceived.ts +0 -9
  67. package/src/bridge/events/parsers/__tests__/theme-changed.ts +0 -42
  68. package/src/bridge/events/parsers/__tests__/viewportChanged.ts +0 -49
  69. package/src/bridge/events/parsers/clipboardTextReceived.ts +0 -26
  70. package/src/bridge/events/parsers/customMethodInvoked.ts +0 -25
  71. package/src/bridge/events/parsers/index.ts +0 -9
  72. package/src/bridge/events/parsers/invoiceClosed.ts +0 -26
  73. package/src/bridge/events/parsers/phoneRequested.ts +0 -14
  74. package/src/bridge/events/parsers/popupClosed.ts +0 -19
  75. package/src/bridge/events/parsers/qrTextReceived.ts +0 -14
  76. package/src/bridge/events/parsers/theme-changed.ts +0 -58
  77. package/src/bridge/events/parsers/viewportChanged.ts +0 -33
  78. package/src/bridge/events/parsers/writeAccessRequested.ts +0 -14
  79. package/src/bridge/events/singletonEmitter.ts +0 -19
  80. package/src/bridge/events/subscribe.ts +0 -15
  81. package/src/bridge/events/unsubscribe.ts +0 -10
  82. package/src/bridge/index.ts +0 -7
  83. package/src/bridge/invokeCustomMethod.ts +0 -56
  84. package/src/bridge/methods/__tests__/createPostEvent.ts +0 -37
  85. package/src/bridge/methods/__tests__/postEvent.ts +0 -137
  86. package/src/bridge/methods/createPostEvent.ts +0 -40
  87. package/src/bridge/methods/custom-methods.ts +0 -68
  88. package/src/bridge/methods/haptic.ts +0 -52
  89. package/src/bridge/methods/index.ts +0 -6
  90. package/src/bridge/methods/methods.ts +0 -370
  91. package/src/bridge/methods/popup.ts +0 -53
  92. package/src/bridge/methods/postEvent.ts +0 -101
  93. package/src/bridge/parseMessage.ts +0 -28
  94. package/src/bridge/request.ts +0 -176
  95. package/src/classnames/__tests__/classNames.ts +0 -20
  96. package/src/classnames/__tests__/mergeClassNames.ts +0 -21
  97. package/src/classnames/classNames.ts +0 -34
  98. package/src/classnames/index.ts +0 -2
  99. package/src/classnames/mergeClassNames.ts +0 -60
  100. package/src/closing-behavior/ClosingBehavior.ts +0 -64
  101. package/src/closing-behavior/__tests__/ClosingBehavior.ts +0 -86
  102. package/src/closing-behavior/index.ts +0 -2
  103. package/src/closing-behavior/types.ts +0 -12
  104. package/src/cloud-storage/CloudStorage.ts +0 -138
  105. package/src/cloud-storage/index.ts +0 -1
  106. package/src/colors/__tests__/isColorDark.ts +0 -12
  107. package/src/colors/__tests__/isRGB.ts +0 -13
  108. package/src/colors/__tests__/isRGBShort.ts +0 -13
  109. package/src/colors/__tests__/toRGB.ts +0 -23
  110. package/src/colors/index.ts +0 -5
  111. package/src/colors/isColorDark.ts +0 -22
  112. package/src/colors/isRGB.ts +0 -9
  113. package/src/colors/isRGBShort.ts +0 -9
  114. package/src/colors/toRGB.ts +0 -49
  115. package/src/colors/types.ts +0 -9
  116. package/src/css/__tests__/bindMiniAppCSSVars.ts +0 -175
  117. package/src/css/__tests__/bindThemeCSSVars.ts +0 -52
  118. package/src/css/__tests__/bindViewportCSSVars.ts +0 -55
  119. package/src/css/__tests__/setCSSVar.ts +0 -14
  120. package/src/css/bindMiniAppCSSVars.ts +0 -51
  121. package/src/css/bindThemeCSSVars.ts +0 -31
  122. package/src/css/bindViewportCSSVars.ts +0 -36
  123. package/src/css/index.ts +0 -4
  124. package/src/css/setCSSVar.ts +0 -8
  125. package/src/event-emitter/EventEmitter.ts +0 -146
  126. package/src/event-emitter/__tests__/EventEmitter.ts +0 -145
  127. package/src/event-emitter/index.ts +0 -2
  128. package/src/event-emitter/types.ts +0 -60
  129. package/src/globals.ts +0 -38
  130. package/src/haptic-feedback/HapticFeedback.ts +0 -70
  131. package/src/haptic-feedback/__tests__/HapticFeedback.ts +0 -68
  132. package/src/haptic-feedback/index.ts +0 -1
  133. package/src/index.ts +0 -185
  134. package/src/init/catchCustomStyles.ts +0 -17
  135. package/src/init/creators/__tests__/createViewport.ts +0 -96
  136. package/src/init/creators/createBackButton.ts +0 -25
  137. package/src/init/creators/createClosingBehavior.ts +0 -24
  138. package/src/init/creators/createMainButton.ts +0 -51
  139. package/src/init/creators/createMiniApp.ts +0 -48
  140. package/src/init/creators/createRequestIdGenerator.ts +0 -13
  141. package/src/init/creators/createSettingsButton.ts +0 -25
  142. package/src/init/creators/createThemeParams.ts +0 -11
  143. package/src/init/creators/createViewport.ts +0 -94
  144. package/src/init/creators/index.ts +0 -8
  145. package/src/init/css/index.ts +0 -1
  146. package/src/init/css/processCSSVarsOption.ts +0 -55
  147. package/src/init/index.ts +0 -2
  148. package/src/init/init.ts +0 -134
  149. package/src/init/types.ts +0 -94
  150. package/src/init-data/InitData.ts +0 -96
  151. package/src/init-data/__tests__/InitData.ts +0 -98
  152. package/src/init-data/__tests__/chatParser.ts +0 -102
  153. package/src/init-data/__tests__/initDataParser.ts +0 -136
  154. package/src/init-data/__tests__/parseInitData.ts +0 -136
  155. package/src/init-data/__tests__/userParser.ts +0 -96
  156. package/src/init-data/chatParser.ts +0 -19
  157. package/src/init-data/index.ts +0 -6
  158. package/src/init-data/initDataParser.ts +0 -41
  159. package/src/init-data/parseInitData.ts +0 -10
  160. package/src/init-data/types.ts +0 -164
  161. package/src/init-data/userParser.ts +0 -45
  162. package/src/invoice/Invoice.ts +0 -123
  163. package/src/invoice/index.ts +0 -2
  164. package/src/invoice/types.ts +0 -11
  165. package/src/launch-params/__tests__/retrieveFromUrl.ts +0 -19
  166. package/src/launch-params/computeLaunchData.ts +0 -81
  167. package/src/launch-params/computePageReload.ts +0 -13
  168. package/src/launch-params/getFirstNavigationEntry.ts +0 -10
  169. package/src/launch-params/index.ts +0 -13
  170. package/src/launch-params/launchParamsParser.ts +0 -45
  171. package/src/launch-params/parseLaunchParams.ts +0 -10
  172. package/src/launch-params/retrieveCurrent.ts +0 -27
  173. package/src/launch-params/retrieveFromLocation.ts +0 -10
  174. package/src/launch-params/retrieveFromPerformance.ts +0 -18
  175. package/src/launch-params/retrieveFromUrl.ts +0 -19
  176. package/src/launch-params/retrieveLaunchData.ts +0 -30
  177. package/src/launch-params/serializeLaunchParams.ts +0 -37
  178. package/src/launch-params/storage.ts +0 -33
  179. package/src/launch-params/types.ts +0 -62
  180. package/src/logger/Logger.ts +0 -72
  181. package/src/logger/__tests__/Logger.ts +0 -107
  182. package/src/logger/index.ts +0 -1
  183. package/src/main-button/MainButton.ts +0 -239
  184. package/src/main-button/__tests__/MainButton.ts +0 -346
  185. package/src/main-button/index.ts +0 -2
  186. package/src/main-button/types.ts +0 -26
  187. package/src/mini-app/MiniApp.ts +0 -348
  188. package/src/mini-app/__tests__/MiniApp.ts +0 -140
  189. package/src/mini-app/contactParser.ts +0 -29
  190. package/src/mini-app/index.ts +0 -2
  191. package/src/mini-app/types.ts +0 -38
  192. package/src/misc/__tests__/isRecord.ts +0 -21
  193. package/src/misc/index.ts +0 -2
  194. package/src/misc/isRecord.ts +0 -7
  195. package/src/misc/isTMA.ts +0 -13
  196. package/src/navigation/HashNavigator/HashNavigator.ts +0 -220
  197. package/src/navigation/HashNavigator/__tests__/HashNavigator.ts +0 -144
  198. package/src/navigation/HashNavigator/__tests__/drop.ts +0 -42
  199. package/src/navigation/HashNavigator/__tests__/go.ts +0 -9
  200. package/src/navigation/HashNavigator/drop.ts +0 -36
  201. package/src/navigation/HashNavigator/go.ts +0 -28
  202. package/src/navigation/HashNavigator/index.ts +0 -2
  203. package/src/navigation/HashNavigator/types.ts +0 -41
  204. package/src/navigation/Navigator/Navigator.ts +0 -282
  205. package/src/navigation/Navigator/index.ts +0 -2
  206. package/src/navigation/Navigator/types.ts +0 -55
  207. package/src/navigation/ensurePrefix.ts +0 -9
  208. package/src/navigation/getHash.ts +0 -17
  209. package/src/navigation/index.ts +0 -4
  210. package/src/parsing/ArrayValueParser.ts +0 -79
  211. package/src/parsing/ParseError.ts +0 -27
  212. package/src/parsing/ParseSchemaFieldError.ts +0 -21
  213. package/src/parsing/ValueParser.ts +0 -71
  214. package/src/parsing/__tests__/ArrayValueParser.ts +0 -18
  215. package/src/parsing/__tests__/toRecord.ts +0 -10
  216. package/src/parsing/createValueParserGenerator.ts +0 -16
  217. package/src/parsing/index.ts +0 -10
  218. package/src/parsing/parseBySchema.ts +0 -65
  219. package/src/parsing/parsers/__tests__/array.ts +0 -39
  220. package/src/parsing/parsers/__tests__/boolean.ts +0 -31
  221. package/src/parsing/parsers/__tests__/date.ts +0 -25
  222. package/src/parsing/parsers/__tests__/json.ts +0 -80
  223. package/src/parsing/parsers/__tests__/number.ts +0 -23
  224. package/src/parsing/parsers/__tests__/rgb.ts +0 -22
  225. package/src/parsing/parsers/__tests__/searchParams.ts +0 -105
  226. package/src/parsing/parsers/__tests__/string.ts +0 -25
  227. package/src/parsing/parsers/array.ts +0 -9
  228. package/src/parsing/parsers/boolean.ts +0 -22
  229. package/src/parsing/parsers/date.ts +0 -11
  230. package/src/parsing/parsers/index.ts +0 -8
  231. package/src/parsing/parsers/json.ts +0 -17
  232. package/src/parsing/parsers/number.ts +0 -21
  233. package/src/parsing/parsers/rgb.ts +0 -10
  234. package/src/parsing/parsers/searchParams.ts +0 -24
  235. package/src/parsing/parsers/string.ts +0 -12
  236. package/src/parsing/toRecord.ts +0 -27
  237. package/src/parsing/types.ts +0 -32
  238. package/src/parsing/unexpectedTypeError.ts +0 -6
  239. package/src/popup/Popup.ts +0 -91
  240. package/src/popup/__tests__/Popup.ts +0 -130
  241. package/src/popup/__tests__/preparePopupParams.ts +0 -85
  242. package/src/popup/index.ts +0 -2
  243. package/src/popup/preparePopupParams.ts +0 -59
  244. package/src/popup/types.ts +0 -69
  245. package/src/qr-scanner/QRScanner.ts +0 -95
  246. package/src/qr-scanner/index.ts +0 -2
  247. package/src/qr-scanner/types.ts +0 -11
  248. package/src/settings-button/SettingsButton.ts +0 -85
  249. package/src/settings-button/index.ts +0 -2
  250. package/src/settings-button/types.ts +0 -15
  251. package/src/state/State.ts +0 -67
  252. package/src/state/index.ts +0 -2
  253. package/src/state/types.ts +0 -31
  254. package/src/storage.ts +0 -69
  255. package/src/supports/__tests__/supports.ts +0 -123
  256. package/src/supports/createSupportsFunc.ts +0 -18
  257. package/src/supports/createSupportsParamFunc.ts +0 -27
  258. package/src/supports/index.ts +0 -4
  259. package/src/supports/supports.ts +0 -84
  260. package/src/supports/types.ts +0 -1
  261. package/src/theme-params/ThemeParams.ts +0 -131
  262. package/src/theme-params/__tests__/keys.ts +0 -19
  263. package/src/theme-params/__tests__/parseThemeParams.ts +0 -29
  264. package/src/theme-params/__tests__/serializeThemeParams.ts +0 -29
  265. package/src/theme-params/__tests__/themeParamsParser.ts +0 -29
  266. package/src/theme-params/index.ts +0 -6
  267. package/src/theme-params/keys.ts +0 -24
  268. package/src/theme-params/parseThemeParams.ts +0 -10
  269. package/src/theme-params/requestThemeParams.ts +0 -13
  270. package/src/theme-params/serializeThemeParams.ts +0 -20
  271. package/src/theme-params/themeParamsParser.ts +0 -22
  272. package/src/theme-params/types.ts +0 -33
  273. package/src/timeout/TimeoutError.ts +0 -6
  274. package/src/timeout/__tests__/isTimeoutError.ts +0 -9
  275. package/src/timeout/__tests__/withTimeout.ts +0 -28
  276. package/src/timeout/index.ts +0 -4
  277. package/src/timeout/isTimeoutError.ts +0 -9
  278. package/src/timeout/sleep.ts +0 -10
  279. package/src/timeout/withTimeout.ts +0 -24
  280. package/src/types/index.ts +0 -4
  281. package/src/types/methods.ts +0 -18
  282. package/src/types/platform.ts +0 -14
  283. package/src/types/request-id.ts +0 -10
  284. package/src/types/utils.ts +0 -50
  285. package/src/utils/Utils.ts +0 -107
  286. package/src/utils/index.ts +0 -1
  287. package/src/version/__tests__/compareVersions.ts +0 -19
  288. package/src/version/compareVersions.ts +0 -28
  289. package/src/version/index.ts +0 -2
  290. package/src/version/types.ts +0 -4
  291. package/src/viewport/Viewport.ts +0 -171
  292. package/src/viewport/__tests__/isStableViewportPlatform.ts +0 -15
  293. package/src/viewport/__tests__/utils.ts +0 -12
  294. package/src/viewport/index.ts +0 -4
  295. package/src/viewport/isStableViewportPlatform.ts +0 -10
  296. package/src/viewport/requestViewport.ts +0 -23
  297. package/src/viewport/types.ts +0 -23
  298. package/src/viewport/utils.ts +0 -7
@@ -1,81 +0,0 @@
1
- import { computePageReload } from './computePageReload.js';
2
- import { retrieveCurrent } from './retrieveCurrent.js';
3
- import { retrieveFromStorage } from './storage.js';
4
- import type { LaunchData } from './types.js';
5
-
6
- /**
7
- * Returns true in case, current environment is iframe.
8
- * @see https://stackoverflow.com/a/326076
9
- */
10
- function isIframe(): boolean {
11
- try {
12
- return window.self !== window.top;
13
- } catch (e) {
14
- return true;
15
- }
16
- }
17
-
18
- /**
19
- * Computes launch data information. Extracts both previous and current launch parameters
20
- * to compute current list of them. Additionally, computes if page was reloaded.
21
- */
22
- export function computeLaunchData(): LaunchData {
23
- // Retrieve launch parameters from the session storage. We consider this value as the launch
24
- // parameters saved previously, in the previous runtime session (before the page reload).
25
- const lpPrevious = retrieveFromStorage();
26
-
27
- // Currently used launch parameters passed to the Mini App.
28
- const lpCurrent = retrieveCurrent();
29
-
30
- const isPageReload = computePageReload();
31
-
32
- if (lpPrevious) {
33
- if (lpCurrent) {
34
- return {
35
- launchParams: lpCurrent,
36
- isPageReload: isIframe()
37
- // In iframes we should check page reload via 2 ways:
38
- // 1. Native one via navigation entry.
39
- // 2. By comparing raw init data representations, when the first step did not return
40
- // explicit true.
41
- //
42
- // The reason is Telegram provides the horrible way of reloading current iframes which
43
- // does not guarantee, that reload will be proceeded properly.
44
- // Issue: https://github.com/morethanwords/tweb/issues/271
45
- //
46
- // We trust isPageReload variable value only in case it is "true". Otherwise, it can be
47
- // wrong. That's why we compare raw init data raw representations, which is unstable
48
- // also. This will not work as expected in cases, user launches applications via
49
- // KeyboardButton-s which can lack of init data. So, this code will not differ reload
50
- // from the fresh start.
51
- ? isPageReload || lpPrevious.initDataRaw === lpCurrent.initDataRaw
52
-
53
- // In environments different from iframe, when we have both previous and current launch
54
- // parameters it is guaranteed that page was reloaded as long as session is created only
55
- // for the Mini App launch and will be automatically disposed after it is closed.
56
- : true,
57
- };
58
- }
59
-
60
- // Explicit page reload allows us to trust previously saved launch parameters.
61
- if (isPageReload) {
62
- return {
63
- launchParams: lpPrevious,
64
- isPageReload,
65
- };
66
- }
67
-
68
- // We can't trust previously saved launch parameters as long as we don't really know if
69
- // current session was born due to restart. It is better to throw an error.
70
- throw new Error('Unable to retrieve current launch parameters, which must exist.');
71
- }
72
-
73
- if (lpCurrent) {
74
- return {
75
- launchParams: lpCurrent,
76
- isPageReload: false,
77
- };
78
- }
79
-
80
- throw new Error('Unable to retrieve any launch parameters.');
81
- }
@@ -1,13 +0,0 @@
1
- import { getFirstNavigationEntry } from './getFirstNavigationEntry.js';
2
-
3
- /**
4
- * Determines if current page was reloaded.
5
- * @returns Boolean if function was able to compute any valid value. Null in case, no
6
- * navigation entries were found.
7
- */
8
- export function computePageReload(): boolean | null {
9
- const firstNavigationEntry = getFirstNavigationEntry();
10
- return firstNavigationEntry
11
- ? firstNavigationEntry.type === 'reload'
12
- : null;
13
- }
@@ -1,10 +0,0 @@
1
- /**
2
- * Returns the first navigation entry from window.performance.
3
- * @returns First navigation entry or null, in case performance functionality is not supported
4
- * or navigation entry was not found.
5
- */
6
- export function getFirstNavigationEntry(): PerformanceNavigationTiming | null {
7
- return (
8
- performance.getEntriesByType('navigation')[0] as PerformanceNavigationTiming | undefined
9
- ) || null;
10
- }
@@ -1,13 +0,0 @@
1
- export * from './computeLaunchData.js';
2
- export * from './computePageReload.js';
3
- export * from './getFirstNavigationEntry.js';
4
- export * from './launchParamsParser.js';
5
- export * from './parseLaunchParams.js';
6
- export * from './retrieveCurrent.js';
7
- export * from './retrieveFromLocation.js';
8
- export * from './retrieveFromPerformance.js';
9
- export * from './retrieveFromUrl.js';
10
- export * from './retrieveLaunchData.js';
11
- export * from './serializeLaunchParams.js';
12
- export * from './storage.js';
13
- export * from './types.js';
@@ -1,45 +0,0 @@
1
- import { initDataParser } from '~/init-data/index.js';
2
- import { boolean, searchParams, string } from '~/parsing/index.js';
3
- import { themeParamsParser } from '~/theme-params/index.js';
4
-
5
- import type { LaunchParams } from './types.js';
6
-
7
- /**
8
- * Returns parser used to parse launch params.
9
- */
10
- export function launchParamsParser() {
11
- return searchParams<LaunchParams>({
12
- botInline: {
13
- type: boolean().optional(),
14
- from: 'tgWebAppBotInline',
15
- },
16
- initData: {
17
- type: initDataParser().optional(),
18
- from: 'tgWebAppData',
19
- },
20
- initDataRaw: {
21
- type: string().optional(),
22
- from: 'tgWebAppData',
23
- },
24
- platform: {
25
- type: string(),
26
- from: 'tgWebAppPlatform',
27
- },
28
- showSettings: {
29
- type: boolean().optional(),
30
- from: 'tgWebAppShowSettings',
31
- },
32
- startParam: {
33
- type: string().optional(),
34
- from: 'tgWebAppStartParam',
35
- },
36
- themeParams: {
37
- type: themeParamsParser(),
38
- from: 'tgWebAppThemeParams',
39
- },
40
- version: {
41
- type: string(),
42
- from: 'tgWebAppVersion',
43
- },
44
- }, 'LaunchParams');
45
- }
@@ -1,10 +0,0 @@
1
- import { launchParamsParser } from './launchParamsParser.js';
2
- import type { LaunchParams } from './types.js';
3
-
4
- /**
5
- * Parses value as launch parameters.
6
- * @param value - value to parse.
7
- */
8
- export function parseLaunchParams(value: unknown): LaunchParams {
9
- return launchParamsParser().parse(value);
10
- }
@@ -1,27 +0,0 @@
1
- import { retrieveFromLocation } from './retrieveFromLocation.js';
2
- import { retrieveFromPerformance } from './retrieveFromPerformance.js';
3
- import type { LaunchParams } from './types.js';
4
-
5
- /**
6
- * Attempts to retrieve launch parameters using every known way.
7
- */
8
- export function retrieveCurrent(): LaunchParams | null {
9
- // First of all, attempt to retrieve launch parameters from the window.performance as long as
10
- // this way is considered the most stable. Nevertheless, this method can return nothing in case,
11
- // location was changed and then page was reloaded.
12
- try {
13
- return retrieveFromPerformance();
14
- // eslint-disable-next-line no-empty
15
- } catch (e) {
16
- }
17
-
18
- // In case, usage of window.performance was unsuccessful, try to retrieve launch parameters
19
- // from the window.location.
20
- try {
21
- return retrieveFromLocation();
22
- // eslint-disable-next-line no-empty
23
- } catch (e) {
24
- }
25
-
26
- return null;
27
- }
@@ -1,10 +0,0 @@
1
- import { retrieveFromUrl } from './retrieveFromUrl.js';
2
- import type { LaunchParams } from './types.js';
3
-
4
- /**
5
- * Attempts to extract launch parameters from the current window location hash.
6
- * @throws {Error} window.location.hash contains invalid data.
7
- */
8
- export function retrieveFromLocation(): LaunchParams {
9
- return retrieveFromUrl(window.location.href);
10
- }
@@ -1,18 +0,0 @@
1
- import { getFirstNavigationEntry } from './getFirstNavigationEntry.js';
2
- import { retrieveFromUrl } from './retrieveFromUrl.js';
3
- import type { LaunchParams } from './types.js';
4
-
5
- /**
6
- * Attempts to read launch parameters using window.performance data.
7
- * @throws {Error} Unable to get first navigation entry.
8
- * @throws {Error} First navigation entry does not contain hash part.
9
- * @throws {TypeError} Unable to parse value.
10
- */
11
- export function retrieveFromPerformance(): LaunchParams {
12
- const navigationEntry = getFirstNavigationEntry();
13
- if (!navigationEntry) {
14
- throw new Error('Unable to get first navigation entry.');
15
- }
16
-
17
- return retrieveFromUrl(navigationEntry.name);
18
- }
@@ -1,19 +0,0 @@
1
- import { parseLaunchParams } from '~/launch-params/parseLaunchParams.js';
2
- import type { LaunchParams } from '~/launch-params/types.js';
3
-
4
- /**
5
- * Retrieves launch parameters from the specified URL.
6
- * @param url - URL to extract launch parameters from.
7
- */
8
- export function retrieveFromUrl(url: string): LaunchParams {
9
- const queryParams = url.includes('?')
10
- // If URL includes "?", we expect it to possibly contain "#". The reason is TMA allows passing
11
- // start parameter via tgWebAppStartParam **query** parameter. Replacing "#" with "&" we
12
- // expect merging query parameters with hash parameters and creating complete launch
13
- // parameters information.
14
- ? url.replace('#', '&').slice(url.indexOf('?') + 1)
15
- // Otherwise, we expect specifying launch parameters only in the hash part.
16
- : url.slice(url.indexOf('#') + 1);
17
-
18
- return parseLaunchParams(queryParams);
19
- }
@@ -1,30 +0,0 @@
1
- import { computeLaunchData } from './computeLaunchData.js';
2
- import { saveToStorage } from './storage.js';
3
- import type { LaunchData } from './types.js';
4
-
5
- const WINDOW_KEY = 'tmajsLaunchData';
6
-
7
- /**
8
- * Returns launch data information. Function ignores passed options in case, it was already
9
- * called. It caches the last returned value.
10
- */
11
- export function retrieveLaunchData(): LaunchData {
12
- // Return previously cached value.
13
- const cached = (window as any)[WINDOW_KEY];
14
- if (cached) {
15
- return cached;
16
- }
17
-
18
- // Get current launch data.
19
- const launchData = computeLaunchData();
20
-
21
- // To prevent the additional computation of launch data and possible break of the code
22
- // logic, we store this data in the window. Several calls of retrieveLaunchData will surely
23
- // break something.
24
- (window as any)[WINDOW_KEY] = launchData;
25
-
26
- // Save launch parameters in the session storage. We will need them during page reloads.
27
- saveToStorage(launchData.launchParams);
28
-
29
- return launchData;
30
- }
@@ -1,37 +0,0 @@
1
- import { serializeThemeParams } from '~/theme-params/index.js';
2
-
3
- import type { LaunchParams } from './types.js';
4
-
5
- /**
6
- * Converts launch parameters to its initial representation.
7
- * @param value - launch parameters.
8
- */
9
- export function serializeLaunchParams(value: LaunchParams): string {
10
- const {
11
- initDataRaw,
12
- themeParams,
13
- platform,
14
- version,
15
- showSettings,
16
- botInline,
17
- } = value;
18
-
19
- const params = new URLSearchParams();
20
-
21
- if (initDataRaw) {
22
- params.set('tgWebAppData', initDataRaw);
23
- }
24
- params.set('tgWebAppPlatform', platform);
25
- params.set('tgWebAppThemeParams', serializeThemeParams(themeParams));
26
- params.set('tgWebAppVersion', version);
27
-
28
- if (typeof showSettings === 'boolean') {
29
- params.set('tgWebAppShowSettings', showSettings ? '1' : '0');
30
- }
31
-
32
- if (typeof botInline === 'boolean') {
33
- params.set('tgWebAppBotInline', botInline ? '1' : '0');
34
- }
35
-
36
- return params.toString();
37
- }
@@ -1,33 +0,0 @@
1
- import { launchParamsParser } from '~/launch-params/launchParamsParser.js';
2
-
3
- import { serializeLaunchParams } from './serializeLaunchParams.js';
4
- import type { LaunchParams } from './types.js';
5
-
6
- const SESSION_STORAGE_KEY = 'telegram-mini-apps-launch-params';
7
-
8
- /**
9
- * Attempts to extract launch parameters directly from the session storage.
10
- * @returns Launch parameters in case, they were stored before or null, if there is no launch
11
- * parameters key in the session storage.
12
- * @throws {Error} Data stored in the session storage is invalid.
13
- */
14
- export function retrieveFromStorage(): LaunchParams | null {
15
- const raw = sessionStorage.getItem(SESSION_STORAGE_KEY);
16
-
17
- return raw
18
- // We are not handling the error on purpose as long as we are waiting for data stored by
19
- // this session storage key to contain the valid launch parameters.
20
- ? launchParamsParser().parse(raw)
21
- : null;
22
- }
23
-
24
- /**
25
- * Saves specified launch parameters in the session storage.
26
- * @param value - launch params to save.
27
- */
28
- export function saveToStorage(value: LaunchParams): void {
29
- // TODO: We probably don't need serialize here. We used it only to correctly serialize Date
30
- // values which being converted strings. To solve the problem we could improve date parser
31
- // to allows parsing such invalid (Dates, converted to strings) values.
32
- sessionStorage.setItem(SESSION_STORAGE_KEY, serializeLaunchParams(value));
33
- }
@@ -1,62 +0,0 @@
1
- import type { InitDataParsed } from '~/init-data/index.js';
2
- import type { ThemeParamsParsed } from '~/theme-params/index.js';
3
- import type { Platform } from '~/types/index.js';
4
-
5
- /**
6
- * Telegram Mini Apps launch parameters.
7
- * @see https://docs.telegram-mini-apps.com/platform/launch-parameters/common-information
8
- */
9
- export interface LaunchParams {
10
- /**
11
- * True if Mini App is currently launched in inline mode.
12
- */
13
- botInline?: boolean;
14
-
15
- /**
16
- * Current launch init data. Can be missing in case, application was launched via
17
- * KeyboardButton.
18
- */
19
- initData?: InitDataParsed;
20
-
21
- /**
22
- * The same as initData but in initial, raw format.
23
- */
24
- initDataRaw?: string;
25
-
26
- /**
27
- * Current Telegram application identifier.
28
- */
29
- platform: Platform;
30
-
31
- /**
32
- * True if application is required to show the Settings Button.
33
- */
34
- showSettings?: boolean;
35
-
36
- /**
37
- * Start parameter passed in the application link.
38
- */
39
- startParam?: string;
40
-
41
- /**
42
- * Mini App palette settings.
43
- */
44
- themeParams: ThemeParamsParsed;
45
-
46
- /**
47
- * Current Mini Apps version.
48
- */
49
- version: string;
50
- }
51
-
52
- export interface LaunchData {
53
- /**
54
- * Was current application reloaded.
55
- */
56
- isPageReload: boolean;
57
-
58
- /**
59
- * Current application launch parameters.
60
- */
61
- launchParams: LaunchParams;
62
- }
@@ -1,72 +0,0 @@
1
- /**
2
- * Message log level.
3
- */
4
- export type LogLevel = 'log' | 'error' | 'warn';
5
-
6
- export class Logger {
7
- constructor(private readonly prefix: string, private enabled: boolean) {
8
- }
9
-
10
- /**
11
- * Prints message into a console in case, logger is currently enabled.
12
- * @param level - log level.
13
- * @param args - arguments.
14
- */
15
- private print(level: LogLevel, ...args: any[]): void {
16
- if (!this.enabled) {
17
- return;
18
- }
19
-
20
- const now = new Date();
21
- const date = Intl
22
- .DateTimeFormat('en-GB', {
23
- hour: '2-digit',
24
- minute: '2-digit',
25
- second: '2-digit',
26
- fractionalSecondDigits: 3,
27
- timeZone: 'UTC',
28
- })
29
- .format(now);
30
-
31
- // eslint-disable-next-line no-console
32
- console[level](`[${date}]`, this.prefix, ...args);
33
- }
34
-
35
- /**
36
- * Disables the logger.
37
- */
38
- disable() {
39
- this.enabled = false;
40
- }
41
-
42
- /**
43
- * Prints error message into a console.
44
- * @param args
45
- */
46
- error(...args: any[]): void {
47
- this.print('error', ...args);
48
- }
49
-
50
- /**
51
- * Enables the logger.
52
- */
53
- enable() {
54
- this.enabled = true;
55
- }
56
-
57
- /**
58
- * Prints log message into a console.
59
- * @param args
60
- */
61
- log(...args: any[]): void {
62
- this.print('log', ...args);
63
- }
64
-
65
- /**
66
- * Prints warning message into a console.
67
- * @param args
68
- */
69
- warn(...args: any[]): void {
70
- this.print('warn', ...args);
71
- }
72
- }
@@ -1,107 +0,0 @@
1
- import {
2
- afterAll,
3
- afterEach,
4
- beforeAll,
5
- describe,
6
- expect,
7
- it,
8
- vi,
9
- } from 'vitest';
10
-
11
- import { Logger } from '../Logger';
12
-
13
- const now = new Date('2022-11-04T09:09:43.007Z');
14
-
15
- afterEach(() => {
16
- vi.resetAllMocks();
17
- });
18
-
19
- beforeAll(() => {
20
- vi.useFakeTimers().setSystemTime(now);
21
- });
22
-
23
- afterAll(() => {
24
- vi.useRealTimers();
25
- });
26
-
27
- it('should not call console methods if logger is disabled', () => {
28
- const logger = new Logger('', false);
29
- const spy = vi.fn();
30
-
31
- vi.spyOn(console, 'log').mockImplementation(spy);
32
- vi.spyOn(console, 'warn').mockImplementation(spy);
33
- vi.spyOn(console, 'error').mockImplementation(spy);
34
-
35
- logger.log();
36
- logger.warn();
37
- logger.error();
38
- expect(spy).not.toHaveBeenCalled();
39
- });
40
-
41
- describe('enable', () => {
42
- it('should start logging messages into console', () => {
43
- const logger = new Logger('', false);
44
- const spy = vi.fn();
45
-
46
- vi.spyOn(console, 'log').mockImplementation(spy);
47
- logger.log();
48
- expect(spy).not.toHaveBeenCalled();
49
-
50
- logger.enable();
51
- logger.log();
52
- expect(spy).toHaveBeenCalledOnce();
53
- });
54
- });
55
-
56
- describe('disable', () => {
57
- it('should stop logging messages into console', () => {
58
- const logger = new Logger('', true);
59
- const spy = vi.fn();
60
-
61
- vi.spyOn(console, 'log').mockImplementation(spy);
62
- logger.log();
63
- expect(spy).toHaveBeenCalledOnce();
64
-
65
- spy.mockReset();
66
-
67
- logger.disable();
68
- logger.log();
69
- expect(spy).not.toHaveBeenCalled();
70
- });
71
- });
72
-
73
- describe('error', () => {
74
- it('should call console.error()', () => {
75
- const logger = new Logger('My prefix', true);
76
- const spy = vi.fn();
77
- vi.spyOn(console, 'error').mockImplementation(spy);
78
-
79
- logger.error('Test');
80
- expect(spy).toHaveBeenCalledOnce();
81
- expect(spy).toHaveBeenCalledWith('[09:09:43.007]', 'My prefix', 'Test');
82
- });
83
- });
84
-
85
- describe('log', () => {
86
- it('should call console.log()', () => {
87
- const logger = new Logger('My prefix', true);
88
- const spy = vi.fn();
89
- vi.spyOn(console, 'log').mockImplementation(spy);
90
-
91
- logger.log('Test');
92
- expect(spy).toHaveBeenCalledOnce();
93
- expect(spy).toHaveBeenCalledWith('[09:09:43.007]', 'My prefix', 'Test');
94
- });
95
- });
96
-
97
- describe('warn', () => {
98
- it('should call console.warn()', () => {
99
- const logger = new Logger('My prefix', true);
100
- const spy = vi.fn();
101
- vi.spyOn(console, 'warn').mockImplementation(spy);
102
-
103
- logger.warn('Test');
104
- expect(spy).toHaveBeenCalledOnce();
105
- expect(spy).toHaveBeenCalledWith('[09:09:43.007]', 'My prefix', 'Test');
106
- });
107
- });
@@ -1 +0,0 @@
1
- export * from './Logger.js';