@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
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2022 Vladislav Kibenko
3
+ Copyright (c) 2025 Vladislav Kibenko
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,28 +1,59 @@
1
1
  # @tma.js/bridge
2
2
 
3
- [code-link]: https://github.com/Telegram-Mini-Apps/tma.js/tree/master/packages/bridge
4
-
5
3
  [code-badge]: https://img.shields.io/badge/source-black?logo=github
6
4
 
7
- [docs-link]: https://docs.telegram-mini-apps.com/packages/typescript/tma-js-bridge
8
-
9
5
  [docs-badge]: https://img.shields.io/badge/documentation-blue?logo=gitbook&logoColor=white
10
6
 
7
+ [link]: https://github.com/Telegram-Mini-Apps/telegram-apps/tree/master/tma.js/bridge
8
+
9
+ [docs-link]: https://docs.telegram-mini-apps.com/packages/tma-js-bridge
10
+
11
11
  [npm-link]: https://npmjs.com/package/@tma.js/bridge
12
12
 
13
13
  [npm-badge]: https://img.shields.io/npm/v/@tma.js/bridge?logo=npm
14
14
 
15
15
  [size-badge]: https://img.shields.io/bundlephobia/minzip/@tma.js/bridge
16
16
 
17
- [![npm-badge]][npm-link]
18
- ![size-badge]
17
+ [![NPM][npm-badge]][npm-link]
18
+ ![Size][size-badge]
19
19
  [![docs-badge]][docs-link]
