@tma.js/sdk 0.11.3

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 (127) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +29 -0
  3. package/dist/lib/browser.js +2 -0
  4. package/dist/lib/browser.js.map +1 -0
  5. package/dist/lib/index.cjs +2 -0
  6. package/dist/lib/index.cjs.map +1 -0
  7. package/dist/lib/index.mjs +2 -0
  8. package/dist/lib/index.mjs.map +1 -0
  9. package/dist/types/components/BackButton/BackButton.d.ts +44 -0
  10. package/dist/types/components/BackButton/index.d.ts +2 -0
  11. package/dist/types/components/BackButton/types.d.ts +9 -0
  12. package/dist/types/components/ClosingBehaviour/ClosingBehaviour.d.ts +35 -0
  13. package/dist/types/components/ClosingBehaviour/index.d.ts +2 -0
  14. package/dist/types/components/ClosingBehaviour/types.d.ts +7 -0
  15. package/dist/types/components/CloudStorage/CloudStorage.d.ts +47 -0
  16. package/dist/types/components/CloudStorage/index.d.ts +1 -0
  17. package/dist/types/components/HapticFeedback/HapticFeedback.d.ts +37 -0
  18. package/dist/types/components/HapticFeedback/index.d.ts +1 -0
  19. package/dist/types/components/InitData/InitData.d.ts +52 -0
  20. package/dist/types/components/InitData/index.d.ts +1 -0
  21. package/dist/types/components/MainButton/MainButton.d.ts +114 -0
  22. package/dist/types/components/MainButton/index.d.ts +2 -0
  23. package/dist/types/components/MainButton/types.d.ts +15 -0
  24. package/dist/types/components/Popup/Popup.d.ts +44 -0
  25. package/dist/types/components/Popup/index.d.ts +2 -0
  26. package/dist/types/components/Popup/types.d.ts +60 -0
  27. package/dist/types/components/Popup/utils.d.ts +7 -0
  28. package/dist/types/components/QRScanner/QRScanner.d.ts +40 -0
  29. package/dist/types/components/QRScanner/index.d.ts +2 -0
  30. package/dist/types/components/QRScanner/types.d.ts +7 -0
  31. package/dist/types/components/ThemeParams/ThemeParams.d.ts +73 -0
  32. package/dist/types/components/ThemeParams/index.d.ts +2 -0
  33. package/dist/types/components/ThemeParams/types.d.ts +9 -0
  34. package/dist/types/components/Viewport/Viewport.d.ts +108 -0
  35. package/dist/types/components/Viewport/index.d.ts +2 -0
  36. package/dist/types/components/Viewport/types.d.ts +10 -0
  37. package/dist/types/components/WebApp/WebApp.d.ts +145 -0
  38. package/dist/types/components/WebApp/index.d.ts +2 -0
  39. package/dist/types/components/WebApp/types.d.ts +11 -0
  40. package/dist/types/components/index.d.ts +11 -0
  41. package/dist/types/env.d.ts +8 -0
  42. package/dist/types/errors/MethodNotSupportedError.d.ts +6 -0
  43. package/dist/types/errors/ParameterNotSupportedError.d.ts +6 -0
  44. package/dist/types/errors/index.d.ts +2 -0
  45. package/dist/types/index.d.ts +8 -0
  46. package/dist/types/init/creators/createBackButton.d.ts +9 -0
  47. package/dist/types/init/creators/createClosingBehavior.d.ts +8 -0
  48. package/dist/types/init/creators/createMainButton.d.ts +11 -0
  49. package/dist/types/init/creators/createPostEvent.d.ts +7 -0
  50. package/dist/types/init/creators/createRequestIdGenerator.d.ts +5 -0
  51. package/dist/types/init/creators/createSyncedThemeParams.d.ts +7 -0
  52. package/dist/types/init/creators/createViewport.d.ts +10 -0
  53. package/dist/types/init/creators/createWebApp.d.ts +14 -0
  54. package/dist/types/init/creators/index.d.ts +8 -0
  55. package/dist/types/init/css.d.ts +57 -0
  56. package/dist/types/init/index.d.ts +2 -0
  57. package/dist/types/init/init.d.ts +6 -0
  58. package/dist/types/init/types.d.ts +107 -0
  59. package/dist/types/launch-params.d.ts +20 -0
  60. package/dist/types/state/State.d.ts +15 -0
  61. package/dist/types/state/index.d.ts +2 -0
  62. package/dist/types/state/types.d.ts +30 -0
  63. package/dist/types/storage.d.ts +48 -0
  64. package/dist/types/supports.d.ts +22 -0
  65. package/dist/types/theme-params.d.ts +18 -0
  66. package/dist/types/types.d.ts +14 -0
  67. package/dist/types/url.d.ts +7 -0
  68. package/package.json +67 -0
  69. package/src/components/BackButton/BackButton.ts +90 -0
  70. package/src/components/BackButton/index.ts +2 -0
  71. package/src/components/BackButton/types.ts +13 -0
  72. package/src/components/ClosingBehaviour/ClosingBehaviour.ts +62 -0
  73. package/src/components/ClosingBehaviour/index.ts +6 -0
  74. package/src/components/ClosingBehaviour/types.ts +12 -0
  75. package/src/components/CloudStorage/CloudStorage.ts +135 -0
  76. package/src/components/CloudStorage/index.ts +1 -0
  77. package/src/components/HapticFeedback/HapticFeedback.ts +62 -0
  78. package/src/components/HapticFeedback/index.ts +1 -0
  79. package/src/components/InitData/InitData.ts +116 -0
  80. package/src/components/InitData/index.ts +1 -0
  81. package/src/components/MainButton/MainButton.ts +243 -0
  82. package/src/components/MainButton/index.ts +2 -0
  83. package/src/components/MainButton/types.ts +20 -0
  84. package/src/components/Popup/Popup.ts +81 -0
  85. package/src/components/Popup/index.ts +8 -0
  86. package/src/components/Popup/types.ts +69 -0
  87. package/src/components/Popup/utils.ts +59 -0
  88. package/src/components/QRScanner/QRScanner.ts +87 -0
  89. package/src/components/QRScanner/index.ts +2 -0
  90. package/src/components/QRScanner/types.ts +11 -0
  91. package/src/components/ThemeParams/ThemeParams.ts +154 -0
  92. package/src/components/ThemeParams/index.ts +2 -0
  93. package/src/components/ThemeParams/types.ts +18 -0
  94. package/src/components/Viewport/Viewport.ts +197 -0
  95. package/src/components/Viewport/index.ts +2 -0
  96. package/src/components/Viewport/types.ts +14 -0
  97. package/src/components/WebApp/WebApp.ts +310 -0
  98. package/src/components/WebApp/index.ts +2 -0
  99. package/src/components/WebApp/types.ts +17 -0
  100. package/src/components/index.ts +11 -0
  101. package/src/env.ts +17 -0
  102. package/src/errors/MethodNotSupportedError.ts +9 -0
  103. package/src/errors/ParameterNotSupportedError.ts +9 -0
  104. package/src/errors/index.ts +2 -0
  105. package/src/index.ts +8 -0
  106. package/src/init/creators/createBackButton.ts +22 -0
  107. package/src/init/creators/createClosingBehavior.ts +22 -0
  108. package/src/init/creators/createMainButton.ts +56 -0
  109. package/src/init/creators/createPostEvent.ts +36 -0
  110. package/src/init/creators/createRequestIdGenerator.ts +13 -0
  111. package/src/init/creators/createSyncedThemeParams.ts +14 -0
  112. package/src/init/creators/createViewport.ts +50 -0
  113. package/src/init/creators/createWebApp.ts +49 -0
  114. package/src/init/creators/index.ts +8 -0
  115. package/src/init/css.ts +166 -0
  116. package/src/init/index.ts +2 -0
  117. package/src/init/init.ts +160 -0
  118. package/src/init/types.ts +133 -0
  119. package/src/launch-params.ts +70 -0
  120. package/src/state/State.ts +53 -0
  121. package/src/state/index.ts +2 -0
  122. package/src/state/types.ts +34 -0
  123. package/src/storage.ts +65 -0
  124. package/src/supports.ts +44 -0
  125. package/src/theme-params.ts +34 -0
  126. package/src/types.ts +28 -0
  127. package/src/url.ts +24 -0
