@tma.js/sdk 1.4.3 → 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 (283) 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/css → css}/bindMiniAppCSSVars.d.ts +2 -2
  6. package/dist/dts/{init/css → css}/bindThemeCSSVars.d.ts +1 -1
  7. package/dist/dts/{init/css → css}/bindViewportCSSVars.d.ts +1 -1
  8. package/dist/dts/css/index.d.ts +4 -0
  9. package/dist/dts/index.d.ts +1 -0
  10. package/dist/dts/init-data/types.d.ts +3 -3
  11. package/dist/dts/launch-params/types.d.ts +1 -1
  12. package/dist/dts/theme-params/types.d.ts +1 -1
  13. package/dist/index.cjs +1 -2
  14. package/dist/index.iife.js +1 -2
  15. package/dist/index.mjs +126 -115
  16. package/package.json +2 -3
  17. package/dist/index.cjs.map +0 -1
  18. package/dist/index.iife.js.map +0 -1
  19. package/dist/index.mjs.map +0 -1
  20. package/src/__tests__/globals.ts +0 -39
  21. package/src/back-button/BackButton.ts +0 -90
  22. package/src/back-button/__tests__/BackButton.ts +0 -129
  23. package/src/back-button/index.ts +0 -2
  24. package/src/back-button/types.ts +0 -14
  25. package/src/bridge/__tests__/parseMessage.ts +0 -23
  26. package/src/bridge/__tests__/request.ts +0 -236
  27. package/src/bridge/env/__tests__/hasExternalNotify.ts +0 -15
  28. package/src/bridge/env/__tests__/hasWebviewProxy.ts +0 -15
  29. package/src/bridge/env/__tests__/isIframe.ts +0 -30
  30. package/src/bridge/env/hasExternalNotify.ts +0 -19
  31. package/src/bridge/env/hasWebviewProxy.ts +0 -19
  32. package/src/bridge/env/index.ts +0 -3
  33. package/src/bridge/env/isIframe.ts +0 -11
  34. package/src/bridge/errors/MethodUnsupportedError.ts +0 -13
  35. package/src/bridge/errors/ParameterUnsupportedError.ts +0 -13
  36. package/src/bridge/errors/index.ts +0 -2
  37. package/src/bridge/events/__tests__/createEmitter.ts +0 -143
  38. package/src/bridge/events/__tests__/off.ts +0 -34
  39. package/src/bridge/events/__tests__/on.ts +0 -49
  40. package/src/bridge/events/__tests__/onTelegramEvent.ts +0 -49
  41. package/src/bridge/events/__tests__/once.ts +0 -64
  42. package/src/bridge/events/__tests__/singletonEmitter.ts +0 -22
  43. package/src/bridge/events/__tests__/subscribe.ts +0 -49
  44. package/src/bridge/events/__tests__/unsubscribe.ts +0 -34
  45. package/src/bridge/events/createEmitter.ts +0 -108
  46. package/src/bridge/events/events.ts +0 -170
  47. package/src/bridge/events/index.ts +0 -9
  48. package/src/bridge/events/off.ts +0 -14
  49. package/src/bridge/events/on.ts +0 -19
  50. package/src/bridge/events/onTelegramEvent.ts +0 -81
  51. package/src/bridge/events/once.ts +0 -18
  52. package/src/bridge/events/parsers/__tests__/clipboardTextReceived.ts +0 -21
  53. package/src/bridge/events/parsers/__tests__/invoiceClosed.ts +0 -12
  54. package/src/bridge/events/parsers/__tests__/popupClosed.ts +0 -10
  55. package/src/bridge/events/parsers/__tests__/qrTextReceived.ts +0 -9
  56. package/src/bridge/events/parsers/__tests__/theme-changed.ts +0 -42
  57. package/src/bridge/events/parsers/__tests__/viewportChanged.ts +0 -49
  58. package/src/bridge/events/parsers/clipboardTextReceived.ts +0 -26
  59. package/src/bridge/events/parsers/customMethodInvoked.ts +0 -25
  60. package/src/bridge/events/parsers/index.ts +0 -9
  61. package/src/bridge/events/parsers/invoiceClosed.ts +0 -26
  62. package/src/bridge/events/parsers/phoneRequested.ts +0 -14
  63. package/src/bridge/events/parsers/popupClosed.ts +0 -19
  64. package/src/bridge/events/parsers/qrTextReceived.ts +0 -14
  65. package/src/bridge/events/parsers/theme-changed.ts +0 -58
  66. package/src/bridge/events/parsers/viewportChanged.ts +0 -33
  67. package/src/bridge/events/parsers/writeAccessRequested.ts +0 -14
  68. package/src/bridge/events/singletonEmitter.ts +0 -19
  69. package/src/bridge/events/subscribe.ts +0 -15
  70. package/src/bridge/events/unsubscribe.ts +0 -10
  71. package/src/bridge/index.ts +0 -7
  72. package/src/bridge/invokeCustomMethod.ts +0 -56
  73. package/src/bridge/methods/__tests__/createPostEvent.ts +0 -37
  74. package/src/bridge/methods/__tests__/postEvent.ts +0 -137
  75. package/src/bridge/methods/createPostEvent.ts +0 -40
  76. package/src/bridge/methods/custom-methods.ts +0 -68
  77. package/src/bridge/methods/haptic.ts +0 -52
  78. package/src/bridge/methods/index.ts +0 -6
  79. package/src/bridge/methods/methods.ts +0 -370
  80. package/src/bridge/methods/popup.ts +0 -53
  81. package/src/bridge/methods/postEvent.ts +0 -101
  82. package/src/bridge/parseMessage.ts +0 -28
  83. package/src/bridge/request.ts +0 -176
  84. package/src/classnames/__tests__/classNames.ts +0 -20
  85. package/src/classnames/__tests__/mergeClassNames.ts +0 -21
  86. package/src/classnames/classNames.ts +0 -34
  87. package/src/classnames/index.ts +0 -2
  88. package/src/classnames/mergeClassNames.ts +0 -60
  89. package/src/closing-behavior/ClosingBehavior.ts +0 -64
  90. package/src/closing-behavior/__tests__/ClosingBehavior.ts +0 -86
  91. package/src/closing-behavior/index.ts +0 -2
  92. package/src/closing-behavior/types.ts +0 -12
  93. package/src/cloud-storage/CloudStorage.ts +0 -138
  94. package/src/cloud-storage/index.ts +0 -1
  95. package/src/colors/__tests__/isColorDark.ts +0 -12
  96. package/src/colors/__tests__/isRGB.ts +0 -13
  97. package/src/colors/__tests__/isRGBShort.ts +0 -13
  98. package/src/colors/__tests__/toRGB.ts +0 -23
  99. package/src/colors/index.ts +0 -5
  100. package/src/colors/isColorDark.ts +0 -22
  101. package/src/colors/isRGB.ts +0 -9
  102. package/src/colors/isRGBShort.ts +0 -9
  103. package/src/colors/toRGB.ts +0 -49
  104. package/src/colors/types.ts +0 -9
  105. package/src/event-emitter/EventEmitter.ts +0 -146
  106. package/src/event-emitter/__tests__/EventEmitter.ts +0 -145
  107. package/src/event-emitter/index.ts +0 -2
  108. package/src/event-emitter/types.ts +0 -60
  109. package/src/globals.ts +0 -38
  110. package/src/haptic-feedback/HapticFeedback.ts +0 -70
  111. package/src/haptic-feedback/__tests__/HapticFeedback.ts +0 -68
  112. package/src/haptic-feedback/index.ts +0 -1
  113. package/src/index.ts +0 -179
  114. package/src/init/catchCustomStyles.ts +0 -17
  115. package/src/init/creators/__tests__/createViewport.ts +0 -96
  116. package/src/init/creators/createBackButton.ts +0 -25
  117. package/src/init/creators/createClosingBehavior.ts +0 -24
  118. package/src/init/creators/createMainButton.ts +0 -51
  119. package/src/init/creators/createMiniApp.ts +0 -48
  120. package/src/init/creators/createRequestIdGenerator.ts +0 -13
  121. package/src/init/creators/createSettingsButton.ts +0 -25
  122. package/src/init/creators/createThemeParams.ts +0 -11
  123. package/src/init/creators/createViewport.ts +0 -94
  124. package/src/init/creators/index.ts +0 -8
  125. package/src/init/css/bindMiniAppCSSVars.ts +0 -48
  126. package/src/init/css/bindThemeCSSVars.ts +0 -31
  127. package/src/init/css/bindViewportCSSVars.ts +0 -36
  128. package/src/init/css/index.ts +0 -1
  129. package/src/init/css/processCSSVarsOption.ts +0 -57
  130. package/src/init/css/setCSSVar.ts +0 -8
  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
  283. /package/dist/dts/{init/css → css}/setCSSVar.d.ts +0 -0
