@tma.js/bridge 1.4.1 → 2.0.1

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 (108) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +44 -13
  3. package/dist/dts/applyPolyfills.d.ts +5 -0
  4. package/dist/dts/base64-url.d.ts +24 -0
  5. package/dist/dts/env/hasWebviewProxy.d.ts +10 -0
  6. package/dist/dts/env/isIframe.d.ts +5 -0
  7. package/dist/dts/env/isTMA.d.ts +31 -0
  8. package/dist/dts/env/mockTelegramEnv.d.ts +57 -0
  9. package/dist/dts/errors.d.ts +27 -0
  10. package/dist/dts/events/createEmitter.d.ts +58 -0
  11. package/dist/dts/events/emitEvent.d.ts +33 -0
  12. package/dist/dts/events/emitter.d.ts +2 -11
  13. package/dist/dts/events/types/events.d.ts +721 -0
  14. package/dist/dts/events/types/index.d.ts +3 -0
  15. package/dist/dts/events/types/listening.d.ts +5 -0
  16. package/dist/dts/events/types/misc.d.ts +16 -0
  17. package/dist/dts/globals.d.ts +45 -14
  18. package/dist/dts/index.d.ts +24 -7
  19. package/dist/dts/launch-params.d.ts +32 -0
  20. package/dist/dts/methods/captureSameReq.d.ts +10 -0
  21. package/dist/dts/methods/createPostEvent.d.ts +27 -7
  22. package/dist/dts/methods/postEvent.d.ts +31 -25
  23. package/dist/dts/methods/postMessage.d.ts +7 -0
  24. package/dist/dts/methods/supports.d.ts +15 -0
  25. package/dist/dts/methods/types/custom-method.d.ts +59 -0
  26. package/dist/dts/methods/types/index.d.ts +6 -0
  27. package/dist/dts/methods/types/methods.d.ts +756 -0
  28. package/dist/dts/methods/types/misc.d.ts +27 -0
  29. package/dist/dts/methods/{popup.d.ts → types/popup.d.ts} +1 -1
  30. package/dist/dts/methods/types/utils.d.ts +9 -0
  31. package/dist/dts/obj-prop-helpers.d.ts +39 -0
  32. package/dist/dts/start-param.d.ts +53 -0
  33. package/dist/dts/utils/compareVersions.d.ts +10 -0
  34. package/dist/dts/utils/invokeCustomMethod.d.ts +34 -0
  35. package/dist/dts/utils/request.d.ts +90 -0
  36. package/dist/index.cjs +3 -1
  37. package/dist/index.cjs.map +1 -1
  38. package/dist/index.iife.js +3 -1
  39. package/dist/index.iife.js.map +1 -1
  40. package/dist/index.js +977 -0
  41. package/dist/index.js.map +1 -0
  42. package/package.json +26 -26
  43. package/dist/dts/env.d.ts +0 -29
  44. package/dist/dts/errors/MethodUnsupportedError.d.ts +0 -8
  45. package/dist/dts/errors/ParameterUnsupportedError.d.ts +0 -8
  46. package/dist/dts/errors/index.d.ts +0 -2
  47. package/dist/dts/events/events.d.ts +0 -130
  48. package/dist/dts/events/index.d.ts +0 -7
  49. package/dist/dts/events/off.d.ts +0 -7
  50. package/dist/dts/events/on.d.ts +0 -10
  51. package/dist/dts/events/onTelegramEvent.d.ts +0 -7
  52. package/dist/dts/events/once.d.ts +0 -9
  53. package/dist/dts/events/parsers/clipboardTextReceived.d.ts +0 -13
  54. package/dist/dts/events/parsers/customMethodInvoked.d.ts +0 -16
  55. package/dist/dts/events/parsers/index.d.ts +0 -9
  56. package/dist/dts/events/parsers/invoiceClosed.d.ts +0 -12
  57. package/dist/dts/events/parsers/phoneRequested.d.ts +0 -8
  58. package/dist/dts/events/parsers/popupClosed.d.ts +0 -8
  59. package/dist/dts/events/parsers/qrTextReceived.d.ts +0 -7
  60. package/dist/dts/events/parsers/theme-changed.d.ts +0 -42
  61. package/dist/dts/events/parsers/viewportChanged.d.ts +0 -19
  62. package/dist/dts/events/parsers/writeAccessRequested.d.ts +0 -8
  63. package/dist/dts/events/subscribe.d.ts +0 -9
  64. package/dist/dts/events/unsubscribe.d.ts +0 -6
  65. package/dist/dts/methods/index.d.ts +0 -6
  66. package/dist/dts/methods/invoke-custom-method.d.ts +0 -24
  67. package/dist/dts/methods/methods.d.ts +0 -330
  68. package/dist/dts/request.d.ts +0 -66
  69. package/dist/dts/shared.d.ts +0 -5
  70. package/dist/dts/supports.d.ts +0 -15
  71. package/dist/index.mjs +0 -317
  72. package/dist/index.mjs.map +0 -1
  73. package/src/env.ts +0 -49
  74. package/src/errors/MethodUnsupportedError.ts +0 -13
  75. package/src/errors/ParameterUnsupportedError.ts +0 -13
  76. package/src/errors/index.ts +0 -2
  77. package/src/events/emitter.ts +0 -126
  78. package/src/events/events.ts +0 -169
  79. package/src/events/index.ts +0 -7
  80. package/src/events/off.ts +0 -12
  81. package/src/events/on.ts +0 -17
  82. package/src/events/onTelegramEvent.ts +0 -83
  83. package/src/events/once.ts +0 -16
  84. package/src/events/parsers/clipboardTextReceived.ts +0 -27
  85. package/src/events/parsers/customMethodInvoked.ts +0 -26
  86. package/src/events/parsers/index.ts +0 -9
  87. package/src/events/parsers/invoiceClosed.ts +0 -26
  88. package/src/events/parsers/phoneRequested.ts +0 -14
  89. package/src/events/parsers/popupClosed.ts +0 -19
  90. package/src/events/parsers/qrTextReceived.ts +0 -14
  91. package/src/events/parsers/theme-changed.ts +0 -58
  92. package/src/events/parsers/viewportChanged.ts +0 -33
  93. package/src/events/parsers/writeAccessRequested.ts +0 -14
  94. package/src/events/subscribe.ts +0 -16
  95. package/src/events/unsubscribe.ts +0 -11
  96. package/src/globals.ts +0 -38
  97. package/src/index.ts +0 -7
  98. package/src/methods/createPostEvent.ts +0 -39
  99. package/src/methods/haptic.ts +0 -52
  100. package/src/methods/index.ts +0 -6
  101. package/src/methods/invoke-custom-method.ts +0 -25
  102. package/src/methods/methods.ts +0 -373
  103. package/src/methods/popup.ts +0 -53
  104. package/src/methods/postEvent.ts +0 -101
  105. package/src/request.ts +0 -168
  106. package/src/shared.ts +0 -5
  107. package/src/supports.ts +0 -82
  108. /package/dist/dts/methods/{haptic.d.ts → types/haptic-feedback.d.ts} +0 -0
