@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
package/src/index.ts
CHANGED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { isRecord, type Version } from '@tma.js/utils';
|
|
2
|
+
|
|
3
|
+
import { supports } from '../supports.js';
|
|
4
|
+
import { MethodUnsupportedError, ParameterUnsupportedError } from '../errors/index.js';
|
|
5
|
+
import { postEvent, type PostEvent } from './index.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Creates function which checks if specified method and parameters are supported. In case,
|
|
9
|
+
* method or parameters are unsupported, an error will be thrown.
|
|
10
|
+
* @param version - Telegram Mini Apps version.
|
|
11
|
+
* @throws {MethodUnsupportedError} Method is unsupported.
|
|
12
|
+
* @throws {ParameterUnsupportedError} Method parameter is unsupported.
|
|
13
|
+
*/
|
|
14
|
+
export function createPostEvent(version: Version): PostEvent {
|
|
15
|
+
return (method: any, params: any) => {
|
|
16
|
+
// Firstly, check if method itself is supported.
|
|
17
|
+
if (!supports(method, version)) {
|
|
18
|
+
throw new MethodUnsupportedError(method, version);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Method could use parameters, which are supported only in specific versions of Telegram
|
|
22
|
+
// Mini Apps.
|
|
23
|
+
if (isRecord(params)) {
|
|
24
|
+
let validateParam: string | undefined;
|
|
25
|
+
|
|
26
|
+
if (method === 'web_app_open_link' && 'try_instant_view' in params) {
|
|
27
|
+
validateParam = 'try_instant_view';
|
|
28
|
+
} else if (method === 'web_app_set_header_color' && 'color' in params) {
|
|
29
|
+
validateParam = 'color';
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (validateParam && !supports(method, validateParam, version)) {
|
|
33
|
+
throw new ParameterUnsupportedError(method, validateParam, version);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return postEvent(method, params);
|
|
38
|
+
};
|
|
39
|
+
}
|
package/src/methods/index.ts
CHANGED
|
@@ -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';
|
|
@@ -16,22 +16,33 @@ export type HeaderColorKey = 'bg_color' | 'secondary_bg_color';
|
|
|
16
16
|
*/
|
|
17
17
|
export type SwitchInlineQueryChatType = 'users' | 'bots' | 'groups' | 'channels';
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
params:
|
|
21
|
-
|
|
22
|
-
}
|
|
19
|
+
interface CreateParams<Params = undefined, VersionedParam extends UnionKeys<Params> = never> {
|
|
20
|
+
params: Params;
|
|
21
|
+
versionedParams: VersionedParam;
|
|
22
|
+
}
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
25
|
* Describes list of events and their parameters that could be posted by Bridge.
|
|
26
26
|
* @see https://docs.telegram-mini-apps.com/apps-communication/methods
|
|
27
27
|
*/
|
|
28
|
-
export interface
|
|
28
|
+
export interface Methods {
|
|
29
29
|
/**
|
|
30
30
|
* Notifies parent iframe about the current frame is ready. This method is only used in the Web
|
|
31
31
|
* version of Telegram. As a result, Mini App will receive `set_custom_style` event.
|
|
32
32
|
* @see https://docs.telegram-mini-apps.com/apps-communication/methods#iframe-ready
|
|
33
33
|
*/
|
|
34
|
-
iframe_ready: CreateParams
|
|
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;
|
|
35
46
|
|
|
36
47
|
/**
|
|
37
48
|
* Closes Mini App.
|
|
@@ -319,40 +330,44 @@ export interface MethodsParams {
|
|
|
319
330
|
}
|
|
320
331
|
|
|
321
332
|
/**
|
|
322
|
-
* Any
|
|
333
|
+
* Any Telegram Mini Apps known method name.
|
|
323
334
|
*/
|
|
324
|
-
export type MethodName = keyof
|
|
335
|
+
export type MethodName = keyof Methods;
|
|
325
336
|
|
|
326
337
|
/**
|
|
327
338
|
* Returns parameters for specified post-available event.
|
|
328
339
|
*/
|
|
329
|
-
export type MethodParams<
|
|
340
|
+
export type MethodParams<M extends MethodName> = Methods[M]['params'];
|
|
330
341
|
|
|
331
342
|
/**
|
|
332
|
-
*
|
|
343
|
+
* True if specified method accepts parameters.
|
|
333
344
|
*/
|
|
334
|
-
export type
|
|
345
|
+
export type MethodAcceptParams<M extends MethodName> =
|
|
346
|
+
Not<IsNever<Exclude<MethodParams<M>, undefined>>>;
|
|
335
347
|
|
|
336
348
|
/**
|
|
337
349
|
* Any post-available event name which does not require arguments.
|
|
338
350
|
*/
|
|
339
351
|
export type EmptyMethodName = {
|
|
340
|
-
[
|
|
352
|
+
[M in MethodName]: undefined extends MethodParams<M> ? M : never;
|
|
341
353
|
}[MethodName];
|
|
342
354
|
|
|
343
355
|
/**
|
|
344
356
|
* Any post-available event name which require arguments.
|
|
345
357
|
*/
|
|
346
|
-
export type NonEmptyMethodName =
|
|
358
|
+
export type NonEmptyMethodName = {
|
|
359
|
+
[M in MethodName]: MethodAcceptParams<M> extends true ? M : never;
|
|
360
|
+
}[MethodName];
|
|
347
361
|
|
|
348
362
|
/**
|
|
349
|
-
* Method names which
|
|
363
|
+
* Method names which have versioned params.
|
|
350
364
|
*/
|
|
351
|
-
export type
|
|
352
|
-
[
|
|
365
|
+
export type MethodWithVersionedParams = {
|
|
366
|
+
[M in MethodName]: IsNever<Methods[M]['versionedParams']> extends true ? never : M;
|
|
353
367
|
}[MethodName];
|
|
354
368
|
|
|
355
369
|
/**
|
|
356
|
-
* Method
|
|
370
|
+
* Method parameters which appear only in the specific Telegram Mini Apps version.
|
|
357
371
|
*/
|
|
358
|
-
export type
|
|
372
|
+
export type MethodVersionedParams<M extends MethodWithVersionedParams> =
|
|
373
|
+
Methods[M]['versionedParams'];
|
package/src/methods/postEvent.ts
CHANGED
package/src/request.ts
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
import { withTimeout, isRecord } from '@tma.js/utils';
|
|
2
|
-
|
|
3
2
|
import type { And, If, IsNever } from '@tma.js/util-types';
|
|
4
3
|
|
|
5
4
|
import { postEvent as defaultPostEvent, type PostEvent } from './methods/postEvent.js';
|
|
6
5
|
import { on, type EventName, type EventParams, type EventHasParams } from './events/index.js';
|
|
7
6
|
|
|
8
7
|
import type {
|
|
9
|
-
EmptyMethodName,
|
|
8
|
+
EmptyMethodName,
|
|
9
|
+
MethodAcceptParams,
|
|
10
10
|
MethodName,
|
|
11
11
|
MethodParams,
|
|
12
12
|
NonEmptyMethodName,
|
|
13
|
-
} from './methods/
|
|
13
|
+
} from './methods/methods.js';
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* Names of methods, which require passing "req_id" parameter.
|
|
17
17
|
*/
|
|
18
|
-
type
|
|
19
|
-
[
|
|
20
|
-
And<
|
|
21
|
-
|
|
18
|
+
type MethodWithRequestId = {
|
|
19
|
+
[M in MethodName]: If<
|
|
20
|
+
And<MethodAcceptParams<M>, MethodParams<M> extends { req_id: string } ? true : false>,
|
|
21
|
+
M,
|
|
22
22
|
never
|
|
23
23
|
>;
|
|
24
24
|
}[MethodName];
|
|
@@ -26,10 +26,10 @@ type MethodNameWithRequestId = {
|
|
|
26
26
|
/**
|
|
27
27
|
* Names of events, which contain "req_id" parameter.
|
|
28
28
|
*/
|
|
29
|
-
type
|
|
30
|
-
[
|
|
31
|
-
And<EventHasParams<
|
|
32
|
-
|
|
29
|
+
type EventWithRequestId = {
|
|
30
|
+
[E in EventName]: If<
|
|
31
|
+
And<EventHasParams<E>, EventParams<E> extends { req_id: string } ? true : false>,
|
|
32
|
+
E,
|
|
33
33
|
never
|
|
34
34
|
>;
|
|
35
35
|
}[EventName];
|
|
@@ -64,15 +64,12 @@ export interface RequestOptionsAdvanced<EventPayload> extends RequestOptions {
|
|
|
64
64
|
* @param event - event or events to listen.
|
|
65
65
|
* @param options - additional execution options.
|
|
66
66
|
*/
|
|
67
|
-
export function request<
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
method: Method,
|
|
72
|
-
params: MethodParams<Method>,
|
|
73
|
-
event: Event | Event[],
|
|
67
|
+
export function request<M extends MethodWithRequestId, E extends EventWithRequestId>(
|
|
68
|
+
method: M,
|
|
69
|
+
params: MethodParams<M>,
|
|
70
|
+
event: E | E[],
|
|
74
71
|
options?: RequestOptions,
|
|
75
|
-
): Promise<EventParams<
|
|
72
|
+
): Promise<EventParams<E>>;
|
|
76
73
|
|
|
77
74
|
/**
|
|
78
75
|
* Calls specified TWA method and captures one of the specified events. Returns promise
|
|
@@ -81,11 +78,11 @@ export function request<
|
|
|
81
78
|
* @param event - event or events to listen.
|
|
82
79
|
* @param options - additional execution options.
|
|
83
80
|
*/
|
|
84
|
-
export function request<
|
|
85
|
-
method:
|
|
86
|
-
event:
|
|
87
|
-
options?: RequestOptionsAdvanced<EventParams<
|
|
88
|
-
): Promise<EventParams<
|
|
81
|
+
export function request<M extends EmptyMethodName, E extends EventName>(
|
|
82
|
+
method: M,
|
|
83
|
+
event: E | E[],
|
|
84
|
+
options?: RequestOptionsAdvanced<EventParams<E>>,
|
|
85
|
+
): Promise<EventParams<E>>;
|
|
89
86
|
|
|
90
87
|
/**
|
|
91
88
|
* Calls specified TWA method and captures one of the specified events. Returns promise
|
|
@@ -95,12 +92,12 @@ export function request<Method extends EmptyMethodName, Event extends EventName>
|
|
|
95
92
|
* @param event - event or events to listen
|
|
96
93
|
* @param options - additional execution options.
|
|
97
94
|
*/
|
|
98
|
-
export function request<
|
|
99
|
-
method:
|
|
100
|
-
params: MethodParams<
|
|
101
|
-
event:
|
|
102
|
-
options?: RequestOptionsAdvanced<EventParams<
|
|
103
|
-
): Promise<EventParams<
|
|
95
|
+
export function request<M extends NonEmptyMethodName, E extends EventName>(
|
|
96
|
+
method: M,
|
|
97
|
+
params: MethodParams<M>,
|
|
98
|
+
event: E | E[],
|
|
99
|
+
options?: RequestOptionsAdvanced<EventParams<E>>,
|
|
100
|
+
): Promise<EventParams<E>>;
|
|
104
101
|
|
|
105
102
|
export function request(
|
|
106
103
|
method: MethodName,
|
package/src/supports.ts
CHANGED
|
@@ -1,40 +1,18 @@
|
|
|
1
1
|
import { compareVersions, type Version } from '@tma.js/utils';
|
|
2
2
|
|
|
3
3
|
import type {
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
MethodVersionedParams,
|
|
5
|
+
MethodWithVersionedParams,
|
|
6
6
|
MethodName,
|
|
7
|
-
MethodParams,
|
|
8
|
-
NonEmptyMethodName,
|
|
9
7
|
} from './methods/index.js';
|
|
10
8
|
|
|
11
|
-
function lessOrEqual(a: Version, b: Version): boolean {
|
|
12
|
-
return compareVersions(a, b) <= 0;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
9
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* @param
|
|
19
|
-
* @param params - method parameters.
|
|
10
|
+
* Returns true if "a" version is less than or equal to "b" version.
|
|
11
|
+
* @param a
|
|
12
|
+
* @param b
|
|
20
13
|
*/
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
params: MethodParams<M>,
|
|
24
|
-
): HasCheckSupportMethodParam<HasCheckSupportMethodName>[] {
|
|
25
|
-
if (method === 'web_app_open_link') {
|
|
26
|
-
if ('try_instant_view' in params) {
|
|
27
|
-
return ['try_instant_view'];
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
if (method === 'web_app_set_header_color') {
|
|
32
|
-
if ('color' in params) {
|
|
33
|
-
return ['color'];
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return [];
|
|
14
|
+
function versionLessOrEqual(a: Version, b: Version): boolean {
|
|
15
|
+
return compareVersions(a, b) <= 0;
|
|
38
16
|
}
|
|
39
17
|
|
|
40
18
|
/**
|
|
@@ -43,17 +21,19 @@ export function detectSupportParams<M extends NonEmptyMethodName>(
|
|
|
43
21
|
* @param param - method parameter
|
|
44
22
|
* @param inVersion - platform version.
|
|
45
23
|
*/
|
|
46
|
-
export function supports<M extends
|
|
24
|
+
export function supports<M extends MethodWithVersionedParams>(
|
|
47
25
|
method: M,
|
|
48
|
-
param:
|
|
26
|
+
param: MethodVersionedParams<M>,
|
|
49
27
|
inVersion: Version,
|
|
50
28
|
): boolean;
|
|
29
|
+
|
|
51
30
|
/**
|
|
52
31
|
* Returns true in case, specified method is supported in passed version.
|
|
53
32
|
* @param method - method name.
|
|
54
33
|
* @param inVersion - platform version.
|
|
55
34
|
*/
|
|
56
35
|
export function supports(method: MethodName, inVersion: Version): boolean;
|
|
36
|
+
|
|
57
37
|
export function supports(
|
|
58
38
|
method: MethodName,
|
|
59
39
|
paramOrVersion: Version | string,
|
|
@@ -63,18 +43,17 @@ export function supports(
|
|
|
63
43
|
if (typeof inVersion === 'string') {
|
|
64
44
|
if (method === 'web_app_open_link') {
|
|
65
45
|
if (paramOrVersion === 'try_instant_view') {
|
|
66
|
-
return
|
|
46
|
+
return versionLessOrEqual('6.4', inVersion);
|
|
67
47
|
}
|
|
68
48
|
}
|
|
69
49
|
|
|
70
50
|
if (method === 'web_app_set_header_color') {
|
|
71
51
|
if (paramOrVersion === 'color') {
|
|
72
|
-
return
|
|
52
|
+
return versionLessOrEqual('6.9', inVersion);
|
|
73
53
|
}
|
|
74
54
|
}
|
|
75
55
|
}
|
|
76
56
|
|
|
77
|
-
// Method name, target version.
|
|
78
57
|
switch (method) {
|
|
79
58
|
case 'web_app_open_tg_link':
|
|
80
59
|
case 'web_app_open_invoice':
|
|
@@ -82,21 +61,21 @@ export function supports(
|
|
|
82
61
|
case 'web_app_set_background_color':
|
|
83
62
|
case 'web_app_set_header_color':
|
|
84
63
|
case 'web_app_trigger_haptic_feedback':
|
|
85
|
-
return
|
|
64
|
+
return versionLessOrEqual('6.1', paramOrVersion);
|
|
86
65
|
case 'web_app_open_popup':
|
|
87
|
-
return
|
|
66
|
+
return versionLessOrEqual('6.2', paramOrVersion);
|
|
88
67
|
case 'web_app_close_scan_qr_popup':
|
|
89
68
|
case 'web_app_open_scan_qr_popup':
|
|
90
69
|
case 'web_app_read_text_from_clipboard':
|
|
91
|
-
return
|
|
70
|
+
return versionLessOrEqual('6.4', paramOrVersion);
|
|
92
71
|
case 'web_app_switch_inline_query':
|
|
93
|
-
return
|
|
72
|
+
return versionLessOrEqual('6.7', paramOrVersion);
|
|
94
73
|
case 'web_app_invoke_custom_method':
|
|
95
74
|
case 'web_app_request_write_access':
|
|
96
75
|
case 'web_app_request_phone':
|
|
97
|
-
return
|
|
76
|
+
return versionLessOrEqual('6.9', paramOrVersion);
|
|
98
77
|
case 'web_app_setup_settings_button':
|
|
99
|
-
return
|
|
78
|
+
return versionLessOrEqual('6.10', paramOrVersion);
|
|
100
79
|
default:
|
|
101
80
|
return true;
|
|
102
81
|
}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import type { ClipboardTextReceivedPayload, CustomMethodInvokedPayload, InvoiceClosedPayload, PhoneRequestedPayload, PopupClosedPayload, QrTextReceivedPayload, ThemeChangedPayload, ViewportChangedPayload, WriteAccessRequestedPayload } from './payloads.js';
|
|
2
|
-
/**
|
|
3
|
-
* Parses incoming value as ThemeChangedPayload.
|
|
4
|
-
*/
|
|
5
|
-
export declare const themeChangedPayload: import("@tma.js/parsing").ValueParser<ThemeChangedPayload, false>;
|
|
6
|
-
/**
|
|
7
|
-
* Parses incoming value as ViewportChangedPayload.
|
|
8
|
-
* @param value - value to parse.
|
|
9
|
-
*/
|
|
10
|
-
export declare const viewportChangedPayload: import("@tma.js/parsing").ValueParser<ViewportChangedPayload, false>;
|
|
11
|
-
/**
|
|
12
|
-
* Parses incoming value as PopupClosedPayload.
|
|
13
|
-
*/
|
|
14
|
-
export declare const popupClosedPayload: import("@tma.js/parsing").ValueParser<PopupClosedPayload, false>;
|
|
15
|
-
/**
|
|
16
|
-
* Parses incoming value as QrTextReceivedPayload.
|
|
17
|
-
*/
|
|
18
|
-
export declare const qrTextReceivedPayload: import("@tma.js/parsing").ValueParser<QrTextReceivedPayload, false>;
|
|
19
|
-
/**
|
|
20
|
-
* Parses incoming value as InvoiceClosedPayload.
|
|
21
|
-
*/
|
|
22
|
-
export declare const invoiceClosedPayload: import("@tma.js/parsing").ValueParser<InvoiceClosedPayload, false>;
|
|
23
|
-
/**
|
|
24
|
-
* Parses incoming value as clipboard text received payload.
|
|
25
|
-
*/
|
|
26
|
-
export declare const clipboardTextReceivedPayload: import("@tma.js/parsing").ValueParser<ClipboardTextReceivedPayload, false>;
|
|
27
|
-
/**
|
|
28
|
-
* Parses incoming value as WriteAccessRequestedPayload.
|
|
29
|
-
*/
|
|
30
|
-
export declare const writeAccessRequestedPayload: import("@tma.js/parsing").ValueParser<WriteAccessRequestedPayload, false>;
|
|
31
|
-
/**
|
|
32
|
-
* Parses incoming value as PhoneRequestedPayload.
|
|
33
|
-
*/
|
|
34
|
-
export declare const phoneRequestedPayload: import("@tma.js/parsing").ValueParser<PhoneRequestedPayload, false>;
|
|
35
|
-
/**
|
|
36
|
-
* Parses incoming value as CustomMethodInvokedPayload.
|
|
37
|
-
*/
|
|
38
|
-
export declare const customMethodInvokedPayload: import("@tma.js/parsing").ValueParser<CustomMethodInvokedPayload<unknown>, false>;
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import type { RGB } from '@tma.js/colors';
|
|
2
|
-
import type { RequestId } from '../shared.js';
|
|
3
|
-
export interface ClipboardTextReceivedPayload {
|
|
4
|
-
/**
|
|
5
|
-
* Passed during the `web_app_read_text_from_clipboard` method invocation `req_id` value.
|
|
6
|
-
*/
|
|
7
|
-
req_id: RequestId;
|
|
8
|
-
/**
|
|
9
|
-
* Data extracted from the clipboard. The returned value will have the type `string` only in
|
|
10
|
-
* the case, application has access to the clipboard.
|
|
11
|
-
*/
|
|
12
|
-
data?: string | null;
|
|
13
|
-
}
|
|
14
|
-
export interface CustomMethodInvokedPayload<R = unknown> {
|
|
15
|
-
/**
|
|
16
|
-
* Unique identifier of this invocation.
|
|
17
|
-
*/
|
|
18
|
-
req_id: RequestId;
|
|
19
|
-
/**
|
|
20
|
-
* Method invocation successful result.
|
|
21
|
-
*/
|
|
22
|
-
result?: R;
|
|
23
|
-
/**
|
|
24
|
-
* Method invocation error code.
|
|
25
|
-
*/
|
|
26
|
-
error?: string;
|
|
27
|
-
}
|
|
28
|
-
export type InvoiceStatus = 'paid' | 'failed' | 'pending' | 'cancelled' | string;
|
|
29
|
-
export interface InvoiceClosedPayload {
|
|
30
|
-
/**
|
|
31
|
-
* Passed during the `web_app_open_invoice` method invocation `slug` value.
|
|
32
|
-
*/
|
|
33
|
-
slug: string;
|
|
34
|
-
/**
|
|
35
|
-
* Invoice status
|
|
36
|
-
*/
|
|
37
|
-
status: InvoiceStatus;
|
|
38
|
-
}
|
|
39
|
-
export interface QrTextReceivedPayload {
|
|
40
|
-
/**
|
|
41
|
-
* Data extracted from the QR.
|
|
42
|
-
*/
|
|
43
|
-
data?: string;
|
|
44
|
-
}
|
|
45
|
-
export interface ThemeChangedPayload {
|
|
46
|
-
/**
|
|
47
|
-
* Map where the key is a theme stylesheet key and value is the corresponding color in
|
|
48
|
-
* `#RRGGBB` format.
|
|
49
|
-
*/
|
|
50
|
-
theme_params: {
|
|
51
|
-
bg_color?: RGB;
|
|
52
|
-
text_color?: RGB;
|
|
53
|
-
hint_color?: RGB;
|
|
54
|
-
link_color?: RGB;
|
|
55
|
-
button_color?: RGB;
|
|
56
|
-
button_text_color?: RGB;
|
|
57
|
-
secondary_bg_color?: RGB;
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
export interface ViewportChangedPayload {
|
|
61
|
-
/**
|
|
62
|
-
* The viewport height.
|
|
63
|
-
*/
|
|
64
|
-
height: number;
|
|
65
|
-
/**
|
|
66
|
-
* The viewport width.
|
|
67
|
-
*/
|
|
68
|
-
width: number;
|
|
69
|
-
/**
|
|
70
|
-
* Is the viewport currently expanded.
|
|
71
|
-
*/
|
|
72
|
-
is_expanded: boolean;
|
|
73
|
-
/**
|
|
74
|
-
* Is the viewport current state stable and not going to change in the next moment.
|
|
75
|
-
*/
|
|
76
|
-
is_state_stable: boolean;
|
|
77
|
-
}
|
|
78
|
-
export interface PopupClosedPayload {
|
|
79
|
-
/**
|
|
80
|
-
* Identifier of the clicked button. In case, the popup was closed without clicking any button,
|
|
81
|
-
* this property will be omitted.
|
|
82
|
-
*/
|
|
83
|
-
button_id?: string;
|
|
84
|
-
}
|
|
85
|
-
export type PhoneRequestedStatus = 'sent' | string;
|
|
86
|
-
export interface PhoneRequestedPayload {
|
|
87
|
-
/**
|
|
88
|
-
* Request status.
|
|
89
|
-
*/
|
|
90
|
-
status: PhoneRequestedStatus;
|
|
91
|
-
}
|
|
92
|
-
export type WriteAccessRequestedStatus = 'allowed' | string;
|
|
93
|
-
export interface WriteAccessRequestedPayload {
|
|
94
|
-
/**
|
|
95
|
-
* Request status.
|
|
96
|
-
*/
|
|
97
|
-
status: WriteAccessRequestedStatus;
|
|
98
|
-
}
|
package/src/events/parsing.ts
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
number,
|
|
3
|
-
string,
|
|
4
|
-
boolean,
|
|
5
|
-
json,
|
|
6
|
-
rgb,
|
|
7
|
-
createValueParserGenerator,
|
|
8
|
-
} from '@tma.js/parsing';
|
|
9
|
-
|
|
10
|
-
import type {
|
|
11
|
-
ClipboardTextReceivedPayload, CustomMethodInvokedPayload,
|
|
12
|
-
InvoiceClosedPayload, PhoneRequestedPayload,
|
|
13
|
-
PopupClosedPayload, QrTextReceivedPayload,
|
|
14
|
-
ThemeChangedPayload,
|
|
15
|
-
ViewportChangedPayload, WriteAccessRequestedPayload,
|
|
16
|
-
} from './payloads.js';
|
|
17
|
-
|
|
18
|
-
function isNullOrUndefined(value: unknown): boolean {
|
|
19
|
-
return value === null || value === undefined;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const rgbOptional = rgb().optional();
|
|
23
|
-
const num = number();
|
|
24
|
-
|
|
25
|
-
const windowWidthParser = createValueParserGenerator(
|
|
26
|
-
(value) => (value === null || value === undefined
|
|
27
|
-
? window.innerWidth
|
|
28
|
-
: num.parse(value)),
|
|
29
|
-
);
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Parses incoming value as ThemeChangedPayload.
|
|
33
|
-
*/
|
|
34
|
-
export const themeChangedPayload = json<ThemeChangedPayload>({
|
|
35
|
-
theme_params: json({
|
|
36
|
-
bg_color: rgbOptional,
|
|
37
|
-
text_color: rgbOptional,
|
|
38
|
-
hint_color: rgbOptional,
|
|
39
|
-
link_color: rgbOptional,
|
|
40
|
-
button_color: rgbOptional,
|
|
41
|
-
button_text_color: rgbOptional,
|
|
42
|
-
secondary_bg_color: rgbOptional,
|
|
43
|
-
}),
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Parses incoming value as ViewportChangedPayload.
|
|
48
|
-
* @param value - value to parse.
|
|
49
|
-
*/
|
|
50
|
-
export const viewportChangedPayload = json<ViewportChangedPayload>({
|
|
51
|
-
height: number(),
|
|
52
|
-
width: windowWidthParser(),
|
|
53
|
-
is_state_stable: boolean(),
|
|
54
|
-
is_expanded: boolean(),
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Parses incoming value as PopupClosedPayload.
|
|
59
|
-
*/
|
|
60
|
-
export const popupClosedPayload = json<PopupClosedPayload>({
|
|
61
|
-
button_id: string({ isEmpty: isNullOrUndefined }).optional(),
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Parses incoming value as QrTextReceivedPayload.
|
|
66
|
-
*/
|
|
67
|
-
export const qrTextReceivedPayload = json<QrTextReceivedPayload>({
|
|
68
|
-
data: string().optional(),
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Parses incoming value as InvoiceClosedPayload.
|
|
73
|
-
*/
|
|
74
|
-
export const invoiceClosedPayload = json<InvoiceClosedPayload>({
|
|
75
|
-
slug: string(),
|
|
76
|
-
status: string(),
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Parses incoming value as clipboard text received payload.
|
|
81
|
-
*/
|
|
82
|
-
export const clipboardTextReceivedPayload = json<ClipboardTextReceivedPayload>({
|
|
83
|
-
req_id: string(),
|
|
84
|
-
data: (value) => (value === null ? value : string().optional().parse(value)),
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Parses incoming value as WriteAccessRequestedPayload.
|
|
89
|
-
*/
|
|
90
|
-
export const writeAccessRequestedPayload = json<WriteAccessRequestedPayload>({ status: string() });
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Parses incoming value as PhoneRequestedPayload.
|
|
94
|
-
*/
|
|
95
|
-
export const phoneRequestedPayload = json<PhoneRequestedPayload>({ status: string() });
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Parses incoming value as CustomMethodInvokedPayload.
|
|
99
|
-
*/
|
|
100
|
-
export const customMethodInvokedPayload = json<CustomMethodInvokedPayload>({
|
|
101
|
-
req_id: string(),
|
|
102
|
-
result: (value) => value,
|
|
103
|
-
error: string().optional(),
|
|
104
|
-
});
|