@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,39 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
-
3
- import { array } from '../array';
4
- import { string } from '../string';
5
-
6
- it('should return value in case, it is array', () => {
7
- expect(array().parse(['abc'])).toStrictEqual(['abc']);
8
- });
9
-
10
- it('should correctly parse JSON Array presented as string', () => {
11
- expect(array().parse('["abc"]')).toStrictEqual(['abc']);
12
- });
13
-
14
- it('should throw an error in case, passed value is not array', () => {
15
- expect(() => array().parse(true)).toThrow();
16
- expect(() => array().parse(1)).toThrow();
17
- expect(() => array().parse('okay')).toThrow();
18
- expect(() => array().parse({})).toThrow();
19
- });
20
-
21
- describe('of', () => {
22
- it('should correctly apply item parser to each array item', () => {
23
- expect(array().of(string()).parse(['abc'])).toStrictEqual(['abc']);
24
- });
25
-
26
- it('should throw an error in case, item parser was unable to parse value', () => {
27
- expect(() => array().of(string()).parse(['abc', {}])).toThrow();
28
- });
29
-
30
- it('should use parsing function directly if it is not ValueParser', () => {
31
- expect(array().of(() => 'Hello!').parse(['abc'])).toStrictEqual(['Hello!']);
32
- });
33
- });
34
-
35
- describe('optional', () => {
36
- it('should return undefined if value is undefined', () => {
37
- expect(array().optional().parse(undefined)).toBe(undefined);
38
- });
39
- });
@@ -1,31 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
-
3
- import { boolean } from '../boolean';
4
-
5
- it('should return value in case, it has type boolean', () => {
6
- expect(boolean().parse(true)).toBe(true);
7
- expect(boolean().parse(false)).toBe(false);
8
- });
9
-
10
- it('should return true in case, value string representation equals "1" or "true"', () => {
11
- expect(boolean().parse(1)).toBe(true);
12
- expect(boolean().parse('1')).toBe(true);
13
- expect(boolean().parse('true')).toBe(true);
14
- });
15
-
16
- it('should return false in case, value string representation equals "0" or "false"', () => {
17
- expect(boolean().parse(0)).toBe(false);
18
- expect(boolean().parse('0')).toBe(false);
19
- expect(boolean().parse('false')).toBe(false);
20
- });
21
-
22
- it('should throw an error in case, passed value is not of type boolean and its string representation is not "0", "1", "true" or "false"', () => {
23
- expect(() => boolean().parse('true!')).toThrow();
24
- expect(() => boolean().parse({})).toThrow();
25
- });
26
-
27
- describe('optional', () => {
28
- it('should return undefined if value is undefined', () => {
29
- expect(boolean().optional().parse(undefined)).toBe(undefined);
30
- });
31
- });
@@ -1,25 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
-
3
- import { date } from '../date';
4
-
5
- it('should return value in case, it is instance of Date', () => {
6
- const d = new Date();
7
- expect(date().parse(d)).toBe(d);
8
- });
9
-
10
- it('should throw an error in case, passed value cannot be converted to number', () => {
11
- expect(() => date().parse('true')).toThrow();
12
- expect(() => date().parse({})).toThrow();
13
- });
14
-
15
- it('should create date multiplying value by 1000 in case this value can be converted to number', () => {
16
- const a = new Date(1000);
17
- expect(date().parse('1')).toStrictEqual(a);
18
- expect(date().parse(1)).toStrictEqual(a);
19
- });
20
-
21
- describe('optional', () => {
22
- it('should return undefined if value is undefined', () => {
23
- expect(date().optional().parse(undefined)).toBe(undefined);
24
- });
25
- });
@@ -1,80 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
-
3
- import { boolean } from '../boolean';
4
- import { json } from '../json';
5
- import { number } from '../number';
6
- import { string } from '../string';
7
-
8
- it('should throw an error in case, passed value is not JSON object or not JSON object converted to string', () => {
9
- const parser = json({});
10
- expect(() => parser.parse('')).toThrow();
11
- expect(() => parser.parse(true)).toThrow();
12
- expect(() => parser.parse('{}')).not.toThrow();
13
- expect(() => parser.parse({})).not.toThrow();
14
- });
15
-
16
- it('should throw an error in case, passed value does not contain required field presented in schema', () => {
17
- const parser = json({ prop: string() });
18
- expect(() => parser.parse({})).toThrow();
19
- });
20
-
21
- it('should ignore field in case its value is undefined', () => {
22
- const parser = json<{ prop?: string }>({ prop: undefined });
23
- expect(parser.parse({})).toStrictEqual({});
24
- });
25
-
26
- describe('field definition as object', () => {
27
- it('should extract field value from the specified property "from"', () => {
28
- const parser = json({
29
- a: {
30
- type: (value) => value,
31
- from: 'b',
32
- },
33
- });
34
- expect(parser.parse({ b: 'Hello there!' })).toStrictEqual({ a: 'Hello there!' });
35
- });
36
-
37
- it('should extract field value from schema field name, if "from" was not specified', () => {
38
- const parser = json({
39
- a: {
40
- type: (value) => value,
41
- },
42
- });
43
- expect(parser.parse({ a: 'Hello there!' })).toStrictEqual({ a: 'Hello there!' });
44
- });
45
-
46
- it('should apply parser passed as ValueParser', () => {
47
- const parser = json({
48
- a: {
49
- type: string(),
50
- },
51
- });
52
- expect(parser.parse({ a: 'Hello there!' })).toStrictEqual({ a: 'Hello there!' });
53
- });
54
- });
55
-
56
- it('should also apply parser presented as function', () => {
57
- const parser = json({
58
- prop: () => 'static value',
59
- });
60
- expect(parser.parse({ prop: 999 })).toStrictEqual({ prop: 'static value' });
61
- });
62
-
63
- it('should throw an error in case, passed value contains field of different type presented in schema', () => {
64
- const parser = json({ prop: string() });
65
- expect(() => parser.parse({ prop: {} })).toThrow();
66
- });
67
-
68
- it('should correctly parse built-in types', () => {
69
- const parser = json({
70
- bool: boolean(),
71
- string: string(),
72
- number: number(),
73
- });
74
- const obj = {
75
- bool: true,
76
- string: '123',
77
- number: 999,
78
- };
79
- expect(parser.parse(obj)).toStrictEqual(obj);
80
- });
@@ -1,23 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
-
3
- import { number } from '../number';
4
-
5
- it('should return value in case, it has type number', () => {
6
- expect(number().parse(9992)).toBe(9992);
7
- });
8
-
9
- it('should return value converted to number in case, it is number converted to string', () => {
10
- expect(number().parse('9992')).toBe(9992);
11
- });
12
-
13
- it('should throw an error in case, passed value is not of type number or does not represent number converted to string', () => {
14
- expect(() => number().parse(true)).toThrow();
15
- expect(() => number().parse('vvv')).toThrow();
16
- expect(() => number().parse({})).toThrow();
17
- });
18
-
19
- describe('optional', () => {
20
- it('should return undefined if value is undefined', () => {
21
- expect(number().optional().parse(undefined)).toBe(undefined);
22
- });
23
- });
@@ -1,22 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
-
3
- import { rgb } from '../rgb';
4
-
5
- it('should return value in case, it represents RGB color', () => {
6
- expect(rgb().parse('#fff')).toBe('#ffffff');
7
- });
8
-
9
- it('should throw an error in case, passed value is not of type string', () => {
10
- expect(() => rgb().parse(true)).toThrow();
11
- expect(() => rgb().parse({})).toThrow();
12
- });
13
-
14
- it('should throw an error in case, passed value does not represent RGB string', () => {
15
- expect(() => rgb().parse('my custom string')).toThrow();
16
- });
17
-
18
- describe('optional', () => {
19
- it('should return undefined if value is undefined', () => {
20
- expect(rgb().optional().parse(undefined)).toBe(undefined);
21
- });
22
- });
@@ -1,105 +0,0 @@
1
- import { expect, it } from 'vitest';
2
-
3
- import { date } from '../date';
4
- import { searchParams } from '../searchParams';
5
- import { string } from '../string';
6
-
7
- it('should throw an error in case, passed value is not of type string or URLSearchParams', () => {
8
- const parser = searchParams({});
9
- expect(() => parser.parse(true)).toThrow();
10
- expect(() => parser.parse({})).toThrow();
11
- expect(() => parser.parse('true')).not.toThrow();
12
- expect(() => parser.parse(new URLSearchParams())).not.toThrow();
13
- });
14
-
15
- it('should throw an error in case, passed value does not contain required field presented in schema', () => {
16
- const parser = searchParams({ prop: string() });
17
-
18
- try {
19
- parser.parse('abc=123');
20
- } catch (e) {
21
- expect(e).toMatchObject({
22
- message: 'Unable to parse value',
23
- cause: {
24
- message: 'Unable to parse field "prop" as string',
25
- cause: {
26
- message: 'Unable to parse value as string',
27
- cause: {
28
- message: 'Value has unexpected type',
29
- },
30
- },
31
- },
32
- });
33
- }
34
- expect.assertions(1);
35
- });
36
-
37
- it('should not throw an error in case, passed value does not contain optional field presented in schema', () => {
38
- const parser = searchParams<{ prop?: string }>({
39
- prop: string().optional(),
40
- });
41
- expect(parser.parse('')).toEqual({});
42
- expect(parser.parse('prop=abc')).toEqual({ prop: 'abc' });
43
- });
44
-
45
- it('should use parser with unspecified type', () => {
46
- const parser = searchParams<{ prop: unknown }>({
47
- prop: () => {
48
- throw new Error('Just an error');
49
- },
50
- });
51
-
52
- try {
53
- parser.parse('prop=');
54
- } catch (e) {
55
- expect(e).toMatchObject({
56
- message: 'Unable to parse value',
57
- cause: {
58
- message: 'Unable to parse field "prop"',
59
- cause: {
60
- message: 'Just an error',
61
- },
62
- },
63
- });
64
- }
65
- expect.assertions(1);
66
- });
67
-
68
- it('should throw an error in case, passed value contains field of different type presented in schema', () => {
69
- const parser = searchParams({ prop: date() });
70
-
71
- try {
72
- parser.parse('prop=abc');
73
- } catch (e) {
74
- expect(e).toMatchObject({
75
- message: 'Unable to parse value',
76
- cause: {
77
- message: 'Unable to parse field "prop" as Date',
78
- cause: {
79
- message: 'Unable to parse value as Date',
80
- cause: {
81
- message: 'Unable to parse value as number',
82
- cause: {
83
- message: 'Value has unexpected type',
84
- },
85
- },
86
- },
87
- },
88
- });
89
- }
90
- expect.assertions(1);
91
- });
92
-
93
- it('should correctly parse built-in types', () => {
94
- const parser = searchParams({
95
- date: date(),
96
- string: string(),
97
- });
98
- const params = new URLSearchParams();
99
- params.set('date', '66653332');
100
- params.set('string', 'some string');
101
- expect(parser.parse(params)).toEqual({
102
- date: new Date(66653332000),
103
- string: 'some string',
104
- });
105
- });
@@ -1,25 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
-
3
- import { string } from '../string';
4
-
5
- it('should return value in case, it has type string', () => {
6
- expect(string().parse('abc')).toBe('abc');
7
- });
8
-
9
- it('should convert value to string in case, it has type number', () => {
10
- expect(string().parse(1)).toBe('1');
11
- });
12
-
13
- it('should throw an error in case, passed value is not string or number', () => {
14
- expect(() => string().parse({})).toThrow();
15
- expect(() => string().parse([])).toThrow();
16
- expect(() => string().parse(false)).toThrow();
17
- expect(() => string().parse(null)).toThrow();
18
- expect(() => string().parse(undefined)).toThrow();
19
- });
20
-
21
- describe('optional', () => {
22
- it('should return undefined if value is undefined', () => {
23
- expect(string().optional().parse(undefined)).toBe(undefined);
24
- });
25
- });
@@ -1,9 +0,0 @@
1
- import { ArrayValueParser } from '../ArrayValueParser.js';
2
-
3
- /**
4
- * Parses incoming value as an array.
5
- * @param type - parser type name.
6
- */
7
- export function array(type?: string): ArrayValueParser<unknown, false> {
8
- return new ArrayValueParser((value) => value, false, type);
9
- }
@@ -1,22 +0,0 @@
1
- import { createValueParserGenerator } from '../createValueParserGenerator.js';
2
- import { unexpectedTypeError } from '../unexpectedTypeError.js';
3
-
4
- /**
5
- * Returns parser to parse value as boolean.
6
- */
7
- export const boolean = createValueParserGenerator<boolean>((value) => {
8
- if (typeof value === 'boolean') {
9
- return value;
10
- }
11
- const asString = String(value);
12
-
13
- if (asString === '1' || asString === 'true') {
14
- return true;
15
- }
16
-
17
- if (asString === '0' || asString === 'false') {
18
- return false;
19
- }
20
-
21
- throw unexpectedTypeError();
22
- }, 'boolean');
@@ -1,11 +0,0 @@
1
- import { number } from './number.js';
2
- import { createValueParserGenerator } from '../createValueParserGenerator.js';
3
-
4
- /**
5
- * Returns parser to parse value as Date.
6
- */
7
- export const date = createValueParserGenerator<Date>((value) => (
8
- value instanceof Date
9
- ? value
10
- : new Date(number().parse(value) * 1000)
11
- ), 'Date');
@@ -1,8 +0,0 @@
1
- export * from './array.js';
2
- export * from './boolean.js';
3
- export * from './date.js';
4
- export * from './json.js';
5
- export * from './number.js';
6
- export * from './rgb.js';
7
- export * from './searchParams.js';
8
- export * from './string.js';
@@ -1,17 +0,0 @@
1
- import { parseBySchema } from '../parseBySchema.js';
2
- import { toRecord } from '../toRecord.js';
3
- import type { Schema } from '../types.js';
4
- import { ValueParser } from '../ValueParser.js';
5
-
6
- /**
7
- * Creates new Json parser according to passed schema.
8
- * @param schema - object schema.
9
- * @param type - parser type name.
10
- */
11
- export function json<T>(schema: Schema<T>, type?: string): ValueParser<T, false> {
12
- return new ValueParser((value) => {
13
- const record = toRecord(value);
14
-
15
- return parseBySchema(schema, (field) => record[field]);
16
- }, false, type);
17
- }
@@ -1,21 +0,0 @@
1
- import { createValueParserGenerator } from '../createValueParserGenerator.js';
2
- import { unexpectedTypeError } from '../unexpectedTypeError.js';
3
-
4
- /**
5
- * Returns parser to parse value as number.
6
- */
7
- export const number = createValueParserGenerator<number>((value) => {
8
- if (typeof value === 'number') {
9
- return value;
10
- }
11
-
12
- if (typeof value === 'string') {
13
- const num = Number(value);
14
-
15
- if (!Number.isNaN(num)) {
16
- return num;
17
- }
18
- }
19
-
20
- throw unexpectedTypeError();
21
- }, 'number');
@@ -1,10 +0,0 @@
1
- import { toRGB } from '~/colors/index.js';
2
- import type { RGB } from '~/colors/index.js';
3
-
4
- import { string } from './string.js';
5
- import { createValueParserGenerator } from '../createValueParserGenerator.js';
6
-
7
- /**
8
- * Returns parser to parse value as RGB color.
9
- */
10
- export const rgb = createValueParserGenerator<RGB>((value) => toRGB(string().parse(value)), 'rgb');
@@ -1,24 +0,0 @@
1
- import { parseBySchema } from '../parseBySchema.js';
2
- import type { Schema } from '../types.js';
3
- import { unexpectedTypeError } from '../unexpectedTypeError.js';
4
- import { ValueParser } from '../ValueParser.js';
5
-
6
- /**
7
- * Creates new search params parser according to passed schema.
8
- * @param schema - object schema.
9
- * @param type - parser type name.
10
- */
11
- export function searchParams<T>(schema: Schema<T>, type?: string): ValueParser<T, false> {
12
- return new ValueParser((value) => {
13
- if (typeof value !== 'string' && !(value instanceof URLSearchParams)) {
14
- throw unexpectedTypeError();
15
- }
16
-
17
- const params = typeof value === 'string' ? new URLSearchParams(value) : value;
18
-
19
- return parseBySchema(schema, (field) => {
20
- const paramValue = params.get(field);
21
- return paramValue === null ? undefined : paramValue;
22
- });
23
- }, false, type);
24
- }
@@ -1,12 +0,0 @@
1
- import { createValueParserGenerator } from '../createValueParserGenerator.js';
2
- import { unexpectedTypeError } from '../unexpectedTypeError.js';
3
-
4
- /**
5
- * Returns parser to parse value as string.
6
- */
7
- export const string = createValueParserGenerator<string>((value) => {
8
- if (typeof value === 'string' || typeof value === 'number') {
9
- return value.toString();
10
- }
11
- throw unexpectedTypeError();
12
- }, 'string');
@@ -1,27 +0,0 @@
1
- import { unexpectedTypeError } from './unexpectedTypeError.js';
2
-
3
- /**
4
- * Converts value to record.
5
- * @param value - value to convert.
6
- * @throws {Error} Value passed as a string does not represent JSON object.
7
- * @throws {Error} Value is not convertable.
8
- */
9
- export function toRecord(value: unknown): Record<string, unknown> {
10
- let formattedValue: any = value;
11
-
12
- // Convert value to JSON in case, it is string. We expect value to be JSON string.
13
- if (typeof formattedValue === 'string') {
14
- formattedValue = JSON.parse(formattedValue);
15
- }
16
-
17
- // We expect json to be usual object.
18
- if (
19
- typeof formattedValue !== 'object'
20
- || formattedValue === null
21
- || Array.isArray(formattedValue)
22
- ) {
23
- throw unexpectedTypeError();
24
- }
25
-
26
- return formattedValue;
27
- }
@@ -1,32 +0,0 @@
1
- /**
2
- * Represents any known parser.
3
- */
4
- export type AnyParser<T> = Parser<T> | { parse: Parser<T> };
5
-
6
- /**
7
- * Detailed schema field options.
8
- */
9
- export interface SchemaFieldDetailed<T> {
10
- /**
11
- * Source property name.
12
- * @default Original property name in schema.
13
- */
14
- from?: string;
15
-
16
- /**
17
- * Function which should parse incoming value and return expected type.
18
- */
19
- type: AnyParser<T>;
20
- }
21
-
22
- /**
23
- * Function which parses incoming value.
24
- */
25
- export type Parser<T> = (value: unknown) => T;
26
-
27
- /**
28
- * Object schema definition.
29
- */
30
- export type Schema<T> = {
31
- [K in keyof T]: AnyParser<T[K]> | SchemaFieldDetailed<T[K]>;
32
- };
@@ -1,6 +0,0 @@
1
- /**
2
- * Creates instance of TypeError stating, that value has unexpected type.
3
- */
4
- export function unexpectedTypeError(): TypeError {
5
- return new TypeError('Value has unexpected type');
6
- }
@@ -1,91 +0,0 @@
1
- import {
2
- type PostEvent,
3
- postEvent as defaultPostEvent,
4
- request,
5
- } from '~/bridge/index.js';
6
- import { EventEmitter } from '~/event-emitter/index.js';
7
- import { State } from '~/state/index.js';
8
- import {
9
- createSupportsFunc,
10
- type SupportsFunc,
11
- } from '~/supports/index.js';
12
- import type { Version } from '~/version/index.js';
13
-
14
- import { preparePopupParams } from './preparePopupParams.js';
15
- import type { OpenPopupOptions, PopupEvents, PopupState } from './types.js';
16
-
17
- /**
18
- * Controls currently displayed application popup. It allows developers to
19
- * open new custom popups and detect popup-connected events.
20
- */
21
- export class Popup {
22
- private readonly ee = new EventEmitter<PopupEvents>();
23
-
24
- private readonly state: State<PopupState>;
25
-
26
- constructor(
27
- version: Version,
28
- private readonly postEvent: PostEvent = defaultPostEvent,
29
- ) {
30
- this.state = new State({ isOpened: false }, this.ee);
31
- this.supports = createSupportsFunc(version, { open: 'web_app_open_popup' });
32
- }
33
-
34
- private set isOpened(value) {
35
- this.state.set('isOpened', value);
36
- }
37
-
38
- /**
39
- * True if popup is currently opened.
40
- */
41
- get isOpened(): boolean {
42
- return this.state.get('isOpened');
43
- }
44
-
45
- /**
46
- * Adds new event listener.
47
- */
48
- on = this.ee.on.bind(this.ee);
49
-
50
- /**
51
- * Removes event listener.
52
- */
53
- off = this.ee.off.bind(this.ee);
54
-
55
- /**
56
- * A method that shows a native popup described by the `params` argument.
57
- * Promise will be resolved when popup is closed. Resolved value will have
58
- * an identifier of pressed button.
59
- *
60
- * In case, user clicked outside the popup or clicked top right popup close
61
- * button, null will be returned.
62
- *
63
- * FIXME: In desktop, this function may work incorrectly.
64
- * Issue: https://github.com/Telegram-Mini-Apps/tma.js/issues/7
65
- * @param options - popup parameters.
66
- * @throws {Error} Popup is already opened.
67
- */
68
- open(options: OpenPopupOptions): Promise<string | null> {
69
- if (this.isOpened) {
70
- throw new Error('Popup is already opened.');
71
- }
72
-
73
- this.isOpened = true;
74
-
75
- return request(
76
- 'web_app_open_popup',
77
- preparePopupParams(options),
78
- 'popup_closed',
79
- { postEvent: this.postEvent },
80
- )
81
- .then(({ button_id = null }) => button_id)
82
- .finally(() => {
83
- this.isOpened = false;
84
- });
85
- }
86
-
87
- /**
88
- * Checks if specified method is supported by current component.
89
- */
90
- supports: SupportsFunc<'open'>;
91
- }