@@ -0,0 +1,44 @@
1
+ import type { Version } from '@tma.js/utils';
2
+ import {
3
+ supports,
4
+ type MethodName,
5
+ type HasCheckSupportMethodName,
6
+ type HasCheckSupportMethodParam,
7
+ } from '@tma.js/bridge';
8
+
9
+ export type SupportsFunc<M extends string> = (method: M) => boolean;
10
+
11
+ type HasCheckSupportMethodTuple = {
12
+ [M in HasCheckSupportMethodName]: [M, HasCheckSupportMethodParam<M>]
13
+ }[HasCheckSupportMethodName];
14
+
15
+ /**
16
+ * Returns function, which accepts predefined method name and checks if it is supported
17
+ * via passed schema and version.
18
+ * @param schema - object which contains methods names and TWA method as a dependency.
19
+ * @param version - platform version.
20
+ */
21
+ export function createSupportsFunc<M extends string>(
22
+ version: Version,
23
+ schema: Record<M, MethodName>,
24
+ ): SupportsFunc<M> {
25
+ return (method) => supports(schema[method], version);
26
+ }
27
+
28
+ /**
29
+ * Returns function, which accepts predefined method name and checks if it is supported
30
+ * via passed schema and version.
31
+ * @param schema - object which contains methods names and TWA methods with specified parameter
32
+ * as a dependency.
33
+ * @param version - platform version.
34
+ */
35
+ export function createSupportsParamFunc<P extends string>(
36
+ version: Version,
37
+ schema: Record<P, HasCheckSupportMethodTuple>,
38
+ ): SupportsFunc<P> {
39
+ return (method) => {
40
+ const [tmaMethod, param] = schema[method];
41
+
42
+ return supports(tmaMethod, param, version);
43
+ };
44
+ }
@@ -0,0 +1,34 @@
1
+ import { json, rgb } from '@tma.js/parsing';
2
+
3
+ import type { RGB } from '@tma.js/colors';
4
+
5
+ /**
6
+ * Application theme parameters. Defines palette used by the Telegram application.
7
+ */
8
+ export interface ThemeParamsType {
9
+ backgroundColor?: RGB;
10
+ buttonColor?: RGB;
11
+ buttonTextColor?: RGB;
12
+ hintColor?: RGB;
13
+ linkColor?: RGB;
14
+ secondaryBackgroundColor?: RGB;
15
+ textColor?: RGB;
16
+ }
17
+
18
+ const themeParamsParser = json<ThemeParamsType>({
19
+ backgroundColor: { type: rgb().optional(), from: 'bg_color' },
20
+ buttonColor: { type: rgb().optional(), from: 'button_color' },
21
+ buttonTextColor: { type: rgb().optional(), from: 'button_text_color' },
22
+ hintColor: { type: rgb().optional(), from: 'hint_color' },
23
+ linkColor: { type: rgb().optional(), from: 'link_color' },
24
+ textColor: { type: rgb().optional(), from: 'text_color' },
25
+ secondaryBackgroundColor: { type: rgb().optional(), from: 'secondary_bg_color' },
26
+ });
27
+
28
+ /**
29
+ * Parses incoming value as theme parameters.
30
+ * @param value - value to parse.
31
+ */
32
+ export function parseThemeParams(value: unknown): ThemeParamsType {
33
+ return themeParamsParser.parse(value);
34
+ }
package/src/types.ts ADDED
@@ -0,0 +1,28 @@
1
+ import type { PostEvent as BridgePostEvent } from '@tma.js/bridge';
2
+
3
+ export type PostEvent = BridgePostEvent;
4
+
5
+ /**
6
+ * Color scheme.
7
+ */
8
+ export type ColorScheme = 'dark' | 'light';
9
+
10
+ /**
11
+ * Native Telegram application identifier.
12
+ */
13
+ export type Platform =
14
+ | 'android'
15
+ | 'android_x'
16
+ | 'ios'
17
+ | 'macos'
18
+ | 'tdesktop'
19
+ | 'web'
20
+ | 'weba'
21
+ | 'unigram'
22
+ | 'unknown'
23
+ | string;
24
+
25
+ /**
26
+ * Function which generates unique request identifiers.
27
+ */
28
+ export type CreateRequestIdFunc = () => string;
package/src/url.ts ADDED
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Converts passed URL to its full form.
3
+ * @param url - URL to format.
4
+ * @throws {Error} URL protocol is not supported by OS, or link has not allowed
5
+ * protocol.
6
+ */
7
+ export function formatURL(url: string): string {
8
+ // TODO: Maybe, there is a bit easier way? window.open will accept
9
+ // unformatted URL too, probably.
10
+
11
+ // We do create new anchor element and assign its href to passed URL. This
12
+ // will format link, so it could be used in `window.open`.
13
+ const anchor = document.createElement('a');
14
+ anchor.href = url;
15
+
16
+ // Check if protocol is correct.
17
+ if (anchor.protocol !== 'http:' && anchor.protocol !== 'https:') {
18
+ throw Error(
19
+ 'URL protocol is not supported by OS, or link has not allowed '
20
+ + `protocol: ${anchor.protocol}`,
21
+ );
22
+ }
23
+ return anchor.href;
24
+ }