@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,348 +0,0 @@
1
- import {
2
- invokeCustomMethod,
3
- type PhoneRequestedStatus,
4
- type PostEvent,
5
- postEvent as defaultPostEvent,
6
- request,
7
- type SwitchInlineQueryChatType,
8
- type WriteAccessRequestedStatus,
9
- } from '~/bridge/index.js';
10
- import {
11
- isColorDark,
12
- isRGB,
13
- type RGB,
14
- } from '~/colors/index.js';
15
- import { EventEmitter } from '~/event-emitter/index.js';
16
- import { State } from '~/state/index.js';
17
- import {
18
- createSupportsFunc,
19
- createSupportsParamFunc,
20
- type SupportsFunc,
21
- } from '~/supports/index.js';
22
- import { sleep, withTimeout } from '~/timeout/index.js';
23
- import type { CreateRequestIdFunc, ExecuteWithTimeout } from '~/types/index.js';
24
-
25
- import { contactParser } from './contactParser.js';
26
- import type {
27
- MiniAppEvents,
28
- MiniAppHeaderColor, MiniAppProps,
29
- MiniAppState, RequestedContact,
30
- } from './types.js';
31
-
32
- /**
33
- * Provides common Mini Apps functionality not covered by other system components.
34
- */
35
- export class MiniApp {
36
- private readonly ee = new EventEmitter<MiniAppEvents>();
37
-
38
- private readonly state: State<MiniAppState>;
39
-
40
- private readonly botInline: boolean;
41
-
42
- private readonly postEvent: PostEvent;
43
-
44
- private readonly createRequestId: CreateRequestIdFunc;
45
-
46
- private requestingPhoneAccess = false;
47
-
48
- private requestingWriteAccess = false;
49
-
50
- constructor(props: MiniAppProps) {
51
- const {
52
- postEvent = defaultPostEvent,
53
- headerColor,
54
- backgroundColor,
55
- version,
56
- botInline,
57
- createRequestId,
58
- } = props;
59
-
60
- const isSupported = createSupportsFunc(version, {
61
- requestPhoneAccess: 'web_app_request_phone',
62
- requestWriteAccess: 'web_app_request_write_access',
63
- switchInlineQuery: 'web_app_switch_inline_query',
64
- setHeaderColor: 'web_app_set_header_color',
65
- setBackgroundColor: 'web_app_set_background_color',
66
- });
67
-
68
- this.postEvent = postEvent;
69
- this.botInline = botInline;
70
- this.createRequestId = createRequestId;
71
- this.supports = (method) => {
72
- if (!isSupported(method)) {
73
- return false;
74
- }
75
-
76
- // web_app_switch_inline_query requires a Mini App to be in inline mode, that's why we
77
- // add 1 more check here.
78
- if (method === 'switchInlineQuery' && !botInline) {
79
- return false;
80
- }
81
- return true;
82
- };
83
-
84
- this.state = new State({ backgroundColor, headerColor }, this.ee);
85
- this.supportsParam = createSupportsParamFunc(version, {
86
- 'setHeaderColor.color': ['web_app_set_header_color', 'color'],
87
- });
88
- }
89
-
90
- /**
91
- * Attempts to get requested contact.
92
- */
93
- private async getRequestedContact(): Promise<RequestedContact> {
94
- return invokeCustomMethod(
95
- 'getRequestedContact',
96
- {},
97
- this.createRequestId(),
98
- {
99
- postEvent: this.postEvent,
100
- timeout: 10000,
101
- },
102
- )
103
- .then((data) => contactParser.parse(data));
104
- }
105
-
106
- /**
107
- * The Mini App background color.
108
- */
109
- get backgroundColor(): RGB {
110
- return this.state.get('backgroundColor');
111
- }
112
-
113
- /**
114
- * Closes the Mini App.
115
- */
116
- close(): void {
117
- this.postEvent('web_app_close');
118
- }
119
-
120
- /**
121
- * The Mini App header color. Could either be a header color key or RGB color.
122
- */
123
- get headerColor(): MiniAppHeaderColor {
124
- return this.state.get('headerColor');
125
- }
126
-
127
- /**
128
- * True if Mini App is currently launched in bot inline mode.
129
- */
130
- get isBotInline(): boolean {
131
- return this.botInline;
132
- }
133
-
134
- /**
135
- * True if current Mini App background color recognized as dark.
136
- */
137
- get isDark(): boolean {
138
- return isColorDark(this.backgroundColor);
139
- }
140
-
141
- /**
142
- * True if phone access is currently being requested.
143
- */
144
- get isRequestingPhoneAccess(): boolean {
145
- return this.requestingPhoneAccess;
146
- }
147
-
148
- /**
149
- * True if write access is currently being requested.
150
- */
151
- get isRequestingWriteAccess(): boolean {
152
- return this.requestingWriteAccess;
153
- }
154
-
155
- /**
156
- * Adds new event listener.
157
- */
158
- on = this.ee.on.bind(this.ee);
159
-
160
- /**
161
- * Removes event listener.
162
- */
163
- off = this.ee.off.bind(this.ee);
164
-
165
- /**
166
- * Informs the Telegram app that the Mini App is ready to be displayed.
167
- *
168
- * It is recommended to call this method as early as possible, as soon as all essential
169
- * interface elements loaded. Once this method called, the loading placeholder is hidden
170
- * and the Mini App shown.
171
- *
172
- * If the method not called, the placeholder will be hidden only when the page fully loaded.
173
- */
174
- ready(): void {
175
- this.postEvent('web_app_ready');
176
- }
177
-
178
- /**
179
- * Requests current user contact information. In contrary to requestPhoneAccess, this method
180
- * returns promise with contact information that rejects in case, user denied access, or request
181
- * failed.
182
- * @param options - additional options.
183
- */
184
- async requestContact({ timeout = 5000 }: ExecuteWithTimeout = {}): Promise<RequestedContact> {
185
- // First of all, let's try to get the requested contact. Probably, we already requested
186
- // it before.
187
- try {
188
- return await this.getRequestedContact();
189
- } catch (e) { /* empty */
190
- }
191
-
192
- // Then, request access to user's phone.
193
- const status = await this.requestPhoneAccess();
194
- if (status !== 'sent') {
195
- throw new Error('Access denied.');
196
- }
197
-
198
- // Expected deadline.
199
- const deadlineAt = Date.now() + timeout;
200
-
201
- // Time to wait before executing the next request.
202
- let sleepTime = 50;
203
-
204
- return withTimeout(async () => {
205
- // We are trying to retrieve the requested contact until deadline was reached.
206
- while (Date.now() < deadlineAt) {
207
- try {
208
- // eslint-disable-next-line no-await-in-loop
209
- return await this.getRequestedContact();
210
- } catch (e) { /* empty */
211
- }
212
-
213
- // Sleep for some time.
214
- // eslint-disable-next-line no-await-in-loop
215
- await sleep(sleepTime);
216
-
217
- // Increase the sleep time not to kill the backend service.
218
- sleepTime += 50;
219
- }
220
-
221
- throw new Error('Unable to retrieve requested contact.');
222
- }, timeout);
223
- }
224
-
225
- /**
226
- * Requests current user phone access. Method returns promise, which resolves
227
- * status of the request. In case, user accepted the request, Mini App bot will receive
228
- * the according notification.
229
- *
230
- * To obtain the retrieved information instead, utilize the requestContact method.
231
- * @param options - additional options.
232
- * @see requestContact
233
- */
234
- requestPhoneAccess(options: ExecuteWithTimeout = {}): Promise<PhoneRequestedStatus> {
235
- if (this.requestingPhoneAccess) {
236
- throw new Error('Phone access is already being requested.');
237
- }
238
- this.requestingPhoneAccess = true;
239
-
240
- return request('web_app_request_phone', 'phone_requested', {
241
- ...options,
242
- postEvent: this.postEvent,
243
- })
244
- .then((data) => data.status)
245
- .finally(() => {
246
- this.requestingPhoneAccess = false;
247
- });
248
- }
249
-
250
- /**
251
- * Requests write message access to current user.
252
- * @param options - additional options.
253
- */
254
- requestWriteAccess(options: ExecuteWithTimeout = {}): Promise<WriteAccessRequestedStatus> {
255
- if (this.requestingWriteAccess) {
256
- throw new Error('Write access is already being requested.');
257
- }
258
- this.requestingWriteAccess = true;
259
-
260
- return request('web_app_request_write_access', 'write_access_requested', {
261
- ...options,
262
- postEvent: this.postEvent,
263
- })
264
- .then((data) => data.status)
265
- .finally(() => {
266
- this.requestingWriteAccess = false;
267
- });
268
- }
269
-
270
- /**
271
- * A method used to send data to the bot. When this method called, a service message sent to
272
- * the bot containing the data of the length up to 4096 bytes, and the Mini App closed. See the
273
- * field `web_app_data` in the class [Message](https://core.telegram.org/bots/api#message).
274
- *
275
- * This method is only available for Mini Apps launched via a Keyboard button.
276
- * @param data - data to send to bot.
277
- * @throws {Error} data has incorrect size.
278
- */
279
- sendData(data: string): void {
280
- const { size } = new Blob([data]);
281
- if (size === 0 || size > 4096) {
282
- throw new Error(`Passed data has incorrect size: ${size}`);
283
- }
284
- this.postEvent('web_app_data_send', { data });
285
- }
286
-
287
- /**
288
- * Updates current Mini App header color.
289
- * @param color - color key or RGB color.
290
- */
291
- setHeaderColor(color: MiniAppHeaderColor): void {
292
- // FIXME: Has no effect on desktop, works incorrectly on Android.
293
- // Issues:
294
- // https://github.com/Telegram-Mini-Apps/tma.js/issues/9
295
- // https://github.com/Telegram-Mini-Apps/tma.js/issues/8
296
- this.postEvent('web_app_set_header_color', isRGB(color) ? { color } : { color_key: color });
297
- this.state.set('headerColor', color);
298
- }
299
-
300
- /**
301
- * Updates current Mini App background color.
302
- * @param color - RGB color.
303
- */
304
- setBackgroundColor(color: RGB): void {
305
- // FIXME: Has no effect on desktop, works incorrectly in Android.
306
- // Issues:
307
- // https://github.com/Telegram-Mini-Apps/tma.js/issues/9
308
- // https://github.com/Telegram-Mini-Apps/tma.js/issues/8
309
- this.postEvent('web_app_set_background_color', { color });
310
- this.state.set('backgroundColor', color);
311
- }
312
-
313
- /**
314
- * Checks if specified method is supported by current component.
315
- */
316
- supports: SupportsFunc<
317
- | 'requestWriteAccess'
318
- | 'requestPhoneAccess'
319
- | 'switchInlineQuery'
320
- | 'setHeaderColor'
321
- | 'setBackgroundColor'
322
- >;
323
-
324
- /**
325
- * Checks if specified method parameter is supported by current component.
326
- */
327
- supportsParam: SupportsFunc<'setHeaderColor.color'>;
328
-
329
- /**
330
- * Inserts the bot's username and the specified inline query in the current chat's input field.
331
- * Query may be empty, in which case only the bot's username will be inserted. The client prompts
332
- * the user to choose a specific chat, then opens that chat and inserts the bot's username and
333
- * the specified inline query in the input field.
334
- * @param text - text which should be inserted in the input after the current bot name. Max
335
- * length is 256 symbols.
336
- * @param chatTypes - List of chat types which could be chosen to send the message. Could be
337
- * empty list.
338
- */
339
- switchInlineQuery(text: string, chatTypes: SwitchInlineQueryChatType[] = []): void {
340
- if (!this.supports('switchInlineQuery') && !this.isBotInline) {
341
- throw new Error('Method is unsupported because Mini App should be launched in inline mode.');
342
- }
343
- this.postEvent('web_app_switch_inline_query', {
344
- query: text,
345
- chat_types: chatTypes,
346
- });
347
- }
348
- }
@@ -1,140 +0,0 @@
1
- import { describe, expect, it, vi } from 'vitest';
2
-
3
- import type { PostEvent } from '../../bridge';
4
- import type { RGB } from '../../colors';
5
- import { createRequestIdGenerator } from '../../init/creators';
6
- import { MiniApp } from '../MiniApp';
7
- import type { MiniAppHeaderColor } from '../types';
8
-
9
- interface CreateWebAppOptions {
10
- backgroundColor?: RGB;
11
- botInline?: boolean;
12
- headerColor?: MiniAppHeaderColor;
13
- postEvent?: PostEvent;
14
- version?: string;
15
- }
16
-
17
- function createMiniApp(options: CreateWebAppOptions = {}): MiniApp {
18
- const {
19
- postEvent = vi.fn(),
20
- backgroundColor = '#000000',
21
- headerColor = 'bg_color',
22
- version = '6.0',
23
- botInline = false,
24
- } = options;
25
-
26
- return new MiniApp({
27
- version,
28
- botInline,
29
- postEvent,
30
- headerColor,
31
- backgroundColor,
32
- createRequestId: createRequestIdGenerator(),
33
- });
34
- }
35
-
36
- describe('setBackgroundColor', () => {
37
- it('should call "web_app_set_background_color" method with { color: {{color}} }', () => {
38
- const postEvent = vi.fn();
39
- const miniApp = createMiniApp({ postEvent });
40
-
41
- expect(postEvent).toHaveBeenCalledTimes(0);
42
- miniApp.setBackgroundColor('#ffaabb');
43
- expect(postEvent).toHaveBeenCalledTimes(1);
44
- expect(postEvent).toHaveBeenCalledWith('web_app_set_background_color', { color: '#ffaabb' });
45
- });
46
-
47
- it('should emit "change:backgroundColor" event with specified value', () => {
48
- const miniApp = createMiniApp({ backgroundColor: '#ffffff' });
49
- const listener = vi.fn();
50
-
51
- miniApp.on('change:backgroundColor', listener);
52
- expect(listener).toHaveBeenCalledTimes(0);
53
- miniApp.setBackgroundColor('#ffaacc');
54
- expect(listener).toHaveBeenCalledTimes(1);
55
- expect(listener).toHaveBeenCalledWith('#ffaacc');
56
- });
57
- });
58
-
59
- describe('setHeaderColor', () => {
60
- it('should call "web_app_set_header_color" method with { color_key: {{color_key}} }', () => {
61
- const postEvent = vi.fn();
62
- const miniApp = createMiniApp({ postEvent, headerColor: 'bg_color' });
63
-
64
- expect(postEvent).toHaveBeenCalledTimes(0);
65
- miniApp.setHeaderColor('secondary_bg_color');
66
- expect(postEvent).toHaveBeenCalledTimes(1);
67
- expect(postEvent).toHaveBeenCalledWith('web_app_set_header_color', { color_key: 'secondary_bg_color' });
68
- });
69
-
70
- it('should emit "change:headerColor" event with specified value', () => {
71
- const miniApp = createMiniApp({ headerColor: 'bg_color' });
72
- const listener = vi.fn();
73
-
74
- miniApp.on('change:headerColor', listener);
75
- expect(listener).toHaveBeenCalledTimes(0);
76
- miniApp.setHeaderColor('secondary_bg_color');
77
- expect(listener).toHaveBeenCalledTimes(1);
78
- expect(listener).toHaveBeenCalledWith('secondary_bg_color');
79
- });
80
- });
81
-
82
- describe('off', () => {
83
- describe('"change:backgroundColor" event', () => {
84
- it('should remove event listener from event', () => {
85
- const listener = vi.fn();
86
- const miniApp = createMiniApp({ backgroundColor: '#ffffff' });
87
-
88
- miniApp.on('change:backgroundColor', listener);
89
-
90
- expect(listener).toHaveBeenCalledTimes(0);
91
- miniApp.setBackgroundColor('#aaddcc');
92
- expect(listener).toHaveBeenCalledTimes(1);
93
-
94
- miniApp.off('change:backgroundColor', listener);
95
- listener.mockClear();
96
-
97
- expect(listener).toHaveBeenCalledTimes(0);
98
- miniApp.setBackgroundColor('#ffaaaa');
99
- expect(listener).toHaveBeenCalledTimes(0);
100
- });
101
- });
102
-
103
- describe('"change:headerColor" event', () => {
104
- it('should remove event listener from event', () => {
105
- const listener = vi.fn();
106
- const miniApp = createMiniApp({ headerColor: 'bg_color' });
107
-
108
- miniApp.on('change:headerColor', listener);
109
-
110
- expect(listener).toHaveBeenCalledTimes(0);
111
- miniApp.setHeaderColor('secondary_bg_color');
112
- expect(listener).toHaveBeenCalledTimes(1);
113
-
114
- miniApp.off('change:headerColor', listener);
115
- listener.mockClear();
116
-
117
- expect(listener).toHaveBeenCalledTimes(0);
118
- miniApp.setHeaderColor('bg_color');
119
- expect(listener).toHaveBeenCalledTimes(0);
120
- });
121
- });
122
- });
123
-
124
- describe('supports', () => {
125
- describe('setHeaderColor / setBackgroundColor', () => {
126
- it('should return true in case, WebApp version is 6.1 or higher. False, otherwise', () => {
127
- const miniApp1 = createMiniApp({ version: '6.0' });
128
- expect(miniApp1.supports('setHeaderColor')).toBe(false);
129
- expect(miniApp1.supports('setBackgroundColor')).toBe(false);
130
-
131
- const miniApp2 = createMiniApp({ version: '6.1' });
132
- expect(miniApp2.supports('setHeaderColor')).toBe(true);
133
- expect(miniApp2.supports('setBackgroundColor')).toBe(true);
134
-
135
- const miniApp3 = createMiniApp({ version: '6.2' });
136
- expect(miniApp3.supports('setHeaderColor')).toBe(true);
137
- expect(miniApp3.supports('setBackgroundColor')).toBe(true);
138
- });
139
- });
140
- });
@@ -1,29 +0,0 @@
1
- import { date, json, number, searchParams, string } from '~/parsing/index.js';
2
-
3
- import type { RequestedContact } from './types.js';
4
-
5
- export const contactParser = searchParams<RequestedContact>({
6
- contact: json({
7
- userId: {
8
- type: number(),
9
- from: 'user_id',
10
- },
11
- phoneNumber: {
12
- type: string(),
13
- from: 'phone_number',
14
- },
15
- firstName: {
16
- type: string(),
17
- from: 'first_name',
18
- },
19
- lastName: {
20
- type: string().optional(),
21
- from: 'last_name',
22
- },
23
- }),
24
- authDate: {
25
- type: date(),
26
- from: 'auth_date',
27
- },
28
- hash: string(),
29
- });
@@ -1,2 +0,0 @@
1
- export * from './MiniApp.js';
2
- export * from './types.js';
@@ -1,38 +0,0 @@
1
- import type { HeaderColorKey, PostEvent } from '~/bridge/index.js';
2
- import type { RGB } from '~/colors/index.js';
3
- import type { StateEvents } from '~/state/index.js';
4
- import type { CreateRequestIdFunc } from '~/types/index.js';
5
- import type { Version } from '~/version/index.js';
6
-
7
- export interface MiniAppProps {
8
- headerColor: MiniAppHeaderColor;
9
- backgroundColor: RGB;
10
- version: Version;
11
- botInline: boolean;
12
- createRequestId: CreateRequestIdFunc;
13
- postEvent?: PostEvent;
14
- }
15
-
16
- export type MiniAppHeaderColor = HeaderColorKey | RGB;
17
-
18
- export interface MiniAppState {
19
- backgroundColor: RGB;
20
- headerColor: MiniAppHeaderColor;
21
- }
22
-
23
- export type MiniAppEvents = StateEvents<MiniAppState>;
24
-
25
- export type MiniAppEventName = keyof MiniAppEvents;
26
-
27
- export type MiniAppEventListener<E extends MiniAppEventName> = MiniAppEvents[E];
28
-
29
- export interface RequestedContact {
30
- contact: {
31
- userId: number;
32
- phoneNumber: string;
33
- firstName: string;
34
- lastName?: string;
35
- };
36
- authDate: Date;
37
- hash: string;
38
- }
@@ -1,21 +0,0 @@
1
- import { expect, it } from 'vitest';
2
-
3
- import { isRecord } from '../isRecord';
4
-
5
- it('should return false for non-object value', () => {
6
- [true, 123, 'abc'].forEach((v) => {
7
- expect(isRecord(v)).toBe(false);
8
- });
9
- });
10
-
11
- it('should return false for null value', () => {
12
- expect(isRecord(null)).toBe(false);
13
- });
14
-
15
- it('should return false for array', () => {
16
- expect(isRecord([])).toBe(false);
17
- });
18
-
19
- it('should return true for object', () => {
20
- expect(isRecord({ a: true })).toBe(true);
21
- });
package/src/misc/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from './isRecord.js';
2
- export * from './isTMA.js';
@@ -1,7 +0,0 @@
1
- /**
2
- * States that passed value is Record and not Array.
3
- * @param value - value to check.
4
- */
5
- export function isRecord(value: unknown): value is Record<string, unknown> {
6
- return typeof value === 'object' && value !== null && !Array.isArray(value);
7
- }
package/src/misc/isTMA.ts DELETED
@@ -1,13 +0,0 @@
1
- import { retrieveLaunchData } from '~/launch-params/index.js';
2
-
3
- /**
4
- * Returns true in case, current environment is Telegram Mini Apps.
5
- */
6
- export function isTMA(): boolean {
7
- try {
8
- retrieveLaunchData();
9
- return true;
10
- } catch (e) {
11
- return false;
12
- }
13
- }