@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.
- package/LICENSE +1 -1
- package/README.md +44 -13
- 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/isTMA.d.ts +31 -0
- package/dist/dts/env/mockTelegramEnv.d.ts +57 -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 -11
- package/dist/dts/events/types/events.d.ts +721 -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 +45 -14
- package/dist/dts/index.d.ts +24 -7
- package/dist/dts/launch-params.d.ts +32 -0
- package/dist/dts/methods/captureSameReq.d.ts +10 -0
- package/dist/dts/methods/createPostEvent.d.ts +27 -7
- package/dist/dts/methods/postEvent.d.ts +31 -25
- 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/index.d.ts +6 -0
- package/dist/dts/methods/types/methods.d.ts +756 -0
- package/dist/dts/methods/types/misc.d.ts +27 -0
- package/dist/dts/methods/{popup.d.ts → types/popup.d.ts} +1 -1
- 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 +53 -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 +90 -0
- package/dist/index.cjs +3 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.iife.js +3 -1
- package/dist/index.iife.js.map +1 -1
- package/dist/index.js +977 -0
- package/dist/index.js.map +1 -0
- package/package.json +26 -26
- package/dist/dts/env.d.ts +0 -29
- package/dist/dts/errors/MethodUnsupportedError.d.ts +0 -8
- package/dist/dts/errors/ParameterUnsupportedError.d.ts +0 -8
- package/dist/dts/errors/index.d.ts +0 -2
- package/dist/dts/events/events.d.ts +0 -130
- package/dist/dts/events/index.d.ts +0 -7
- package/dist/dts/events/off.d.ts +0 -7
- package/dist/dts/events/on.d.ts +0 -10
- package/dist/dts/events/onTelegramEvent.d.ts +0 -7
- package/dist/dts/events/once.d.ts +0 -9
- package/dist/dts/events/parsers/clipboardTextReceived.d.ts +0 -13
- package/dist/dts/events/parsers/customMethodInvoked.d.ts +0 -16
- package/dist/dts/events/parsers/index.d.ts +0 -9
- package/dist/dts/events/parsers/invoiceClosed.d.ts +0 -12
- package/dist/dts/events/parsers/phoneRequested.d.ts +0 -8
- package/dist/dts/events/parsers/popupClosed.d.ts +0 -8
- package/dist/dts/events/parsers/qrTextReceived.d.ts +0 -7
- package/dist/dts/events/parsers/theme-changed.d.ts +0 -42
- package/dist/dts/events/parsers/viewportChanged.d.ts +0 -19
- package/dist/dts/events/parsers/writeAccessRequested.d.ts +0 -8
- package/dist/dts/events/subscribe.d.ts +0 -9
- package/dist/dts/events/unsubscribe.d.ts +0 -6
- package/dist/dts/methods/index.d.ts +0 -6
- package/dist/dts/methods/invoke-custom-method.d.ts +0 -24
- package/dist/dts/methods/methods.d.ts +0 -330
- package/dist/dts/request.d.ts +0 -66
- package/dist/dts/shared.d.ts +0 -5
- package/dist/dts/supports.d.ts +0 -15
- package/dist/index.mjs +0 -317
- package/dist/index.mjs.map +0 -1
- package/src/env.ts +0 -49
- package/src/errors/MethodUnsupportedError.ts +0 -13
- package/src/errors/ParameterUnsupportedError.ts +0 -13
- package/src/errors/index.ts +0 -2
- package/src/events/emitter.ts +0 -126
- package/src/events/events.ts +0 -169
- package/src/events/index.ts +0 -7
- package/src/events/off.ts +0 -12
- package/src/events/on.ts +0 -17
- package/src/events/onTelegramEvent.ts +0 -83
- package/src/events/once.ts +0 -16
- package/src/events/parsers/clipboardTextReceived.ts +0 -27
- package/src/events/parsers/customMethodInvoked.ts +0 -26
- package/src/events/parsers/index.ts +0 -9
- package/src/events/parsers/invoiceClosed.ts +0 -26
- package/src/events/parsers/phoneRequested.ts +0 -14
- package/src/events/parsers/popupClosed.ts +0 -19
- package/src/events/parsers/qrTextReceived.ts +0 -14
- package/src/events/parsers/theme-changed.ts +0 -58
- package/src/events/parsers/viewportChanged.ts +0 -33
- package/src/events/parsers/writeAccessRequested.ts +0 -14
- package/src/events/subscribe.ts +0 -16
- package/src/events/unsubscribe.ts +0 -11
- package/src/globals.ts +0 -38
- package/src/index.ts +0 -7
- package/src/methods/createPostEvent.ts +0 -39
- package/src/methods/haptic.ts +0 -52
- package/src/methods/index.ts +0 -6
- package/src/methods/invoke-custom-method.ts +0 -25
- package/src/methods/methods.ts +0 -373
- package/src/methods/popup.ts +0 -53
- package/src/methods/postEvent.ts +0 -101
- package/src/request.ts +0 -168
- package/src/shared.ts +0 -5
- package/src/supports.ts +0 -82
- /package/dist/dts/methods/{haptic.d.ts → types/haptic-feedback.d.ts} +0 -0
package/src/events/events.ts
DELETED
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
EventEmitter as UtilEventEmitter,
|
|
3
|
-
EventListener as UtilEventListener,
|
|
4
|
-
EventParams as UtilEventParams,
|
|
5
|
-
AnySubscribeListener,
|
|
6
|
-
} from '@tma.js/event-emitter';
|
|
7
|
-
import type { IsNever, Not } from '@tma.js/util-types';
|
|
8
|
-
|
|
9
|
-
import type {
|
|
10
|
-
ClipboardTextReceivedPayload,
|
|
11
|
-
CustomMethodInvokedPayload,
|
|
12
|
-
InvoiceClosedPayload,
|
|
13
|
-
PhoneRequestedPayload,
|
|
14
|
-
PopupClosedPayload,
|
|
15
|
-
QrTextReceivedPayload,
|
|
16
|
-
ThemeChangedPayload,
|
|
17
|
-
ViewportChangedPayload,
|
|
18
|
-
WriteAccessRequestedPayload,
|
|
19
|
-
} from './parsers/index.js';
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Map where key is known event name, and value is its listener.
|
|
23
|
-
* @see https://docs.telegram-mini-apps.com/apps-communication/events
|
|
24
|
-
*/
|
|
25
|
-
export interface Events {
|
|
26
|
-
/**
|
|
27
|
-
* User clicked back button.
|
|
28
|
-
* @since v6.1
|
|
29
|
-
* @see https://docs.telegram-mini-apps.com/apps-communication/events#back-button-pressed
|
|
30
|
-
*/
|
|
31
|
-
back_button_pressed: () => void;
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Telegram application attempted to extract text from clipboard.
|
|
35
|
-
* @param payload - event payload.
|
|
36
|
-
* @since v6.4
|
|
37
|
-
* @see https://docs.telegram-mini-apps.com/apps-communication/events#clipboard-text-received
|
|
38
|
-
*/
|
|
39
|
-
clipboard_text_received: (payload: ClipboardTextReceivedPayload) => void;
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Custom method invocation completed.
|
|
43
|
-
* @param payload - event payload.
|
|
44
|
-
* @since v6.9
|
|
45
|
-
* @see https://docs.telegram-mini-apps.com/apps-communication/events#custom-method-invoked
|
|
46
|
-
*/
|
|
47
|
-
custom_method_invoked: (payload: CustomMethodInvokedPayload) => void;
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* An invoice was closed.
|
|
51
|
-
* @param payload - invoice close information.
|
|
52
|
-
* @see https://docs.telegram-mini-apps.com/apps-communication/events#invoice-closed
|
|
53
|
-
*/
|
|
54
|
-
invoice_closed: (payload: InvoiceClosedPayload) => void;
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* User clicked the Main Button.
|
|
58
|
-
* @see https://docs.telegram-mini-apps.com/apps-communication/events#main-button-pressed
|
|
59
|
-
*/
|
|
60
|
-
main_button_pressed: () => void;
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Application received phone access request status.
|
|
64
|
-
* @param payload - event payload.
|
|
65
|
-
* @since v6.9
|
|
66
|
-
* @see https://docs.telegram-mini-apps.com/apps-communication/events#phone-requested
|
|
67
|
-
*/
|
|
68
|
-
phone_requested: (payload: PhoneRequestedPayload) => void;
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Popup was closed.
|
|
72
|
-
* @param payload - event payload.
|
|
73
|
-
* @see https://docs.telegram-mini-apps.com/apps-communication/events#popup-closed
|
|
74
|
-
*/
|
|
75
|
-
popup_closed: (payload: PopupClosedPayload) => void;
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Parent iframe requested current iframe reload.
|
|
79
|
-
* @see https://docs.telegram-mini-apps.com/apps-communication/events#reload-iframe
|
|
80
|
-
*/
|
|
81
|
-
reload_iframe: () => void;
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* The QR scanner scanned some QR and extracted its content.
|
|
85
|
-
* @param payload - event payload.
|
|
86
|
-
* @since v6.4
|
|
87
|
-
* @see https://docs.telegram-mini-apps.com/apps-communication/events#qr-text-received
|
|
88
|
-
*/
|
|
89
|
-
qr_text_received: (payload: QrTextReceivedPayload) => void;
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* QR scanner was closed.
|
|
93
|
-
* @since v6.4
|
|
94
|
-
* @see https://docs.telegram-mini-apps.com/apps-communication/events#scan-qr-popup-closed
|
|
95
|
-
*/
|
|
96
|
-
scan_qr_popup_closed: () => void;
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* The event which is usually sent by the Telegram web application. Its payload represents
|
|
100
|
-
* `<style/>` tag html content, a developer could use. The stylesheet described in the payload
|
|
101
|
-
* will help the developer to stylize the app scrollbar (but he is still able to do it himself).
|
|
102
|
-
* @param html - `style` tag inner HTML.
|
|
103
|
-
* @see https://docs.telegram-mini-apps.com/apps-communication/events#set-custom-style
|
|
104
|
-
*/
|
|
105
|
-
set_custom_style: (html: string) => void;
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Occurs when the Settings Button was pressed.
|
|
109
|
-
* @since v6.1
|
|
110
|
-
* @see https://docs.telegram-mini-apps.com/apps-communication/events#settings-button-pressed
|
|
111
|
-
*/
|
|
112
|
-
settings_button_pressed: () => void;
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Occurs whenever theme settings are changed in the user's Telegram app
|
|
116
|
-
* (including switching to night mode).
|
|
117
|
-
* @param payload - event payload.
|
|
118
|
-
* @see https://docs.telegram-mini-apps.com/apps-communication/events#theme-changed
|
|
119
|
-
*/
|
|
120
|
-
theme_changed: (payload: ThemeChangedPayload) => void;
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Occurs whenever the viewport has been changed. For example, when the user started
|
|
124
|
-
* dragging the application or called the expansion method.
|
|
125
|
-
* @param payload - event payload.
|
|
126
|
-
* @see https://docs.telegram-mini-apps.com/apps-communication/events#viewport-changed
|
|
127
|
-
*/
|
|
128
|
-
viewport_changed: (payload: ViewportChangedPayload) => void;
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Application received write access request status.
|
|
132
|
-
* @param payload - event payload.
|
|
133
|
-
* @since v6.9
|
|
134
|
-
* @see https://docs.telegram-mini-apps.com/apps-communication/events#write-access-requested
|
|
135
|
-
*/
|
|
136
|
-
write_access_requested: (payload: WriteAccessRequestedPayload) => void;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Any known event name.
|
|
141
|
-
*/
|
|
142
|
-
export type EventName = keyof Events;
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Parameters of specified event.
|
|
146
|
-
*/
|
|
147
|
-
export type EventParams<E extends EventName> = UtilEventParams<Events[E]>[0];
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Returns event listener for specified event name.
|
|
151
|
-
*/
|
|
152
|
-
export type EventListener<E extends EventName> = UtilEventListener<Events[E]>;
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Event emitter, based describe events map.
|
|
156
|
-
*/
|
|
157
|
-
export type EventEmitter = UtilEventEmitter<Events>;
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* Returns true in case, event has parameters.
|
|
161
|
-
*/
|
|
162
|
-
export type EventHasParams<E extends EventName> = Not<IsNever<EventParams<E>>>;
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Event listener used in `subscribe` and `unsubscribe` functions.
|
|
166
|
-
*/
|
|
167
|
-
export type GlobalEventListener =
|
|
168
|
-
| AnySubscribeListener<Events>
|
|
169
|
-
| ((event: string, data: unknown) => void);
|
package/src/events/index.ts
DELETED
package/src/events/off.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { singletonEmitter } from './emitter.js';
|
|
2
|
-
|
|
3
|
-
import type { EventName, EventListener } from './events.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Removes listener from specified event.
|
|
7
|
-
* @param event - event to listen.
|
|
8
|
-
* @param listener - event listener.
|
|
9
|
-
*/
|
|
10
|
-
export function off<E extends EventName>(event: E, listener: EventListener<E>): void {
|
|
11
|
-
singletonEmitter().off(event, listener);
|
|
12
|
-
}
|
package/src/events/on.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { singletonEmitter } from './emitter.js';
|
|
2
|
-
import { off } from './off.js';
|
|
3
|
-
|
|
4
|
-
import type { EventName, EventListener } from './events.js';
|
|
5
|
-
|
|
6
|
-
type StopListening = () => void;
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Adds new listener to the specified event. Returns handler
|
|
10
|
-
* which allows to stop listening to event.
|
|
11
|
-
* @param event - event name.
|
|
12
|
-
* @param listener - event listener.
|
|
13
|
-
*/
|
|
14
|
-
export function on<E extends EventName>(event: E, listener: EventListener<E>): StopListening {
|
|
15
|
-
singletonEmitter().on(event, listener);
|
|
16
|
-
return () => off(event, listener);
|
|
17
|
-
}
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import { json, string } from '@tma.js/parsing';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Extracts event data from native application event.
|
|
5
|
-
*/
|
|
6
|
-
const eventDataJson = json<{ eventType: string; eventData?: unknown }>({
|
|
7
|
-
eventType: string(),
|
|
8
|
-
eventData: (value) => value,
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Emits event sent from Telegram native application like it was sent in
|
|
13
|
-
* default web environment between 2 iframes. It dispatches new MessageEvent
|
|
14
|
-
* and expects it to be handled via `window.addEventListener('message', ...)`
|
|
15
|
-
* as developer would do it to handle messages sent from parent iframe.
|
|
16
|
-
* @param eventType - event name.
|
|
17
|
-
* @param eventData - event payload.
|
|
18
|
-
*/
|
|
19
|
-
function emitEvent(eventType: string, eventData: unknown): void {
|
|
20
|
-
window.dispatchEvent(new MessageEvent('message', {
|
|
21
|
-
data: JSON.stringify({ eventType, eventData }),
|
|
22
|
-
}));
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Defines special handlers by known paths, which are recognized by
|
|
27
|
-
* Telegram as ports to receive events. This function also sets special
|
|
28
|
-
* function in global window object to prevent duplicate declaration.
|
|
29
|
-
*/
|
|
30
|
-
function defineEventHandlers(): void {
|
|
31
|
-
const wnd: any = window;
|
|
32
|
-
|
|
33
|
-
// Prevent from duplicate event handlers definition.
|
|
34
|
-
if ('TelegramGameProxy_receiveEvent' in wnd) {
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// Iterate over each path, where "receiveEvent" function should be
|
|
39
|
-
// defined. This function is called by external environment in case,
|
|
40
|
-
// it wants to emit some event.
|
|
41
|
-
[
|
|
42
|
-
['TelegramGameProxy_receiveEvent'], // Windows Phone.
|
|
43
|
-
['TelegramGameProxy', 'receiveEvent'], // Desktop.
|
|
44
|
-
['Telegram', 'WebView', 'receiveEvent'], // Android and iOS.
|
|
45
|
-
].forEach((path) => {
|
|
46
|
-
// Path starts from "window" object.
|
|
47
|
-
let pointer = wnd;
|
|
48
|
-
|
|
49
|
-
path.forEach((item, idx, arr) => {
|
|
50
|
-
// We are on the last iteration, where function property name is passed.
|
|
51
|
-
if (idx === arr.length - 1) {
|
|
52
|
-
pointer[item] = emitEvent;
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
if (!(item in pointer)) {
|
|
57
|
-
pointer[item] = {};
|
|
58
|
-
}
|
|
59
|
-
pointer = pointer[item];
|
|
60
|
-
});
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Adds listener to window "message" event assuming, that this event could
|
|
66
|
-
* be sent by Telegram native application. Calls passed callback with event
|
|
67
|
-
* type and data.
|
|
68
|
-
* @param cb - callback to call.
|
|
69
|
-
*/
|
|
70
|
-
export function onTelegramEvent(cb: (eventType: string, eventData: unknown) => void): void {
|
|
71
|
-
// Define event handlers to make sure, message handler will work correctly.
|
|
72
|
-
defineEventHandlers();
|
|
73
|
-
|
|
74
|
-
// We expect Telegram to send us new event through "message" event.
|
|
75
|
-
window.addEventListener('message', (event) => {
|
|
76
|
-
try {
|
|
77
|
-
const { eventType, eventData } = eventDataJson.parse(event.data);
|
|
78
|
-
cb(eventType, eventData);
|
|
79
|
-
} catch {
|
|
80
|
-
// We ignore incorrect messages as they could be generated by any other code.
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
}
|
package/src/events/once.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { singletonEmitter } from './emitter.js';
|
|
2
|
-
import { off } from './off.js';
|
|
3
|
-
|
|
4
|
-
import type { EventName, EventListener } from './events.js';
|
|
5
|
-
|
|
6
|
-
type StopListening = () => void;
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Works the same as "on" method, but after catching the event, will remove event listener.
|
|
10
|
-
* @param event - event name.
|
|
11
|
-
* @param listener - event listener.
|
|
12
|
-
*/
|
|
13
|
-
export function once<E extends EventName>(event: E, listener: EventListener<E>): StopListening {
|
|
14
|
-
singletonEmitter().once(event, listener);
|
|
15
|
-
return () => off(event, listener);
|
|
16
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { json, string } from '@tma.js/parsing';
|
|
2
|
-
|
|
3
|
-
import type { RequestId } from '../../shared.js';
|
|
4
|
-
|
|
5
|
-
export interface ClipboardTextReceivedPayload {
|
|
6
|
-
/**
|
|
7
|
-
* Passed during the `web_app_read_text_from_clipboard` method invocation `req_id` value.
|
|
8
|
-
*/
|
|
9
|
-
req_id: RequestId;
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Data extracted from the clipboard. The returned value will have the type `string` only in
|
|
13
|
-
* the case, application has access to the clipboard.
|
|
14
|
-
*/
|
|
15
|
-
data?: string | null;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export function clipboardTextReceived() {
|
|
19
|
-
return json<ClipboardTextReceivedPayload>({
|
|
20
|
-
req_id: string(),
|
|
21
|
-
data: (value) => (
|
|
22
|
-
value === null
|
|
23
|
-
? value
|
|
24
|
-
: string().optional().parse(value)
|
|
25
|
-
),
|
|
26
|
-
});
|
|
27
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { json, string } from '@tma.js/parsing';
|
|
2
|
-
|
|
3
|
-
import type { RequestId } from '../../shared.js';
|
|
4
|
-
|
|
5
|
-
export interface CustomMethodInvokedPayload<R = unknown> {
|
|
6
|
-
/**
|
|
7
|
-
* Unique identifier of this invocation.
|
|
8
|
-
*/
|
|
9
|
-
req_id: RequestId;
|
|
10
|
-
/**
|
|
11
|
-
* Method invocation successful result.
|
|
12
|
-
*/
|
|
13
|
-
result?: R;
|
|
14
|
-
/**
|
|
15
|
-
* Method invocation error code.
|
|
16
|
-
*/
|
|
17
|
-
error?: string;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function customMethodInvoked() {
|
|
21
|
-
return json<CustomMethodInvokedPayload>({
|
|
22
|
-
req_id: string(),
|
|
23
|
-
result: (value) => value,
|
|
24
|
-
error: string().optional(),
|
|
25
|
-
});
|
|
26
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
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';
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { json, string } from '@tma.js/parsing';
|
|
2
|
-
|
|
3
|
-
export type InvoiceStatus =
|
|
4
|
-
| 'paid'
|
|
5
|
-
| 'failed'
|
|
6
|
-
| 'pending'
|
|
7
|
-
| 'cancelled'
|
|
8
|
-
| string;
|
|
9
|
-
|
|
10
|
-
export interface InvoiceClosedPayload {
|
|
11
|
-
/**
|
|
12
|
-
* Passed during the `web_app_open_invoice` method invocation `slug` value.
|
|
13
|
-
*/
|
|
14
|
-
slug: string;
|
|
15
|
-
/**
|
|
16
|
-
* Invoice status
|
|
17
|
-
*/
|
|
18
|
-
status: InvoiceStatus;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function invoiceClosed() {
|
|
22
|
-
return json<InvoiceClosedPayload>({
|
|
23
|
-
slug: string(),
|
|
24
|
-
status: string(),
|
|
25
|
-
});
|
|
26
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { json, string } from '@tma.js/parsing';
|
|
2
|
-
|
|
3
|
-
export type PhoneRequestedStatus = 'sent' | string;
|
|
4
|
-
|
|
5
|
-
export interface PhoneRequestedPayload {
|
|
6
|
-
/**
|
|
7
|
-
* Request status.
|
|
8
|
-
*/
|
|
9
|
-
status: PhoneRequestedStatus;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export function phoneRequested() {
|
|
13
|
-
return json<PhoneRequestedPayload>({ status: string() });
|
|
14
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { json, string } from '@tma.js/parsing';
|
|
2
|
-
|
|
3
|
-
export interface PopupClosedPayload {
|
|
4
|
-
/**
|
|
5
|
-
* Identifier of the clicked button. In case, the popup was closed without clicking any button,
|
|
6
|
-
* this property will be omitted.
|
|
7
|
-
*/
|
|
8
|
-
button_id?: string;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function popupClosed() {
|
|
12
|
-
return json<PopupClosedPayload>({
|
|
13
|
-
button_id: (value) => (
|
|
14
|
-
value === null || value === undefined
|
|
15
|
-
? undefined
|
|
16
|
-
: string().parse(value)
|
|
17
|
-
),
|
|
18
|
-
});
|
|
19
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { json, string } from '@tma.js/parsing';
|
|
2
|
-
|
|
3
|
-
export interface QrTextReceivedPayload {
|
|
4
|
-
/**
|
|
5
|
-
* Data extracted from the QR.
|
|
6
|
-
*/
|
|
7
|
-
data?: string;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export function qrTextReceived() {
|
|
11
|
-
return json<QrTextReceivedPayload>({
|
|
12
|
-
data: string().optional(),
|
|
13
|
-
});
|
|
14
|
-
}
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { json, rgb, toRecord } from '@tma.js/parsing';
|
|
2
|
-
import type { RGB } from '@tma.js/colors';
|
|
3
|
-
|
|
4
|
-
export interface ThemeChangedPayload {
|
|
5
|
-
/**
|
|
6
|
-
* Map where the key is a theme stylesheet key and value is the corresponding color in
|
|
7
|
-
* `#RRGGBB` format.
|
|
8
|
-
*/
|
|
9
|
-
theme_params: {
|
|
10
|
-
/**
|
|
11
|
-
* @since v6.10
|
|
12
|
-
*/
|
|
13
|
-
accent_text_color?: RGB;
|
|
14
|
-
bg_color?: RGB;
|
|
15
|
-
button_color?: RGB;
|
|
16
|
-
button_text_color?: RGB;
|
|
17
|
-
/**
|
|
18
|
-
* @since v6.10
|
|
19
|
-
*/
|
|
20
|
-
destructive_text_color?: RGB;
|
|
21
|
-
/**
|
|
22
|
-
* @since v6.10
|
|
23
|
-
*/
|
|
24
|
-
header_bg_color?: RGB;
|
|
25
|
-
hint_color?: RGB;
|
|
26
|
-
link_color?: RGB;
|
|
27
|
-
secondary_bg_color?: RGB;
|
|
28
|
-
/**
|
|
29
|
-
* @since v6.10
|
|
30
|
-
*/
|
|
31
|
-
section_bg_color?: RGB;
|
|
32
|
-
/**
|
|
33
|
-
* @since v6.10
|
|
34
|
-
*/
|
|
35
|
-
section_header_text_color?: RGB;
|
|
36
|
-
/**
|
|
37
|
-
* @since v6.10
|
|
38
|
-
*/
|
|
39
|
-
subtitle_text_color?: RGB;
|
|
40
|
-
text_color?: RGB;
|
|
41
|
-
[key: string]: RGB | undefined; // Future unknown palette keys.
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export function themeChanged() {
|
|
46
|
-
return json<ThemeChangedPayload>({
|
|
47
|
-
theme_params: (value) => {
|
|
48
|
-
const parser = rgb().optional();
|
|
49
|
-
|
|
50
|
-
return Object
|
|
51
|
-
.entries(toRecord(value))
|
|
52
|
-
.reduce<Partial<Record<string, RGB>>>((acc, [k, v]) => {
|
|
53
|
-
acc[k] = parser.parse(v);
|
|
54
|
-
return acc;
|
|
55
|
-
}, {});
|
|
56
|
-
},
|
|
57
|
-
});
|
|
58
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { boolean, json, number } from '@tma.js/parsing';
|
|
2
|
-
|
|
3
|
-
export interface ViewportChangedPayload {
|
|
4
|
-
/**
|
|
5
|
-
* The viewport height.
|
|
6
|
-
*/
|
|
7
|
-
height: number;
|
|
8
|
-
/**
|
|
9
|
-
* The viewport width.
|
|
10
|
-
*/
|
|
11
|
-
width: number;
|
|
12
|
-
/**
|
|
13
|
-
* Is the viewport currently expanded.
|
|
14
|
-
*/
|
|
15
|
-
is_expanded: boolean;
|
|
16
|
-
/**
|
|
17
|
-
* Is the viewport current state stable and not going to change in the next moment.
|
|
18
|
-
*/
|
|
19
|
-
is_state_stable: boolean;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export function viewportChanged() {
|
|
23
|
-
return json<ViewportChangedPayload>({
|
|
24
|
-
height: number(),
|
|
25
|
-
width: (value) => (
|
|
26
|
-
value === null || value === undefined
|
|
27
|
-
? window.innerWidth
|
|
28
|
-
: number().parse(value)
|
|
29
|
-
),
|
|
30
|
-
is_state_stable: boolean(),
|
|
31
|
-
is_expanded: boolean(),
|
|
32
|
-
});
|
|
33
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { json, string } from '@tma.js/parsing';
|
|
2
|
-
|
|
3
|
-
export type WriteAccessRequestedStatus = 'allowed' | string;
|
|
4
|
-
|
|
5
|
-
export interface WriteAccessRequestedPayload {
|
|
6
|
-
/**
|
|
7
|
-
* Request status.
|
|
8
|
-
*/
|
|
9
|
-
status: WriteAccessRequestedStatus;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export function writeAccessRequested() {
|
|
13
|
-
return json<WriteAccessRequestedPayload>({ status: string() });
|
|
14
|
-
}
|
package/src/events/subscribe.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { singletonEmitter } from './emitter.js';
|
|
2
|
-
import { unsubscribe } from './unsubscribe.js';
|
|
3
|
-
|
|
4
|
-
import type { GlobalEventListener } from './events.js';
|
|
5
|
-
|
|
6
|
-
type StopListening = () => void;
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Subscribes to all events sent from the native Telegram application.
|
|
10
|
-
* Returns function used to remove added event listener.
|
|
11
|
-
* @param listener - event listener.
|
|
12
|
-
*/
|
|
13
|
-
export function subscribe(listener: GlobalEventListener): StopListening {
|
|
14
|
-
singletonEmitter().subscribe(listener);
|
|
15
|
-
return () => unsubscribe(listener);
|
|
16
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { singletonEmitter } from './emitter.js';
|
|
2
|
-
|
|
3
|
-
import type { GlobalEventListener } from './events.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Removes global event listener.
|
|
7
|
-
* @param listener - event listener.
|
|
8
|
-
*/
|
|
9
|
-
export function unsubscribe(listener: GlobalEventListener): void {
|
|
10
|
-
singletonEmitter().unsubscribe(listener);
|
|
11
|
-
}
|
package/src/globals.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { Logger } from '@tma.js/logger';
|
|
2
|
-
|
|
3
|
-
let currentTargetOrigin = 'https://web.telegram.org';
|
|
4
|
-
|
|
5
|
-
export const logger = new Logger('[Bridge]', false);
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Sets new debug mode. Enabling debug mode leads to printing
|
|
9
|
-
* additional messages in console, related to the processes
|
|
10
|
-
* inside the package.
|
|
11
|
-
* @param value - should debug mode be enabled.
|
|
12
|
-
*/
|
|
13
|
-
export function setDebug(value: boolean): void {
|
|
14
|
-
if (value) {
|
|
15
|
-
logger.enable();
|
|
16
|
-
return;
|
|
17
|
-
}
|
|
18
|
-
logger.disable();
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Sets new global targetOrigin, used by `postEvent` method.
|
|
23
|
-
* Default value is "https://web.telegram.org". You don't need to
|
|
24
|
-
* use this method until you know what you are doing.
|
|
25
|
-
*
|
|
26
|
-
* This method could be used for test purposes.
|
|
27
|
-
* @param value - new target origin.
|
|
28
|
-
*/
|
|
29
|
-
export function setTargetOrigin(value: string): void {
|
|
30
|
-
currentTargetOrigin = value;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Returns current global target origin.
|
|
35
|
-
*/
|
|
36
|
-
export function targetOrigin(): string {
|
|
37
|
-
return currentTargetOrigin;
|
|
38
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
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
|
-
}
|