20
- [![code-badge]][code-link]
20
+ [![code-badge]][link]
21
+
22
+ The lowest level communication layer with Telegram Mini Apps.
23
+
24
+ This package provides fundamental utilities and types for developing applications on the Telegram
25
+ Mini Apps platform.
26
+
27
+ While a developer can use this package alone, it's recommended to use a higher-level package
28
+ like [@tma.js/sdk](https://docs.telegram-mini-apps.com/packages/tma-js-sdk).
29
+
30
+ ## Installation
31
+
32
+ ```bash
33
+ pnpm i @tma.js/bridge
34
+ # or
35
+ npm i @tma.js/bridge
36
+ # or
37
+ yarn add @tma.js/bridge
38
+ ```
39
+
40
+ ## Usage
41
+
42
+ Here’s a basic example of how to use this package. For more details, refer to the package complete
43
+ [documentation](https://docs.telegram-mini-apps.com/packages/tma-js-bridge).
44
+
45
+ ```ts
46
+ import { on, postEvent, applyPolyfills } from '@tma.js/bridge';
47
+
48
+ // The package uses some polyfills, so let's use them.
49
+ applyPolyfills();
21
50
 
22
- Package which provides utilities to simplify communication flow between
23
- frontend and Telegram native applications. It also solves some across-platform
24
- data difference problems to protect developers code and save their time.
51
+ // Show the back button, wait for it to be clicked once,
52
+ // then hide it.
53
+ postEvent('web_app_setup_back_button', { is_visible: true });
25
54
 
26
- This library is a part of TypeScript packages ecosystem around Telegram Web
27
- Apps. You can learn more about this package in this
28
- [documentation][docs-link].
55
+ const off = on('back_button_pressed', () => {
56
+ postEvent('web_app_setup_back_button', { is_visible: false });
57
+ off();
58
+ });
59
+ ```
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Applies polyfills required for stable work of the package:
3
+ * - `Object.hasOwn` - used by `valibot`
4
+ */
5
+ export declare function applyPolyfills(): void;
@@ -0,0 +1,24 @@
1
+ import * as E from 'fp-ts/Either';
2
+ export type DecodeBase64UrlError = DOMException;
3
+ /**
4
+ * Decodes a base-64-url ASCII string.
5
+ * @param value - the value to decode.
6
+ * @see Learn more about base64url:
7
+ * https://herongyang.com/Encoding/Base64URL-Encoding-Algorithm.html
8
+ * @see Source:
9
+ * https://developer.mozilla.org/ru/docs/Glossary/Base64#solution_1_–_escaping_the_string_before_encoding_it
10
+ */
11
+ export declare function decodeBase64UrlFp(value: string): E.Either<DecodeBase64UrlError, string>;
12
+ /**
13
+ * @see decodeBase64UrlFp
14
+ */
15
+ export declare const decodeBase64Url: (value: string) => string;
16
+ /**
17
+ * Creates a base-64-url encoded ASCII string from the passed value.
18
+ * @param value - the value to encode.
19
+ * @see Learn more about base64url:
20
+ * https://herongyang.com/Encoding/Base64URL-Encoding-Algorithm.html
21
+ * @see Source:
22
+ * https://developer.mozilla.org/ru/docs/Glossary/Base64#solution_1_–_escaping_the_string_before_encoding_it
23
+ */
24
+ export declare function encodeBase64Url(value: string): string;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Returns true in case, passed value contains path `TelegramWebviewProxy.postEvent` property and
3
+ * `postEvent` is a function.
4
+ * @param value - value to check.
5
+ */
6
+ export declare function hasWebviewProxy<T>(value: T): value is T & {
7
+ TelegramWebviewProxy: {
8
+ postEvent: (...args: unknown[]) => unknown;
9
+ };
10
+ };
@@ -0,0 +1,5 @@
1
+ /**
2
+ * @see https://stackoverflow.com/a/326076
3
+ * @returns True, if current environment is iframe.
4
+ */
5
+ export declare function isIframe(): boolean;
@@ -0,0 +1,31 @@
1
+ import { BetterPromise, BetterPromiseOptions, TimeoutError } from 'better-promises';
2
+ import { RequestError } from '../utils/request.js';
3
+ import * as TE from 'fp-ts/TaskEither';
4
+ export type isTMAError = Exclude<RequestError, TimeoutError>;
5
+ /**
6
+ * @see isTMAFp
7
+ */
8
+ export declare function isTMA(): boolean;
9
+ /**
10
+ * @see isTMAFp
11
+ */
12
+ export declare function isTMA(type: 'complete', options?: BetterPromiseOptions): BetterPromise<boolean>;
13
+ /**
14
+ * Returns true if the current environment is Telegram Mini Apps.
15
+ *
16
+ * It uses the `retrieveLaunchParams` function to determine if the environment
17
+ * contains launch parameters. In case it does, true will be returned.
18
+ *
19
+ * In case you need stricter checks, use async override of this function.
20
+ */
21
+ export declare function isTMAFp(): boolean;
22
+ /**
23
+ * Returns promise with true if the current environment is Telegram Mini Apps.
24
+ *
25
+ * First of all, it checks if the current environment contains traits specific
26
+ * to the Mini Apps environment. Then, it attempts to call a Mini Apps method
27
+ * and waits for a response to be received.
28
+ *
29
+ * In case you need less strict checks, use sync override of this function.
30
+ */
31
+ export declare function isTMAFp(type: 'complete', options?: BetterPromiseOptions): TE.TaskEither<isTMAError, boolean>;
@@ -0,0 +1,57 @@
1
+ import { LaunchParamsLike } from '@tma.js/transformers';
2
+ import { MethodName, MethodParams } from '../methods/types/index.js';
3
+ /**
4
+ * Mocks the environment and imitates Telegram Mini Apps behavior.
5
+ *
6
+ * We usually use this function in the following cases:
7
+ * 1. We are developing an application outside the Telegram environment and would like to imitate
8
+ * the Telegram client in order to re-create the same communication behavior.
9
+ * 2. We would like to intercept some Telegram Mini Apps methods' calls in order to enhance them
10
+ * or write a custom behavior. It is extremely useful in some Telegram clients improperly handling
11
+ * Mini Apps methods' calls and not even responding.
12
+ *
13
+ * Note that calling this function in Telegram web clients, the `postMessageImplementation` signal
14
+ * value will be updated with a new one, enhancing previously set signal value to allow wrapping
15
+ * the original `window.parent.postMessage` function. In other words, calling `mockTelegramEnv`
16
+ * function N times, you will effectively wrap previously set implementation N times, so be
17
+ * careful calling this function several times during a single lifecycle of the app. In case you
18
+ * would like to avoid such kind of behavior, use the `resetPostMessage` option.
19
+ */
20
+ export declare function mockTelegramEnv({ launchParams, onEvent, resetPostMessage }?: {
21
+ /**
22
+ * Launch parameters to mock. They will be saved in the storage, so the SDK functions could
23
+ * retrieve them.
24
+ *
25
+ * Note that this value must have `tgWebAppData` presented in a raw format as long as you will
26
+ * need it when retrieving init data in this format. Otherwise, init data may be broken.
27
+ */
28
+ launchParams?: (Omit<LaunchParamsLike, 'tgWebAppData'> & {
29
+ tgWebAppData?: string | URLSearchParams;
30
+ }) | string | URLSearchParams;
31
+ /**
32
+ * Function that will be called if a Mini Apps method call was requested by the mini app.
33
+ *
34
+ * It receives a Mini Apps method name along with the passed payload.
35
+ *
36
+ * Note that using the `next` function, in non-web environments it uses the
37
+ * `window.TelegramWebviewProxy.postEvent` method.
38
+ *
39
+ * Talking about the web versions of Telegram, the value of `next` is a bit more complex - it
40
+ * will be equal to the value stored in the `postMessageImpl` signal set previously. By default,
41
+ * this value contains a function utilizing the `window.parent.postMessage` method.
42
+ * @param event - event information.
43
+ * @param next - function to call the original method used to call a Mini Apps method.
44
+ */
45
+ onEvent?: (event: {
46
+ [M in MethodName]: {
47
+ name: M;
48
+ params: MethodParams<M>;
49
+ };
50
+ }[MethodName] | [string, unknown], next: () => void) => void;
51
+ /**
52
+ * Removes all previously set enhancements of the `window.parent.postMessage` function set
53
+ * by other `mockTelegramEnv` calls.
54
+ * @default false
55
+ */
56
+ resetPostMessage?: boolean;
57
+ }): void;
@@ -0,0 +1,27 @@
1
+ declare const MethodUnsupportedError_base: import('error-kid').ErrorClass<[method: string, version: string]>;
2
+ export declare class MethodUnsupportedError extends MethodUnsupportedError_base {
3
+ }
4
+ declare const MethodParameterUnsupportedError_base: import('error-kid').ErrorClass<[method: string, param: string, version: string]>;
5
+ export declare class MethodParameterUnsupportedError extends MethodParameterUnsupportedError_base {
6
+ }
7
+ declare const LaunchParamsRetrieveError_base: import('error-kid').ErrorClassWithData<[{
8
+ source: string;
9
+ error: unknown;
10
+ }[]], {
11
+ errors: {
12
+ source: string;
13
+ error: unknown;
14
+ }[];
15
+ }>;
16
+ export declare class LaunchParamsRetrieveError extends LaunchParamsRetrieveError_base {
17
+ }
18
+ declare const InvalidLaunchParamsError_base: import('error-kid').ErrorClass<[launchParams: string, cause: unknown]>;
19
+ export declare class InvalidLaunchParamsError extends InvalidLaunchParamsError_base {
20
+ }
21
+ declare const UnknownEnvError_base: import('error-kid').ErrorClass<[]>;
22
+ export declare class UnknownEnvError extends UnknownEnvError_base {
23
+ }
24
+ declare const InvokeCustomMethodFailedError_base: import('error-kid').ErrorClass<[error: string]>;
25
+ export declare class InvokeCustomMethodFailedError extends InvokeCustomMethodFailedError_base {
26
+ }
27
+ export {};
@@ -0,0 +1,58 @@
1
+ import { If, IsNever, IsUndefined, Or } from '@tma.js/toolkit';
2
+ import { Handler } from 'mitt';
3
+ export type WildcardHandler<E> = Handler<{
4
+ [K in keyof E]: {
5
+ name: K;
6
+ payload: If<Or<IsNever<E[K]>, IsUndefined<E[K]>>, never, E[K]>;
7
+ };
8
+ }[keyof E]>;
9
+ export interface OnFn<E> {
10
+ /**
11
+ * Adds a new listener for the specified event.
12
+ * @param type - event name.
13
+ * @param handler - event listener.
14
+ * @param once - should this listener be called only once.
15
+ * @returns Function to remove bound event listener.
16
+ */
17
+ <K extends keyof E>(type: K, handler: Handler<E[K]>, once?: boolean): VoidFunction;
18
+ /**
19
+ * Adds a listener to the wildcard event.
20
+ * @param type - event name.
21
+ * @param handler - event listener.
22
+ * @param once - should this listener be called only once.
23
+ * @returns Function to remove bound event listener.
24
+ */
25
+ (type: '*', handler: WildcardHandler<E>, once?: boolean): VoidFunction;
26
+ }
27
+ export interface OffFn<E> {
28
+ /**
29
+ * Removes a listener from the specified event.
30
+ * @param type - event to listen.
31
+ * @param handler - event listener to remove.
32
+ * @param once - had this listener to be called only once.
33
+ */
34
+ <K extends keyof E>(type: K, handler: Handler<E[K]>, once?: boolean): void;
35
+ /**
36
+ * Removes a listener from the wildcard event.
37
+ * @param type - event to stop listening.
38
+ * @param handler - event listener to remove.
39
+ * @param once - should this listener be called only once.
40
+ */
41
+ (type: '*', handler: WildcardHandler<E>, once?: boolean): void;
42
+ }
43
+ export interface EmitFn<E> {
44
+ <K extends keyof E>(type: K, event: E[K]): void;
45
+ <K extends keyof E>(type: undefined extends E[K] ? K : never): void;
46
+ }
47
+ /**
48
+ * Creates a new enhanced event emitter.
49
+ * @param onFirst - a function to call every time when the events map appeared to be empty during
50
+ * the event listener creation.
51
+ * @param onEmpty - a function to call every tume when the events map became empty.
52
+ */
53
+ export declare function createEmitter<E extends object>(onFirst: VoidFunction, onEmpty: VoidFunction): {
54
+ on: OnFn<E>;
55
+ off: OffFn<E>;
56
+ emit: EmitFn<E>;
57
+ clear: VoidFunction;
58
+ };
@@ -0,0 +1,33 @@
1
+ import { EventPayload, EventWithoutPayload, EventWithPayload } from './types/index.js';
2
+ /**
3
+ * Emits an event without payload sent from the Telegram native application like it was sent in
4
+ * a default web environment between two iframes.
5
+ *
6
+ * It dispatches a new MessageEvent and expects it to be handled via
7
+ * the `window.addEventListener('message', ...)` call, as a developer would do it to handle
8
+ * messages sent from the parent iframe.
9
+ * @param eventType - event name.
10
+ */
11
+ export declare function emitEvent<E extends EventWithoutPayload>(eventType: E): void;
12
+ /**
13
+ * Emits an event with payload sent from the Telegram native application like it was sent in
14
+ * a default web environment between two iframes.
15
+ *
16
+ * It dispatches a new MessageEvent and expects it to be handled via
17
+ * the `window.addEventListener('message', ...)` call, as a developer would do it to handle
18
+ * messages sent from the parent iframe.
19
+ * @param eventType - event name.
20
+ * @param eventData - event payload.
21
+ */
22
+ export declare function emitEvent<E extends EventWithPayload>(eventType: E, eventData: EventPayload<E>): void;
23
+ /**
24
+ * Emits an unknown event sent from the Telegram native application like it was sent in a default
25
+ * web environment between two iframes.
26
+ *
27
+ * It dispatches a new MessageEvent and expects it to be handled via
28
+ * the `window.addEventListener('message', ...)` call, as a developer would do it to handle
29
+ * messages sent from the parent iframe.
30
+ * @param eventType - event name.
31
+ * @param eventData - event payload.
32
+ */
33
+ export declare function emitEvent<E extends string>(eventType: E, eventData: E extends EventWithoutPayload ? never : E extends EventWithPayload ? EventPayload<E> : unknown): void;
@@ -1,11 +1,2 @@
1
- import type { EventEmitter } from './events.js';
2
- /**
3
- * Returns event emitter which could be safely used, to process events from
4
- * Telegram native application.
5
- */
6
- export declare function createEmitter(): EventEmitter;
7
- /**
8
- * Returns singleton instance of bridge EventEmitter. Also, defines
9
- * Telegram event handlers.
10
- */
11
- export declare function singletonEmitter(): EventEmitter;
1
+ import { Events } from './types/index.js';
2
+ export declare const on: import('./createEmitter.js').OnFn<Events>, off: import('./createEmitter.js').OffFn<Events>, emit: import('./createEmitter.js').EmitFn<Events>, offAll: VoidFunction;