@tma.js/sdk 1.4.4 → 1.4.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (298) hide show
  1. package/README.md +2 -2
  2. package/dist/dts/bridge/env/index.d.ts +0 -1
  3. package/dist/dts/bridge/events/events.d.ts +16 -16
  4. package/dist/dts/bridge/methods/methods.d.ts +27 -27
  5. package/dist/dts/bridge/methods/popup.d.ts +1 -1
  6. package/dist/dts/index.d.ts +3 -3
  7. package/dist/dts/init-data/types.d.ts +3 -3
  8. package/dist/dts/launch-params/index.d.ts +1 -4
  9. package/dist/dts/launch-params/retrieveFromLocation.d.ts +2 -2
  10. package/dist/dts/launch-params/retrieveFromPerformance.d.ts +3 -4
  11. package/dist/dts/launch-params/retrieveFromUrl.d.ts +3 -2
  12. package/dist/dts/launch-params/retrieveLaunchData.d.ts +1 -0
  13. package/dist/dts/launch-params/retrieveLaunchParams.d.ts +6 -0
  14. package/dist/dts/launch-params/storage.d.ts +3 -5
  15. package/dist/dts/launch-params/types.d.ts +1 -1
  16. package/dist/dts/{launch-params → misc}/getFirstNavigationEntry.d.ts +1 -1
  17. package/dist/dts/misc/index.d.ts +3 -0
  18. package/dist/dts/{bridge/env → misc}/isIframe.d.ts +1 -1
  19. package/dist/dts/misc/isPageReload.d.ts +5 -0
  20. package/dist/dts/theme-params/types.d.ts +1 -1
  21. package/dist/index.cjs +1 -2
  22. package/dist/index.iife.js +1 -2
  23. package/dist/index.mjs +841 -866
  24. package/package.json +2 -3
  25. package/dist/dts/launch-params/computeLaunchData.d.ts +0 -6
  26. package/dist/dts/launch-params/computePageReload.d.ts +0 -6
  27. package/dist/dts/launch-params/retrieveCurrent.d.ts +0 -5
  28. package/dist/index.cjs.map +0 -1
  29. package/dist/index.iife.js.map +0 -1
  30. package/dist/index.mjs.map +0 -1
  31. package/src/__tests__/globals.ts +0 -39
  32. package/src/back-button/BackButton.ts +0 -90
  33. package/src/back-button/__tests__/BackButton.ts +0 -129
  34. package/src/back-button/index.ts +0 -2
  35. package/src/back-button/types.ts +0 -14
  36. package/src/bridge/__tests__/parseMessage.ts +0 -23
  37. package/src/bridge/__tests__/request.ts +0 -236
  38. package/src/bridge/env/__tests__/hasExternalNotify.ts +0 -15
  39. package/src/bridge/env/__tests__/hasWebviewProxy.ts +0 -15
  40. package/src/bridge/env/__tests__/isIframe.ts +0 -30
  41. package/src/bridge/env/hasExternalNotify.ts +0 -19
  42. package/src/bridge/env/hasWebviewProxy.ts +0 -19
  43. package/src/bridge/env/index.ts +0 -3
  44. package/src/bridge/env/isIframe.ts +0 -11
  45. package/src/bridge/errors/MethodUnsupportedError.ts +0 -13
  46. package/src/bridge/errors/ParameterUnsupportedError.ts +0 -13
  47. package/src/bridge/errors/index.ts +0 -2
  48. package/src/bridge/events/__tests__/createEmitter.ts +0 -143
  49. package/src/bridge/events/__tests__/off.ts +0 -34
  50. package/src/bridge/events/__tests__/on.ts +0 -49
  51. package/src/bridge/events/__tests__/onTelegramEvent.ts +0 -49
  52. package/src/bridge/events/__tests__/once.ts +0 -64
  53. package/src/bridge/events/__tests__/singletonEmitter.ts +0 -22
  54. package/src/bridge/events/__tests__/subscribe.ts +0 -49
  55. package/src/bridge/events/__tests__/unsubscribe.ts +0 -34
  56. package/src/bridge/events/createEmitter.ts +0 -108
  57. package/src/bridge/events/events.ts +0 -170
  58. package/src/bridge/events/index.ts +0 -9
  59. package/src/bridge/events/off.ts +0 -14
  60. package/src/bridge/events/on.ts +0 -19
  61. package/src/bridge/events/onTelegramEvent.ts +0 -81
  62. package/src/bridge/events/once.ts +0 -18
  63. package/src/bridge/events/parsers/__tests__/clipboardTextReceived.ts +0 -21
  64. package/src/bridge/events/parsers/__tests__/invoiceClosed.ts +0 -12
  65. package/src/bridge/events/parsers/__tests__/popupClosed.ts +0 -10
  66. package/src/bridge/events/parsers/__tests__/qrTextReceived.ts +0 -9
  67. package/src/bridge/events/parsers/__tests__/theme-changed.ts +0 -42
  68. package/src/bridge/events/parsers/__tests__/viewportChanged.ts +0 -49
  69. package/src/bridge/events/parsers/clipboardTextReceived.ts +0 -26
  70. package/src/bridge/events/parsers/customMethodInvoked.ts +0 -25
  71. package/src/bridge/events/parsers/index.ts +0 -9
  72. package/src/bridge/events/parsers/invoiceClosed.ts +0 -26
  73. package/src/bridge/events/parsers/phoneRequested.ts +0 -14
  74. package/src/bridge/events/parsers/popupClosed.ts +0 -19
  75. package/src/bridge/events/parsers/qrTextReceived.ts +0 -14
  76. package/src/bridge/events/parsers/theme-changed.ts +0 -58
  77. package/src/bridge/events/parsers/viewportChanged.ts +0 -33
  78. package/src/bridge/events/parsers/writeAccessRequested.ts +0 -14
  79. package/src/bridge/events/singletonEmitter.ts +0 -19
  80. package/src/bridge/events/subscribe.ts +0 -15
  81. package/src/bridge/events/unsubscribe.ts +0 -10
  82. package/src/bridge/index.ts +0 -7
  83. package/src/bridge/invokeCustomMethod.ts +0 -56
  84. package/src/bridge/methods/__tests__/createPostEvent.ts +0 -37
  85. package/src/bridge/methods/__tests__/postEvent.ts +0 -137
  86. package/src/bridge/methods/createPostEvent.ts +0 -40
  87. package/src/bridge/methods/custom-methods.ts +0 -68
  88. package/src/bridge/methods/haptic.ts +0 -52
  89. package/src/bridge/methods/index.ts +0 -6
  90. package/src/bridge/methods/methods.ts +0 -370
  91. package/src/bridge/methods/popup.ts +0 -53
  92. package/src/bridge/methods/postEvent.ts +0 -101
  93. package/src/bridge/parseMessage.ts +0 -28
  94. package/src/bridge/request.ts +0 -176
  95. package/src/classnames/__tests__/classNames.ts +0 -20
  96. package/src/classnames/__tests__/mergeClassNames.ts +0 -21
  97. package/src/classnames/classNames.ts +0 -34
  98. package/src/classnames/index.ts +0 -2
  99. package/src/classnames/mergeClassNames.ts +0 -60
  100. package/src/closing-behavior/ClosingBehavior.ts +0 -64
  101. package/src/closing-behavior/__tests__/ClosingBehavior.ts +0 -86
  102. package/src/closing-behavior/index.ts +0 -2
  103. package/src/closing-behavior/types.ts +0 -12
  104. package/src/cloud-storage/CloudStorage.ts +0 -138
  105. package/src/cloud-storage/index.ts +0 -1
  106. package/src/colors/__tests__/isColorDark.ts +0 -12
  107. package/src/colors/__tests__/isRGB.ts +0 -13
  108. package/src/colors/__tests__/isRGBShort.ts +0 -13
  109. package/src/colors/__tests__/toRGB.ts +0 -23
  110. package/src/colors/index.ts +0 -5
  111. package/src/colors/isColorDark.ts +0 -22
  112. package/src/colors/isRGB.ts +0 -9
  113. package/src/colors/isRGBShort.ts +0 -9
  114. package/src/colors/toRGB.ts +0 -49
  115. package/src/colors/types.ts +0 -9
  116. package/src/css/__tests__/bindMiniAppCSSVars.ts +0 -175
  117. package/src/css/__tests__/bindThemeCSSVars.ts +0 -52
  118. package/src/css/__tests__/bindViewportCSSVars.ts +0 -55
  119. package/src/css/__tests__/setCSSVar.ts +0 -14
  120. package/src/css/bindMiniAppCSSVars.ts +0 -51
  121. package/src/css/bindThemeCSSVars.ts +0 -31
  122. package/src/css/bindViewportCSSVars.ts +0 -36
  123. package/src/css/index.ts +0 -4
  124. package/src/css/setCSSVar.ts +0 -8
  125. package/src/event-emitter/EventEmitter.ts +0 -146
  126. package/src/event-emitter/__tests__/EventEmitter.ts +0 -145
  127. package/src/event-emitter/index.ts +0 -2
  128. package/src/event-emitter/types.ts +0 -60
  129. package/src/globals.ts +0 -38
  130. package/src/haptic-feedback/HapticFeedback.ts +0 -70
  131. package/src/haptic-feedback/__tests__/HapticFeedback.ts +0 -68
  132. package/src/haptic-feedback/index.ts +0 -1
  133. package/src/index.ts +0 -185
  134. package/src/init/catchCustomStyles.ts +0 -17
  135. package/src/init/creators/__tests__/createViewport.ts +0 -96
  136. package/src/init/creators/createBackButton.ts +0 -25
  137. package/src/init/creators/createClosingBehavior.ts +0 -24
  138. package/src/init/creators/createMainButton.ts +0 -51
  139. package/src/init/creators/createMiniApp.ts +0 -48
  140. package/src/init/creators/createRequestIdGenerator.ts +0 -13
  141. package/src/init/creators/createSettingsButton.ts +0 -25
  142. package/src/init/creators/createThemeParams.ts +0 -11
  143. package/src/init/creators/createViewport.ts +0 -94
  144. package/src/init/creators/index.ts +0 -8
  145. package/src/init/css/index.ts +0 -1
  146. package/src/init/css/processCSSVarsOption.ts +0 -55
  147. package/src/init/index.ts +0 -2
  148. package/src/init/init.ts +0 -134
  149. package/src/init/types.ts +0 -94
  150. package/src/init-data/InitData.ts +0 -96
  151. package/src/init-data/__tests__/InitData.ts +0 -98
  152. package/src/init-data/__tests__/chatParser.ts +0 -102
  153. package/src/init-data/__tests__/initDataParser.ts +0 -136
  154. package/src/init-data/__tests__/parseInitData.ts +0 -136
  155. package/src/init-data/__tests__/userParser.ts +0 -96
  156. package/src/init-data/chatParser.ts +0 -19
  157. package/src/init-data/index.ts +0 -6
  158. package/src/init-data/initDataParser.ts +0 -41
  159. package/src/init-data/parseInitData.ts +0 -10
  160. package/src/init-data/types.ts +0 -164
  161. package/src/init-data/userParser.ts +0 -45
  162. package/src/invoice/Invoice.ts +0 -123
  163. package/src/invoice/index.ts +0 -2
  164. package/src/invoice/types.ts +0 -11
  165. package/src/launch-params/__tests__/retrieveFromUrl.ts +0 -19
  166. package/src/launch-params/computeLaunchData.ts +0 -81
  167. package/src/launch-params/computePageReload.ts +0 -13
  168. package/src/launch-params/getFirstNavigationEntry.ts +0 -10
  169. package/src/launch-params/index.ts +0 -13
  170. package/src/launch-params/launchParamsParser.ts +0 -45
  171. package/src/launch-params/parseLaunchParams.ts +0 -10
  172. package/src/launch-params/retrieveCurrent.ts +0 -27
  173. package/src/launch-params/retrieveFromLocation.ts +0 -10
  174. package/src/launch-params/retrieveFromPerformance.ts +0 -18
  175. package/src/launch-params/retrieveFromUrl.ts +0 -19
  176. package/src/launch-params/retrieveLaunchData.ts +0 -30
  177. package/src/launch-params/serializeLaunchParams.ts +0 -37
  178. package/src/launch-params/storage.ts +0 -33
  179. package/src/launch-params/types.ts +0 -62
  180. package/src/logger/Logger.ts +0 -72
  181. package/src/logger/__tests__/Logger.ts +0 -107
  182. package/src/logger/index.ts +0 -1
  183. package/src/main-button/MainButton.ts +0 -239
  184. package/src/main-button/__tests__/MainButton.ts +0 -346
  185. package/src/main-button/index.ts +0 -2
  186. package/src/main-button/types.ts +0 -26
  187. package/src/mini-app/MiniApp.ts +0 -348
  188. package/src/mini-app/__tests__/MiniApp.ts +0 -140
  189. package/src/mini-app/contactParser.ts +0 -29
  190. package/src/mini-app/index.ts +0 -2
  191. package/src/mini-app/types.ts +0 -38
  192. package/src/misc/__tests__/isRecord.ts +0 -21
  193. package/src/misc/index.ts +0 -2
  194. package/src/misc/isRecord.ts +0 -7
  195. package/src/misc/isTMA.ts +0 -13
  196. package/src/navigation/HashNavigator/HashNavigator.ts +0 -220
  197. package/src/navigation/HashNavigator/__tests__/HashNavigator.ts +0 -144
  198. package/src/navigation/HashNavigator/__tests__/drop.ts +0 -42
  199. package/src/navigation/HashNavigator/__tests__/go.ts +0 -9
  200. package/src/navigation/HashNavigator/drop.ts +0 -36
  201. package/src/navigation/HashNavigator/go.ts +0 -28
  202. package/src/navigation/HashNavigator/index.ts +0 -2
  203. package/src/navigation/HashNavigator/types.ts +0 -41
  204. package/src/navigation/Navigator/Navigator.ts +0 -282
  205. package/src/navigation/Navigator/index.ts +0 -2
  206. package/src/navigation/Navigator/types.ts +0 -55
  207. package/src/navigation/ensurePrefix.ts +0 -9
  208. package/src/navigation/getHash.ts +0 -17
  209. package/src/navigation/index.ts +0 -4
  210. package/src/parsing/ArrayValueParser.ts +0 -79
  211. package/src/parsing/ParseError.ts +0 -27
  212. package/src/parsing/ParseSchemaFieldError.ts +0 -21
  213. package/src/parsing/ValueParser.ts +0 -71
  214. package/src/parsing/__tests__/ArrayValueParser.ts +0 -18
  215. package/src/parsing/__tests__/toRecord.ts +0 -10
  216. package/src/parsing/createValueParserGenerator.ts +0 -16
  217. package/src/parsing/index.ts +0 -10
  218. package/src/parsing/parseBySchema.ts +0 -65
  219. package/src/parsing/parsers/__tests__/array.ts +0 -39
  220. package/src/parsing/parsers/__tests__/boolean.ts +0 -31
  221. package/src/parsing/parsers/__tests__/date.ts +0 -25
  222. package/src/parsing/parsers/__tests__/json.ts +0 -80
  223. package/src/parsing/parsers/__tests__/number.ts +0 -23
  224. package/src/parsing/parsers/__tests__/rgb.ts +0 -22
  225. package/src/parsing/parsers/__tests__/searchParams.ts +0 -105
  226. package/src/parsing/parsers/__tests__/string.ts +0 -25
  227. package/src/parsing/parsers/array.ts +0 -9
  228. package/src/parsing/parsers/boolean.ts +0 -22
  229. package/src/parsing/parsers/date.ts +0 -11
  230. package/src/parsing/parsers/index.ts +0 -8
  231. package/src/parsing/parsers/json.ts +0 -17
  232. package/src/parsing/parsers/number.ts +0 -21
  233. package/src/parsing/parsers/rgb.ts +0 -10
  234. package/src/parsing/parsers/searchParams.ts +0 -24
  235. package/src/parsing/parsers/string.ts +0 -12
  236. package/src/parsing/toRecord.ts +0 -27
  237. package/src/parsing/types.ts +0 -32
  238. package/src/parsing/unexpectedTypeError.ts +0 -6
  239. package/src/popup/Popup.ts +0 -91
  240. package/src/popup/__tests__/Popup.ts +0 -130
  241. package/src/popup/__tests__/preparePopupParams.ts +0 -85
  242. package/src/popup/index.ts +0 -2
  243. package/src/popup/preparePopupParams.ts +0 -59
  244. package/src/popup/types.ts +0 -69
  245. package/src/qr-scanner/QRScanner.ts +0 -95
  246. package/src/qr-scanner/index.ts +0 -2
  247. package/src/qr-scanner/types.ts +0 -11
  248. package/src/settings-button/SettingsButton.ts +0 -85
  249. package/src/settings-button/index.ts +0 -2
  250. package/src/settings-button/types.ts +0 -15
  251. package/src/state/State.ts +0 -67
  252. package/src/state/index.ts +0 -2
  253. package/src/state/types.ts +0 -31
  254. package/src/storage.ts +0 -69
  255. package/src/supports/__tests__/supports.ts +0 -123
  256. package/src/supports/createSupportsFunc.ts +0 -18
  257. package/src/supports/createSupportsParamFunc.ts +0 -27
  258. package/src/supports/index.ts +0 -4
  259. package/src/supports/supports.ts +0 -84
  260. package/src/supports/types.ts +0 -1
  261. package/src/theme-params/ThemeParams.ts +0 -131
  262. package/src/theme-params/__tests__/keys.ts +0 -19
  263. package/src/theme-params/__tests__/parseThemeParams.ts +0 -29
  264. package/src/theme-params/__tests__/serializeThemeParams.ts +0 -29
  265. package/src/theme-params/__tests__/themeParamsParser.ts +0 -29
  266. package/src/theme-params/index.ts +0 -6
  267. package/src/theme-params/keys.ts +0 -24
  268. package/src/theme-params/parseThemeParams.ts +0 -10
  269. package/src/theme-params/requestThemeParams.ts +0 -13
  270. package/src/theme-params/serializeThemeParams.ts +0 -20
  271. package/src/theme-params/themeParamsParser.ts +0 -22
  272. package/src/theme-params/types.ts +0 -33
  273. package/src/timeout/TimeoutError.ts +0 -6
  274. package/src/timeout/__tests__/isTimeoutError.ts +0 -9
  275. package/src/timeout/__tests__/withTimeout.ts +0 -28
  276. package/src/timeout/index.ts +0 -4
  277. package/src/timeout/isTimeoutError.ts +0 -9
  278. package/src/timeout/sleep.ts +0 -10
  279. package/src/timeout/withTimeout.ts +0 -24
  280. package/src/types/index.ts +0 -4
  281. package/src/types/methods.ts +0 -18
  282. package/src/types/platform.ts +0 -14
  283. package/src/types/request-id.ts +0 -10
  284. package/src/types/utils.ts +0 -50
  285. package/src/utils/Utils.ts +0 -107
  286. package/src/utils/index.ts +0 -1
  287. package/src/version/__tests__/compareVersions.ts +0 -19
  288. package/src/version/compareVersions.ts +0 -28
  289. package/src/version/index.ts +0 -2
  290. package/src/version/types.ts +0 -4
  291. package/src/viewport/Viewport.ts +0 -171
  292. package/src/viewport/__tests__/isStableViewportPlatform.ts +0 -15
  293. package/src/viewport/__tests__/utils.ts +0 -12
  294. package/src/viewport/index.ts +0 -4
  295. package/src/viewport/isStableViewportPlatform.ts +0 -10
  296. package/src/viewport/requestViewport.ts +0 -23
  297. package/src/viewport/types.ts +0 -23
  298. package/src/viewport/utils.ts +0 -7
