@tma.js/sdk 1.4.4 → 1.4.6

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 (282) hide show
  1. package/README.md +2 -2
  2. package/dist/dts/bridge/events/events.d.ts +16 -16
  3. package/dist/dts/bridge/methods/methods.d.ts +27 -27
  4. package/dist/dts/bridge/methods/popup.d.ts +1 -1
  5. package/dist/dts/init-data/types.d.ts +3 -3
  6. package/dist/dts/launch-params/types.d.ts +1 -1
  7. package/dist/dts/theme-params/types.d.ts +1 -1
  8. package/dist/index.cjs +0 -1
  9. package/dist/index.iife.js +0 -1
  10. package/dist/index.mjs +0 -1
  11. package/package.json +2 -3
  12. package/dist/index.cjs.map +0 -1
  13. package/dist/index.iife.js.map +0 -1
  14. package/dist/index.mjs.map +0 -1
  15. package/src/__tests__/globals.ts +0 -39
  16. package/src/back-button/BackButton.ts +0 -90
  17. package/src/back-button/__tests__/BackButton.ts +0 -129
  18. package/src/back-button/index.ts +0 -2
  19. package/src/back-button/types.ts +0 -14
  20. package/src/bridge/__tests__/parseMessage.ts +0 -23
  21. package/src/bridge/__tests__/request.ts +0 -236
  22. package/src/bridge/env/__tests__/hasExternalNotify.ts +0 -15
  23. package/src/bridge/env/__tests__/hasWebviewProxy.ts +0 -15
  24. package/src/bridge/env/__tests__/isIframe.ts +0 -30
  25. package/src/bridge/env/hasExternalNotify.ts +0 -19
  26. package/src/bridge/env/hasWebviewProxy.ts +0 -19
  27. package/src/bridge/env/index.ts +0 -3
  28. package/src/bridge/env/isIframe.ts +0 -11
  29. package/src/bridge/errors/MethodUnsupportedError.ts +0 -13
  30. package/src/bridge/errors/ParameterUnsupportedError.ts +0 -13
  31. package/src/bridge/errors/index.ts +0 -2
  32. package/src/bridge/events/__tests__/createEmitter.ts +0 -143
  33. package/src/bridge/events/__tests__/off.ts +0 -34
  34. package/src/bridge/events/__tests__/on.ts +0 -49
  35. package/src/bridge/events/__tests__/onTelegramEvent.ts +0 -49
  36. package/src/bridge/events/__tests__/once.ts +0 -64
  37. package/src/bridge/events/__tests__/singletonEmitter.ts +0 -22
  38. package/src/bridge/events/__tests__/subscribe.ts +0 -49
  39. package/src/bridge/events/__tests__/unsubscribe.ts +0 -34
  40. package/src/bridge/events/createEmitter.ts +0 -108
  41. package/src/bridge/events/events.ts +0 -170
  42. package/src/bridge/events/index.ts +0 -9
  43. package/src/bridge/events/off.ts +0 -14
  44. package/src/bridge/events/on.ts +0 -19
  45. package/src/bridge/events/onTelegramEvent.ts +0 -81
  46. package/src/bridge/events/once.ts +0 -18
  47. package/src/bridge/events/parsers/__tests__/clipboardTextReceived.ts +0 -21
  48. package/src/bridge/events/parsers/__tests__/invoiceClosed.ts +0 -12
  49. package/src/bridge/events/parsers/__tests__/popupClosed.ts +0 -10
  50. package/src/bridge/events/parsers/__tests__/qrTextReceived.ts +0 -9
  51. package/src/bridge/events/parsers/__tests__/theme-changed.ts +0 -42
  52. package/src/bridge/events/parsers/__tests__/viewportChanged.ts +0 -49
  53. package/src/bridge/events/parsers/clipboardTextReceived.ts +0 -26
  54. package/src/bridge/events/parsers/customMethodInvoked.ts +0 -25
  55. package/src/bridge/events/parsers/index.ts +0 -9
  56. package/src/bridge/events/parsers/invoiceClosed.ts +0 -26
  57. package/src/bridge/events/parsers/phoneRequested.ts +0 -14
  58. package/src/bridge/events/parsers/popupClosed.ts +0 -19
  59. package/src/bridge/events/parsers/qrTextReceived.ts +0 -14
  60. package/src/bridge/events/parsers/theme-changed.ts +0 -58
  61. package/src/bridge/events/parsers/viewportChanged.ts +0 -33
  62. package/src/bridge/events/parsers/writeAccessRequested.ts +0 -14
  63. package/src/bridge/events/singletonEmitter.ts +0 -19
  64. package/src/bridge/events/subscribe.ts +0 -15
  65. package/src/bridge/events/unsubscribe.ts +0 -10
  66. package/src/bridge/index.ts +0 -7
  67. package/src/bridge/invokeCustomMethod.ts +0 -56
  68. package/src/bridge/methods/__tests__/createPostEvent.ts +0 -37
  69. package/src/bridge/methods/__tests__/postEvent.ts +0 -137
  70. package/src/bridge/methods/createPostEvent.ts +0 -40
  71. package/src/bridge/methods/custom-methods.ts +0 -68
  72. package/src/bridge/methods/haptic.ts +0 -52
  73. package/src/bridge/methods/index.ts +0 -6
  74. package/src/bridge/methods/methods.ts +0 -370
  75. package/src/bridge/methods/popup.ts +0 -53
  76. package/src/bridge/methods/postEvent.ts +0 -101
  77. package/src/bridge/parseMessage.ts +0 -28
  78. package/src/bridge/request.ts +0 -176
  79. package/src/classnames/__tests__/classNames.ts +0 -20
  80. package/src/classnames/__tests__/mergeClassNames.ts +0 -21
  81. package/src/classnames/classNames.ts +0 -34
  82. package/src/classnames/index.ts +0 -2
  83. package/src/classnames/mergeClassNames.ts +0 -60
  84. package/src/closing-behavior/ClosingBehavior.ts +0 -64
  85. package/src/closing-behavior/__tests__/ClosingBehavior.ts +0 -86
  86. package/src/closing-behavior/index.ts +0 -2
  87. package/src/closing-behavior/types.ts +0 -12
  88. package/src/cloud-storage/CloudStorage.ts +0 -138
  89. package/src/cloud-storage/index.ts +0 -1
  90. package/src/colors/__tests__/isColorDark.ts +0 -12
  91. package/src/colors/__tests__/isRGB.ts +0 -13
  92. package/src/colors/__tests__/isRGBShort.ts +0 -13
  93. package/src/colors/__tests__/toRGB.ts +0 -23
  94. package/src/colors/index.ts +0 -5
  95. package/src/colors/isColorDark.ts +0 -22
  96. package/src/colors/isRGB.ts +0 -9
  97. package/src/colors/isRGBShort.ts +0 -9
  98. package/src/colors/toRGB.ts +0 -49
  99. package/src/colors/types.ts +0 -9
  100. package/src/css/__tests__/bindMiniAppCSSVars.ts +0 -175
  101. package/src/css/__tests__/bindThemeCSSVars.ts +0 -52
  102. package/src/css/__tests__/bindViewportCSSVars.ts +0 -55
  103. package/src/css/__tests__/setCSSVar.ts +0 -14
  104. package/src/css/bindMiniAppCSSVars.ts +0 -51
  105. package/src/css/bindThemeCSSVars.ts +0 -31
  106. package/src/css/bindViewportCSSVars.ts +0 -36
  107. package/src/css/index.ts +0 -4
  108. package/src/css/setCSSVar.ts +0 -8
  109. package/src/event-emitter/EventEmitter.ts +0 -146
  110. package/src/event-emitter/__tests__/EventEmitter.ts +0 -145
  111. package/src/event-emitter/index.ts +0 -2
  112. package/src/event-emitter/types.ts +0 -60
  113. package/src/globals.ts +0 -38
  114. package/src/haptic-feedback/HapticFeedback.ts +0 -70
  115. package/src/haptic-feedback/__tests__/HapticFeedback.ts +0 -68
  116. package/src/haptic-feedback/index.ts +0 -1
  117. package/src/index.ts +0 -185
  118. package/src/init/catchCustomStyles.ts +0 -17
  119. package/src/init/creators/__tests__/createViewport.ts +0 -96
  120. package/src/init/creators/createBackButton.ts +0 -25
  121. package/src/init/creators/createClosingBehavior.ts +0 -24
  122. package/src/init/creators/createMainButton.ts +0 -51
  123. package/src/init/creators/createMiniApp.ts +0 -48
  124. package/src/init/creators/createRequestIdGenerator.ts +0 -13
  125. package/src/init/creators/createSettingsButton.ts +0 -25
  126. package/src/init/creators/createThemeParams.ts +0 -11
  127. package/src/init/creators/createViewport.ts +0 -94
  128. package/src/init/creators/index.ts +0 -8
  129. package/src/init/css/index.ts +0 -1
  130. package/src/init/css/processCSSVarsOption.ts +0 -55
  131. package/src/init/index.ts +0 -2
  132. package/src/init/init.ts +0 -134
  133. package/src/init/types.ts +0 -94
  134. package/src/init-data/InitData.ts +0 -96
  135. package/src/init-data/__tests__/InitData.ts +0 -98
  136. package/src/init-data/__tests__/chatParser.ts +0 -102
  137. package/src/init-data/__tests__/initDataParser.ts +0 -136
  138. package/src/init-data/__tests__/parseInitData.ts +0 -136
  139. package/src/init-data/__tests__/userParser.ts +0 -96
  140. package/src/init-data/chatParser.ts +0 -19
  141. package/src/init-data/index.ts +0 -6
  142. package/src/init-data/initDataParser.ts +0 -41
  143. package/src/init-data/parseInitData.ts +0 -10
  144. package/src/init-data/types.ts +0 -164
  145. package/src/init-data/userParser.ts +0 -45
  146. package/src/invoice/Invoice.ts +0 -123
  147. package/src/invoice/index.ts +0 -2
  148. package/src/invoice/types.ts +0 -11
  149. package/src/launch-params/__tests__/retrieveFromUrl.ts +0 -19
  150. package/src/launch-params/computeLaunchData.ts +0 -81
  151. package/src/launch-params/computePageReload.ts +0 -13
  152. package/src/launch-params/getFirstNavigationEntry.ts +0 -10
  153. package/src/launch-params/index.ts +0 -13
  154. package/src/launch-params/launchParamsParser.ts +0 -45
  155. package/src/launch-params/parseLaunchParams.ts +0 -10
  156. package/src/launch-params/retrieveCurrent.ts +0 -27
  157. package/src/launch-params/retrieveFromLocation.ts +0 -10
  158. package/src/launch-params/retrieveFromPerformance.ts +0 -18
  159. package/src/launch-params/retrieveFromUrl.ts +0 -19
  160. package/src/launch-params/retrieveLaunchData.ts +0 -30
  161. package/src/launch-params/serializeLaunchParams.ts +0 -37
  162. package/src/launch-params/storage.ts +0 -33
  163. package/src/launch-params/types.ts +0 -62
  164. package/src/logger/Logger.ts +0 -72
  165. package/src/logger/__tests__/Logger.ts +0 -107
  166. package/src/logger/index.ts +0 -1
  167. package/src/main-button/MainButton.ts +0 -239
  168. package/src/main-button/__tests__/MainButton.ts +0 -346
  169. package/src/main-button/index.ts +0 -2
  170. package/src/main-button/types.ts +0 -26
  171. package/src/mini-app/MiniApp.ts +0 -348
  172. package/src/mini-app/__tests__/MiniApp.ts +0 -140
  173. package/src/mini-app/contactParser.ts +0 -29
  174. package/src/mini-app/index.ts +0 -2
  175. package/src/mini-app/types.ts +0 -38
  176. package/src/misc/__tests__/isRecord.ts +0 -21
  177. package/src/misc/index.ts +0 -2
  178. package/src/misc/isRecord.ts +0 -7
  179. package/src/misc/isTMA.ts +0 -13
  180. package/src/navigation/HashNavigator/HashNavigator.ts +0 -220
  181. package/src/navigation/HashNavigator/__tests__/HashNavigator.ts +0 -144
  182. package/src/navigation/HashNavigator/__tests__/drop.ts +0 -42
  183. package/src/navigation/HashNavigator/__tests__/go.ts +0 -9
  184. package/src/navigation/HashNavigator/drop.ts +0 -36
  185. package/src/navigation/HashNavigator/go.ts +0 -28
  186. package/src/navigation/HashNavigator/index.ts +0 -2
  187. package/src/navigation/HashNavigator/types.ts +0 -41
  188. package/src/navigation/Navigator/Navigator.ts +0 -282
  189. package/src/navigation/Navigator/index.ts +0 -2
  190. package/src/navigation/Navigator/types.ts +0 -55
  191. package/src/navigation/ensurePrefix.ts +0 -9
  192. package/src/navigation/getHash.ts +0 -17
  193. package/src/navigation/index.ts +0 -4
  194. package/src/parsing/ArrayValueParser.ts +0 -79
  195. package/src/parsing/ParseError.ts +0 -27
  196. package/src/parsing/ParseSchemaFieldError.ts +0 -21
  197. package/src/parsing/ValueParser.ts +0 -71
  198. package/src/parsing/__tests__/ArrayValueParser.ts +0 -18
  199. package/src/parsing/__tests__/toRecord.ts +0 -10
  200. package/src/parsing/createValueParserGenerator.ts +0 -16
  201. package/src/parsing/index.ts +0 -10
  202. package/src/parsing/parseBySchema.ts +0 -65
  203. package/src/parsing/parsers/__tests__/array.ts +0 -39
  204. package/src/parsing/parsers/__tests__/boolean.ts +0 -31
  205. package/src/parsing/parsers/__tests__/date.ts +0 -25
  206. package/src/parsing/parsers/__tests__/json.ts +0 -80
  207. package/src/parsing/parsers/__tests__/number.ts +0 -23
  208. package/src/parsing/parsers/__tests__/rgb.ts +0 -22
  209. package/src/parsing/parsers/__tests__/searchParams.ts +0 -105
  210. package/src/parsing/parsers/__tests__/string.ts +0 -25
  211. package/src/parsing/parsers/array.ts +0 -9
  212. package/src/parsing/parsers/boolean.ts +0 -22
  213. package/src/parsing/parsers/date.ts +0 -11
  214. package/src/parsing/parsers/index.ts +0 -8
  215. package/src/parsing/parsers/json.ts +0 -17
  216. package/src/parsing/parsers/number.ts +0 -21
  217. package/src/parsing/parsers/rgb.ts +0 -10
  218. package/src/parsing/parsers/searchParams.ts +0 -24
  219. package/src/parsing/parsers/string.ts +0 -12
  220. package/src/parsing/toRecord.ts +0 -27
  221. package/src/parsing/types.ts +0 -32
  222. package/src/parsing/unexpectedTypeError.ts +0 -6
  223. package/src/popup/Popup.ts +0 -91
  224. package/src/popup/__tests__/Popup.ts +0 -130
  225. package/src/popup/__tests__/preparePopupParams.ts +0 -85
  226. package/src/popup/index.ts +0 -2
  227. package/src/popup/preparePopupParams.ts +0 -59
  228. package/src/popup/types.ts +0 -69
  229. package/src/qr-scanner/QRScanner.ts +0 -95
  230. package/src/qr-scanner/index.ts +0 -2
  231. package/src/qr-scanner/types.ts +0 -11
  232. package/src/settings-button/SettingsButton.ts +0 -85
  233. package/src/settings-button/index.ts +0 -2
  234. package/src/settings-button/types.ts +0 -15
  235. package/src/state/State.ts +0 -67
  236. package/src/state/index.ts +0 -2
  237. package/src/state/types.ts +0 -31
  238. package/src/storage.ts +0 -69
  239. package/src/supports/__tests__/supports.ts +0 -123
  240. package/src/supports/createSupportsFunc.ts +0 -18
  241. package/src/supports/createSupportsParamFunc.ts +0 -27
  242. package/src/supports/index.ts +0 -4
  243. package/src/supports/supports.ts +0 -84
  244. package/src/supports/types.ts +0 -1
  245. package/src/theme-params/ThemeParams.ts +0 -131
  246. package/src/theme-params/__tests__/keys.ts +0 -19
  247. package/src/theme-params/__tests__/parseThemeParams.ts +0 -29
  248. package/src/theme-params/__tests__/serializeThemeParams.ts +0 -29
  249. package/src/theme-params/__tests__/themeParamsParser.ts +0 -29
  250. package/src/theme-params/index.ts +0 -6
  251. package/src/theme-params/keys.ts +0 -24
  252. package/src/theme-params/parseThemeParams.ts +0 -10
  253. package/src/theme-params/requestThemeParams.ts +0 -13
  254. package/src/theme-params/serializeThemeParams.ts +0 -20
  255. package/src/theme-params/themeParamsParser.ts +0 -22
  256. package/src/theme-params/types.ts +0 -33
  257. package/src/timeout/TimeoutError.ts +0 -6
  258. package/src/timeout/__tests__/isTimeoutError.ts +0 -9
  259. package/src/timeout/__tests__/withTimeout.ts +0 -28
  260. package/src/timeout/index.ts +0 -4
  261. package/src/timeout/isTimeoutError.ts +0 -9
  262. package/src/timeout/sleep.ts +0 -10
  263. package/src/timeout/withTimeout.ts +0 -24
  264. package/src/types/index.ts +0 -4
  265. package/src/types/methods.ts +0 -18
  266. package/src/types/platform.ts +0 -14
  267. package/src/types/request-id.ts +0 -10
  268. package/src/types/utils.ts +0 -50
  269. package/src/utils/Utils.ts +0 -107
  270. package/src/utils/index.ts +0 -1
  271. package/src/version/__tests__/compareVersions.ts +0 -19
  272. package/src/version/compareVersions.ts +0 -28
  273. package/src/version/index.ts +0 -2
  274. package/src/version/types.ts +0 -4
  275. package/src/viewport/Viewport.ts +0 -171
  276. package/src/viewport/__tests__/isStableViewportPlatform.ts +0 -15
  277. package/src/viewport/__tests__/utils.ts +0 -12
  278. package/src/viewport/index.ts +0 -4
  279. package/src/viewport/isStableViewportPlatform.ts +0 -10
  280. package/src/viewport/requestViewport.ts +0 -23
  281. package/src/viewport/types.ts +0 -23
  282. package/src/viewport/utils.ts +0 -7
