@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,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
- });