@@ -1,176 +0,0 @@
1
- import { isRecord } from '~/misc/index.js';
2
- import { withTimeout } from '~/timeout/index.js';
3
- import type { And, ExecuteWithOptions, If, IsNever } from '~/types/index.js';
4
-
5
- import {
6
- type MiniAppsEventHasParams,
7
- type MiniAppsEventName,
8
- type MiniAppsEventParams,
9
- on,
10
- } from './events/index.js';
11
- import {
12
- type MiniAppsEmptyMethodName,
13
- type MiniAppsMethodAcceptParams,
14
- type MiniAppsMethodName,
15
- type MiniAppsMethodParams,
16
- type MiniAppsNonEmptyMethodName,
17
- postEvent as defaultPostEvent,
18
- } from './methods/index.js';
19
-
20
- /**
21
- * Names of methods, which require passing "req_id" parameter.
22
- */
23
- type MethodWithRequestId = {
24
- [M in MiniAppsMethodName]: If<
25
- And<
26
- MiniAppsMethodAcceptParams<M>,
27
- MiniAppsMethodParams<M> extends { req_id: string } ? true : false
28
- >,
29
- M,
30
- never
31
- >;
32
- }[MiniAppsMethodName];
33
-
34
- /**
35
- * Names of events, which contain "req_id" parameter.
36
- */
37
- type EventWithRequestId = {
38
- [E in MiniAppsEventName]: If<
39
- And<MiniAppsEventHasParams<E>, MiniAppsEventParams<E> extends {
40
- req_id: string
41
- } ? true : false>,
42
- E,
43
- never
44
- >;
45
- }[MiniAppsEventName];
46
-
47
- export interface RequestOptions extends ExecuteWithOptions {
48
- }
49
-
50
- export interface RequestOptionsAdvanced<EventPayload> extends RequestOptions {
51
- /**
52
- * Should return true in case, this event should be captured. If not specified,
53
- * request is not skipping captured events.
54
- */
55
- capture?: If<IsNever<EventPayload>, () => boolean, (payload: EventPayload) => boolean>;
56
- }
57
-
58
- /**
59
- * Calls specified TWA method and captures one of the specified events. Returns promise
60
- * which will be resolved in case, event with specified in method request identifier
61
- * was captured.
62
- * @param method - method to execute.
63
- * @param params - method parameters.
64
- * @param event - event or events to listen.
65
- * @param options - additional execution options.
66
- */
67
- export function request<M extends MethodWithRequestId, E extends EventWithRequestId>(
68
- method: M,
69
- params: MiniAppsMethodParams<M>,
70
- event: E | E[],
71
- options?: RequestOptions,
72
- ): Promise<MiniAppsEventParams<E>>;
73
-
74
- /**
75
- * Calls specified TWA method and captures one of the specified events. Returns promise
76
- * which will be resolved in case, specified event was captured.
77
- * @param method - method to execute.
78
- * @param event - event or events to listen.
79
- * @param options - additional execution options.
80
- */
81
- export function request<M extends MiniAppsEmptyMethodName, E extends MiniAppsEventName>(
82
- method: M,
83
- event: E | E[],
84
- options?: RequestOptionsAdvanced<MiniAppsEventParams<E>>,
85
- ): Promise<MiniAppsEventParams<E>>;
86
-
87
- /**
88
- * Calls specified TWA method and captures one of the specified events. Returns promise
89
- * which will be resolved in case, specified event was captured.
90
- * @param method - method to execute
91
- * @param params - method parameters.
92
- * @param event - event or events to listen
93
- * @param options - additional execution options.
94
- */
95
- export function request<M extends MiniAppsNonEmptyMethodName, E extends MiniAppsEventName>(
96
- method: M,
97
- params: MiniAppsMethodParams<M>,
98
- event: E | E[],
99
- options?: RequestOptionsAdvanced<MiniAppsEventParams<E>>,
100
- ): Promise<MiniAppsEventParams<E>>;
101
-
102
- export function request(
103
- method: MiniAppsMethodName,
104
- eventOrParams: MiniAppsEventName | MiniAppsEventName[] | MiniAppsEventParams<any>,
105
- eventOrOptions?:
106
- | MiniAppsEventName
107
- | MiniAppsEventName[]
108
- | RequestOptions
109
- | RequestOptionsAdvanced<any>,
110
- options?: RequestOptions | RequestOptionsAdvanced<any>,
111
- ): Promise<any> {
112
- let executionOptions: RequestOptions | RequestOptionsAdvanced<any> | undefined;
113
- let methodParams: MiniAppsEventParams<any> | undefined;
114
- let events: MiniAppsEventName[];
115
- let requestId: string | undefined;
116
-
117
- if (typeof eventOrParams === 'string' || Array.isArray(eventOrParams)) {
118
- // Override: [method, event, options?]
119
- events = Array.isArray(eventOrParams) ? eventOrParams : [eventOrParams] as MiniAppsEventName[];
120
- executionOptions = eventOrOptions as (RequestOptionsAdvanced<any> | undefined);
121
- } else {
122
- // Override: [method, params, event, options?]
123
- methodParams = eventOrParams as MiniAppsEventParams<any>;
124
- events = Array.isArray(eventOrOptions)
125
- ? eventOrOptions
126
- : [eventOrOptions] as MiniAppsEventName[];
127
- executionOptions = options;
128
- }
129
-
130
- // In case, method parameters were passed, and they contained request identifier, we should store
131
- // it and wait for the event with this identifier to occur.
132
- if (isRecord(methodParams) && typeof methodParams.req_id === 'string') {
133
- requestId = methodParams.req_id;
134
- }
135
-
136
- const { postEvent = defaultPostEvent, timeout } = executionOptions || {};
137
- const capture = executionOptions && 'capture' in executionOptions
138
- ? executionOptions.capture
139
- : null;
140
-
141
- const execute = () => {
142
- return new Promise((res, rej) => {
143
- // Iterate over each event and create event listener.
144
- const stoppers = events.map((ev) => on(ev, (data?) => {
145
- // If request identifier was specified, we are waiting for event with the same value
146
- // to occur.
147
- if (requestId && (!isRecord(data) || data.req_id !== requestId)) {
148
- return;
149
- }
150
-
151
- if (typeof capture === 'function' && !capture(data)) {
152
- return;
153
- }
154
-
155
- // eslint-disable-next-line @typescript-eslint/no-use-before-define
156
- stopListening();
157
- res(data);
158
- }));
159
-
160
- // Function which removes all event listeners.
161
- const stopListening = () => stoppers.forEach((stop) => stop());
162
-
163
- try {
164
- // We are wrapping this call in try catch, because it can throw errors in case,
165
- // compatibility check was enabled. We want an error to be captured by promise, not by
166
- // another one external try catch.
167
- postEvent(method as any, methodParams);
168
- } catch (e) {
169
- stopListening();
170
- rej(e);
171
- }
172
- });
173
- };
174
-
175
- return typeof timeout === 'number' ? withTimeout(execute, timeout) : execute();
176
- }
@@ -1,20 +0,0 @@
1
- import { expect, it } from 'vitest';
2
-
3
- import { classNames } from '../classNames';
4
-
5
- it('should ignore all non-empty strings and objects', () => {
6
- expect(classNames('', 2, 'b', null, undefined, false, true, [], 'a', 'c')).toBe('b a c');
7
- });
8
-
9
- it('should pick only keys which values are truthy', () => {
10
- expect(classNames({
11
- a: true,
12
- b: null,
13
- c: false,
14
- d: undefined,
15
- e: {},
16
- f: 3,
17
- g: 0,
18
- h: '',
19
- })).toBe('a e f');
20
- });
@@ -1,21 +0,0 @@
1
- import { expect, it } from 'vitest';
2
-
3
- import { mergeClassNames } from '../mergeClassNames';
4
-
5
- it('should ignore non-object values', () => {
6
- expect(mergeClassNames({}, null, undefined, false, true, { tma: 'good' })).toStrictEqual({ tma: 'good' });
7
- });
8
-
9
- it('should merge objects keys by values applying classNames function', () => {
10
- expect(mergeClassNames(
11
- { a: 'hey there', b: 'space' },
12
- { a: 'John', b: 'station' },
13
- { c: 'wowow' },
14
- { f: { mod: true, ignore: false, add: 'non empty string' } },
15
- )).toStrictEqual({
16
- a: 'hey there John',
17
- b: 'space station',
18
- c: 'wowow',
19
- f: 'mod add',
20
- });
21
- });
@@ -1,34 +0,0 @@
1
- /**
2
- * Inserts a space between a and b in case both of them are
3
- * non-empty strings.
4
- * @param a
5
- * @param b
6
- */
7
- function space(a: string, b: string): string {
8
- return a + (a.length > 0 && b.length > 0 ? ` ${b}` : b);
9
- }
10
-
11
- /**
12
- * Function which joins passed values with space following these rules:
13
- * 1. If value is non-empty string, it will be added to output.
14
- * 2. If value is object, only those keys will be added, which values are truthy.
15
- * 3. All other values are ignored.
16
- *
17
- * You can find this function to similar one from package {@link https://www.npmjs.com/package/classnames|classnames}.
18
- * @param values - values array.
19
- */
20
- export function classNames(...values: any[]): string {
21
- return values.reduce<string>((acc, value) => {
22
- let formattedValue = '';
23
-
24
- if (typeof value === 'string') {
25
- formattedValue = value;
26
- } else if (typeof value === 'object' && value !== null) {
27
- formattedValue = Object
28
- .entries(value)
29
- .reduce<string>((valueAcc, [className, enable]) => (enable ? space(valueAcc, className) : valueAcc), '');
30
- }
31
-
32
- return space(acc, formattedValue);
33
- }, '');
34
- }
@@ -1,2 +0,0 @@
1
- export * from './classNames.js';
2
- export * from './mergeClassNames.js';
@@ -1,60 +0,0 @@
1
- import { isRecord } from '~/misc/index.js';
2
-
3
- import { classNames } from './classNames.js';
4
-
5
- /**
6
- * Returns union keys removing those, which values are not strings.
7
- */
8
- type UnionStringKeys<U> = U extends U
9
- ? {
10
- [K in keyof U]-?: U[K] extends string | undefined ? K : never;
11
- }[keyof U]
12
- : never;
13
-
14
- /**
15
- * Returns union required keys.
16
- */
17
- type UnionRequiredKeys<U> = U extends U
18
- ? {
19
- [K in UnionStringKeys<U>]: ({} extends Pick<U, K> ? never : K)
20
- }[UnionStringKeys<U>]
21
- : never;
22
-
23
- /**
24
- * Returns union optional keys.
25
- */
26
- type UnionOptionalKeys<U> = Exclude<UnionStringKeys<U>, UnionRequiredKeys<U>>;
27
-
28
- type MergeClassNames<Tuple extends any[]> =
29
- // Removes all types from union which will be ignored by the mergeClassNames function.
30
- Exclude<Tuple[number], number | string | null | undefined | any[] | boolean> extends infer Union
31
- ? {
32
- [K in UnionRequiredKeys<Union>]: string;
33
- } & {
34
- [K in UnionOptionalKeys<Union>]?: string;
35
- }
36
- : never;
37
-
38
- /**
39
- * Merges 2 sets of parameters. Function expects passing an array of objects with values, which
40
- * could be passed to `classNames` function. As the result, it returns an object with keys
41
- * from all objects with merged values.
42
- * @see classNames
43
- */
44
- export function mergeClassNames<T extends any[]>(...partials: T): MergeClassNames<T> {
45
- return partials.reduce<MergeClassNames<T>>((acc, partial) => {
46
- if (!isRecord(partial)) {
47
- return acc;
48
- }
49
-
50
- Object.entries(partial).forEach(([key, value]) => {
51
- const className = classNames((acc as any)[key], value);
52
-
53
- if (className.length > 0) {
54
- (acc as any)[key] = className;
55
- }
56
- });
57
-
58
- return acc;
59
- }, {} as MergeClassNames<T>);
60
- }
@@ -1,64 +0,0 @@
1
- import { type PostEvent, postEvent as defaultPostEvent } from '~/bridge/index.js';
2
- import { EventEmitter } from '~/event-emitter/index.js';
3
- import { State } from '~/state/index.js';
4
-
5
- import type {
6
- ClosingBehaviorEvents,
7
- ClosingBehaviorState,
8
- } from './types.js';
9
-
10
- /**
11
- * Component responsible for controlling current closing confirmation
12
- * status.
13
- */
14
- export class ClosingBehavior {
15
- private readonly ee = new EventEmitter<ClosingBehaviorEvents>();
16
-
17
- private readonly state: State<ClosingBehaviorState>;
18
-
19
- constructor(
20
- isConfirmationNeeded: boolean,
21
- private readonly postEvent: PostEvent = defaultPostEvent,
22
- ) {
23
- this.state = new State({ isConfirmationNeeded }, this.ee);
24
- }
25
-
26
- private set isConfirmationNeeded(value: boolean) {
27
- this.state.set('isConfirmationNeeded', value);
28
- this.postEvent('web_app_setup_closing_behavior', { need_confirmation: value });
29
- }
30
-
31
- /**
32
- * Returns true, if the confirmation dialog enabled while the user is trying
33
- * to close the Mini App.
34
- */
35
- get isConfirmationNeeded(): boolean {
36
- return this.state.get('isConfirmationNeeded');
37
- }
38
-
39
- /**
40
- * Disables the confirmation dialog while the user is trying to close the
41
- * Mini App.
42
- */
43
- disableConfirmation(): void {
44
- this.isConfirmationNeeded = false;
45
- }
46
-
47
- /**
48
- * Enables the confirmation dialog while the user is trying to close the
49
- * Mini App.
50
- */
51
- enableConfirmation(): void {
52
- this.isConfirmationNeeded = true;
53
- }
54
-
55
- /**
56
- * Adds new event listener.
57
- */
58
- on = this.ee.on.bind(this.ee);
59
-
60
- /**
61
- * Removes event listener.
62
- */
63
- off = this.ee.off.bind(this.ee);
64
- }
@@ -1,86 +0,0 @@
1
- import { describe, expect, it, vi } from 'vitest';
2
-
3
- import { ClosingBehavior } from '../ClosingBehavior';
4
-
5
- describe('disable', () => {
6
- it('should call "web_app_setup_closing_behavior" method with "need_confirmation" equal to false', () => {
7
- const postEvent = vi.fn();
8
- const confirmation = new ClosingBehavior(true, postEvent);
9
-
10
- expect(postEvent).toHaveBeenCalledTimes(0);
11
- confirmation.disableConfirmation();
12
- expect(postEvent).toHaveBeenCalledTimes(1);
13
- expect(postEvent).toHaveBeenCalledWith('web_app_setup_closing_behavior', { need_confirmation: false });
14
- });
15
-
16
- it('should emit "isConfirmationNeededChanged" event with false value', () => {
17
- const confirmation = new ClosingBehavior(true, vi.fn());
18
- const listener = vi.fn();
19
-
20
- confirmation.on('change:isConfirmationNeeded', listener);
21
- expect(listener).toHaveBeenCalledTimes(0);
22
- confirmation.disableConfirmation();
23
- expect(listener).toHaveBeenCalledTimes(1);
24
- expect(listener).toHaveBeenCalledWith(false);
25
- });
26
- });
27
-
28
- describe('enable', () => {
29
- it('should call "web_app_setup_closing_behavior" method with "need_confirmation" equal to true', () => {
30
- const postEvent = vi.fn();
31
- const confirmation = new ClosingBehavior(false, postEvent);
32
-
33
- expect(postEvent).toHaveBeenCalledTimes(0);
34
- confirmation.enableConfirmation();
35
- expect(postEvent).toHaveBeenCalledTimes(1);
36
- expect(postEvent).toHaveBeenCalledWith('web_app_setup_closing_behavior', { need_confirmation: true });
37
- });
38
-
39
- it('should emit "isConfirmationNeededChanged" event with true value', () => {
40
- const confirmation = new ClosingBehavior(false, vi.fn());
41
- const listener = vi.fn();
42
-
43
- confirmation.on('change:isConfirmationNeeded', listener);
44
- expect(listener).toHaveBeenCalledTimes(0);
45
- confirmation.enableConfirmation();
46
- expect(listener).toHaveBeenCalledTimes(1);
47
- expect(listener).toHaveBeenCalledWith(true);
48
- });
49
- });
50
-
51
- describe('on', () => {
52
- describe('"isConfirmationNeededChanged" event', () => {
53
- it('should add event listener to event', () => {
54
- const listener = vi.fn();
55
- const confirmation = new ClosingBehavior(false, vi.fn());
56
-
57
- confirmation.on('change:isConfirmationNeeded', listener);
58
-
59
- expect(listener).toHaveBeenCalledTimes(0);
60
- confirmation.enableConfirmation();
61
- expect(listener).toHaveBeenCalledTimes(1);
62
- });
63
- });
64
- });
65
-
66
- describe('off', () => {
67
- describe('"isConfirmationNeededChanged" event', () => {
68
- it('should remove event listener from event', () => {
69
- const listener = vi.fn();
70
- const confirmation = new ClosingBehavior(false, vi.fn());
71
-
72
- confirmation.on('change:isConfirmationNeeded', listener);
73
-
74
- expect(listener).toHaveBeenCalledTimes(0);
75
- confirmation.enableConfirmation();
76
- expect(listener).toHaveBeenCalledTimes(1);
77
-
78
- confirmation.off('change:isConfirmationNeeded', listener);
79
- listener.mockClear();
80
-
81
- expect(listener).toHaveBeenCalledTimes(0);
82
- confirmation.disableConfirmation();
83
- expect(listener).toHaveBeenCalledTimes(0);
84
- });
85
- });
86
- });
@@ -1,2 +0,0 @@
1
- export * from './ClosingBehavior.js';
2
- export * from './types.js';
@@ -1,12 +0,0 @@
1
- import type { StateEvents } from '~/state/index.js';
2
-
3
- export interface ClosingBehaviorState {
4
- isConfirmationNeeded: boolean;
5
- }
6
-
7
- export type ClosingBehaviorEvents = StateEvents<ClosingBehaviorState>;
8
-
9
- export type ClosingBehaviorEventName = keyof ClosingBehaviorEvents;
10
-
11
- export type ClosingBehaviorEventListener<E extends ClosingBehaviorEventName> =
12
- ClosingBehaviorEvents[E];
@@ -1,138 +0,0 @@
1
- import {
2
- invokeCustomMethod,
3
- postEvent as defaultPostEvent,
4
- } from '~/bridge/index.js';
5
- import {
6
- array,
7
- json,
8
- string,
9
- } from '~/parsing/index.js';
10
- import {
11
- createSupportsFunc,
12
- type SupportsFunc,
13
- } from '~/supports/index.js';
14
- import type { CreateRequestIdFunc, ExecuteWithTimeout } from '~/types/index.js';
15
- import type { Version } from '~/version/index.js';
16
-
17
- function objectFromKeys<K extends string, V>(keys: K[], value: V): Record<K, V> {
18
- return keys.reduce<Record<K, V>>((acc, key) => {
19
- acc[key] = value;
20
- return acc;
21
- }, {} as Record<K, V>);
22
- }
23
-
24
- export class CloudStorage {
25
- constructor(
26
- version: Version,
27
- private readonly createRequestId: CreateRequestIdFunc,
28
- private readonly postEvent = defaultPostEvent,
29
- ) {
30
- this.supports = createSupportsFunc(version, {
31
- delete: 'web_app_invoke_custom_method',
32
- get: 'web_app_invoke_custom_method',
33
- getKeys: 'web_app_invoke_custom_method',
34
- set: 'web_app_invoke_custom_method',
35
- });
36
- }
37
-
38
- /**
39
- * Deletes specified key or keys from the cloud storage.
40
- * @param keyOrKeys - key or keys to delete.
41
- * @param options - request execution options.
42
- */
43
- async delete(keyOrKeys: string | string[], options: ExecuteWithTimeout = {}): Promise<void> {
44
- const keys = Array.isArray(keyOrKeys) ? keyOrKeys : [keyOrKeys];
45
- if (keys.length === 0) {
46
- return;
47
- }
48
-
49
- await invokeCustomMethod(
50
- 'deleteStorageValues',
51
- { keys },
52
- this.createRequestId(),
53
- { ...options, postEvent: this.postEvent },
54
- );
55
- }
56
-
57
- /**
58
- * Returns list of all keys presented in the cloud storage.
59
- * @param options - request execution options.
60
- */
61
- async getKeys(options: ExecuteWithTimeout = {}): Promise<string[]> {
62
- const result = await invokeCustomMethod(
63
- 'getStorageKeys',
64
- {},
65
- this.createRequestId(),
66
- { ...options, postEvent: this.postEvent },
67
- );
68
-
69
- return array().of(string()).parse(result);
70
- }
71
-
72
- /**
73
- * Returns map, where key is one of the specified in keys argument, and value is according
74
- * storage value.
75
- * @param keys - keys list.
76
- * @param options - request execution options.
77
- */
78
- get<K extends string>(
79
- keys: K[],
80
- options?: ExecuteWithTimeout,
81
- ): Promise<Record<K, string>>;
82
-
83
- /**
84
- * Returns value of the specified key.
85
- * @param key - cloud storage key.
86
- * @param options - request execution options.
87
- * @return Value of the specified key. In case, key was not created previously, function
88
- * will return empty string.
89
- */
90
- get(key: string, options?: ExecuteWithTimeout): Promise<string>;
91
-
92
- async get(
93
- keyOrKeys: string | string[],
94
- options: ExecuteWithTimeout = {},
95
- ): Promise<string | Record<string, string>> {
96
- const keys = Array.isArray(keyOrKeys) ? keyOrKeys : [keyOrKeys];
97
- if (keys.length === 0) {
98
- return objectFromKeys<string, string>(keys, '');
99
- }
100
-
101
- const schema = json(
102
- objectFromKeys(keys, string()),
103
- );
104
- const result = await invokeCustomMethod(
105
- 'getStorageValues',
106
- { keys },
107
- this.createRequestId(),
108
- { ...options, postEvent: this.postEvent },
109
- ).then((data) => schema.parse(data));
110
-
111
- return Array.isArray(keyOrKeys) ? result : result[keyOrKeys];
112
- }
113
-
114
- /**
115
- * Saves specified value by key.
116
- * @param key - storage key.
117
- * @param value - storage value.
118
- * @param options - request execution options.
119
- */
120
- async set(key: string, value: string, options: ExecuteWithTimeout = {}): Promise<void> {
121
- await invokeCustomMethod(
122
- 'saveStorageValue',
123
- { key, value },
124
- this.createRequestId(),
125
- { ...options, postEvent: this.postEvent },
126
- );
127
- }
128
-
129
- /**
130
- * Checks if specified method is supported by current component.
131
- */
132
- supports: SupportsFunc<
133
- | 'delete'
134
- | 'get'
135
- | 'getKeys'
136
- | 'set'
137
- >;
138
- }
@@ -1 +0,0 @@
1
- export * from './CloudStorage.js';
@@ -1,12 +0,0 @@
1
- import { expect, it } from 'vitest';
2
-
3
- import { isColorDark } from '../isColorDark';
4
-
5
- it('should return true in case, the value which is equal to Math.sqrt(0.299 * R * R + 0.587 * G * G + 0.114 * B * B) is less than 120 and false otherwise', () => {
6
- expect(isColorDark('#17212b')).toBe(true);
7
- expect(isColorDark('#f5f5f5')).toBe(false);
8
- });
9
-
10
- it('should throw an error in case, passed value has not convertable to RGB format', () => {
11
- expect(() => isColorDark('abc')).toThrow();
12
- });
@@ -1,13 +0,0 @@
1
- import { expect, it } from 'vitest';
2
-
3
- import { isRGB } from '../isRGB';
4
-
5
- it('should return true for correct full RGB representation', () => {
6
- expect(isRGB('#ffffff')).toBe(true);
7
- });
8
-
9
- it('should return false for any other value', () => {
10
- ['abc', '#ffff', '#fff', '#fffffg'].forEach((v) => {
11
- expect(isRGB(v)).toBe(false);
12
- });
13
- });
@@ -1,13 +0,0 @@
1
- import { expect, it } from 'vitest';
2
-
3
- import { isRGBShort } from '../isRGBShort';
4
-
5
- it('should return true for correct short RGB representation', () => {
6
- expect(isRGBShort('#fff')).toBe(true);
7
- });
8
-
9
- it('should return false for any other value', () => {
10
- ['abc', '#ffff', '#ffffff', '#ggg'].forEach((v) => {
11
- expect(isRGBShort(v)).toBe(false);
12
- });
13
- });