package/src/storage.ts DELETED
@@ -1,69 +0,0 @@
1
- import type { HeaderColorKey } from '~/bridge/index.js';
2
- import type { RGB } from '~/colors/index.js';
3
-
4
- // fixme: components-related. Should probably rename
5
-
6
- /**
7
- * Describes storage keys and according values.
8
- */
9
- interface StorageParams {
10
- 'back-button': {
11
- isVisible: boolean
12
- };
13
- 'closing-behavior': {
14
- isConfirmationNeeded: boolean;
15
- };
16
- 'main-button': {
17
- backgroundColor: RGB;
18
- isEnabled: boolean;
19
- isLoaderVisible: boolean;
20
- isVisible: boolean;
21
- text: string;
22
- textColor: RGB;
23
- };
24
- 'settings-button': {
25
- isVisible: boolean
26
- };
27
- viewport: {
28
- height: number;
29
- isExpanded: boolean;
30
- stableHeight: number;
31
- width: number;
32
- };
33
- 'mini-app': {
34
- backgroundColor: RGB;
35
- headerColor: HeaderColorKey | RGB;
36
- };
37
- }
38
-
39
- /**
40
- * Key which could be used to store data in storage.
41
- */
42
- type StorageKey = keyof StorageParams;
43
-
44
- /**
45
- * Formats key which could be used during the communication with the storage.
46
- * @param key - session storage key.
47
- */
48
- function formatKey(key: StorageKey): string {
49
- return `telegram-mini-apps-${key}`;
50
- }
51
-
52
- /**
53
- * Saves value in sessionStorage.
54
- * @param key - storage key.
55
- * @param value - storage value.
56
- */
57
- export function saveStorageValue<K extends StorageKey>(key: K, value: StorageParams[K]): void {
58
- sessionStorage.setItem(formatKey(key), JSON.stringify(value));
59
- }
60
-
61
- /**
62
- * Extracts value from the sessionStorage.
63
- * @param key - storage key.
64
- */
65
- export function getStorageValue<K extends StorageKey>(key: K): StorageParams[K] | null {
66
- const value = sessionStorage.getItem(formatKey(key));
67
-
68
- return value ? JSON.parse(value) : null;
69
- }
@@ -1,123 +0,0 @@
1
- import { expect, it } from 'vitest';
2
-
3
- import type {
4
- MiniAppsMethodName,
5
- MiniAppsMethodVersionedParams,
6
- MiniAppsMethodWithVersionedParams,
7
- } from '../../bridge';
8
- import type { Version } from '../../version';
9
- import { supports } from '../supports';
10
-
11
- type HaveCheckSupportMethodTuple = {
12
- [M in MiniAppsMethodWithVersionedParams]: [M, MiniAppsMethodVersionedParams<M>]
13
- }[MiniAppsMethodWithVersionedParams];
14
-
15
- /**
16
- * Increases specified version by amount of updates.
17
- * @param version - initial version.
18
- * @param amount - count of bumps.
19
- */
20
- function increaseVersion(version: Version, amount: number): string {
21
- const match = version.match(/(.+\.)(\d+)$/);
22
- if (!match) {
23
- throw new Error(`Invalid version: ${version}`);
24
- }
25
- return `${match[1]}${parseInt(match[2], 10) + amount}`;
26
- }
27
-
28
- const tests: [
29
- version: Version | 'any',
30
- methods: (MiniAppsMethodName | HaveCheckSupportMethodTuple)[],
31
- ][] = [
32
- ['any', [
33
- 'iframe_ready',
34
- 'iframe_will_reload',
35
- 'web_app_close',
36
- 'web_app_data_send',
37
- 'web_app_expand',
38
- 'web_app_open_link',
39
- 'web_app_ready',
40
- 'web_app_request_theme',
41
- 'web_app_request_viewport',
42
- 'web_app_setup_main_button',
43
- 'web_app_setup_closing_behavior',
44
- ]],
45
- ['6.1', [
46
- 'web_app_open_tg_link',
47
- 'web_app_open_invoice',
48
- 'web_app_setup_back_button',
49
- 'web_app_set_background_color',
50
- 'web_app_set_header_color',
51
- 'web_app_trigger_haptic_feedback',
52
- ]],
53
- ['6.2', [
54
- 'web_app_open_popup',
55
- ]],
56
- ['6.4', [
57
- 'web_app_read_text_from_clipboard',
58
- 'web_app_close_scan_qr_popup',
59
- 'web_app_close_scan_qr_popup',
60
- ['web_app_open_link', 'try_instant_view'],
61
- ]],
62
- ['6.7', [
63
- 'web_app_switch_inline_query',
64
- ]],
65
- ['6.9', [
66
- 'web_app_invoke_custom_method',
67
- 'web_app_request_write_access',
68
- 'web_app_request_phone',
69
- ['web_app_set_header_color', 'color'],
70
- ]],
71
- ['6.10', [
72
- 'web_app_setup_settings_button',
73
- ]],
74
- ];
75
-
76
- tests.forEach(([version, methods]) => {
77
- if (version === 'any') {
78
- methods.forEach((methodOrTuple) => {
79
- if (Array.isArray(methodOrTuple)) {
80
- const [method, param] = methodOrTuple;
81
- it(`should return true in case, passed method is "${method}", parameter is "${param}" and version is 1`, () => {
82
- expect(supports(method, param, '1')).toBe(true);
83
- });
84
-
85
- return;
86
- }
87
-
88
- const method = methodOrTuple;
89
- it(`should return true in case, passed method is "${method}" and version is 1`, () => {
90
- expect(supports(method, '1')).toBe(true);
91
- });
92
- });
93
- return;
94
- }
95
-
96
- methods.forEach((methodOrTuple) => {
97
- if (Array.isArray(methodOrTuple)) {
98
- const [method, param] = methodOrTuple;
99
-
100
- it(`should return true in case, passed method is "${method}", parameter is "${param}" and version is ${version} or higher`, () => {
101
- expect(supports(method, param, version)).toBe(true);
102
- expect(supports(method, param, increaseVersion(version, 1))).toBe(true);
103
- });
104
-
105
- it(`should return false in case, passed method is "${method}", parameter is "${param}" and version is lower than ${version}`, () => {
106
- expect(supports(method, param, increaseVersion(version, -1))).toBe(false);
107
- });
108
-
109
- return;
110
- }
111
-
112
- const method = methodOrTuple;
113
-
114
- it(`should return true in case, passed method is "${method}" and version is ${version} or higher`, () => {
115
- expect(supports(method, version)).toBe(true);
116
- expect(supports(method, increaseVersion(version, 1))).toBe(true);
117
- });
118
-
119
- it(`should return false in case, passed method is "${method}" and version is lower than ${version}`, () => {
120
- expect(supports(method, increaseVersion(version, -1))).toBe(false);
121
- });
122
- });
123
- });
@@ -1,18 +0,0 @@
1
- import type { MiniAppsMethodName } from '~/bridge/index.js';
2
- import type { Version } from '~/version/index.js';
3
-
4
- import { supports } from './supports.js';
5
- import type { SupportsFunc } from './types.js';
6
-
7
- /**
8
- * Returns function, which accepts predefined method name and checks if it is supported
9
- * via passed schema and version.
10
- * @param schema - object which contains methods names and TWA method as a dependency.
11
- * @param version - platform version.
12
- */
13
- export function createSupportsFunc<M extends string>(
14
- version: Version,
15
- schema: Record<M, MiniAppsMethodName>,
16
- ): SupportsFunc<M> {
17
- return (method) => supports(schema[method], version);
18
- }
@@ -1,27 +0,0 @@
1
- import type { MiniAppsMethodVersionedParams, MiniAppsMethodWithVersionedParams } from '~/bridge/index.js';
2
- import type { Version } from '~/version/index.js';
3
-
4
- import { supports } from './supports.js';
5
- import type { SupportsFunc } from './types.js';
6
-
7
- type HasCheckSupportMethodTuple = {
8
- [M in MiniAppsMethodWithVersionedParams]: [M, MiniAppsMethodVersionedParams<M>]
9
- }[MiniAppsMethodWithVersionedParams];
10
-
11
- /**
12
- * Returns function, which accepts predefined method name and checks if it is supported
13
- * via passed schema and version.
14
- * @param schema - object which contains methods names and TWA methods with specified parameter
15
- * as a dependency.
16
- * @param version - platform version.
17
- */
18
- export function createSupportsParamFunc<P extends string>(
19
- version: Version,
20
- schema: Record<P, HasCheckSupportMethodTuple>,
21
- ): SupportsFunc<P> {
22
- return (method) => {
23
- const [tmaMethod, param] = schema[method];
24
-
25
- return supports(tmaMethod, param, version);
26
- };
27
- }
@@ -1,4 +0,0 @@
1
- export * from './createSupportsFunc.js';
2
- export * from './createSupportsParamFunc.js';
3
- export * from './supports.js';
4
- export * from './types.js';
@@ -1,84 +0,0 @@
1
- import {
2
- compareVersions,
3
- type Version,
4
- } from '~/version/index.js';
5
- import type {
6
- MiniAppsMethodName,
7
- MiniAppsMethodVersionedParams,
8
- MiniAppsMethodWithVersionedParams,
9
- } from '~/bridge/methods/index.js';
10
-
11
- /**
12
- * Returns true if "a" version is less than or equal to "b" version.
13
- * @param a
14
- * @param b
15
- */
16
- function versionLessOrEqual(a: Version, b: Version): boolean {
17
- return compareVersions(a, b) <= 0;
18
- }
19
-
20
- /**
21
- * Returns true in case, passed parameter in specified method is supported.
22
- * @param method - method name
23
- * @param param - method parameter
24
- * @param inVersion - platform version.
25
- */
26
- export function supports<M extends MiniAppsMethodWithVersionedParams>(
27
- method: M,
28
- param: MiniAppsMethodVersionedParams<M>,
29
- inVersion: Version,
30
- ): boolean;
31
-
32
- /**
33
- * Returns true in case, specified method is supported in passed version.
34
- * @param method - method name.
35
- * @param inVersion - platform version.
36
- */
37
- export function supports(method: MiniAppsMethodName, inVersion: Version): boolean;
38
-
39
- export function supports(
40
- method: MiniAppsMethodName,
41
- paramOrVersion: Version | string,
42
- inVersion?: string,
43
- ): boolean {
44
- // Method name, parameter, target version.
45
- if (typeof inVersion === 'string') {
46
- if (method === 'web_app_open_link') {
47
- if (paramOrVersion === 'try_instant_view') {
48
- return versionLessOrEqual('6.4', inVersion);
49
- }
50
- }
51
-
52
- if (method === 'web_app_set_header_color') {
53
- if (paramOrVersion === 'color') {
54
- return versionLessOrEqual('6.9', inVersion);
55
- }
56
- }
57
- }
58
-
59
- switch (method) {
60
- case 'web_app_open_tg_link':
61
- case 'web_app_open_invoice':
62
- case 'web_app_setup_back_button':
63
- case 'web_app_set_background_color':
64
- case 'web_app_set_header_color':
65
- case 'web_app_trigger_haptic_feedback':
66
- return versionLessOrEqual('6.1', paramOrVersion);
67
- case 'web_app_open_popup':
68
- return versionLessOrEqual('6.2', paramOrVersion);
69
- case 'web_app_close_scan_qr_popup':
70
- case 'web_app_open_scan_qr_popup':
71
- case 'web_app_read_text_from_clipboard':
72
- return versionLessOrEqual('6.4', paramOrVersion);
73
- case 'web_app_switch_inline_query':
74
- return versionLessOrEqual('6.7', paramOrVersion);
75
- case 'web_app_invoke_custom_method':
76
- case 'web_app_request_write_access':
77
- case 'web_app_request_phone':
78
- return versionLessOrEqual('6.9', paramOrVersion);
79
- case 'web_app_setup_settings_button':
80
- return versionLessOrEqual('6.10', paramOrVersion);
81
- default:
82
- return true;
83
- }
84
- }
@@ -1 +0,0 @@
1
- export type SupportsFunc<M extends string> = (method: M) => boolean;
@@ -1,131 +0,0 @@
1
- import { on } from '~/bridge/index.js';
2
- import { isColorDark, type RGB } from '~/colors/index.js';
3
- import { EventEmitter } from '~/event-emitter/index.js';
4
- import { State } from '~/state/index.js';
5
-
6
- import { parseThemeParams } from './parseThemeParams.js';
7
- import type {
8
- ThemeParamsEvents,
9
- ThemeParamsParsed,
10
- ThemeParamsState,
11
- } from './types.js';
12
-
13
- export class ThemeParams {
14
- private readonly ee = new EventEmitter<ThemeParamsEvents>();
15
-
16
- private readonly state: State<ThemeParamsState>;
17
-
18
- constructor(params: ThemeParamsParsed) {
19
- this.state = new State(params, this.ee);
20
- }
21
-
22
- /**
23
- * @since v6.10
24
- */
25
- get accentTextColor(): RGB | undefined {
26
- return this.get('accentTextColor');
27
- }
28
-
29
- get backgroundColor(): RGB | undefined {
30
- return this.get('backgroundColor');
31
- }
32
-
33
- get buttonColor(): RGB | undefined {
34
- return this.get('buttonColor');
35
- }
36
-
37
- get buttonTextColor(): RGB | undefined {
38
- return this.get('buttonTextColor');
39
- }
40
-
41
- get destructiveTextColor(): RGB | undefined {
42
- return this.get('destructiveTextColor');
43
- }
44
-
45
- /**
46
- * Retrieves palette color value by its name.
47
- * @param key - palette key name.
48
- */
49
- get(key: Extract<keyof ThemeParamsParsed, string>): RGB | undefined {
50
- return this.state.get(key);
51
- }
52
-
53
- /**
54
- * Returns the copy of the internal state of the current component instance.
55
- */
56
- getState(): ThemeParamsParsed {
57
- return this.state.clone();
58
- }
59
-
60
- /**
61
- * @since v6.10
62
- */
63
- get headerBackgroundColor(): RGB | undefined {
64
- return this.get('headerBackgroundColor');
65
- }
66
-
67
- get hintColor(): RGB | undefined {
68
- return this.get('hintColor');
69
- }
70
-
71
- /**
72
- * Returns true in case, current color scheme is recognized as dark. This
73
- * value is calculated according to theme background color.
74
- */
75
- get isDark(): boolean {
76
- return !this.backgroundColor || isColorDark(this.backgroundColor);
77
- }
78
-
79
- get linkColor(): RGB | undefined {
80
- return this.get('linkColor');
81
- }
82
-
83
- /**
84
- * Adds new event listener.
85
- */
86
- on = this.ee.on.bind(this.ee);
87
-
88
- /**
89
- * Removes event listener.
90
- */
91
- off = this.ee.off.bind(this.ee);
92
-
93
- get secondaryBackgroundColor(): RGB | undefined {
94
- return this.get('secondaryBackgroundColor');
95
- }
96
-
97
- /**
98
- * @since v6.10
99
- */
100
- get sectionBackgroundColor(): RGB | undefined {
101
- return this.get('sectionBackgroundColor');
102
- }
103
-
104
- /**
105
- * @since v6.10
106
- */
107
- get sectionHeaderTextColor(): RGB | undefined {
108
- return this.get('sectionHeaderTextColor');
109
- }
110
-
111
- /**
112
- * Starts listening to theme changes and applies them.
113
- * @returns Function to stop listening.
114
- */
115
- listen() {
116
- return on('theme_changed', (event) => {
117
- this.state.set(parseThemeParams(event.theme_params));
118
- });
119
- }
120
-
121
- /**
122
- * @since v6.10
123
- */
124
- get subtitleTextColor(): RGB | undefined {
125
- return this.get('subtitleTextColor');
126
- }
127
-
128
- get textColor(): RGB | undefined {
129
- return this.get('textColor');
130
- }
131
- }
@@ -1,19 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
-
3
- import { keyToExternal, keyToLocal } from '../keys';
4
-
5
- describe('keyToLocal', () => {
6
- it('should replace "^bg" and "_bg" with "background" and "_background". Then replace _[a-z] with [A-Z]', () => {
7
- expect(keyToLocal('bg_color')).toBe('backgroundColor');
8
- expect(keyToLocal('secondary_bg_color')).toBe('secondaryBackgroundColor');
9
- expect(keyToLocal('text_color')).toBe('textColor');
10
- });
11
- });
12
-
13
- describe('keyToExternal', () => {
14
- it('should replace [A-Z] with _[a-z]. Then replace "background" and "_background" with "^bg" and "_bg"', () => {
15
- expect(keyToExternal('backgroundColor')).toBe('bg_color');
16
- expect(keyToExternal('secondaryBackgroundColor')).toBe('secondary_bg_color');
17
- expect(keyToExternal('textColor')).toBe('text_color');
18
- });
19
- });
@@ -1,29 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
-
3
- import { parseThemeParams } from '../parseThemeParams';
4
-
5
- [
6
- ['accent_text_color', 'accentTextColor'],
7
- ['bg_color', 'backgroundColor'],
8
- ['button_color', 'buttonColor'],
9
- ['button_text_color', 'buttonTextColor'],
10
- ['destructive_text_color', 'destructiveTextColor'],
11
- ['header_bg_color', 'headerBackgroundColor'],
12
- ['hint_color', 'hintColor'],
13
- ['link_color', 'linkColor'],
14
- ['secondary_bg_color', 'secondaryBackgroundColor'],
15
- ['section_header_text_color', 'sectionHeaderTextColor'],
16
- ['section_bg_color', 'sectionBackgroundColor'],
17
- ['subtitle_text_color', 'subtitleTextColor'],
18
- ['text_color', 'textColor'],
19
- ].forEach(([from, to]) => {
20
- describe(to, () => {
21
- it(`should throw if "${from}" property contains not a string in format "#RRGGBB"`, () => {
22
- expect(() => parseThemeParams({ [from]: 999 })).toThrow();
23
- });
24
-
25
- it(`should map to "${to}" property parsing it as string in "#RRGGBB" format`, () => {
26
- expect(parseThemeParams({ [from]: '#aabbcc' })).toStrictEqual({ [to]: '#aabbcc' });
27
- });
28
- });
29
- });
@@ -1,29 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
-
3
- import { serializeThemeParams } from '../serializeThemeParams';
4
-
5
- [
6
- ['accent_text_color', 'accentTextColor'],
7
- ['bg_color', 'backgroundColor'],
8
- ['button_color', 'buttonColor'],
9
- ['button_text_color', 'buttonTextColor'],
10
- ['destructive_text_color', 'destructiveTextColor'],
11
- ['header_bg_color', 'headerBackgroundColor'],
12
- ['hint_color', 'hintColor'],
13
- ['link_color', 'linkColor'],
14
- ['secondary_bg_color', 'secondaryBackgroundColor'],
15
- ['section_header_text_color', 'sectionHeaderTextColor'],
16
- ['section_bg_color', 'sectionBackgroundColor'],
17
- ['subtitle_text_color', 'subtitleTextColor'],
18
- ['text_color', 'textColor'],
19
- ].forEach(([to, from]) => {
20
- describe(from, () => {
21
- it(`should omit the "${to}" property in case this property is missing`, () => {
22
- expect(serializeThemeParams({})).not.toMatch(`"${to}"`);
23
- });
24
-
25
- it(`should map this property to "${to}" property`, () => {
26
- expect(serializeThemeParams({ [from]: '#aabbcc' })).toBe(`{"${to}":"#aabbcc"}`);
27
- });
28
- });
29
- });
@@ -1,29 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
-
3
- import { themeParamsParser } from '../themeParamsParser';
4
-
5
- [
6
- ['accent_text_color', 'accentTextColor'],
7
- ['bg_color', 'backgroundColor'],
8
- ['button_color', 'buttonColor'],
9
- ['button_text_color', 'buttonTextColor'],
10
- ['destructive_text_color', 'destructiveTextColor'],
11
- ['header_bg_color', 'headerBackgroundColor'],
12
- ['hint_color', 'hintColor'],
13
- ['link_color', 'linkColor'],
14
- ['secondary_bg_color', 'secondaryBackgroundColor'],
15
- ['section_header_text_color', 'sectionHeaderTextColor'],
16
- ['section_bg_color', 'sectionBackgroundColor'],
17
- ['subtitle_text_color', 'subtitleTextColor'],
18
- ['text_color', 'textColor'],
19
- ].forEach(([from, to]) => {
20
- describe(to, () => {
21
- it(`should throw if "${from}" property contains not a string in format "#RRGGBB"`, () => {
22
- expect(() => themeParamsParser().parse({ [from]: 999 })).toThrow();
23
- });
24
-
25
- it(`should map to "${to}" property parsing it as string in "#RRGGBB" format`, () => {
26
- expect(themeParamsParser().parse({ [from]: '#aabbcc' })).toStrictEqual({ [to]: '#aabbcc' });
27
- });
28
- });
29
- });
@@ -1,6 +0,0 @@
1
- export * from './parseThemeParams.js';
2
- export * from './requestThemeParams.js';
3
- export * from './serializeThemeParams.js';
4
- export * from './ThemeParams.js';
5
- export * from './themeParamsParser.js';
6
- export * from './types.js';
@@ -1,24 +0,0 @@
1
- /**
2
- * Converts palette key from Telegram application to representation used by the package.
3
- * @param key - palette key.
4
- */
5
- export function keyToLocal(key: string): string {
6
- return key
7
- // Replace all "background" strings to "bg".
8
- .replace(/(^|_)bg/, (_, prefix) => `${prefix}background`)
9
- // Convert camel case to snake case.
10
- .replace(/_([a-z])/g, (_match, letter) => letter.toUpperCase());
11
- }
12
-
13
- /**
14
- * Converts palette key from local representation to representation sent from the Telegram
15
- * application.
16
- * @param key - palette key.
17
- */
18
- export function keyToExternal(key: string): string {
19
- return key
20
- // Convert camel case to snake case.
21
- .replace(/[A-Z]/g, (match) => `_${match.toLowerCase()}`)
22
- // Replace all "background" strings to "bg".
23
- .replace(/(^|_)background/, (_, prefix) => `${prefix}bg`);
24
- }
@@ -1,10 +0,0 @@
1
- import { themeParamsParser } from './themeParamsParser.js';
2
- import type { ThemeParamsParsed } from './types.js';
3
-
4
- /**
5
- * Parses incoming value as theme parameters.
6
- * @param value - value to parse.
7
- */
8
- export function parseThemeParams(value: unknown): ThemeParamsParsed {
9
- return themeParamsParser().parse(value);
10
- }
@@ -1,13 +0,0 @@
1
- import { request, type RequestOptions } from '~/bridge/index.js';
2
-
3
- import { parseThemeParams } from './parseThemeParams.js';
4
- import type { ThemeParamsParsed } from './types.js';
5
-
6
- /**
7
- * Requests current theme parameters from the Telegram application.
8
- * @param options - request options.
9
- */
10
- export function requestThemeParams(options: RequestOptions = {}): Promise<ThemeParamsParsed> {
11
- return request('web_app_request_theme', 'theme_changed', options)
12
- .then(parseThemeParams);
13
- }
@@ -1,20 +0,0 @@
1
- import type { RGB } from '~/colors/index.js';
2
-
3
- import { keyToExternal } from './keys.js';
4
- import type { ThemeParamsParsed } from './types.js';
5
-
6
- /**
7
- * Serializes theme parameters to representation sent from the Telegram application.
8
- */
9
- export function serializeThemeParams(themeParams: ThemeParamsParsed): string {
10
- return JSON.stringify(
11
- Object
12
- .entries(themeParams)
13
- .reduce<Record<string, RGB>>((acc, [key, value]) => {
14
- if (value) {
15
- acc[keyToExternal(key)] = value;
16
- }
17
- return acc;
18
- }, {}),
19
- );
20
- }
@@ -1,22 +0,0 @@
1
- import {
2
- createValueParserGenerator,
3
- rgb,
4
- toRecord,
5
- } from '~/parsing/index.js';
6
-
7
- import { keyToLocal } from './keys.js';
8
- import type { ThemeParamsParsed } from './types.js';
9
-
10
- export const themeParamsParser = createValueParserGenerator<ThemeParamsParsed>(
11
- (value) => {
12
- const rgbOptional = rgb().optional();
13
-
14
- return Object
15
- .entries(toRecord(value))
16
- .reduce<ThemeParamsParsed>((acc, [k, v]) => {
17
- acc[keyToLocal(k)] = rgbOptional.parse(v);
18
- return acc;
19
- }, {});
20
- },
21
- 'ThemeParams',
22
- );