@vbotma/bridge 2.2.4
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.
- package/LICENSE +21 -0
- package/README.md +59 -0
- package/dist/dts/applyPolyfills.d.ts +5 -0
- package/dist/dts/base64-url.d.ts +24 -0
- package/dist/dts/env/hasWebviewProxy.d.ts +10 -0
- package/dist/dts/env/isIframe.d.ts +5 -0
- package/dist/dts/env/isVBMA.d.ts +31 -0
- package/dist/dts/env/mockVBotEnv.d.ts +60 -0
- package/dist/dts/errors.d.ts +27 -0
- package/dist/dts/events/createEmitter.d.ts +58 -0
- package/dist/dts/events/emitEvent.d.ts +33 -0
- package/dist/dts/events/emitter.d.ts +2 -0
- package/dist/dts/events/types/events.d.ts +780 -0
- package/dist/dts/events/types/index.d.ts +3 -0
- package/dist/dts/events/types/listening.d.ts +5 -0
- package/dist/dts/events/types/misc.d.ts +16 -0
- package/dist/dts/globals.d.ts +53 -0
- package/dist/dts/index.d.ts +26 -0
- package/dist/dts/launch-params.d.ts +31 -0
- package/dist/dts/methods/captureSameReq.d.ts +10 -0
- package/dist/dts/methods/createPostEvent.d.ts +30 -0
- package/dist/dts/methods/getReleaseVersion.d.ts +15 -0
- package/dist/dts/methods/postEvent.d.ts +35 -0
- package/dist/dts/methods/postMessage.d.ts +7 -0
- package/dist/dts/methods/supports.d.ts +15 -0
- package/dist/dts/methods/types/custom-method.d.ts +59 -0
- package/dist/dts/methods/types/haptic-feedback.d.ts +40 -0
- package/dist/dts/methods/types/index.d.ts +7 -0
- package/dist/dts/methods/types/methods.d.ts +807 -0
- package/dist/dts/methods/types/misc.d.ts +27 -0
- package/dist/dts/methods/types/notification.d.ts +20 -0
- package/dist/dts/methods/types/popup.d.ts +49 -0
- package/dist/dts/methods/types/utils.d.ts +9 -0
- package/dist/dts/obj-prop-helpers.d.ts +39 -0
- package/dist/dts/start-param.d.ts +52 -0
- package/dist/dts/utils/compareVersions.d.ts +10 -0
- package/dist/dts/utils/invokeCustomMethod.d.ts +34 -0
- package/dist/dts/utils/request.d.ts +102 -0
- package/dist/dts/utils/request2.d.ts +62 -0
- package/dist/index.cjs +4 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.iife.js +4 -0
- package/dist/index.iife.js.map +1 -0
- package/dist/index.js +758 -0
- package/dist/index.js.map +1 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Vladislav Kibenko
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# @vbotma/bridge
|
|
2
|
+
|
|
3
|
+
[code-badge]: https://img.shields.io/badge/source-black?logo=github
|
|
4
|
+
|
|
5
|
+
[docs-badge]: https://img.shields.io/badge/documentation-blue?logo=gitbook&logoColor=white
|
|
6
|
+
|
|
7
|
+
[link]: https://github.com/Telegram-Mini-Apps/tma.js/tree/master/tma.js/bridge
|
|
8
|
+
|
|
9
|
+
[docs-link]: https://docs.vbot-mini-apps.com/packages/tma-js-bridge
|
|
10
|
+
|
|
11
|
+
[npm-link]: https://npmjs.com/package/@vbotma/bridge
|
|
12
|
+
|
|
13
|
+
[npm-badge]: https://img.shields.io/npm/v/@vbotma/bridge?logo=npm
|
|
14
|
+
|
|
15
|
+
[size-badge]: https://img.shields.io/bundlephobia/minzip/@vbotma/bridge
|
|
16
|
+
|
|
17
|
+
[![NPM][npm-badge]][npm-link]
|
|
18
|
+
![Size][size-badge]
|
|
19
|
+
[![docs-badge]][docs-link]
|
|
20
|
+
[![code-badge]][link]
|
|
21
|
+
|
|
22
|
+
The lowest level communication layer with VBot 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 [@vbotma/sdk](https://docs.vbot-mini-apps.com/packages/tma-js-sdk).
|
|
29
|
+
|
|
30
|
+
## Installation
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
pnpm i @vbotma/bridge
|
|
34
|
+
# or
|
|
35
|
+
npm i @vbotma/bridge
|
|
36
|
+
# or
|
|
37
|
+
yarn add @vbotma/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.vbot-mini-apps.com/packages/tma-js-bridge).
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import { on, postEvent, applyPolyfills } from '@vbotma/bridge';
|
|
47
|
+
|
|
48
|
+
// The package uses some polyfills, so let's use them.
|
|
49
|
+
applyPolyfills();
|
|
50
|
+
|
|
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 });
|
|
54
|
+
|
|
55
|
+
const off = on('back_button_pressed', () => {
|
|
56
|
+
postEvent('web_app_setup_back_button', { is_visible: false });
|
|
57
|
+
off();
|
|
58
|
+
});
|
|
59
|
+
```
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { either as E } from 'fp-ts';
|
|
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 `VBotWebviewProxy.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
|
+
VBotWebviewProxy: {
|
|
8
|
+
postEvent: (...args: unknown[]) => unknown;
|
|
9
|
+
};
|
|
10
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { BetterPromise, BetterPromiseOptions, TimeoutError } from 'better-promises';
|
|
2
|
+
import { taskEither as TE } from 'fp-ts';
|
|
3
|
+
import { Request2Error } from '../utils/request2.js';
|
|
4
|
+
export type isVBMAError = Exclude<Request2Error, TimeoutError>;
|
|
5
|
+
/**
|
|
6
|
+
* @see isVBMAFp
|
|
7
|
+
*/
|
|
8
|
+
export declare function isVBMA(): boolean;
|
|
9
|
+
/**
|
|
10
|
+
* @see isVBMAFp
|
|
11
|
+
*/
|
|
12
|
+
export declare function isVBMA(type: 'complete', options?: BetterPromiseOptions): BetterPromise<boolean>;
|
|
13
|
+
/**
|
|
14
|
+
* Returns true if the current environment is VBot 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 isVBMAFp(): boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Returns promise with true if the current environment is VBot 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 isVBMAFp(type: 'complete', options?: BetterPromiseOptions): TE.TaskEither<isVBMAError, boolean>;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { LaunchParamsLike } from '@vbotma/transformers';
|
|
2
|
+
import { MethodName, MethodParams } from '../methods/types/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Mocks the environment and imitates VBot Mini Apps behavior.
|
|
5
|
+
*
|
|
6
|
+
* We usually use this function in the following cases:
|
|
7
|
+
* 1. We are developing an application outside the VBot environment and would like to imitate
|
|
8
|
+
* the VBot client in order to re-create the same communication behavior.
|
|
9
|
+
* 2. We would like to intercept some VBot Mini Apps methods' calls in order to enhance them
|
|
10
|
+
* or write a custom behavior. It is extremely useful in some VBot 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 `mockVBotEnv`
|
|
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 mockVBotEnv({ 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 `vbWebAppData` 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, 'vbWebAppData'> & {
|
|
29
|
+
vbWebAppData?: 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.VBotWebviewProxy.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] | {
|
|
51
|
+
name: string;
|
|
52
|
+
params: unknown;
|
|
53
|
+
}, next: () => void) => void;
|
|
54
|
+
/**
|
|
55
|
+
* Removes all previously set enhancements of the `window.parent.postMessage` function set
|
|
56
|
+
* by other `mockVBotEnv` calls.
|
|
57
|
+
* @default false
|
|
58
|
+
*/
|
|
59
|
+
resetPostMessage?: boolean;
|
|
60
|
+
}): void;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
declare const MethodUnsupportedError_base: import('error-kid').ErrorClass<[method: string, version: string]>;
|
|
2
|
+
export declare class MethodUnsupportedError extends /* @__PURE__ */ MethodUnsupportedError_base {
|
|
3
|
+
}
|
|
4
|
+
declare const MethodParameterUnsupportedError_base: import('error-kid').ErrorClass<[method: string, param: string, version: string]>;
|
|
5
|
+
export declare class MethodParameterUnsupportedError extends /* @__PURE__ */ 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 /* @__PURE__ */ LaunchParamsRetrieveError_base {
|
|
17
|
+
}
|
|
18
|
+
declare const InvalidLaunchParamsError_base: import('error-kid').ErrorClass<[launchParams: string, cause: unknown]>;
|
|
19
|
+
export declare class InvalidLaunchParamsError extends /* @__PURE__ */ InvalidLaunchParamsError_base {
|
|
20
|
+
}
|
|
21
|
+
declare const UnknownEnvError_base: import('error-kid').ErrorClass<[]>;
|
|
22
|
+
export declare class UnknownEnvError extends /* @__PURE__ */ UnknownEnvError_base {
|
|
23
|
+
}
|
|
24
|
+
declare const InvokeCustomMethodFailedError_base: import('error-kid').ErrorClass<[error: string]>;
|
|
25
|
+
export declare class InvokeCustomMethodFailedError extends /* @__PURE__ */ InvokeCustomMethodFailedError_base {
|
|
26
|
+
}
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { If, IsNever, IsUndefined, Or } from '@vbotma/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;
|