@tma.js/bridge 1.4.0 → 2.0.0

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 (92) 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/parsing.d.ts +0 -38
  54. package/dist/dts/events/payloads.d.ts +0 -93
  55. package/dist/dts/events/subscribe.d.ts +0 -9
  56. package/dist/dts/events/unsubscribe.d.ts +0 -6
  57. package/dist/dts/methods/index.d.ts +0 -6
  58. package/dist/dts/methods/invoke-custom-method.d.ts +0 -24
  59. package/dist/dts/methods/methods.d.ts +0 -330
  60. package/dist/dts/request.d.ts +0 -66
  61. package/dist/dts/shared.d.ts +0 -5
  62. package/dist/dts/supports.d.ts +0 -15
  63. package/dist/index.mjs +0 -297
  64. package/dist/index.mjs.map +0 -1
  65. package/src/env.ts +0 -49
  66. package/src/errors/MethodUnsupportedError.ts +0 -13
  67. package/src/errors/ParameterUnsupportedError.ts +0 -13
  68. package/src/errors/index.ts +0 -2
  69. package/src/events/emitter.ts +0 -126
  70. package/src/events/events.ts +0 -169
  71. package/src/events/index.ts +0 -7
  72. package/src/events/off.ts +0 -12
  73. package/src/events/on.ts +0 -17
  74. package/src/events/onTelegramEvent.ts +0 -83
  75. package/src/events/once.ts +0 -16
  76. package/src/events/parsing.ts +0 -110
  77. package/src/events/payloads.ts +0 -125
  78. package/src/events/subscribe.ts +0 -16
  79. package/src/events/unsubscribe.ts +0 -11
  80. package/src/globals.ts +0 -38
  81. package/src/index.ts +0 -7
  82. package/src/methods/createPostEvent.ts +0 -39
  83. package/src/methods/haptic.ts +0 -52
  84. package/src/methods/index.ts +0 -6
  85. package/src/methods/invoke-custom-method.ts +0 -25
  86. package/src/methods/methods.ts +0 -373
  87. package/src/methods/popup.ts +0 -53
  88. package/src/methods/postEvent.ts +0 -101
  89. package/src/request.ts +0 -168
  90. package/src/shared.ts +0 -5
  91. package/src/supports.ts +0 -82
  92. /package/dist/dts/methods/{haptic.d.ts → types/haptic-feedback.d.ts} +0 -0