@@ -1,33 +0,0 @@
1
- import type { RGB } from '~/colors/index.js';
2
- import type { StateEvents } from '~/state/index.js';
3
-
4
- export type ThemeParamsKey =
5
- | 'accentTextColor'
6
- | 'backgroundColor'
7
- | 'buttonColor'
8
- | 'buttonTextColor'
9
- | 'destructiveTextColor'
10
- | 'headerBackgroundColor'
11
- | 'hintColor'
12
- | 'linkColor'
13
- | 'secondaryBackgroundColor'
14
- | 'sectionBackgroundColor'
15
- | 'sectionHeaderTextColor'
16
- | 'subtitleTextColor'
17
- | 'textColor';
18
-
19
- /**
20
- * Application [theme parameters](https://docs.telegram-mini-apps.com/platform/functionality/theming).
21
- * Defines palette used by the Telegram application.
22
- */
23
- export interface ThemeParamsParsed {
24
- [key: ThemeParamsKey | string]: RGB | undefined;
25
- }
26
-
27
- export type ThemeParamsState = ThemeParamsParsed;
28
-
29
- export type ThemeParamsEvents = StateEvents<ThemeParamsState>;
30
-
31
- export type ThemeParamsEventName = keyof ThemeParamsEvents;
32
-
33
- export type ThemeParamsEventListener<E extends ThemeParamsEventName> = ThemeParamsEvents[E];
@@ -1,6 +0,0 @@
1
- export class TimeoutError extends Error {
2
- constructor(timeout: number) {
3
- super(`Async call timeout exceeded. Timeout: ${timeout}`);
4
- Object.setPrototypeOf(this, TimeoutError.prototype);
5
- }
6
- }
@@ -1,9 +0,0 @@
1
- import { expect, it } from 'vitest';
2
-
3
- import { isTimeoutError } from '../isTimeoutError';
4
- import { TimeoutError } from '../TimeoutError';
5
-
6
- it('should return true if passed value is instance of TimeoutError', () => {
7
- expect(isTimeoutError(null)).toBe(false);
8
- expect(isTimeoutError(new TimeoutError(1000))).toBe(true);
9
- });
@@ -1,28 +0,0 @@
1
- import { afterAll, beforeAll, expect, it, vi } from 'vitest';
2
-
3
- import { TimeoutError, withTimeout } from '../index';
4
-
5
- beforeAll(() => {
6
- vi.useFakeTimers();
7
- });
8
-
9
- afterAll(() => {
10
- vi.useRealTimers();
11
- });
12
-
13
- it('should throw an error in case timeout reached', () => {
14
- const promise = withTimeout(() => new Promise((res) => {
15
- setTimeout(res, 500);
16
- }), 100);
17
-
18
- Promise.resolve().then(() => vi.advanceTimersByTime(500));
19
- expect(promise).rejects.toStrictEqual(new TimeoutError(100));
20
- }, 1000);
21
-
22
- it('should return resolved value by wrapped function', () => {
23
- const promise = withTimeout(() => new Promise((res) => {
24
- res('I am fine');
25
- }), 100);
26
-
27
- expect(promise).resolves.toBe('I am fine');
28
- }, 1000);
@@ -1,4 +0,0 @@
1
- export * from './isTimeoutError.js';
2
- export * from './sleep.js';
3
- export * from './TimeoutError.js';
4
- export * from './withTimeout.js';
@@ -1,9 +0,0 @@
1
- import { TimeoutError } from './TimeoutError.js';
2
-
3
- /**
4
- * Returns true in case, passed value is TimeoutError.
5
- * @param value - checked value.
6
- */
7
- export function isTimeoutError(value: unknown): value is TimeoutError {
8
- return value instanceof TimeoutError;
9
- }
@@ -1,10 +0,0 @@
1
- /**
2
- * Awaits for specified amount of time.
3
- * @param duration - duration to await.
4
- */
5
- export function sleep(duration: number): Promise<void> {
6
- // eslint-disable-next-line no-await-in-loop,@typescript-eslint/no-loop-func
7
- return new Promise((res) => {
8
- setTimeout(res, duration);
9
- });
10
- }
@@ -1,24 +0,0 @@
1
- import { TimeoutError } from './TimeoutError.js';
2
-
3
- /**
4
- * Creates promise which rejects after timeout milliseconds.
5
- * @param timeout - timeout in milliseconds.
6
- */
7
- function createTimeoutPromise(timeout: number): Promise<never> {
8
- return new Promise((_, rej) => {
9
- setTimeout(rej, timeout, new TimeoutError(timeout));
10
- });
11
- }
12
-
13
- /**
14
- * Accepts specified function and instantly executes. It waits for timeout milliseconds for
15
- * it to complete and throws an error in case, deadline was reached.
16
- * @param func - function to execute.
17
- * @param timeout - completion timeout.
18
- */
19
- export function withTimeout<T>(func: () => Promise<T>, timeout: number): Promise<T> {
20
- return Promise.race([
21
- func(),
22
- createTimeoutPromise(timeout),
23
- ]);
24
- }
@@ -1,4 +0,0 @@
1
- export * from './methods.js';
2
- export * from './platform.js';
3
- export * from './request-id.js';
4
- export * from './utils.js';
@@ -1,18 +0,0 @@
1
- import type { PostEvent } from '~/bridge/index.js';
2
-
3
- export interface ExecuteWithTimeout {
4
- /**
5
- * Timeout to execute method.
6
- */
7
- timeout?: number;
8
- }
9
-
10
- export interface ExecuteWithPostEvent {
11
- /**
12
- * postEvent function to use to call Telegram Mini Apps methods.
13
- */
14
- postEvent?: PostEvent;
15
- }
16
-
17
- export interface ExecuteWithOptions extends ExecuteWithTimeout, ExecuteWithPostEvent {
18
- }
@@ -1,14 +0,0 @@
1
- /**
2
- * Telegram application platform name.
3
- */
4
- export type Platform =
5
- | 'android'
6
- | 'android_x'
7
- | 'ios'
8
- | 'macos'
9
- | 'tdesktop'
10
- | 'unigram'
11
- | 'unknown'
12
- | 'web'
13
- | 'weba'
14
- | string;
@@ -1,10 +0,0 @@
1
- /**
2
- * Request identifier which should be generated locally. Native Telegram application
3
- * uses it to generate a response to called method.
4
- */
5
- export type RequestId = string;
6
-
7
- /**
8
- * Function which generates unique request identifiers.
9
- */
10
- export type CreateRequestIdFunc = () => RequestId;
@@ -1,50 +0,0 @@
1
- /**
2
- * Returns true in case, T is never.
3
- */
4
- export type IsNever<T> = [T] extends [never] ? true : false;
5
-
6
- /**
7
- * Represents classic "if" condition.
8
- */
9
- export type If<Cond extends boolean, True, False> = Cond extends true ? True : False;
10
-
11
- /**
12
- * Represents classic "not" bitwise operation.
13
- */
14
- export type Not<Cond extends boolean> = If<Cond, false, true>;
15
-
16
- /**
17
- * Represents classic "and" bitwise operation.
18
- */
19
- export type And<A extends boolean, B extends boolean> = A extends true
20
- ? B extends true
21
- ? true
22
- : false
23
- : false;
24
-
25
- /**
26
- * Represents classic "or" bitwise operation.
27
- */
28
- export type Or<A extends boolean, B extends boolean> = A extends true
29
- ? true
30
- : (B extends true ? true : false);
31
-
32
- /**
33
- * True if value includes undefined.
34
- */
35
- export type HasUndefined<T> = undefined extends T ? true : false;
36
-
37
- /**
38
- * Returns true if specified property in type is marked as optional.
39
- */
40
- export type IsOptional<Type, Key extends keyof Type> = {} extends Pick<Type, Key> ? true : false;
41
-
42
- /**
43
- * Returns union object keys.
44
- */
45
- export type UnionKeys<T> = T extends T ? keyof T : never;
46
-
47
- /**
48
- * Returns object string keys.
49
- */
50
- export type StringKeys<T extends object> = Extract<keyof T, string>;
@@ -1,107 +0,0 @@
1
- import {
2
- type PostEvent,
3
- postEvent as defaultPostEvent,
4
- request,
5
- } from '~/bridge/index.js';
6
- import {
7
- createSupportsFunc,
8
- createSupportsParamFunc,
9
- supports,
10
- type SupportsFunc,
11
- } from '~/supports/index.js';
12
- import type { CreateRequestIdFunc } from '~/types/index.js';
13
- import type { Version } from '~/version/index.js';
14
-
15
- /**
16
- * Provides common Mini Apps functionality not covered by other system components.
17
- */
18
- export class Utils {
19
- constructor(
20
- private readonly version: Version,
21
- private readonly createRequestId: CreateRequestIdFunc,
22
- private readonly postEvent: PostEvent = defaultPostEvent,
23
- ) {
24
- this.supports = createSupportsFunc(version, {
25
- readTextFromClipboard: 'web_app_read_text_from_clipboard',
26
- });
27
-
28
- this.supportsParam = createSupportsParamFunc(version, {
29
- 'openLink.tryInstantView': ['web_app_open_link', 'try_instant_view'],
30
- });
31
- }
32
-
33
- /**
34
- * Opens a link in an external browser. The Mini App will not be closed.
35
- *
36
- * Note that this method can be called only in response to the user
37
- * interaction with the Mini App interface (e.g. click inside the Mini App
38
- * or on the main button).
39
- * @param url - URL to be opened.
40
- * @param tryInstantView
41
- */
42
- openLink(url: string, tryInstantView?: boolean): void {
43
- const formattedUrl = new URL(url, window.location.href).toString();
44
-
45
- // If method is not supported, we are doing it in legacy way.
46
- if (!supports('web_app_open_link', this.version)) {
47
- window.open(formattedUrl, '_blank');
48
- return;
49
- }
50
-
51
- // Otherwise, do it normally.
52
- this.postEvent('web_app_open_link', {
53
- url: formattedUrl,
54
- ...(typeof tryInstantView === 'boolean' ? { try_instant_view: tryInstantView } : {}),
55
- });
56
- }
57
-
58
- /**
59
- * Opens a Telegram link inside Telegram app. The Mini App will be closed. It expects passing
60
- * link in full format, with hostname "t.me".
61
- * @param url - URL to be opened.
62
- * @throws {Error} URL has not allowed hostname.
63
- */
64
- openTelegramLink(url: string): void {
65
- const {
66
- hostname,
67
- pathname,
68
- search,
69
- } = new URL(url, window.location.href);
70
-
71
- if (hostname !== 't.me') {
72
- throw new Error(`URL has not allowed hostname: ${hostname}. Only "t.me" is allowed`);
73
- }
74
-
75
- if (!supports('web_app_open_tg_link', this.version)) {
76
- window.location.href = url;
77
- return;
78
- }
79
-
80
- this.postEvent('web_app_open_tg_link', { path_full: pathname + search });
81
- }
82
-
83
- /**
84
- * Reads text from clipboard and returns string or null. null is returned
85
- * in cases:
86
- * - Value in clipboard is not text
87
- * - Access to clipboard is not allowed
88
- */
89
- readTextFromClipboard(): Promise<string | null> {
90
- return request(
91
- 'web_app_read_text_from_clipboard',
92
- { req_id: this.createRequestId() },
93
- 'clipboard_text_received',
94
- { postEvent: this.postEvent },
95
- ).then(({ data = null }) => data);
96
- }
97
-
98
- /**
99
- * Checks if specified method is supported by current component.
100
- */
101
- supports: SupportsFunc<'readTextFromClipboard'>;
102
-
103
- /**
104
- * Checks if specified method parameter is supported by current component.
105
- */
106
- supportsParam: SupportsFunc<'openLink.tryInstantView'>;
107
- }
@@ -1 +0,0 @@
1
- export * from './Utils.js';
@@ -1,19 +0,0 @@
1
- import { expect, it } from 'vitest';
2
-
3
- import { compareVersions } from '../compareVersions';
4
-
5
- it('should return 1 in case "a" is greater than "b"', () => {
6
- expect(compareVersions('6.1', '6.0')).toBe(1);
7
- expect(compareVersions('6.1', '6')).toBe(1);
8
- });
9
-
10
- it('should return 0 in case "a" is equal to "b"', () => {
11
- expect(compareVersions('6', '6')).toBe(0);
12
- expect(compareVersions('6', '6.0')).toBe(0);
13
- expect(compareVersions('6.0', '6')).toBe(0);
14
- });
15
-
16
- it('should return -1 in case "a" is lower than "b"', () => {
17
- expect(compareVersions('5', '6.0')).toBe(-1);
18
- expect(compareVersions('6.0', '6.1')).toBe(-1);
19
- });
@@ -1,28 +0,0 @@
1
- /**
2
- * Returns 1 in case, version "a" is greater than "b".
3
- * Returns 0 in case, version "a" equal to "b".
4
- * Returns -1 in case, version "a" is lower than "b".
5
- * @param a - first version.
6
- * @param b - second version.
7
- */
8
- export function compareVersions(a: string, b: string): number {
9
- // Split both of the version by dot.
10
- const aParts = a.split('.');
11
- const bParts = b.split('.');
12
-
13
- // Compute maximum length.
14
- const len = Math.max(aParts.length, bParts.length);
15
-
16
- // Iterate over each part of version and compare them. In case, part is
17
- // missing, assume its value is equal to 0.
18
- for (let i = 0; i < len; i += 1) {
19
- const aVal = parseInt(aParts[i] || '0', 10);
20
- const bVal = parseInt(bParts[i] || '0', 10);
21
-
22
- if (aVal === bVal) {
23
- continue;
24
- }
25
- return aVal > bVal ? 1 : -1;
26
- }
27
- return 0;
28
- }
@@ -1,2 +0,0 @@
1
- export * from './compareVersions.js';
2
- export * from './types.js';
@@ -1,4 +0,0 @@
1
- /**
2
- * Telegram Mini Apps version in format like "\d+.\d+".
3
- */
4
- export type Version = string;
@@ -1,171 +0,0 @@
1
- import {
2
- on,
3
- type PostEvent,
4
- postEvent as defaultPostEvent,
5
- type RequestOptions,
6
- } from '~/bridge/index.js';
7
- import { EventEmitter } from '~/event-emitter/index.js';
8
- import { State } from '~/state/index.js';
9
- import { requestViewport } from '~/viewport/requestViewport.js';
10
-
11
- import type {
12
- ViewportEvents,
13
- ViewportProps,
14
- ViewportState,
15
- } from './types.js';
16
- import { truncate } from './utils.js';
17
-
18
- /**
19
- * Contains information about current WebApp device viewport, its dimensions
20
- * and state.
21
- */
22
- export class Viewport {
23
- private readonly ee = new EventEmitter<ViewportEvents>();
24
-
25
- private readonly state: State<ViewportState>;
26
-
27
- private readonly postEvent: PostEvent;
28
-
29
- constructor(props: ViewportProps) {
30
- const {
31
- height,
32
- isExpanded,
33
- width,
34
- stableHeight,
35
- postEvent = defaultPostEvent,
36
- } = props;
37
- this.postEvent = postEvent;
38
- this.state = new State({
39
- height: truncate(height),
40
- isExpanded,
41
- stableHeight: truncate(stableHeight),
42
- width: truncate(width),
43
- }, this.ee);
44
- }
45
-
46
- /**
47
- * Request viewport information from the Telegram application and updates current Viewport
48
- * instance.
49
- * @param options - options to request fresh data.
50
- */
51
- sync(options?: RequestOptions): Promise<void> {
52
- return requestViewport(options).then(({ height, isExpanded, width, isStateStable }) => {
53
- this.state.set({
54
- height,
55
- width,
56
- isExpanded,
57
- stableHeight: isStateStable ? height : this.state.get('stableHeight'),
58
- });
59
- });
60
- }
61
-
62
- /**
63
- * The current height of the visible area of the Mini App.
64
- *
65
- * The application can display just the top part of the Mini App, with its
66
- * lower part remaining outside the screen area. From this position, the
67
- * user can "pull" the Mini App to its maximum height, while the bot can do
68
- * the same by calling `expand` method. As the position of the Mini App
69
- * changes, the current height value of the visible area will be updated
70
- * in real time.
71
- *
72
- * Please note that the refresh rate of this value is not sufficient
73
- * to smoothly follow the lower border of the window. It should not be
74
- * used to pin interface elements to the bottom of the visible area. It's
75
- * more appropriate to use the value of the `stableHeight`
76
- * field for this purpose.
77
- */
78
- get height(): number {
79
- return this.state.get('height');
80
- }
81
-
82
- /**
83
- * The height of the visible area of the Mini App in its last stable state.
84
- *
85
- * The application can display just the top part of the Mini App, with its
86
- * lower part remaining outside the screen area. From this position,
87
- * the user can "pull" the Mini App to its maximum height, while the bot can
88
- * do the same by calling `expand` method.
89
- *
90
- * Unlike the value of `height`, the value of `stableHeight`
91
- * does not change as the position of the Mini App changes with user
92
- * gestures or during animations. The value of `stableHeight`
93
- * will be updated after all gestures and animations are completed and
94
- * the Mini App reaches its final size.
95
- */
96
- get stableHeight(): number {
97
- return this.state.get('stableHeight');
98
- }
99
-
100
- /**
101
- * Starts listening to viewport changes and applies them.
102
- * @returns Function to stop listening.
103
- */
104
- listen() {
105
- return on('viewport_changed', (event) => {
106
- const {
107
- height,
108
- width,
109
- is_expanded: isExpanded,
110
- is_state_stable: isStateStable,
111
- } = event;
112
- const state: Partial<ViewportState> = {
113
- height: truncate(height),
114
- isExpanded,
115
- width: truncate(width),
116
- };
117
-
118
- if (isStateStable) {
119
- state.stableHeight = state.height;
120
- }
121
-
122
- this.state.set(state);
123
- });
124
- }
125
-
126
- /**
127
- * Returns true if the Mini App is expanded to the maximum available height.
128
- * Otherwise, if the Mini App occupies part of the screen and can be expanded
129
- * to the full height using `expand` method.
130
- * @see expand
131
- */
132
- get isExpanded(): boolean {
133
- return this.state.get('isExpanded');
134
- }
135
-
136
- /**
137
- * Current viewport width.
138
- */
139
- get width(): number {
140
- return this.state.get('width');
141
- }
142
-
143
- /**
144
- * A method that expands the Mini App to the maximum available height. To
145
- * find out if the Mini App is expanded to the maximum height, refer to the
146
- * value of the `isExpanded`.
147
- * @see isExpanded
148
- */
149
- expand(): void {
150
- this.postEvent('web_app_expand');
151
- this.state.set('isExpanded', true);
152
- }
153
-
154
- /**
155
- * Returns true in case current viewport height is stable and is not going to
156
- * change in the next moment.
157
- */
158
- get isStable(): boolean {
159
- return this.stableHeight === this.height;
160
- }
161
-
162
- /**
163
- * Adds new event listener.
164
- */
165
- on = this.ee.on.bind(this.ee);
166
-
167
- /**
168
- * Removes event listener.
169
- */
170
- off = this.ee.off.bind(this.ee);
171
- }
@@ -1,15 +0,0 @@
1
- import { expect, it } from 'vitest';
2
-
3
- import { isStableViewportPlatform } from '../isStableViewportPlatform';
4
-
5
- ['macos', 'tdesktop', 'unigram', 'web', 'weba'].forEach((platform) => {
6
- it(`should return true if passed platform is "${platform}"`, () => {
7
- expect(isStableViewportPlatform(platform)).toBe(true);
8
- });
9
- });
10
-
11
- ['unknown', 'android', 'android_x'].forEach((platform) => {
12
- it(`should return false if passed platform is "${platform}"`, () => {
13
- expect(isStableViewportPlatform(platform)).toBe(false);
14
- });
15
- });
@@ -1,12 +0,0 @@
1
- import { expect, it } from 'vitest';
2
-
3
- import { truncate } from '../utils';
4
-
5
- it('should return zero if value is less than 0', () => {
6
- expect(truncate(-1)).toBe(0);
7
- });
8
-
9
- it('should return value if it is equals to or higher than 0', () => {
10
- expect(truncate(0)).toBe(0);
11
- expect(truncate(20)).toBe(20);
12
- });
@@ -1,4 +0,0 @@
1
- export * from './isStableViewportPlatform.js';
2
- export * from './requestViewport.js';
3
- export * from './types.js';
4
- export * from './Viewport.js';
@@ -1,10 +0,0 @@
1
- import type { Platform } from '~/types/index.js';
2
-
3
- /**
4
- * Returns true if specified platform has stable viewport. Stable means not changing from time to
5
- * time.
6
- * @param platform - platform identifier.
7
- */
8
- export function isStableViewportPlatform(platform: Platform): boolean {
9
- return ['macos', 'tdesktop', 'unigram', 'web', 'weba'].includes(platform);
10
- }
@@ -1,23 +0,0 @@
1
- import { request, type RequestOptions } from '~/bridge/index.js';
2
-
3
- export interface RequestViewportResult {
4
- height: number;
5
- isStateStable: boolean;
6
- isExpanded: boolean;
7
- width: number;
8
- }
9
-
10
- /**
11
- * Requests viewport actual information from the Telegram application.
12
- * @param options - request options.
13
- */
14
- export async function requestViewport(options?: RequestOptions): Promise<RequestViewportResult> {
15
- const data = await request('web_app_request_viewport', 'viewport_changed', options);
16
-
17
- return {
18
- height: data.height,
19
- width: data.width,
20
- isExpanded: data.is_expanded,
21
- isStateStable: data.is_state_stable,
22
- };
23
- }
@@ -1,23 +0,0 @@
1
- import type { PostEvent } from '~/bridge/index.js';
2
- import type { StateEvents } from '~/state/index.js';
3
-
4
- export interface ViewportProps {
5
- height: number;
6
- width: number;
7
- stableHeight: number;
8
- isExpanded: boolean;
9
- postEvent?: PostEvent;
10
- }
11
-
12
- export interface ViewportState {
13
- height: number;
14
- isExpanded: boolean;
15
- stableHeight: number;
16
- width: number;
17
- }
18
-
19
- export type ViewportEvents = StateEvents<ViewportState>;
20
-
21
- export type ViewportEventName = keyof ViewportEvents;
22
-
23
- export type ViewportEventListener<E extends ViewportEventName> = ViewportEvents[E];