@@ -1,330 +0,0 @@
1
- import type { RGB } from '@tma.js/colors';
2
- import type { IsNever, Not, UnionKeys } from '@tma.js/util-types';
3
- import type { PopupParams } from './popup.js';
4
- import type { AnyHapticFeedbackParams } from './haptic.js';
5
- import type { RequestId } from '../shared.js';
6
- import type { AnyInvokeCustomMethodParams } from './invoke-custom-method.js';
7
- /**
8
- * Color key which could be used to update header color.
9
- */
10
- export type HeaderColorKey = 'bg_color' | 'secondary_bg_color';
11
- /**
12
- * Chat type which could be used when calling `web_app_switch_inline_query` method.
13
- */
14
- export type SwitchInlineQueryChatType = 'users' | 'bots' | 'groups' | 'channels';
15
- interface CreateParams<Params = undefined, VersionedParam extends UnionKeys<Params> = never> {
16
- params: Params;
17
- versionedParams: VersionedParam;
18
- }
19
- /**
20
- * Describes list of events and their parameters that could be posted by Bridge.
21
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods
22
- */
23
- export interface Methods {
24
- /**
25
- * Notifies parent iframe about the current frame is ready. This method is only used in the Web
26
- * version of Telegram. As a result, Mini App will receive `set_custom_style` event.
27
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#iframe-ready
28
- */
29
- iframe_ready: CreateParams<{
30
- /**
31
- * True, if current Mini App supports native reloading.
32
- */
33
- reload_supported?: boolean;
34
- } | undefined>;
35
- /**
36
- * Notifies parent iframe about the current iframe is going to reload.
37
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#iframe-will-reload
38
- */
39
- iframe_will_reload: CreateParams;
40
- /**
41
- * Closes Mini App.
42
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-close
43
- */
44
- web_app_close: CreateParams;
45
- /**
46
- * Closes a QR scanner. The Telegram application creates `scan_qr_popup_closed` event.
47
- * @since v6.4
48
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-close-scan-qr-popup
49
- */
50
- web_app_close_scan_qr_popup: CreateParams;
51
- /**
52
- * Sends data to the bot. When this method is called, a service message is sent to the bot
53
- * containing the data of the length up to 4096 bytes. Then, Mini App will be closed.
54
- *
55
- * To get more information, take a look at `web_app_data` field in the
56
- * class [Message](https://core.telegram.org/bots/api#message).
57
- *
58
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-data-send
59
- */
60
- web_app_data_send: CreateParams<{
61
- /**
62
- * Data to send to a bot. Should not have size of more than 4096 bytes.
63
- */
64
- data: string;
65
- }>;
66
- /**
67
- * Expands the Mini App.
68
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-expand
69
- */
70
- web_app_expand: CreateParams;
71
- /**
72
- * Invokes custom method.
73
- * @since v6.9
74
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-invoke-custom-method
75
- */
76
- web_app_invoke_custom_method: CreateParams<AnyInvokeCustomMethodParams>;
77
- /**
78
- * Opens an invoice by its specified slug. More information about invoices in
79
- * this [documentation](https://core.telegram.org/bots/payments).
80
- * @since v6.1
81
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-open-invoice
82
- */
83
- web_app_open_invoice: CreateParams<{
84
- /**
85
- * Invoice unique identifier.
86
- */
87
- slug: string;
88
- }>;
89
- /**
90
- * Opens link in the default browser. Mini App will not be closed.
91
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-open-link
92
- */
93
- web_app_open_link: CreateParams<{
94
- /**
95
- * URL to be opened by Telegram application. Should be a full path with `https` protocol.
96
- */
97
- url: string;
98
- /**
99
- * Link will be opened in Instant View mode if possible.
100
- * @since v6.4
101
- * @see https://instantview.telegram.org/
102
- */
103
- try_instant_view?: boolean;
104
- }, 'try_instant_view'>;
105
- /**
106
- * Opens a new popup. When user closes the popup, Telegram creates the `popup_closed` event.
107
- * @since v6.2
108
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-open-popup
109
- */
110
- web_app_open_popup: CreateParams<PopupParams>;
111
- /**
112
- * Opens a QR scanner. When the scanner was closed, the Telegram application creates
113
- * the `scan_qr_popup_closed` event. When the scanner reads QR, Telegram creates the
114
- * `qr_text_received` event.
115
- * @since v6.4
116
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-open-scan-qr-popup
117
- */
118
- web_app_open_scan_qr_popup: CreateParams<{
119
- /**
120
- * Text to be displayed in the QR scanner.
121
- */
122
- text?: string;
123
- }>;
124
- /**
125
- * Opens the Telegram link by its pathname and query parameters. The link will be opened in the
126
- * Telegram app, Mini App will be closed.
127
- * @since v6.1
128
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-open-tg-link
129
- */
130
- web_app_open_tg_link: CreateParams<{
131
- /**
132
- * Should be a value taken from the link of this format: `https://t.me/{path_full}`. Can
133
- * additionally contain query parameters.
134
- */
135
- path_full: string;
136
- }>;
137
- /**
138
- * Reads text from the clipboard. The method accepts a request identifier which is used to
139
- * appropriately retrieve the method execution result from the `clipboard_text_received` event.
140
- * @since v6.4
141
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-read-text-from-clipboard
142
- */
143
- web_app_read_text_from_clipboard: CreateParams<{
144
- /**
145
- * Unique request identifier. Should be any unique string to handle the generated event
146
- * appropriately.
147
- */
148
- req_id: RequestId;
149
- }>;
150
- /**
151
- * Notifies Telegram about current application is ready to be shown. This method will make
152
- * Telegram to remove application loader and display Mini App.
153
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-ready
154
- */
155
- web_app_ready: CreateParams;
156
- /**
157
- * Requests access to current user's phone.
158
- * @since v6.9
159
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-request-phone
160
- */
161
- web_app_request_phone: CreateParams;
162
- /**
163
- * Requests current theme from Telegram. As a result, Telegram will create `theme_changed` event.
164
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-request-theme
165
- */
166
- web_app_request_theme: CreateParams;
167
- /**
168
- * Requests current viewport information from Telegram. As a result, Telegram will create
169
- * `viewport_changed` event.
170
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-request-viewport
171
- */
172
- web_app_request_viewport: CreateParams;
173
- /**
174
- * Requests write message access to current user.
175
- * @since v6.9
176
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-rqeuest-write-access
177
- */
178
- web_app_request_write_access: CreateParams;
179
- /**
180
- * Updates the Mini App background color.
181
- * @since v6.1
182
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-set-background-color
183
- */
184
- web_app_set_background_color: CreateParams<{
185
- /**
186
- * The Mini App background color in `#RRGGBB` format.
187
- */
188
- color: RGB;
189
- }>;
190
- /**
191
- * Updates the Mini App header color.
192
- * @since v6.1
193
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-set-header-color
194
- */
195
- web_app_set_header_color: CreateParams<{
196
- /**
197
- * The Mini App header color key.
198
- */
199
- color_key: HeaderColorKey;
200
- } | {
201
- /**
202
- * Color in RGB format.
203
- * @since v6.9
204
- */
205
- color: RGB;
206
- }, 'color'>;
207
- /**
208
- * Updates the Back Button settings.
209
- * @since v6.1
210
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-setup-back-button
211
- */
212
- web_app_setup_back_button: CreateParams<{
213
- /**
214
- * Should the Back Button be visible.
215
- */
216
- is_visible: boolean;
217
- }>;
218
- /**
219
- * Updates current closing behavior.
220
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-setup-closing-behavior
221
- */
222
- web_app_setup_closing_behavior: CreateParams<{
223
- /**
224
- * Will user be prompted in case, an application is going to be closed.
225
- */
226
- need_confirmation: boolean;
227
- }>;
228
- /**
229
- * Updates the Main Button settings.
230
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-setup-main-button
231
- */
232
- web_app_setup_main_button: CreateParams<{
233
- /**
234
- * Should the Main Button be displayed.
235
- */
236
- is_visible?: boolean;
237
- /**
238
- * Should the Main Button be enabled.
239
- */
240
- is_active?: boolean;
241
- /**
242
- * Should loader inside the Main Button be displayed. Use this property in case, some
243
- * operation takes time. This loader will make user notified about it.
244
- */
245
- is_progress_visible?: boolean;
246
- /**
247
- * Text inside the Main Button.
248
- */
249
- text?: string;
250
- /**
251
- * The Main Button background color in `#RRGGBB` format.
252
- */
253
- color?: string;
254
- /**
255
- * The Main Button text color in `#RRGGBB` format.
256
- */
257
- text_color?: string;
258
- }>;
259
- /**
260
- * Updates current state of Settings Button.
261
- * @since v6.10
262
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-setup-settings-button
263
- */
264
- web_app_setup_settings_button: CreateParams<{
265
- /**
266
- * Should the Settings Button be displayed.
267
- */
268
- is_visible: boolean;
269
- }>;
270
- /**
271
- * Inserts the bot's username and the specified inline query in the current chat's input field.
272
- * Query may be empty, in which case only the bot's username will be inserted. The client prompts
273
- * the user to choose a specific chat, then opens that chat and inserts the bot's username and
274
- * the specified inline query in the input field.
275
- * @since v6.7
276
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-switch-inline-query
277
- */
278
- web_app_switch_inline_query: CreateParams<{
279
- /**
280
- * Text which should be inserted in the input after the current bot name. Max length is
281
- * 256 symbols.
282
- */
283
- query: string;
284
- /**
285
- * List of chat types which could be chosen to send the message. Could be empty list.
286
- */
287
- chat_types: SwitchInlineQueryChatType[];
288
- }>;
289
- /**
290
- * Generates haptic feedback event.
291
- * @since v6.1
292
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-trigger-haptic-feedback
293
- */
294
- web_app_trigger_haptic_feedback: CreateParams<AnyHapticFeedbackParams>;
295
- }
296
- /**
297
- * Any Telegram Mini Apps known method name.
298
- */
299
- export type MethodName = keyof Methods;
300
- /**
301
- * Returns parameters for specified post-available event.
302
- */
303
- export type MethodParams<M extends MethodName> = Methods[M]['params'];
304
- /**
305
- * True if specified method accepts parameters.
306
- */
307
- export type MethodAcceptParams<M extends MethodName> = Not<IsNever<Exclude<MethodParams<M>, undefined>>>;
308
- /**
309
- * Any post-available event name which does not require arguments.
310
- */
311
- export type EmptyMethodName = {
312
- [M in MethodName]: undefined extends MethodParams<M> ? M : never;
313
- }[MethodName];
314
- /**
315
- * Any post-available event name which require arguments.
316
- */
317
- export type NonEmptyMethodName = {
318
- [M in MethodName]: MethodAcceptParams<M> extends true ? M : never;
319
- }[MethodName];
320
- /**
321
- * Method names which have versioned params.
322
- */
323
- export type MethodWithVersionedParams = {
324
- [M in MethodName]: IsNever<Methods[M]['versionedParams']> extends true ? never : M;
325
- }[MethodName];
326
- /**
327
- * Method parameters which appear only in the specific Telegram Mini Apps version.
328
- */
329
- export type MethodVersionedParams<M extends MethodWithVersionedParams> = Methods[M]['versionedParams'];
330
- export {};
@@ -1,66 +0,0 @@
1
- import type { And, If, IsNever } from '@tma.js/util-types';
2
- import { type PostEvent } from './methods/postEvent.js';
3
- import { type EventName, type EventParams, type EventHasParams } from './events/index.js';
4
- import type { EmptyMethodName, MethodAcceptParams, MethodName, MethodParams, NonEmptyMethodName } from './methods/methods.js';
5
- /**
6
- * Names of methods, which require passing "req_id" parameter.
7
- */
8
- type MethodWithRequestId = {
9
- [M in MethodName]: If<And<MethodAcceptParams<M>, MethodParams<M> extends {
10
- req_id: string;
11
- } ? true : false>, M, never>;
12
- }[MethodName];
13
- /**
14
- * Names of events, which contain "req_id" parameter.
15
- */
16
- type EventWithRequestId = {
17
- [E in EventName]: If<And<EventHasParams<E>, EventParams<E> extends {
18
- req_id: string;
19
- } ? true : false>, E, never>;
20
- }[EventName];
21
- export interface RequestOptions {
22
- /**
23
- * Bridge postEvent method.
24
- * @default Global postEvent method.
25
- */
26
- postEvent?: PostEvent;
27
- /**
28
- * Execution timeout.
29
- */
30
- timeout?: number;
31
- }
32
- export interface RequestOptionsAdvanced<EventPayload> extends RequestOptions {
33
- /**
34
- * Should return true in case, this event should be captured. If not specified,
35
- * request is not skipping captured events.
36
- */
37
- capture?: If<IsNever<EventPayload>, () => boolean, (payload: EventPayload) => boolean>;
38
- }
39
- /**
40
- * Calls specified TWA method and captures one of the specified events. Returns promise
41
- * which will be resolved in case, event with specified in method request identifier
42
- * was captured.
43
- * @param method - method to execute.
44
- * @param params - method parameters.
45
- * @param event - event or events to listen.
46
- * @param options - additional execution options.
47
- */
48
- export declare function request<M extends MethodWithRequestId, E extends EventWithRequestId>(method: M, params: MethodParams<M>, event: E | E[], options?: RequestOptions): Promise<EventParams<E>>;
49
- /**
50
- * Calls specified TWA method and captures one of the specified events. Returns promise
51
- * which will be resolved in case, specified event was captured.
52
- * @param method - method to execute.
53
- * @param event - event or events to listen.
54
- * @param options - additional execution options.
55
- */
56
- export declare function request<M extends EmptyMethodName, E extends EventName>(method: M, event: E | E[], options?: RequestOptionsAdvanced<EventParams<E>>): Promise<EventParams<E>>;
57
- /**
58
- * Calls specified TWA method and captures one of the specified events. Returns promise
59
- * which will be resolved in case, specified event was captured.
60
- * @param method - method to execute
61
- * @param params - method parameters.
62
- * @param event - event or events to listen
63
- * @param options - additional execution options.
64
- */
65
- export declare function request<M extends NonEmptyMethodName, E extends EventName>(method: M, params: MethodParams<M>, event: E | E[], options?: RequestOptionsAdvanced<EventParams<E>>): Promise<EventParams<E>>;
66
- export {};
@@ -1,5 +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;
@@ -1,15 +0,0 @@
1
- import { type Version } from '@tma.js/utils';
2
- import type { MethodVersionedParams, MethodWithVersionedParams, MethodName } from './methods/index.js';
3
- /**
4
- * Returns true in case, passed parameter in specified method is supported.
5
- * @param method - method name
6
- * @param param - method parameter
7
- * @param inVersion - platform version.
8
- */
9
- export declare function supports<M extends MethodWithVersionedParams>(method: M, param: MethodVersionedParams<M>, inVersion: Version): boolean;
10
- /**
11
- * Returns true in case, specified method is supported in passed version.
12
- * @param method - method name.
13
- * @param inVersion - platform version.
14
- */
15
- export declare function supports(method: MethodName, inVersion: Version): boolean;