@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,136 +0,0 @@
1
- import { toSearchParams } from 'test-utils';
2
- import { describe, expect, it } from 'vitest';
3
-
4
- import { parseInitData } from '../parseInitData';
5
-
6
- describe('auth_date', () => {
7
- it('should throw an error in case, this property is missing', () => {
8
- expect(() => parseInitData(toSearchParams({ hash: 'abcd' }))).toThrow();
9
- });
10
-
11
- it('should parse source property as Date and pass it to the "authDate" property', () => {
12
- expect(parseInitData(toSearchParams({ auth_date: 1, hash: 'abcd' }))).toMatchObject({
13
- authDate: new Date(1000),
14
- });
15
- });
16
- });
17
-
18
- describe('can_send_after', () => {
19
- it('should parse source property as Date and pass it to the "canSendAfter" property', () => {
20
- expect(
21
- parseInitData(toSearchParams({
22
- auth_date: 1,
23
- hash: 'abcd',
24
- can_send_after: 8882,
25
- })),
26
- ).toMatchObject({
27
- canSendAfter: 8882,
28
- });
29
- });
30
- });
31
-
32
- describe('chat', () => {
33
- it('should parse source property as Chat and pass it to the "chat" property', () => {
34
- expect(
35
- parseInitData(toSearchParams({
36
- auth_date: 1,
37
- hash: 'abcd',
38
- chat: {
39
- id: 5,
40
- type: 'group chat',
41
- title: 'My Chat',
42
- photo_url: 'https://johny.com',
43
- username: 'Johny Chat',
44
- },
45
- })),
46
- ).toMatchObject({
47
- chat: {
48
- id: 5,
49
- type: 'group chat',
50
- title: 'My Chat',
51
- photoUrl: 'https://johny.com',
52
- username: 'Johny Chat',
53
- },
54
- });
55
- });
56
- });
57
-
58
- describe('hash', () => {
59
- it('should throw an error in case, this property is missing', () => {
60
- expect(
61
- () => parseInitData(toSearchParams({
62
- auth_date: 1,
63
- })),
64
- ).toThrow();
65
- });
66
-
67
- it('should parse source property as string and pass it to the "hash" property', () => {
68
- expect(
69
- parseInitData(toSearchParams({
70
- auth_date: 1,
71
- hash: 'abcd',
72
- })),
73
- ).toMatchObject({
74
- hash: 'abcd',
75
- });
76
- });
77
- });
78
-
79
- [
80
- ['chat_instance', 'chatInstance'],
81
- ['chat_type', 'chatType'],
82
- ['query_id', 'queryId'],
83
- ['start_param', 'startParam'],
84
- ].forEach(([from, to]) => {
85
- describe(from, () => {
86
- it(`should parse source property as string and pass it to the "${to}" property`, () => {
87
- expect(
88
- parseInitData(toSearchParams({
89
- auth_date: 1,
90
- hash: 'abcd',
91
- [from]: 'my custom property',
92
- })),
93
- ).toMatchObject({
94
- [to]: 'my custom property',
95
- });
96
- });
97
- });
98
- });
99
-
100
- ['user', 'receiver'].forEach((property) => {
101
- describe(property, () => {
102
- it('should parse source property as User and pass it to the property with the same name', () => {
103
- expect(
104
- parseInitData(toSearchParams({
105
- auth_date: 1,
106
- hash: 'abcd',
107
- [property]: {
108
- added_to_attachment_menu: true,
109
- allows_write_to_pm: false,
110
- first_name: 'Johny',
111
- id: 333,
112
- is_bot: false,
113
- is_premium: true,
114
- language_code: 'en',
115
- last_name: 'Bravo',
116
- photo_url: 'https://johny.com',
117
- username: 'johnybravo',
118
- },
119
- })),
120
- ).toMatchObject({
121
- [property]: {
122
- addedToAttachmentMenu: true,
123
- allowsWriteToPm: false,
124
- firstName: 'Johny',
125
- id: 333,
126
- isBot: false,
127
- isPremium: true,
128
- languageCode: 'en',
129
- lastName: 'Bravo',
130
- photoUrl: 'https://johny.com',
131
- username: 'johnybravo',
132
- },
133
- });
134
- });
135
- });
136
- });
@@ -1,96 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
-
3
- import { userParser } from '../userParser';
4
-
5
- describe('first_name', () => {
6
- it('should throw an error in case, this property is missing', () => {
7
- expect(() => userParser().parse({ id: 123 })).toThrow();
8
- });
9
-
10
- it('should parse source property as string and pass it to the "firstName" property', () => {
11
- expect(
12
- userParser().parse({ id: 123, first_name: 'Pavel' }),
13
- ).toMatchObject({
14
- firstName: 'Pavel',
15
- });
16
- });
17
- });
18
-
19
- describe('id', () => {
20
- it('should throw an error in case, this property is missing', () => {
21
- expect(() => userParser().parse({ first_name: 'Pavel' })).toThrow();
22
- });
23
-
24
- it('should parse source property as number and pass it to the "id" property', () => {
25
- expect(
26
- userParser().parse({
27
- id: 123,
28
- first_name: 'Pavel',
29
- }),
30
- ).toMatchObject({
31
- id: 123,
32
- });
33
- });
34
- });
35
-
36
- // Check optional booleans.
37
- [
38
- ['added_to_attachment_menu', 'addedToAttachmentMenu'],
39
- ['allows_write_to_pm', 'allowsWriteToPm'],
40
- ['is_bot', 'isBot'],
41
- ['is_premium', 'isPremium'],
42
- ].forEach(([from, to]) => {
43
- describe(from, () => {
44
- it(`should parse source property as boolean and pass it to the "${to}" property`, () => {
45
- expect(
46
- userParser().parse({
47
- id: 123,
48
- first_name: 'Pavel',
49
- [from]: false,
50
- }),
51
- ).toMatchObject({
52
- [to]: false,
53
- });
54
-
55
- expect(
56
- () => userParser().parse({
57
- id: 123,
58
- first_name: 'Pavel',
59
- [from]: 'non-boolean',
60
- }),
61
- ).toThrow();
62
- });
63
- });
64
- });
65
-
66
- // Check optional strings.
67
- [
68
- ['language_code', 'languageCode'],
69
- ['last_name', 'lastName'],
70
- ['photo_url', 'photoUrl'],
71
- ['username', 'username'],
72
- ].forEach(([from, to]) => {
73
- describe(from, () => {
74
- it(`should parse source property as string and pass it to the "${to}" property`, () => {
75
- expect(
76
- userParser().parse({
77
- id: 123,
78
- first_name: 'Pavel',
79
- [from]: 'my custom property',
80
- }),
81
- ).toMatchObject({
82
- [to]: 'my custom property',
83
- });
84
-
85
- expect(
86
- () => userParser().parse({
87
- id: 123,
88
- first_name: 'Pavel',
89
- [from]: {
90
- key: 'cant parse it',
91
- },
92
- }),
93
- ).toThrow();
94
- });
95
- });
96
- });
@@ -1,19 +0,0 @@
1
- import { json, number, string } from '~/parsing/index.js';
2
-
3
- import type { Chat } from './types.js';
4
-
5
- /**
6
- * Returns parser used to parse chat data.
7
- */
8
- export function chatParser() {
9
- return json<Chat>({
10
- id: number(),
11
- type: string(),
12
- title: string(),
13
- photoUrl: {
14
- type: string().optional(),
15
- from: 'photo_url',
16
- },
17
- username: string().optional(),
18
- }, 'Chat');
19
- }
@@ -1,6 +0,0 @@
1
- export * from './chatParser.js';
2
- export * from './InitData.js';
3
- export * from './initDataParser.js';
4
- export * from './parseInitData.js';
5
- export * from './types.js';
6
- export * from './userParser.js';
@@ -1,41 +0,0 @@
1
- import { date, number, searchParams, string } from '~/parsing/index.js';
2
-
3
- import { chatParser } from './chatParser.js';
4
- import type { InitDataParsed } from './types.js';
5
- import { userParser } from './userParser.js';
6
-
7
- /**
8
- * Returns parser used to parse init data, presented as search params.
9
- */
10
- export function initDataParser() {
11
- return searchParams<InitDataParsed>({
12
- authDate: {
13
- type: date(),
14
- from: 'auth_date',
15
- },
16
- canSendAfter: {
17
- type: number().optional(),
18
- from: 'can_send_after',
19
- },
20
- chat: chatParser().optional(),
21
- chatInstance: {
22
- type: string().optional(),
23
- from: 'chat_instance',
24
- },
25
- chatType: {
26
- type: string().optional(),
27
- from: 'chat_type',
28
- },
29
- hash: string(),
30
- queryId: {
31
- type: string().optional(),
32
- from: 'query_id',
33
- },
34
- receiver: userParser().optional(),
35
- startParam: {
36
- type: string().optional(),
37
- from: 'start_param',
38
- },
39
- user: userParser().optional(),
40
- }, 'InitData');
41
- }
@@ -1,10 +0,0 @@
1
- import { initDataParser } from './initDataParser.js';
2
- import type { InitDataParsed } from './types.js';
3
-
4
- /**
5
- * Parses incoming value as init data.
6
- * @param value - value to parse.
7
- */
8
- export function parseInitData(value: unknown): InitDataParsed {
9
- return initDataParser().parse(value);
10
- }
@@ -1,164 +0,0 @@
1
- /**
2
- * Known type of chat.
3
- */
4
- export type ChatType =
5
- | 'sender'
6
- | 'private'
7
- | 'group'
8
- | 'supergroup'
9
- | 'channel'
10
- | string;
11
-
12
- /**
13
- * Describes Telegram Mini Apps [User](https://docs.telegram-mini-apps.com/platform/launch-parameters/init-data#user) type.
14
- */
15
- export interface User {
16
- /**
17
- * True, if this user added the bot to the attachment menu.
18
- */
19
- addedToAttachmentMenu?: boolean;
20
-
21
- /**
22
- * True, if this user allowed the bot to message them.
23
- */
24
- allowsWriteToPm?: boolean;
25
-
26
- /**
27
- * First name of the user or bot.
28
- */
29
- firstName: string;
30
-
31
- /**
32
- * A unique identifier for the user or bot.
33
- */
34
- id: number;
35
-
36
- /**
37
- * True, if this user is a bot. Returned in the `receiver` field only.
38
- * @see InitDataParsed.receiver
39
- */
40
- isBot?: boolean;
41
-
42
- /**
43
- * True, if this user is a Telegram Premium user.
44
- */
45
- isPremium?: boolean;
46
-
47
- /**
48
- * Last name of the user or bot.
49
- */
50
- lastName?: string;
51
-
52
- /**
53
- * [IETF language tag](https://en.wikipedia.org/wiki/IETF_language_tag) of the user's language.
54
- * Returns in user field only.
55
- */
56
- languageCode?: string;
57
-
58
- /**
59
- * URL of the user’s profile photo. The photo can be in .jpeg or .svg
60
- * formats. Only returned for Mini Apps launched from the attachment menu.
61
- */
62
- photoUrl?: string;
63
-
64
- /**
65
- * Username of the user or bot.
66
- */
67
- username?: string;
68
- }
69
-
70
- /**
71
- * Describes Telegram Mini Apps [Chat](https://docs.telegram-mini-apps.com/platform/launch-parameters/init-data#chat) type.
72
- */
73
- export interface Chat {
74
- /**
75
- * Unique identifier for this chat.
76
- */
77
- id: number;
78
-
79
- /**
80
- * URL of the chat’s photo. The photo can be in .jpeg or .svg formats.
81
- * Only returned for Mini Apps launched from the attachment menu.
82
- */
83
- photoUrl?: string;
84
-
85
- /**
86
- * Type of chat.
87
- */
88
- type: 'group' | 'supergroup' | 'channel' | string;
89
-
90
- /**
91
- * Title of the chat.
92
- */
93
- title: string;
94
-
95
- /**
96
- * Username of the chat.
97
- */
98
- username?: string;
99
- }
100
-
101
- /**
102
- * Describes Telegram Mini Apps [InitData](https://docs.telegram-mini-apps.com/platform/launch-parameters/init-data#parameters-list)
103
- * type.
104
- */
105
- export interface InitDataParsed {
106
- /**
107
- * Init data generation date.
108
- */
109
- authDate: Date;
110
-
111
- /**
112
- * The number of seconds after which a message can be sent via the method [answerWebAppQuery](https://core.telegram.org/bots/api#answerwebappquery).
113
- */
114
- canSendAfter?: number;
115
-
116
- /**
117
- * An object containing data about the chat where the bot was launched via
118
- * the attachment menu. Returned for supergroups, channels and group
119
- * chats – only for Mini Apps launched via the attachment menu.
120
- */
121
- chat?: Chat;
122
-
123
- /**
124
- * The type of chat from which Mini App was opened.
125
- */
126
- chatType?: ChatType;
127
-
128
- /**
129
- * A global identifier indicating the chat from which Mini App was opened. Returned only for
130
- * applications opened by direct link.
131
- */
132
- chatInstance?: string;
133
-
134
- /**
135
- * A hash of all passed parameters, which the bot server can use to
136
- * check their [validity](https://core.telegram.org/bots/webapps#validating-data-received-via-the-web-app).
137
- */
138
- hash: string;
139
-
140
- /**
141
- * A unique identifier for the Mini App session, required for sending
142
- * messages via the [answerWebAppQuery](https://core.telegram.org/bots/api#answerwebappquery) method.
143
- */
144
- queryId?: string;
145
-
146
- /**
147
- * An object containing data about the chat partner of the current user in
148
- * the chat where the bot was launched via the attachment menu.
149
- * Returned only for private chats and only for Mini Apps launched
150
- * via the attachment menu.
151
- */
152
- receiver?: User;
153
-
154
- /**
155
- * The value of the `startattach` parameter, passed via link. Only returned for
156
- * Mini Apps when launched from the attachment menu via link.
157
- */
158
- startParam?: string;
159
-
160
- /**
161
- * An object containing data about the current user.
162
- */
163
- user?: User;
164
- }
@@ -1,45 +0,0 @@
1
- import { boolean, json, number, string } from '~/parsing/index.js';
2
-
3
- import type { User } from './types.js';
4
-
5
- /**
6
- * Returns parser used to parse user data.
7
- */
8
- export function userParser() {
9
- return json<User>({
10
- addedToAttachmentMenu: {
11
- type: boolean().optional(),
12
- from: 'added_to_attachment_menu',
13
- },
14
- allowsWriteToPm: {
15
- type: boolean().optional(),
16
- from: 'allows_write_to_pm',
17
- },
18
- firstName: {
19
- type: string(),
20
- from: 'first_name',
21
- },
22
- id: number(),
23
- isBot: {
24
- type: boolean().optional(),
25
- from: 'is_bot',
26
- },
27
- isPremium: {
28
- type: boolean().optional(),
29
- from: 'is_premium',
30
- },
31
- languageCode: {
32
- type: string().optional(),
33
- from: 'language_code',
34
- },
35
- lastName: {
36
- type: string().optional(),
37
- from: 'last_name',
38
- },
39
- photoUrl: {
40
- type: string().optional(),
41
- from: 'photo_url',
42
- },
43
- username: string().optional(),
44
- }, 'User');
45
- }
@@ -1,123 +0,0 @@
1
- import {
2
- type InvoiceStatus,
3
- type PostEvent,
4
- postEvent as defaultPostEvent,
5
- request,
6
- } from '~/bridge/index.js';
7
- import { EventEmitter } from '~/event-emitter/index.js';
8
- import { State } from '~/state/index.js';
9
- import {
10
- createSupportsFunc,
11
- type SupportsFunc,
12
- } from '~/supports/index.js';
13
- import type { Version } from '~/version/index.js';
14
-
15
- import type { InvoiceEvents, InvoiceState } from './types.js';
16
-
17
- /**
18
- * Extracts invoice slug from URL.
19
- * @param url - url to extract slug from.
20
- */
21
- function slugFromUrl(url: string): string {
22
- const { hostname, pathname } = new URL(url, window.location.href);
23
- if (hostname !== 't.me') {
24
- throw new Error(`Incorrect hostname: ${hostname}`);
25
- }
26
-
27
- // Valid examples:
28
- // "/invoice/my-slug"
29
- // "/$my-slug"
30
- const match = pathname.match(/^\/(\$|invoice\/)([A-Za-z0-9\-_=]+)$/);
31
-
32
- if (match === null) {
33
- // eslint-disable-next-line no-template-curly-in-string
34
- throw new Error('Link pathname has incorrect format. Expected to receive "/invoice/{slug}" or "/${slug}"');
35
- }
36
- return match[2];
37
- }
38
-
39
- /**
40
- * Controls currently displayed invoice.
41
- */
42
- export class Invoice {
43
- private readonly ee = new EventEmitter<InvoiceEvents>();
44
-
45
- private readonly state: State<InvoiceState>;
46
-
47
- constructor(
48
- version: Version,
49
- private readonly postEvent: PostEvent = defaultPostEvent,
50
- ) {
51
- this.state = new State({ isOpened: false }, this.ee);
52
- this.supports = createSupportsFunc(version, { open: 'web_app_open_invoice' });
53
- }
54
-
55
- private set isOpened(value) {
56
- this.state.set('isOpened', value);
57
- }
58
-
59
- /**
60
- * True if invoice is currently opened.
61
- */
62
- get isOpened(): boolean {
63
- return this.state.get('isOpened');
64
- }
65
-
66
- /**
67
- * Adds new event listener.
68
- */
69
- on = this.ee.on.bind(this.ee);
70
-
71
- /**
72
- * Removes event listener.
73
- */
74
- off = this.ee.off.bind(this.ee);
75
-
76
- /**
77
- * Opens an invoice using its slug.
78
- * @param slug - invoice slug.
79
- * @throws {Error} Invoice is already opened.
80
- */
81
- open(slug: string): Promise<InvoiceStatus>;
82
-
83
- /**
84
- * Opens an invoice using its url. It expects passing link in full format, with hostname "t.me".
85
- * @param url - invoice URL.
86
- * @param type - value type.
87
- * @throws {Error} Invoice is already opened.
88
- */
89
- open(url: string, type: 'url'): Promise<InvoiceStatus>;
90
-
91
- async open(urlOrSlug: string, type?: 'url'): Promise<InvoiceStatus> {
92
- if (this.isOpened) {
93
- throw new Error('Invoice is already opened');
94
- }
95
-
96
- const slug = type ? slugFromUrl(urlOrSlug) : urlOrSlug;
97
-
98
- this.isOpened = true;
99
-
100
- try {
101
- const result = await request(
102
- 'web_app_open_invoice',
103
- { slug },
104
- 'invoice_closed',
105
- {
106
- postEvent: this.postEvent,
107
- capture(data) {
108
- return slug === data.slug;
109
- },
110
- },
111
- );
112
-
113
- return result.status;
114
- } finally {
115
- this.isOpened = false;
116
- }
117
- }
118
-
119
- /**
120
- * Checks if specified method is supported by current component.
121
- */
122
- supports: SupportsFunc<'open'>;
123
- }
@@ -1,2 +0,0 @@
1
- export * from './Invoice.js';
2
- export * from './types.js';
@@ -1,11 +0,0 @@
1
- import type { StateEvents } from '~/state/index.js';
2
-
3
- export interface InvoiceState {
4
- isOpened: boolean;
5
- }
6
-
7
- export type InvoiceEvents = StateEvents<InvoiceState>;
8
-
9
- export type InvoiceEventName = keyof InvoiceEvents;
10
-
11
- export type InvoiceEventListener<E extends InvoiceEventName> = InvoiceEvents[E];
@@ -1,19 +0,0 @@
1
- import { expect, it } from 'vitest';
2
- import { retrieveFromUrl } from '../retrieveFromUrl';
3
-
4
- it('should return launch parameters based on hash only in case, URL does not contain the query part', () => {
5
- expect(retrieveFromUrl('#tgWebAppPlatform=tdesktop&tgWebAppVersion=7.0&tgWebAppThemeParams=%7B%7D')).toEqual({
6
- platform: 'tdesktop',
7
- version: '7.0',
8
- themeParams: {},
9
- });
10
- });
11
-
12
- it('should return launch parameters based on query params and hash in case, URL contains both parts', () => {
13
- expect(retrieveFromUrl('?tgWebAppStartParam=900#tgWebAppPlatform=tdesktop&tgWebAppVersion=7.0&tgWebAppThemeParams=%7B%7D')).toEqual({
14
- platform: 'tdesktop',
15
- version: '7.0',
16
- themeParams: {},
17
- startParam: '900',
18
- });
19
- });