@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
@@ -1,130 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
-
3
- // import {emitDesktopEvent, mockDesktop} from '../../../tests';
4
- // import { Popup } from './Popup';
5
- // import {Bridge} from '@tma.js/bridge';
6
- // import {PopupParams} from './types';
7
- // import {preparePopupParams} from './utils';
8
-
9
- // beforeAll(mockDesktop);
10
-
11
- describe('components', () => {
12
- describe('Popup', () => {
13
- it('should be fine', () => {
14
- expect(true).toBe(true);
15
- });
16
- // it('isOpened', () => {
17
- // it('should emit event "openChange" with specified value in setter', () => {
18
- // const popup = new Popup('7');
19
- // const spy = vi.spyOn((popup as any).ee, 'emit');
20
- // (popup as any).isOpened = true;
21
- // expect(spy).lastCalledWith('openChange', true);
22
- //
23
- // (popup as any).isOpened = false;
24
- // expect(spy).lastCalledWith('openChange', false);
25
- // });
26
- // });
27
- //
28
- // it('show', () => {
29
- // it('should throw an error in case, current version is less than 6.2', () => {
30
- // expect(() => new Popup('6.1').show({message: 'Hey!'})).toThrow();
31
- // expect(() => new Popup('6.2').show({message: 'Hey!'})).not.toThrow();
32
- // });
33
- //
34
- // it('should call bridge event "web_app_open_popup" with prepared parameters', () => {
35
- // const bridge = new Bridge();
36
- // const spy = vi.spyOn(bridge, 'postEvent');
37
- // const popup = new Popup('7', {bridge});
38
- // const params: PopupParams = {message: 'Message'};
39
- // const prepared = preparePopupParams(params);
40
- // popup.show(params);
41
- // expect(spy).toHaveBeenCalledWith('web_app_open_popup', prepared);
42
- // });
43
- //
44
- // it('should emit event "open" with prepared params', () => {
45
- // const popup = new Popup('7');
46
- // const spy = vi.spyOn((popup as any).ee, 'emit');
47
- // const params: PopupParams = {message: 'Message'};
48
- // const prepared = preparePopupParams(params);
49
- // popup.show(params);
50
- // expect(spy).toHaveBeenCalledWith('open', prepared);
51
- // });
52
- //
53
- // it('should set "isOpened" property to true', () => {
54
- // const popup = new Popup('7');
55
- // expect(popup.isOpened).toBe(false);
56
- // popup.show({message: 'Message'});
57
- // expect(popup.isOpened).toBe(true);
58
- // });
59
- //
60
- // it('should set "isOpened" property to false when promise is resolved', async () => {
61
- // const popup = new Popup('7');
62
- // const promise = popup.show({message: 'Message'});
63
- //
64
- // emitDesktopEvent('popup_closed', {button_id: null});
65
- // await promise;
66
- // expect(popup.isOpened).toBe(false);
67
- // });
68
- //
69
- // it('should set "isOpened" property to true', () => {
70
- // const popup = new Popup('7');
71
- // popup.show({message: 'Message'});
72
- // expect(popup.isOpened).toBe(true);
73
- // });
74
- //
75
- // it('should emit close event with specified button_id when promise is resolved',
76
- // async () => {
77
- // const popup = new Popup('7');
78
- // const spy = vi.spyOn((popup as any).ee, 'emit');
79
- // const promise = popup.show({message: 'Message'});
80
- // emitDesktopEvent('popup_closed', {button_id: 'Hey!'});
81
- // await promise;
82
- // expect(spy).lastCalledWith('close', 'Hey!');
83
- // });
84
- // });
85
- //
86
- // it('showAlert', () => {
87
- // it('should call "show" with passed message and buttons = [{type: "close"}]', () => {
88
- // const popup = new Popup('7');
89
- // const spy = vi.spyOn(popup, 'show');
90
- //
91
- // popup.showAlert('Message');
92
- //
93
- // expect(spy).toHaveBeenCalledWith({
94
- // message: 'Message',
95
- // buttons: [{type: 'close'}],
96
- // });
97
- // });
98
- // });
99
- //
100
- // it('showConfirm', () => {
101
- // test(
102
- // 'should call "show" with passed message and ' +
103
- // 'buttons = [{type: "ok", id: "ok"}, {id: "cancel", type: "cancel"}]',
104
- // () => {
105
- // const popup = new Popup('7');
106
- // const spy = vi.spyOn(popup, 'show');
107
- //
108
- // popup.showConfirm('Message');
109
- //
110
- // expect(spy).toHaveBeenCalledWith({
111
- // message: 'Message',
112
- // buttons: [{type: 'ok', id: 'ok'}, {id: 'cancel', type: 'cancel'}],
113
- // });
114
- // },
115
- // );
116
- //
117
- // it('should resolve true in case, user pressed button with id "ok"', async () => {
118
- // const promise = new Popup('7').showConfirm('Message');
119
- // emitDesktopEvent('popup_closed', {button_id: 'ok'});
120
- // expect(await promise).toBe(true);
121
- // });
122
- //
123
- // it('should resolve false in case, user pressed any other button', async () => {
124
- // const promise = new Popup('7').showConfirm('Message');
125
- // emitDesktopEvent('popup_closed', {button_id: 'cancel'});
126
- // await expect(promise).resolves.toBe(false);
127
- // });
128
- // });
129
- });
130
- });
@@ -1,85 +0,0 @@
1
- import { expect, it } from 'vitest';
2
-
3
- import { preparePopupParams } from '../preparePopupParams';
4
-
5
- /**
6
- * Creates test text.
7
- * @param length - text length.
8
- */
9
- function createText(length: number): string {
10
- return new Array(length).fill('a').join('');
11
- }
12
-
13
- it('should throw an error in case, title length is more than 64 symbols', () => {
14
- expect(() => preparePopupParams({
15
- title: createText(65),
16
- message: 'Hey!',
17
- })).toThrow(/^Title has incorrect size/);
18
-
19
- expect(() => preparePopupParams({
20
- title: createText(64),
21
- message: 'Hey!',
22
- })).not.toThrow();
23
- });
24
-
25
- it('should throw an error in case, message length is zero or more than 256 symbols', () => {
26
- expect(() => preparePopupParams({
27
- message: '',
28
- })).toThrow(/^Message has incorrect size/);
29
-
30
- expect(() => preparePopupParams({
31
- message: createText(257),
32
- })).toThrow(/^Message has incorrect size/);
33
-
34
- expect(() => preparePopupParams({
35
- message: createText(256),
36
- })).not.toThrow();
37
- });
38
-
39
- it('should throw an error in case, buttons count is more than 3', () => {
40
- expect(() => preparePopupParams({
41
- message: 'a',
42
- buttons: new Array(4).fill({ type: 'close' }),
43
- })).toThrow(/^Buttons have incorrect size/);
44
-
45
- expect(() => preparePopupParams({
46
- message: 'a',
47
- buttons: new Array(3).fill({ type: 'close' }),
48
- })).not.toThrow();
49
- });
50
-
51
- it('should append button type "close" in case, buttons array is empty', () => {
52
- expect(preparePopupParams({ message: 'a' }).buttons).toStrictEqual([{
53
- type: 'close',
54
- id: '',
55
- }]);
56
- });
57
-
58
- it('should throw an error in case, some button text length size is zero or more than 64 symbols', () => {
59
- expect(() => preparePopupParams({
60
- message: 'A',
61
- buttons: [{ type: 'default', text: createText(65) }],
62
- })).toThrowError(/^Button text with type/);
63
-
64
- expect(() => preparePopupParams({
65
- message: 'A',
66
- buttons: [{ type: 'default', text: '' }],
67
- })).toThrowError(/^Button text with type/);
68
-
69
- expect(() => preparePopupParams({
70
- message: 'A',
71
- buttons: [{ type: 'default', text: createText(64) }],
72
- })).not.toThrow();
73
- });
74
-
75
- it('should fulfill all optional popup parameters', () => {
76
- expect(preparePopupParams({
77
- message: 'Message',
78
- buttons: [{ type: 'default', text: 'Wow!' }, { type: 'close' }],
79
- }))
80
- .toStrictEqual({
81
- title: '',
82
- message: 'Message',
83
- buttons: [{ id: '', type: 'default', text: 'Wow!' }, { type: 'close', id: '' }],
84
- });
85
- });
@@ -1,2 +0,0 @@
1
- export * from './Popup.js';
2
- export * from './types.js';
@@ -1,59 +0,0 @@
1
- import type { PopupButton, PopupParams as BridgePopupParams } from '~/bridge/index.js';
2
-
3
- import type { OpenPopupOptions } from './types.js';
4
-
5
- /**
6
- * Prepares popup parameters before sending them to native app.
7
- * @param params - popup parameters.
8
- */
9
- export function preparePopupParams(params: OpenPopupOptions): BridgePopupParams {
10
- const message = params.message.trim();
11
- const title = (params.title || '').trim();
12
- const buttons = params.buttons || [];
13
- let preparedButtons: PopupButton[];
14
-
15
- // Check title.
16
- if (title.length > 64) {
17
- throw new Error(`Title has incorrect size: ${title.length}`);
18
- }
19
-
20
- // Check message.
21
- if (message.length === 0 || message.length > 256) {
22
- throw new Error(`Message has incorrect size: ${message.length}`);
23
- }
24
-
25
- // Check buttons.
26
- if (buttons.length > 3) {
27
- throw new Error(`Buttons have incorrect size: ${buttons.length}`);
28
- }
29
-
30
- // Append button in case, there are no buttons passed.
31
- if (buttons.length === 0) {
32
- preparedButtons = [{ type: 'close', id: '' }];
33
- } else {
34
- // Otherwise, check all the buttons.
35
- preparedButtons = buttons.map((b) => {
36
- const { id = '' } = b;
37
-
38
- // Check button ID.
39
- if (id.length > 64) {
40
- throw new Error(`Button ID has incorrect size: ${id}`);
41
- }
42
-
43
- if (b.type === undefined || b.type === 'default' || b.type === 'destructive') {
44
- const text = b.text.trim();
45
-
46
- if (text.length === 0 || text.length > 64) {
47
- const type = b.type || 'default';
48
-
49
- throw new Error(`Button text with type "${type}" has incorrect size: ${b.text.length}`);
50
- }
51
-
52
- return { ...b, text, id };
53
- }
54
-
55
- return { ...b, id };
56
- });
57
- }
58
- return { title, message, buttons: preparedButtons };
59
- }
@@ -1,69 +0,0 @@
1
- import type { StateEvents } from '~/state/index.js';
2
-
3
- export interface PopupState {
4
- isOpened: boolean;
5
- }
6
-
7
- export type PopupEvents = StateEvents<PopupState>;
8
-
9
- export type PopupEventName = keyof PopupEvents;
10
-
11
- export type PopupEventListener<E extends PopupEventName> = PopupEvents[E];
12
-
13
- /**
14
- * This object describes the native popup.
15
- * @see https://core.telegram.org/bots/webapps#popupparams
16
- */
17
- export interface OpenPopupOptions {
18
- /**
19
- * The text to be displayed in the popup title, 0-64 characters.
20
- * @default ""
21
- */
22
- title?: string;
23
-
24
- /**
25
- * The message to be displayed in the body of the popup, 1-256 characters.
26
- */
27
- message: string;
28
-
29
- /**
30
- * List of buttons to be displayed in the popup, 1-3 buttons.
31
- * @default [{type: 'close'}]
32
- */
33
- buttons?: OpenPopupOptionsButton[];
34
- }
35
-
36
- /**
37
- * This object describes the native popup button.
38
- * @see https://core.telegram.org/bots/webapps#popupbutton
39
- */
40
- export type OpenPopupOptionsButton = {
41
- /**
42
- * Identifier of the button, 0-64 characters.
43
- * @default ""
44
- */
45
- id?: string;
46
- } & (
47
- {
48
- /**
49
- * Type of the button:
50
- * - `default`, a button with the default style;
51
- * - `destructive`, a button with a style that indicates a destructive
52
- * action (e.g. "Remove", "Delete", etc.).
53
- *
54
- * @default "default"
55
- */
56
- type?: 'default' | 'destructive';
57
- /**
58
- * The text to be displayed on the button, 0-64 characters.
59
- */
60
- text: string;
61
- } | {
62
- /**
63
- * Type of the button:
64
- * - `ok`, a button with the localized text "OK";
65
- * - `close`, a button with the localized text "Close";
66
- * - `cancel`, a button with the localized text "Cancel".
67
- */
68
- type: 'ok' | 'close' | 'cancel';
69
- });
@@ -1,95 +0,0 @@
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';
13
-
14
- import type { QRScannerEvents, QRScannerState } from './types.js';
15
-
16
- /**
17
- * Provides QR scanner functionality.
18
- */
19
- export class QRScanner {
20
- private readonly ee = new EventEmitter<QRScannerEvents>();
21
-
22
- private readonly state: State<QRScannerState>;
23
-
24
- constructor(
25
- version: Version,
26
- private readonly postEvent: PostEvent = defaultPostEvent,
27
- ) {
28
- this.state = new State({ isOpened: false }, this.ee);
29
- this.supports = createSupportsFunc(version, {
30
- close: 'web_app_close_scan_qr_popup',
31
- open: 'web_app_open_scan_qr_popup',
32
- });
33
- }
34
-
35
- /**
36
- * Closes scanner.
37
- */
38
- close(): void {
39
- this.postEvent('web_app_close_scan_qr_popup');
40
- this.isOpened = false;
41
- }
42
-
43
- private set isOpened(value) {
44
- this.state.set('isOpened', value);
45
- }
46
-
47
- /**
48
- * Returns true in case, QR scanner is currently opened.
49
- */
50
- get isOpened(): boolean {
51
- return this.state.get('isOpened');
52
- }
53
-
54
- /**
55
- * Opens scanner with specified title shown to user. Method returns promise
56
- * with scanned QR content in case, it was scanned. It will contain null in
57
- * case, scanner was closed.
58
- * @param text - title to display.
59
- */
60
- async open(text?: string): Promise<string | null> {
61
- if (this.isOpened) {
62
- throw new Error('QR scanner is already opened.');
63
- }
64
-
65
- this.isOpened = true;
66
-
67
- try {
68
- const result = await request(
69
- 'web_app_open_scan_qr_popup',
70
- { text },
71
- ['qr_text_received', 'scan_qr_popup_closed'],
72
- { postEvent: this.postEvent },
73
- );
74
-
75
- return typeof result === 'object' && typeof result.data === 'string' ? result.data : null;
76
- } finally {
77
- this.isOpened = false;
78
- }
79
- }
80
-
81
- /**
82
- * Adds new event listener.
83
- */
84
- on = this.ee.on.bind(this.ee);
85
-
86
- /**
87
- * Removes event listener.
88
- */
89
- off = this.ee.off.bind(this.ee);
90
-
91
- /**
92
- * Checks if specified method is supported by current component.
93
- */
94
- supports: SupportsFunc<'open' | 'close'>;
95
- }
@@ -1,2 +0,0 @@
1
- export * from './QRScanner.js';
2
- export * from './types.js';
@@ -1,11 +0,0 @@
1
- import type { StateEvents } from '~/state/index.js';
2
-
3
- export interface QRScannerState {
4
- isOpened: boolean;
5
- }
6
-
7
- export type QRScannerEvents = StateEvents<QRScannerState>;
8
-
9
- export type QRScannerEventName = keyof QRScannerEvents;
10
-
11
- export type QRScannerEventListener<E extends QRScannerEventName> = QRScannerEvents[E];
@@ -1,85 +0,0 @@
1
- import {
2
- off,
3
- on,
4
- type PostEvent,
5
- postEvent as defaultPostEvent,
6
- } from '~/bridge/index.js';
7
- import { EventEmitter } from '~/event-emitter/index.js';
8
- import { State } from '~/state/index.js';
9
- import { createSupportsFunc, type SupportsFunc } from '~/supports/index.js';
10
- import type { Version } from '~/version/index.js';
11
-
12
- import type { SettingsButtonEvents, SettingsButtonState } from './types.js';
13
-
14
- type Emitter = EventEmitter<SettingsButtonEvents>;
15
-
16
- export class SettingsButton {
17
- private readonly ee: Emitter = new EventEmitter();
18
-
19
- private readonly state: State<SettingsButtonState>;
20
-
21
- constructor(
22
- isVisible: boolean,
23
- version: Version,
24
- private readonly postEvent: PostEvent = defaultPostEvent,
25
- ) {
26
- this.state = new State({ isVisible }, this.ee);
27
- this.supports = createSupportsFunc(version, {
28
- show: 'web_app_setup_settings_button',
29
- hide: 'web_app_setup_settings_button',
30
- });
31
- }
32
-
33
- private set isVisible(visible: boolean) {
34
- this.state.set('isVisible', visible);
35
- this.postEvent('web_app_setup_settings_button', { is_visible: visible });
36
- }
37
-
38
- /**
39
- * True if SettingsButton is currently visible.
40
- */
41
- get isVisible(): boolean {
42
- return this.state.get('isVisible');
43
- }
44
-
45
- /**
46
- * Hides the SettingsButton.
47
- */
48
- hide(): void {
49
- this.isVisible = false;
50
- }
51
-
52
- /**
53
- * Adds event listener.
54
- * @param event - event name.
55
- * @param listener - event listener.
56
- */
57
- on: Emitter['on'] = (event, listener) => (
58
- event === 'click'
59
- ? on('settings_button_pressed', listener)
60
- : this.ee.on(event, listener)
61
- );
62
-
63
- /**
64
- * Removes event listener.
65
- * @param event - event name.
66
- * @param listener - event listener.
67
- */
68
- off: Emitter['off'] = (event, listener) => (
69
- event === 'click'
70
- ? off('settings_button_pressed', listener)
71
- : this.ee.off(event, listener)
72
- );
73
-
74
- /**
75
- * Shows the SettingsButton.
76
- */
77
- show(): void {
78
- this.isVisible = true;
79
- }
80
-
81
- /**
82
- * Checks if specified method is supported by current component.
83
- */
84
- supports: SupportsFunc<'show' | 'hide'>;
85
- }
@@ -1,2 +0,0 @@
1
- export * from './SettingsButton.js';
2
- export * from './types.js';
@@ -1,15 +0,0 @@
1
- import type { MiniAppsEventListener } from '~/bridge/index.js';
2
- import type { StateEvents } from '~/state/index.js';
3
-
4
- export interface SettingsButtonState {
5
- isVisible: boolean;
6
- }
7
-
8
- export interface SettingsButtonEvents extends StateEvents<SettingsButtonState> {
9
- click: MiniAppsEventListener<'settings_button_pressed'>;
10
- }
11
-
12
- export type SettingsButtonEventName = keyof SettingsButtonEvents;
13
-
14
- export type SettingsButtonEventListener<E extends SettingsButtonEventName> =
15
- SettingsButtonEvents[E];
@@ -1,67 +0,0 @@
1
- import type { EventEmitter } from '~/event-emitter/index.js';
2
- import type { StringKeys } from '~/types/index.js';
3
-
4
- import type { StateEvents } from './types.js';
5
-
6
- /**
7
- * Represents state which is observable via passed EventEmitter.
8
- */
9
- export class State<S extends object> {
10
- constructor(
11
- private readonly state: S,
12
- private readonly ee: Pick<EventEmitter<StateEvents<S>>, 'on' | 'off' | 'emit'>,
13
- ) {
14
- }
15
-
16
- private internalSet<K extends StringKeys<S>>(key: K, value: S[K]): boolean {
17
- if (this.state[key] === value || value === undefined) {
18
- return false;
19
- }
20
-
21
- this.state[key] = value;
22
- (this.ee as any).emit(`change:${key}`, value);
23
-
24
- return true;
25
- }
26
-
27
- /**
28
- * Returns copy of current state.
29
- */
30
- clone(): S {
31
- return { ...this.state };
32
- }
33
-
34
- /**
35
- * Sets value by key.
36
- * @param key - state key.
37
- * @param value - value to set.
38
- */
39
- set<K extends StringKeys<S>>(key: K, value: S[K]): void;
40
- set(state: Partial<S>): void;
41
- set(keyOrState: StringKeys<S> | Partial<S>, value?: S[keyof S]): void {
42
- let didChange = false;
43
-
44
- if (typeof keyOrState === 'string') {
45
- didChange = this.internalSet(keyOrState, value as any);
46
- } else {
47
- // eslint-disable-next-line
48
- for (const key in keyOrState) {
49
- if (this.internalSet(key, keyOrState[key] as any)) {
50
- didChange = true;
51
- }
52
- }
53
- }
54
-
55
- if (didChange) {
56
- (this.ee as any).emit('change');
57
- }
58
- }
59
-
60
- /**
61
- * Returns value by specified key.
62
- * @param key - state key.
63
- */
64
- get<K extends StringKeys<S>>(key: K): S[K] {
65
- return this.state[key];
66
- }
67
- }
@@ -1,2 +0,0 @@
1
- export * from './State.js';
2
- export * from './types.js';
@@ -1,31 +0,0 @@
1
- import type { StringKeys } from '~/types/index.js';
2
-
3
- /**
4
- * Computes state property changed event.
5
- */
6
- export type PropChangedEvent<K extends string> = `change:${K}`;
7
-
8
- /**
9
- * Extracts state property type by its computed change event name.
10
- */
11
- export type PropertyType<State extends object, Event extends string> = {
12
- [Key in StringKeys<State>]: Event extends PropChangedEvent<Key> ? State[Key] : never;
13
- }[StringKeys<State>];
14
-
15
- /**
16
- * Creates map, where key is event name which is used, when state property was changed.
17
- * Value is according listener.
18
- */
19
- export type PropChangedEventsMap<State extends object> = {
20
- [Event in PropChangedEvent<StringKeys<State>>]: (value: PropertyType<State, Event>) => void;
21
- };
22
-
23
- /**
24
- * Creates map with all events emitted by state.
25
- */
26
- export type StateEvents<State extends object> = PropChangedEventsMap<State> & {
27
- /**
28
- * Being called whenever any property was updated.
29
- */
30
- change: () => void;
31
- };