@tma.js/bridge 1.3.12 → 1.4.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.
- package/dist/dts/errors/MethodUnsupportedError.d.ts +8 -0
- package/dist/dts/errors/ParameterUnsupportedError.d.ts +8 -0
- package/dist/dts/errors/index.d.ts +2 -0
- package/dist/dts/events/events.d.ts +8 -3
- package/dist/dts/events/index.d.ts +2 -2
- package/dist/dts/events/parsers/clipboardTextReceived.d.ts +13 -0
- package/dist/dts/events/parsers/customMethodInvoked.d.ts +16 -0
- package/dist/dts/events/parsers/index.d.ts +9 -0
- package/dist/dts/events/parsers/invoiceClosed.d.ts +12 -0
- package/dist/dts/events/parsers/phoneRequested.d.ts +8 -0
- package/dist/dts/events/parsers/popupClosed.d.ts +8 -0
- package/dist/dts/events/parsers/qrTextReceived.d.ts +7 -0
- package/dist/dts/events/parsers/theme-changed.d.ts +42 -0
- package/dist/dts/events/parsers/viewportChanged.d.ts +19 -0
- package/dist/dts/events/parsers/writeAccessRequested.d.ts +8 -0
- package/dist/dts/index.d.ts +1 -0
- package/dist/dts/methods/createPostEvent.d.ts +10 -0
- package/dist/dts/methods/index.d.ts +5 -4
- package/dist/dts/methods/{params.d.ts → methods.d.ts} +30 -18
- package/dist/dts/methods/postEvent.d.ts +1 -1
- package/dist/dts/request.d.ts +10 -10
- package/dist/dts/supports.d.ts +2 -9
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.iife.js +1 -1
- package/dist/index.iife.js.map +1 -1
- package/dist/index.mjs +201 -153
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/errors/MethodUnsupportedError.ts +13 -0
- package/src/errors/ParameterUnsupportedError.ts +13 -0
- package/src/errors/index.ts +2 -0
- package/src/events/emitter.ts +20 -19
- package/src/events/events.ts +11 -6
- package/src/events/index.ts +2 -2
- package/src/events/parsers/clipboardTextReceived.ts +27 -0
- package/src/events/parsers/customMethodInvoked.ts +26 -0
- package/src/events/parsers/index.ts +9 -0
- package/src/events/parsers/invoiceClosed.ts +26 -0
- package/src/events/parsers/phoneRequested.ts +14 -0
- package/src/events/parsers/popupClosed.ts +19 -0
- package/src/events/parsers/qrTextReceived.ts +14 -0
- package/src/events/parsers/theme-changed.ts +58 -0
- package/src/events/parsers/viewportChanged.ts +33 -0
- package/src/events/parsers/writeAccessRequested.ts +14 -0
- package/src/index.ts +1 -0
- package/src/methods/createPostEvent.ts +39 -0
- package/src/methods/index.ts +5 -4
- package/src/methods/{params.ts → methods.ts} +33 -18
- package/src/methods/postEvent.ts +1 -1
- package/src/request.ts +27 -30
- package/src/supports.ts +19 -40
- package/dist/dts/events/parsing.d.ts +0 -38
- package/dist/dts/events/payloads.d.ts +0 -98
- package/src/events/parsing.ts +0 -104
- package/src/events/payloads.ts +0 -116
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Version } from '@tma.js/utils';
|
|
2
|
+
import type { MethodName } from '../methods/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Error thrown in case, unsupported method was called.
|
|
5
|
+
*/
|
|
6
|
+
export declare class MethodUnsupportedError extends Error {
|
|
7
|
+
constructor(method: MethodName, version: Version);
|
|
8
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Version } from '@tma.js/utils';
|
|
2
|
+
import type { MethodName } from '../methods/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Error thrown in case, unsupported parameter was used.
|
|
5
|
+
*/
|
|
6
|
+
export declare class ParameterUnsupportedError extends Error {
|
|
7
|
+
constructor(method: MethodName, param: string, version: Version);
|
|
8
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { EventEmitter as UtilEventEmitter,
|
|
1
|
+
import type { EventEmitter as UtilEventEmitter, EventListener as UtilEventListener, EventParams as UtilEventParams, AnySubscribeListener } from '@tma.js/event-emitter';
|
|
2
2
|
import type { IsNever, Not } from '@tma.js/util-types';
|
|
3
|
-
import type { ClipboardTextReceivedPayload, CustomMethodInvokedPayload, InvoiceClosedPayload, PhoneRequestedPayload, PopupClosedPayload, QrTextReceivedPayload, ThemeChangedPayload, ViewportChangedPayload, WriteAccessRequestedPayload } from './
|
|
3
|
+
import type { ClipboardTextReceivedPayload, CustomMethodInvokedPayload, InvoiceClosedPayload, PhoneRequestedPayload, PopupClosedPayload, QrTextReceivedPayload, ThemeChangedPayload, ViewportChangedPayload, WriteAccessRequestedPayload } from './parsers/index.js';
|
|
4
4
|
/**
|
|
5
5
|
* Map where key is known event name, and value is its listener.
|
|
6
6
|
* @see https://docs.telegram-mini-apps.com/apps-communication/events
|
|
@@ -50,6 +50,11 @@ export interface Events {
|
|
|
50
50
|
* @see https://docs.telegram-mini-apps.com/apps-communication/events#popup-closed
|
|
51
51
|
*/
|
|
52
52
|
popup_closed: (payload: PopupClosedPayload) => void;
|
|
53
|
+
/**
|
|
54
|
+
* Parent iframe requested current iframe reload.
|
|
55
|
+
* @see https://docs.telegram-mini-apps.com/apps-communication/events#reload-iframe
|
|
56
|
+
*/
|
|
57
|
+
reload_iframe: () => void;
|
|
53
58
|
/**
|
|
54
59
|
* The QR scanner scanned some QR and extracted its content.
|
|
55
60
|
* @param payload - event payload.
|
|
@@ -102,7 +107,7 @@ export interface Events {
|
|
|
102
107
|
/**
|
|
103
108
|
* Any known event name.
|
|
104
109
|
*/
|
|
105
|
-
export type EventName =
|
|
110
|
+
export type EventName = keyof Events;
|
|
106
111
|
/**
|
|
107
112
|
* Parameters of specified event.
|
|
108
113
|
*/
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from './parsers/index.js';
|
|
2
|
+
export * from './events.js';
|
|
2
3
|
export * from './off.js';
|
|
3
4
|
export * from './on.js';
|
|
4
5
|
export * from './once.js';
|
|
5
|
-
export type * from './payloads.js';
|
|
6
6
|
export * from './subscribe.js';
|
|
7
7
|
export * from './unsubscribe.js';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { RequestId } from '../../shared.js';
|
|
2
|
+
export interface ClipboardTextReceivedPayload {
|
|
3
|
+
/**
|
|
4
|
+
* Passed during the `web_app_read_text_from_clipboard` method invocation `req_id` value.
|
|
5
|
+
*/
|
|
6
|
+
req_id: RequestId;
|
|
7
|
+
/**
|
|
8
|
+
* Data extracted from the clipboard. The returned value will have the type `string` only in
|
|
9
|
+
* the case, application has access to the clipboard.
|
|
10
|
+
*/
|
|
11
|
+
data?: string | null;
|
|
12
|
+
}
|
|
13
|
+
export declare function clipboardTextReceived(): import("@tma.js/parsing").ValueParser<ClipboardTextReceivedPayload, false>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { RequestId } from '../../shared.js';
|
|
2
|
+
export interface CustomMethodInvokedPayload<R = unknown> {
|
|
3
|
+
/**
|
|
4
|
+
* Unique identifier of this invocation.
|
|
5
|
+
*/
|
|
6
|
+
req_id: RequestId;
|
|
7
|
+
/**
|
|
8
|
+
* Method invocation successful result.
|
|
9
|
+
*/
|
|
10
|
+
result?: R;
|
|
11
|
+
/**
|
|
12
|
+
* Method invocation error code.
|
|
13
|
+
*/
|
|
14
|
+
error?: string;
|
|
15
|
+
}
|
|
16
|
+
export declare function customMethodInvoked(): import("@tma.js/parsing").ValueParser<CustomMethodInvokedPayload<unknown>, false>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from './clipboardTextReceived.js';
|
|
2
|
+
export * from './customMethodInvoked.js';
|
|
3
|
+
export * from './invoiceClosed.js';
|
|
4
|
+
export * from './phoneRequested.js';
|
|
5
|
+
export * from './popupClosed.js';
|
|
6
|
+
export * from './qrTextReceived.js';
|
|
7
|
+
export * from './theme-changed.js';
|
|
8
|
+
export * from './viewportChanged.js';
|
|
9
|
+
export * from './writeAccessRequested.js';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type InvoiceStatus = 'paid' | 'failed' | 'pending' | 'cancelled' | string;
|
|
2
|
+
export interface InvoiceClosedPayload {
|
|
3
|
+
/**
|
|
4
|
+
* Passed during the `web_app_open_invoice` method invocation `slug` value.
|
|
5
|
+
*/
|
|
6
|
+
slug: string;
|
|
7
|
+
/**
|
|
8
|
+
* Invoice status
|
|
9
|
+
*/
|
|
10
|
+
status: InvoiceStatus;
|
|
11
|
+
}
|
|
12
|
+
export declare function invoiceClosed(): import("@tma.js/parsing").ValueParser<InvoiceClosedPayload, false>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type PhoneRequestedStatus = 'sent' | string;
|
|
2
|
+
export interface PhoneRequestedPayload {
|
|
3
|
+
/**
|
|
4
|
+
* Request status.
|
|
5
|
+
*/
|
|
6
|
+
status: PhoneRequestedStatus;
|
|
7
|
+
}
|
|
8
|
+
export declare function phoneRequested(): import("@tma.js/parsing").ValueParser<PhoneRequestedPayload, false>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export interface PopupClosedPayload {
|
|
2
|
+
/**
|
|
3
|
+
* Identifier of the clicked button. In case, the popup was closed without clicking any button,
|
|
4
|
+
* this property will be omitted.
|
|
5
|
+
*/
|
|
6
|
+
button_id?: string;
|
|
7
|
+
}
|
|
8
|
+
export declare function popupClosed(): import("@tma.js/parsing").ValueParser<PopupClosedPayload, false>;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { RGB } from '@tma.js/colors';
|
|
2
|
+
export interface ThemeChangedPayload {
|
|
3
|
+
/**
|
|
4
|
+
* Map where the key is a theme stylesheet key and value is the corresponding color in
|
|
5
|
+
* `#RRGGBB` format.
|
|
6
|
+
*/
|
|
7
|
+
theme_params: {
|
|
8
|
+
/**
|
|
9
|
+
* @since v6.10
|
|
10
|
+
*/
|
|
11
|
+
accent_text_color?: RGB;
|
|
12
|
+
bg_color?: RGB;
|
|
13
|
+
button_color?: RGB;
|
|
14
|
+
button_text_color?: RGB;
|
|
15
|
+
/**
|
|
16
|
+
* @since v6.10
|
|
17
|
+
*/
|
|
18
|
+
destructive_text_color?: RGB;
|
|
19
|
+
/**
|
|
20
|
+
* @since v6.10
|
|
21
|
+
*/
|
|
22
|
+
header_bg_color?: RGB;
|
|
23
|
+
hint_color?: RGB;
|
|
24
|
+
link_color?: RGB;
|
|
25
|
+
secondary_bg_color?: RGB;
|
|
26
|
+
/**
|
|
27
|
+
* @since v6.10
|
|
28
|
+
*/
|
|
29
|
+
section_bg_color?: RGB;
|
|
30
|
+
/**
|
|
31
|
+
* @since v6.10
|
|
32
|
+
*/
|
|
33
|
+
section_header_text_color?: RGB;
|
|
34
|
+
/**
|
|
35
|
+
* @since v6.10
|
|
36
|
+
*/
|
|
37
|
+
subtitle_text_color?: RGB;
|
|
38
|
+
text_color?: RGB;
|
|
39
|
+
[key: string]: RGB | undefined;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
export declare function themeChanged(): import("@tma.js/parsing").ValueParser<ThemeChangedPayload, false>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface ViewportChangedPayload {
|
|
2
|
+
/**
|
|
3
|
+
* The viewport height.
|
|
4
|
+
*/
|
|
5
|
+
height: number;
|
|
6
|
+
/**
|
|
7
|
+
* The viewport width.
|
|
8
|
+
*/
|
|
9
|
+
width: number;
|
|
10
|
+
/**
|
|
11
|
+
* Is the viewport currently expanded.
|
|
12
|
+
*/
|
|
13
|
+
is_expanded: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Is the viewport current state stable and not going to change in the next moment.
|
|
16
|
+
*/
|
|
17
|
+
is_state_stable: boolean;
|
|
18
|
+
}
|
|
19
|
+
export declare function viewportChanged(): import("@tma.js/parsing").ValueParser<ViewportChangedPayload, false>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type WriteAccessRequestedStatus = 'allowed' | string;
|
|
2
|
+
export interface WriteAccessRequestedPayload {
|
|
3
|
+
/**
|
|
4
|
+
* Request status.
|
|
5
|
+
*/
|
|
6
|
+
status: WriteAccessRequestedStatus;
|
|
7
|
+
}
|
|
8
|
+
export declare function writeAccessRequested(): import("@tma.js/parsing").ValueParser<WriteAccessRequestedPayload, false>;
|
package/dist/dts/index.d.ts
CHANGED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type Version } from '@tma.js/utils';
|
|
2
|
+
import { type PostEvent } from './index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Creates function which checks if specified method and parameters are supported. In case,
|
|
5
|
+
* method or parameters are unsupported, an error will be thrown.
|
|
6
|
+
* @param version - Telegram Mini Apps version.
|
|
7
|
+
* @throws {MethodUnsupportedError} Method is unsupported.
|
|
8
|
+
* @throws {ParameterUnsupportedError} Method parameter is unsupported.
|
|
9
|
+
*/
|
|
10
|
+
export declare function createPostEvent(version: Version): PostEvent;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export
|
|
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';
|
|
5
6
|
export * from './postEvent.js';
|
|
@@ -12,21 +12,31 @@ export type HeaderColorKey = 'bg_color' | 'secondary_bg_color';
|
|
|
12
12
|
* Chat type which could be used when calling `web_app_switch_inline_query` method.
|
|
13
13
|
*/
|
|
14
14
|
export type SwitchInlineQueryChatType = 'users' | 'bots' | 'groups' | 'channels';
|
|
15
|
-
|
|
16
|
-
params:
|
|
17
|
-
|
|
18
|
-
}
|
|
15
|
+
interface CreateParams<Params = undefined, VersionedParam extends UnionKeys<Params> = never> {
|
|
16
|
+
params: Params;
|
|
17
|
+
versionedParams: VersionedParam;
|
|
18
|
+
}
|
|
19
19
|
/**
|
|
20
20
|
* Describes list of events and their parameters that could be posted by Bridge.
|
|
21
21
|
* @see https://docs.telegram-mini-apps.com/apps-communication/methods
|
|
22
22
|
*/
|
|
23
|
-
export interface
|
|
23
|
+
export interface Methods {
|
|
24
24
|
/**
|
|
25
25
|
* Notifies parent iframe about the current frame is ready. This method is only used in the Web
|
|
26
26
|
* version of Telegram. As a result, Mini App will receive `set_custom_style` event.
|
|
27
27
|
* @see https://docs.telegram-mini-apps.com/apps-communication/methods#iframe-ready
|
|
28
28
|
*/
|
|
29
|
-
iframe_ready: CreateParams
|
|
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;
|
|
30
40
|
/**
|
|
31
41
|
* Closes Mini App.
|
|
32
42
|
* @see https://docs.telegram-mini-apps.com/apps-communication/methods#web-app-close
|
|
@@ -284,35 +294,37 @@ export interface MethodsParams {
|
|
|
284
294
|
web_app_trigger_haptic_feedback: CreateParams<AnyHapticFeedbackParams>;
|
|
285
295
|
}
|
|
286
296
|
/**
|
|
287
|
-
* Any
|
|
297
|
+
* Any Telegram Mini Apps known method name.
|
|
288
298
|
*/
|
|
289
|
-
export type MethodName = keyof
|
|
299
|
+
export type MethodName = keyof Methods;
|
|
290
300
|
/**
|
|
291
301
|
* Returns parameters for specified post-available event.
|
|
292
302
|
*/
|
|
293
|
-
export type MethodParams<
|
|
303
|
+
export type MethodParams<M extends MethodName> = Methods[M]['params'];
|
|
294
304
|
/**
|
|
295
|
-
*
|
|
305
|
+
* True if specified method accepts parameters.
|
|
296
306
|
*/
|
|
297
|
-
export type
|
|
307
|
+
export type MethodAcceptParams<M extends MethodName> = Not<IsNever<Exclude<MethodParams<M>, undefined>>>;
|
|
298
308
|
/**
|
|
299
309
|
* Any post-available event name which does not require arguments.
|
|
300
310
|
*/
|
|
301
311
|
export type EmptyMethodName = {
|
|
302
|
-
[
|
|
312
|
+
[M in MethodName]: undefined extends MethodParams<M> ? M : never;
|
|
303
313
|
}[MethodName];
|
|
304
314
|
/**
|
|
305
315
|
* Any post-available event name which require arguments.
|
|
306
316
|
*/
|
|
307
|
-
export type NonEmptyMethodName =
|
|
317
|
+
export type NonEmptyMethodName = {
|
|
318
|
+
[M in MethodName]: MethodAcceptParams<M> extends true ? M : never;
|
|
319
|
+
}[MethodName];
|
|
308
320
|
/**
|
|
309
|
-
* Method names which
|
|
321
|
+
* Method names which have versioned params.
|
|
310
322
|
*/
|
|
311
|
-
export type
|
|
312
|
-
[
|
|
323
|
+
export type MethodWithVersionedParams = {
|
|
324
|
+
[M in MethodName]: IsNever<Methods[M]['versionedParams']> extends true ? never : M;
|
|
313
325
|
}[MethodName];
|
|
314
326
|
/**
|
|
315
|
-
* Method
|
|
327
|
+
* Method parameters which appear only in the specific Telegram Mini Apps version.
|
|
316
328
|
*/
|
|
317
|
-
export type
|
|
329
|
+
export type MethodVersionedParams<M extends MethodWithVersionedParams> = Methods[M]['versionedParams'];
|
|
318
330
|
export {};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { EmptyMethodName, MethodParams, NonEmptyMethodName } from './
|
|
1
|
+
import type { EmptyMethodName, MethodParams, NonEmptyMethodName } from './methods.js';
|
|
2
2
|
interface PostEventOptions {
|
|
3
3
|
/**
|
|
4
4
|
* Origin used while posting message. This option is only used in case,
|
package/dist/dts/request.d.ts
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import type { And, If, IsNever } from '@tma.js/util-types';
|
|
2
2
|
import { type PostEvent } from './methods/postEvent.js';
|
|
3
3
|
import { type EventName, type EventParams, type EventHasParams } from './events/index.js';
|
|
4
|
-
import type { EmptyMethodName,
|
|
4
|
+
import type { EmptyMethodName, MethodAcceptParams, MethodName, MethodParams, NonEmptyMethodName } from './methods/methods.js';
|
|
5
5
|
/**
|
|
6
6
|
* Names of methods, which require passing "req_id" parameter.
|
|
7
7
|
*/
|
|
8
|
-
type
|
|
9
|
-
[
|
|
8
|
+
type MethodWithRequestId = {
|
|
9
|
+
[M in MethodName]: If<And<MethodAcceptParams<M>, MethodParams<M> extends {
|
|
10
10
|
req_id: string;
|
|
11
|
-
} ? true : false>,
|
|
11
|
+
} ? true : false>, M, never>;
|
|
12
12
|
}[MethodName];
|
|
13
13
|
/**
|
|
14
14
|
* Names of events, which contain "req_id" parameter.
|
|
15
15
|
*/
|
|
16
|
-
type
|
|
17
|
-
[
|
|
16
|
+
type EventWithRequestId = {
|
|
17
|
+
[E in EventName]: If<And<EventHasParams<E>, EventParams<E> extends {
|
|
18
18
|
req_id: string;
|
|
19
|
-
} ? true : false>,
|
|
19
|
+
} ? true : false>, E, never>;
|
|
20
20
|
}[EventName];
|
|
21
21
|
export interface RequestOptions {
|
|
22
22
|
/**
|
|
@@ -45,7 +45,7 @@ export interface RequestOptionsAdvanced<EventPayload> extends RequestOptions {
|
|
|
45
45
|
* @param event - event or events to listen.
|
|
46
46
|
* @param options - additional execution options.
|
|
47
47
|
*/
|
|
48
|
-
export declare function request<
|
|
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
49
|
/**
|
|
50
50
|
* Calls specified TWA method and captures one of the specified events. Returns promise
|
|
51
51
|
* which will be resolved in case, specified event was captured.
|
|
@@ -53,7 +53,7 @@ export declare function request<Method extends MethodNameWithRequestId, Event ex
|
|
|
53
53
|
* @param event - event or events to listen.
|
|
54
54
|
* @param options - additional execution options.
|
|
55
55
|
*/
|
|
56
|
-
export declare function request<
|
|
56
|
+
export declare function request<M extends EmptyMethodName, E extends EventName>(method: M, event: E | E[], options?: RequestOptionsAdvanced<EventParams<E>>): Promise<EventParams<E>>;
|
|
57
57
|
/**
|
|
58
58
|
* Calls specified TWA method and captures one of the specified events. Returns promise
|
|
59
59
|
* which will be resolved in case, specified event was captured.
|
|
@@ -62,5 +62,5 @@ export declare function request<Method extends EmptyMethodName, Event extends Ev
|
|
|
62
62
|
* @param event - event or events to listen
|
|
63
63
|
* @param options - additional execution options.
|
|
64
64
|
*/
|
|
65
|
-
export declare function request<
|
|
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
66
|
export {};
|
package/dist/dts/supports.d.ts
CHANGED
|
@@ -1,19 +1,12 @@
|
|
|
1
1
|
import { type Version } from '@tma.js/utils';
|
|
2
|
-
import type {
|
|
3
|
-
/**
|
|
4
|
-
* By specified method and parameters extracts properties which could be used by
|
|
5
|
-
* supports function as TWA method parameter.
|
|
6
|
-
* @param method - TWA method.
|
|
7
|
-
* @param params - method parameters.
|
|
8
|
-
*/
|
|
9
|
-
export declare function detectSupportParams<M extends NonEmptyMethodName>(method: M, params: MethodParams<M>): HasCheckSupportMethodParam<HasCheckSupportMethodName>[];
|
|
2
|
+
import type { MethodVersionedParams, MethodWithVersionedParams, MethodName } from './methods/index.js';
|
|
10
3
|
/**
|
|
11
4
|
* Returns true in case, passed parameter in specified method is supported.
|
|
12
5
|
* @param method - method name
|
|
13
6
|
* @param param - method parameter
|
|
14
7
|
* @param inVersion - platform version.
|
|
15
8
|
*/
|
|
16
|
-
export declare function supports<M extends
|
|
9
|
+
export declare function supports<M extends MethodWithVersionedParams>(method: M, param: MethodVersionedParams<M>, inVersion: Version): boolean;
|
|
17
10
|
/**
|
|
18
11
|
* Returns true in case, specified method is supported in passed version.
|
|
19
12
|
* @param method - method name.
|
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("@tma.js/utils"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("@tma.js/utils"),B=require("@tma.js/logger"),F=require("@tma.js/event-emitter"),i=require("@tma.js/parsing");class f extends Error{constructor(t,r){super(`Method "${t}" is unsupported in the Mini Apps version ${r}.`),Object.setPrototypeOf(this,f.prototype)}}class g extends Error{constructor(t,r,n){super(`Parameter "${r}" in method "${t}" is unsupported in the Mini Apps version ${n}.`),Object.setPrototypeOf(this,g.prototype)}}function R(){return i.json({req_id:i.string(),data:e=>e===null?e:i.string().optional().parse(e)})}function P(){return i.json({req_id:i.string(),result:e=>e,error:i.string().optional()})}function j(){return i.json({slug:i.string(),status:i.string()})}function k(){return i.json({status:i.string()})}function A(){return i.json({button_id:e=>e==null?void 0:i.string().parse(e)})}function C(){return i.json({data:i.string().optional()})}function M(){return i.json({theme_params:e=>{const t=i.rgb().optional();return Object.entries(i.toRecord(e)).reduce((r,[n,o])=>(r[n]=t.parse(o),r),{})}})}function W(){return i.json({height:i.number(),width:e=>e==null?window.innerWidth:i.number().parse(e),is_state_stable:i.boolean(),is_expanded:i.boolean()})}function T(){return i.json({status:i.string()})}let $="https://web.telegram.org";const a=new B.Logger("[Bridge]",!1);function K(e){if(e){a.enable();return}a.disable()}function Q(e){$=e}function X(){return $}const Y=i.json({eventType:i.string(),eventData:e=>e});function Z(e,t){window.dispatchEvent(new MessageEvent("message",{data:JSON.stringify({eventType:e,eventData:t})}))}function D(){const e=window;"TelegramGameProxy_receiveEvent"in e||[["TelegramGameProxy_receiveEvent"],["TelegramGameProxy","receiveEvent"],["Telegram","WebView","receiveEvent"]].forEach(t=>{let r=e;t.forEach((n,o,s)=>{if(o===s.length-1){r[n]=Z;return}n in r||(r[n]={}),r=r[n]})})}function V(e){D(),window.addEventListener("message",t=>{try{const{eventType:r,eventData:n}=Y.parse(t.data);e(r,n)}catch{}})}const b="__telegram-cached-emitter__";function O(){const e=new F.EventEmitter,t=(r,...n)=>{a.log("Emitting processed event:",r,...n),e.emit(r,...n)};return window.addEventListener("resize",()=>{t("viewport_changed",{width:window.innerWidth,height:window.innerHeight,is_state_stable:!0,is_expanded:!0})}),V((r,n)=>{a.log("Received raw event:",r,n);try{switch(r){case"viewport_changed":return t(r,W().parse(n));case"theme_changed":return t(r,M().parse(n));case"popup_closed":return n==null?t(r,{}):t(r,A().parse(n));case"set_custom_style":return t(r,i.string().parse(n));case"qr_text_received":return t(r,C().parse(n));case"clipboard_text_received":return t(r,R().parse(n));case"invoice_closed":return t(r,j().parse(n));case"phone_requested":return t("phone_requested",k().parse(n));case"custom_method_invoked":return t("custom_method_invoked",P().parse(n));case"write_access_requested":return t("write_access_requested",T().parse(n));case"main_button_pressed":case"back_button_pressed":case"settings_button_pressed":case"scan_qr_popup_closed":case"reload_iframe":return t(r);default:return t(r,n)}}catch(o){a.error("Error processing event:",o)}}),e}function _(){const e=window;return e[b]===void 0&&(e[b]=O()),e[b]}function E(e,t){_().off(e,t)}function I(e,t){return _().on(e,t),()=>E(e,t)}function ee(e,t){return _().once(e,t),()=>E(e,t)}function N(e){_().unsubscribe(e)}function te(e){return _().subscribe(e),()=>N(e)}function c(e,t){return u.compareVersions(e,t)<=0}function h(e,t,r){if(typeof r=="string"){if(e==="web_app_open_link"&&t==="try_instant_view")return c("6.4",r);if(e==="web_app_set_header_color"&&t==="color")return c("6.9",r)}switch(e){case"web_app_open_tg_link":case"web_app_open_invoice":case"web_app_setup_back_button":case"web_app_set_background_color":case"web_app_set_header_color":case"web_app_trigger_haptic_feedback":return c("6.1",t);case"web_app_open_popup":return c("6.2",t);case"web_app_close_scan_qr_popup":case"web_app_open_scan_qr_popup":case"web_app_read_text_from_clipboard":return c("6.4",t);case"web_app_switch_inline_query":return c("6.7",t);case"web_app_invoke_custom_method":case"web_app_request_write_access":case"web_app_request_phone":return c("6.9",t);case"web_app_setup_settings_button":return c("6.10",t);default:return!0}}function re(e){return(t,r)=>{if(!h(t,e))throw new f(t,e);if(u.isRecord(r)){let n;if(t==="web_app_open_link"&&"try_instant_view"in r?n="try_instant_view":t==="web_app_set_header_color"&&"color"in r&&(n="color"),n&&!h(t,n,e))throw new g(t,n,e)}return y(t,r)}}function S(e){return"external"in e&&u.isRecord(e.external)&&"notify"in e.external&&typeof e.external.notify=="function"}function J(e){return"TelegramWebviewProxy"in e&&u.isRecord(e.TelegramWebviewProxy)&&"postEvent"in e.TelegramWebviewProxy&&typeof e.TelegramWebviewProxy.postEvent=="function"}function L(){try{return window.self!==window.top}catch{return!0}}function y(e,t,r){let n={},o;t===void 0&&r===void 0?n={}:t!==void 0&&r!==void 0?(n=r,o=t):t!==void 0&&("targetOrigin"in t?n=t:o=t);const{targetOrigin:s=X()}=n;if(a.log(`Calling method "${e}"`,o),L()){window.parent.postMessage(JSON.stringify({eventType:e,eventData:o}),s);return}if(S(window)){window.external.notify(JSON.stringify({eventType:e,eventData:o}));return}if(J(window)){window.TelegramWebviewProxy.postEvent(e,JSON.stringify(o));return}throw new Error("Unable to determine current environment and possible way to send event.")}function ne(e,t,r,n){let o,s,w,l;typeof t=="string"||Array.isArray(t)?(w=Array.isArray(t)?t:[t],o=r):(s=t,w=Array.isArray(r)?r:[r],o=n),u.isRecord(s)&&typeof s.req_id=="string"&&(l=s.req_id);const{postEvent:G=y,timeout:m}=o||{},q=o&&"capture"in o?o.capture:null,v=new Promise((H,U)=>{const z=w.map(p=>I(p,d=>{typeof l=="string"&&(!u.isRecord(d)||d.req_id!==l)||typeof q=="function"&&!q(d)||(x(),H(d))})),x=()=>z.forEach(p=>p());try{G(e,s)}catch(p){x(),U(p)}});return typeof m=="number"?u.withTimeout(v,m):v}exports.MethodUnsupportedError=f;exports.ParameterUnsupportedError=g;exports.clipboardTextReceived=R;exports.createPostEvent=re;exports.customMethodInvoked=P;exports.hasExternalNotify=S;exports.hasWebviewProxy=J;exports.invoiceClosed=j;exports.isIframe=L;exports.off=E;exports.on=I;exports.once=ee;exports.phoneRequested=k;exports.popupClosed=A;exports.postEvent=y;exports.qrTextReceived=C;exports.request=ne;exports.setDebug=K;exports.setTargetOrigin=Q;exports.subscribe=te;exports.supports=h;exports.themeChanged=M;exports.unsubscribe=N;exports.viewportChanged=W;exports.writeAccessRequested=T;
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|