@@ -1,52 +0,0 @@
1
- /**
2
- * Generic type which creates new types of haptic feedback.
3
- */
4
- type CreateHapticFeedbackParams<T extends string, P> = { type: T } & P;
5
-
6
- /**
7
- * Style of impact occurred haptic event.
8
- * - `light`, indicates a collision between small or lightweight UI objects,
9
- * - `medium`, indicates a collision between medium-sized or medium-weight UI objects,
10
- * - `heavy`, indicates a collision between large or heavyweight UI objects,
11
- * - `rigid`, indicates a collision between hard or inflexible UI objects,
12
- * - `soft`, indicates a collision between soft or flexible UI objects.
13
- */
14
- export type ImpactHapticFeedbackStyle =
15
- | 'light'
16
- | 'medium'
17
- | 'heavy'
18
- | 'rigid'
19
- | 'soft';
20
-
21
- /**
22
- * Type of notification occurred type event.
23
- * - `error`, indicates that a task or action has failed,
24
- * - `success`, indicates that a task or action has completed successfully,
25
- * - `warning`, indicates that a task or action produced a warning.
26
- */
27
- export type NotificationHapticFeedbackType = 'error' | 'success' | 'warning';
28
-
29
- /**
30
- * `impactOccurred` haptic feedback.
31
- */
32
- export type ImpactHapticFeedbackParams = CreateHapticFeedbackParams<'impact', {
33
- impact_style: ImpactHapticFeedbackStyle;
34
- }>;
35
-
36
- /**
37
- * `notificationOccurred` haptic feedback.
38
- */
39
- export type NotificationHapticFeedbackParams = CreateHapticFeedbackParams<'notification', {
40
- notification_type: NotificationHapticFeedbackType;
41
- }>;
42
-
43
- /**
44
- * `selectionChanged` haptic feedback.
45
- */
46
- // eslint-disable-next-line @typescript-eslint/ban-types
47
- export type SelectionHapticFeedbackParams = CreateHapticFeedbackParams<'selection_change', {}>;
48
-
49
- export type AnyHapticFeedbackParams =
50
- | ImpactHapticFeedbackParams
51
- | NotificationHapticFeedbackParams
52
- | SelectionHapticFeedbackParams;
@@ -1,6 +0,0 @@
1
- export * from './createPostEvent.js';
2
- export * from './haptic.js';
3
- export * from './invoke-custom-method.js';
4
- export * from './methods.js';
5
- export * from './popup.js';
6
- export * from './postEvent.js';
@@ -1,25 +0,0 @@
1
- import type { RequestId } from '../shared.js';
2
-
3
- interface CreateInvokeCustomMethodParams<M extends string, Params extends object> {
4
- /**
5
- * Unique request identifier.
6
- */
7
- req_id: RequestId;
8
-
9
- /**
10
- * Method name.
11
- */
12
- method: M;
13
-
14
- /**
15
- * Method specific parameters.
16
- */
17
- params: Params;
18
- }
19
-
20
- export type AnyInvokeCustomMethodParams =
21
- | CreateInvokeCustomMethodParams<'deleteStorageValues', { keys: string | string[] }>
22
- | CreateInvokeCustomMethodParams<'getStorageValues', { keys: string | string[] }>
23
- | CreateInvokeCustomMethodParams<'getStorageKeys', {}>
24
- | CreateInvokeCustomMethodParams<'saveStorageValue', { key: string, value: string }>
25
- | CreateInvokeCustomMethodParams<string, any>;
@@ -1,373 +0,0 @@
1
- import type { RGB } from '@tma.js/colors';
2
- import type { IsNever, Not, UnionKeys } from '@tma.js/util-types';
3
-
4
- import type { PopupParams } from './popup.js';
5
- import type { AnyHapticFeedbackParams } from './haptic.js';
6
- import type { RequestId } from '../shared.js';
7
- import type { AnyInvokeCustomMethodParams } from './invoke-custom-method.js';
8
-
9
- /**
10
- * Color key which could be used to update header color.
11
- */
12
- export type HeaderColorKey = 'bg_color' | 'secondary_bg_color';
13
-
14
- /**
15
- * Chat type which could be used when calling `web_app_switch_inline_query` method.
16
- */
17
- export type SwitchInlineQueryChatType = 'users' | 'bots' | 'groups' | 'channels';
18
-
19
- interface CreateParams<Params = undefined, VersionedParam extends UnionKeys<Params> = never> {
20
- params: Params;
21
- versionedParams: VersionedParam;
22
- }
23
-
24
- /**
25
- * Describes list of events and their parameters that could be posted by Bridge.
26
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods
27
- */
28
- export interface Methods {
29
- /**
30
- * Notifies parent iframe about the current frame is ready. This method is only used in the Web
31
- * version of Telegram. As a result, Mini App will receive `set_custom_style` event.
32
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#iframe-ready
33
- */
34
- iframe_ready: CreateParams<{
35
- /**
36
- * True, if current Mini App supports native reloading.
37
- */
38
- reload_supported?: boolean;
39
- } | undefined>;
40
-
41
- /**
42
- * Notifies parent iframe about the current iframe is going to reload.
43
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#iframe-will-reload
44
- */
45
- iframe_will_reload: CreateParams;
46
-
47
- /**
48
- * Closes Mini App.
49
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-close
50
- */
51
- web_app_close: CreateParams;
52
-
53
- /**
54
- * Closes a QR scanner. The Telegram application creates `scan_qr_popup_closed` event.
55
- * @since v6.4
56
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-close-scan-qr-popup
57
- */
58
- web_app_close_scan_qr_popup: CreateParams;
59
-
60
- /**
61
- * Sends data to the bot. When this method is called, a service message is sent to the bot
62
- * containing the data of the length up to 4096 bytes. Then, Mini App will be closed.
63
- *
64
- * To get more information, take a look at `web_app_data` field in the
65
- * class [Message](https://core.telegram.org/bots/api#message).
66
- *
67
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-data-send
68
- */
69
- web_app_data_send: CreateParams<{
70
- /**
71
- * Data to send to a bot. Should not have size of more than 4096 bytes.
72
- */
73
- data: string;
74
- }>;
75
-
76
- /**
77
- * Expands the Mini App.
78
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-expand
79
- */
80
- web_app_expand: CreateParams;
81
-
82
- // TODO: 'getRequestedContact'. https://telegram.org/js/telegram-web-app.js
83
- /**
84
- * Invokes custom method.
85
- * @since v6.9
86
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-invoke-custom-method
87
- */
88
- web_app_invoke_custom_method: CreateParams<AnyInvokeCustomMethodParams>;
89
-
90
- /**
91
- * Opens an invoice by its specified slug. More information about invoices in
92
- * this [documentation](https://core.telegram.org/bots/payments).
93
- * @since v6.1
94
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-open-invoice
95
- */
96
- web_app_open_invoice: CreateParams<{
97
- /**
98
- * Invoice unique identifier.
99
- */
100
- slug: string;
101
- }>;
102
-
103
- /**
104
- * Opens link in the default browser. Mini App will not be closed.
105
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-open-link
106
- */
107
- web_app_open_link: CreateParams<{
108
- /**
109
- * URL to be opened by Telegram application. Should be a full path with `https` protocol.
110
- */
111
- url: string;
112
-
113
- /**
114
- * Link will be opened in Instant View mode if possible.
115
- * @since v6.4
116
- * @see https://instantview.telegram.org/
117
- */
118
- try_instant_view?: boolean;
119
- }, 'try_instant_view'>;
120
-
121
- /**
122
- * Opens a new popup. When user closes the popup, Telegram creates the `popup_closed` event.
123
- * @since v6.2
124
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-open-popup
125
- */
126
- web_app_open_popup: CreateParams<PopupParams>;
127
-
128
- /**
129
- * Opens a QR scanner. When the scanner was closed, the Telegram application creates
130
- * the `scan_qr_popup_closed` event. When the scanner reads QR, Telegram creates the
131
- * `qr_text_received` event.
132
- * @since v6.4
133
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-open-scan-qr-popup
134
- */
135
- web_app_open_scan_qr_popup: CreateParams<{
136
- /**
137
- * Text to be displayed in the QR scanner.
138
- */
139
- text?: string;
140
- }>;
141
-
142
- /**
143
- * Opens the Telegram link by its pathname and query parameters. The link will be opened in the
144
- * Telegram app, Mini App will be closed.
145
- * @since v6.1
146
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-open-tg-link
147
- */
148
- web_app_open_tg_link: CreateParams<{
149
- /**
150
- * Should be a value taken from the link of this format: `https://t.me/{path_full}`. Can
151
- * additionally contain query parameters.
152
- */
153
- path_full: string;
154
- }>;
155
-
156
- /**
157
- * Reads text from the clipboard. The method accepts a request identifier which is used to
158
- * appropriately retrieve the method execution result from the `clipboard_text_received` event.
159
- * @since v6.4
160
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-read-text-from-clipboard
161
- */
162
- web_app_read_text_from_clipboard: CreateParams<{
163
- /**
164
- * Unique request identifier. Should be any unique string to handle the generated event
165
- * appropriately.
166
- */
167
- req_id: RequestId;
168
- }>;
169
-
170
- /**
171
- * Notifies Telegram about current application is ready to be shown. This method will make
172
- * Telegram to remove application loader and display Mini App.
173
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-ready
174
- */
175
- web_app_ready: CreateParams;
176
-
177
- // TODO: Check if it is right. It probably requests other user phone.
178
- /**
179
- * Requests access to current user's phone.
180
- * @since v6.9
181
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-request-phone
182
- */
183
- web_app_request_phone: CreateParams;
184
-
185
- /**
186
- * Requests current theme from Telegram. As a result, Telegram will create `theme_changed` event.
187
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-request-theme
188
- */
189
- web_app_request_theme: CreateParams;
190
-
191
- /**
192
- * Requests current viewport information from Telegram. As a result, Telegram will create
193
- * `viewport_changed` event.
194
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-request-viewport
195
- */
196
- web_app_request_viewport: CreateParams;
197
-
198
- /**
199
- * Requests write message access to current user.
200
- * @since v6.9
201
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-rqeuest-write-access
202
- */
203
- web_app_request_write_access: CreateParams;
204
-
205
- /**
206
- * Updates the Mini App background color.
207
- * @since v6.1
208
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-set-background-color
209
- */
210
- web_app_set_background_color: CreateParams<{
211
- /**
212
- * The Mini App background color in `#RRGGBB` format.
213
- */
214
- color: RGB;
215
- }>;
216
-
217
- /**
218
- * Updates the Mini App header color.
219
- * @since v6.1
220
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-set-header-color
221
- */
222
- web_app_set_header_color: CreateParams<
223
- | {
224
- /**
225
- * The Mini App header color key.
226
- */
227
- color_key: HeaderColorKey
228
- }
229
- | {
230
- /**
231
- * Color in RGB format.
232
- * @since v6.9
233
- */
234
- color: RGB;
235
- }, 'color'>;
236
-
237
- /**
238
- * Updates the Back Button settings.
239
- * @since v6.1
240
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-setup-back-button
241
- */
242
- web_app_setup_back_button: CreateParams<{
243
- /**
244
- * Should the Back Button be visible.
245
- */
246
- is_visible: boolean;
247
- }>;
248
-
249
- /**
250
- * Updates current closing behavior.
251
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-setup-closing-behavior
252
- */
253
- web_app_setup_closing_behavior: CreateParams<{
254
- /**
255
- * Will user be prompted in case, an application is going to be closed.
256
- */
257
- need_confirmation: boolean;
258
- }>;
259
-
260
- /**
261
- * Updates the Main Button settings.
262
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-setup-main-button
263
- */
264
- web_app_setup_main_button: CreateParams<{
265
- /**
266
- * Should the Main Button be displayed.
267
- */
268
- is_visible?: boolean;
269
- /**
270
- * Should the Main Button be enabled.
271
- */
272
- is_active?: boolean;
273
- /**
274
- * Should loader inside the Main Button be displayed. Use this property in case, some
275
- * operation takes time. This loader will make user notified about it.
276
- */
277
- is_progress_visible?: boolean;
278
- /**
279
- * Text inside the Main Button.
280
- */
281
- text?: string;
282
- /**
283
- * The Main Button background color in `#RRGGBB` format.
284
- */
285
- color?: string;
286
- /**
287
- * The Main Button text color in `#RRGGBB` format.
288
- */
289
- text_color?: string;
290
- }>;
291
-
292
- /**
293
- * Updates current state of Settings Button.
294
- * @since v6.10
295
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-setup-settings-button
296
- */
297
- web_app_setup_settings_button: CreateParams<{
298
- /**
299
- * Should the Settings Button be displayed.
300
- */
301
- is_visible: boolean;
302
- }>;
303
-
304
- /**
305
- * Inserts the bot's username and the specified inline query in the current chat's input field.
306
- * Query may be empty, in which case only the bot's username will be inserted. The client prompts
307
- * the user to choose a specific chat, then opens that chat and inserts the bot's username and
308
- * the specified inline query in the input field.
309
- * @since v6.7
310
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-switch-inline-query
311
- */
312
- web_app_switch_inline_query: CreateParams<{
313
- /**
314
- * Text which should be inserted in the input after the current bot name. Max length is
315
- * 256 symbols.
316
- */
317
- query: string;
318
- /**
319
- * List of chat types which could be chosen to send the message. Could be empty list.
320
- */
321
- chat_types: SwitchInlineQueryChatType[];
322
- }>;
323
-
324
- /**
325
- * Generates haptic feedback event.
326
- * @since v6.1
327
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-trigger-haptic-feedback
328
- */
329
- web_app_trigger_haptic_feedback: CreateParams<AnyHapticFeedbackParams>;
330
- }
331
-
332
- /**
333
- * Any Telegram Mini Apps known method name.
334
- */
335
- export type MethodName = keyof Methods;
336
-
337
- /**
338
- * Returns parameters for specified post-available event.
339
- */
340
- export type MethodParams<M extends MethodName> = Methods[M]['params'];
341
-
342
- /**
343
- * True if specified method accepts parameters.
344
- */
345
- export type MethodAcceptParams<M extends MethodName> =
346
- Not<IsNever<Exclude<MethodParams<M>, undefined>>>;
347
-
348
- /**
349
- * Any post-available event name which does not require arguments.
350
- */
351
- export type EmptyMethodName = {
352
- [M in MethodName]: undefined extends MethodParams<M> ? M : never;
353
- }[MethodName];
354
-
355
- /**
356
- * Any post-available event name which require arguments.
357
- */
358
- export type NonEmptyMethodName = {
359
- [M in MethodName]: MethodAcceptParams<M> extends true ? M : never;
360
- }[MethodName];
361
-
362
- /**
363
- * Method names which have versioned params.
364
- */
365
- export type MethodWithVersionedParams = {
366
- [M in MethodName]: IsNever<Methods[M]['versionedParams']> extends true ? never : M;
367
- }[MethodName];
368
-
369
- /**
370
- * Method parameters which appear only in the specific Telegram Mini Apps version.
371
- */
372
- export type MethodVersionedParams<M extends MethodWithVersionedParams> =
373
- Methods[M]['versionedParams'];
@@ -1,53 +0,0 @@
1
- /**
2
- * Describes the native popup.
3
- */
4
- export interface PopupParams {
5
- /**
6
- * The text to be displayed in the popup title, 0-64 characters.
7
- */
8
- title: string;
9
-
10
- /**
11
- * The message to be displayed in the body of the popup, 1-256 characters.
12
- */
13
- message: string;
14
-
15
- /**
16
- * List of buttons to be displayed in the popup, 1-3 buttons.
17
- */
18
- buttons: PopupButton[];
19
- }
20
-
21
- /**
22
- * Describes the native popup button.
23
- * @see https://docs.telegram-mini-apps.com/apps-communication/methods#popupbutton
24
- */
25
- export type PopupButton = {
26
- /**
27
- * Identifier of the button, 0-64 characters.
28
- */
29
- id: string;
30
- } & ({
31
- /**
32
- * Type of the button:
33
- * - `default`, a button with the default style;
34
- * - `destructive`, a button with a style that indicates a destructive
35
- * action (e.g. "Remove", "Delete", etc.).
36
- *
37
- * @default "default"
38
- */
39
- type?: 'default' | 'destructive';
40
-
41
- /**
42
- * The text to be displayed on the button, 0-64 characters.
43
- */
44
- text: string;
45
- } | {
46
- /**
47
- * Type of the button:
48
- * - `ok`, a button with the localized text "OK";
49
- * - `close`, a button with the localized text "Close";
50
- * - `cancel`, a button with the localized text "Cancel".
51
- */
52
- type: 'ok' | 'close' | 'cancel';
53
- });
@@ -1,101 +0,0 @@
1
- import {
2
- isIframe,
3
- hasExternalNotify,
4
- hasWebviewProxy,
5
- } from '../env.js';
6
- import { logger, targetOrigin as globalTargetOrigin } from '../globals.js';
7
- import type {
8
- EmptyMethodName,
9
- MethodName,
10
- MethodParams,
11
- NonEmptyMethodName,
12
- } from './methods.js';
13
-
14
- interface PostEventOptions {
15
- /**
16
- * Origin used while posting message. This option is only used in case,
17
- * current environment is browser (Web version of Telegram) and could
18
- * be used for test purposes.
19
- * @default 'https://web.telegram.org'
20
- */
21
- targetOrigin?: string;
22
- }
23
-
24
- export type PostEvent = typeof postEvent;
25
-
26
- /**
27
- * Sends event to native application which launched Mini App. This function
28
- * accepts only events, which require arguments.
29
- * @param eventType - event name.
30
- * @param params - event parameters.
31
- * @param options - posting options.
32
- * @throws {Error} Bridge could not determine current environment and possible way to send event.
33
- */
34
- export function postEvent<E extends NonEmptyMethodName>(
35
- eventType: E,
36
- params: MethodParams<E>,
37
- options?: PostEventOptions,
38
- ): void;
39
-
40
- /**
41
- * Sends event to native application which launched Mini App. This function
42
- * accepts only events, which require arguments.
43
- * @param eventType - event name.
44
- * @param options - posting options.
45
- * @throws {Error} Bridge could not determine current environment and possible way to send event.
46
- */
47
- export function postEvent(eventType: EmptyMethodName, options?: PostEventOptions): void;
48
-
49
- export function postEvent(
50
- eventType: MethodName,
51
- paramsOrOptions?: MethodParams<MethodName> | PostEventOptions,
52
- options?: PostEventOptions,
53
- ): void {
54
- let postOptions: PostEventOptions = {};
55
- let eventData: any;
56
-
57
- if (paramsOrOptions === undefined && options === undefined) {
58
- // Parameters and options were not passed.
59
- postOptions = {};
60
- } else if (paramsOrOptions !== undefined && options !== undefined) {
61
- // Both parameters and options passed.
62
- postOptions = options;
63
- eventData = paramsOrOptions;
64
- } else if (paramsOrOptions !== undefined) {
65
- // Only parameters were passed.
66
- if ('targetOrigin' in paramsOrOptions) {
67
- postOptions = paramsOrOptions;
68
- } else {
69
- eventData = paramsOrOptions;
70
- }
71
- }
72
- const { targetOrigin = globalTargetOrigin() } = postOptions;
73
-
74
- logger.log(`Calling method "${eventType}"`, eventData);
75
-
76
- // Telegram Web.
77
- if (isIframe()) {
78
- window.parent.postMessage(JSON.stringify({
79
- eventType,
80
- eventData,
81
- }), targetOrigin);
82
- return;
83
- }
84
-
85
- // Telegram for Windows Phone or Android.
86
- if (hasExternalNotify(window)) {
87
- window.external.notify(JSON.stringify({ eventType, eventData }));
88
- return;
89
- }
90
-
91
- // Telegram for iOS and macOS.
92
- if (hasWebviewProxy(window)) {
93
- window.TelegramWebviewProxy.postEvent(eventType, JSON.stringify(eventData));
94
- return;
95
- }
96
-
97
- // Otherwise current environment is unknown, and we are not able to send event.
98
- throw new Error(
99
- 'Unable to determine current environment and possible way to send event.',
100
- );
101
- }