@tma.js/bridge 1.4.0 → 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/events/events.d.ts +1 -1
- 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/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 +179 -159
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/events/emitter.ts +19 -19
- package/src/events/events.ts +1 -1
- 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/dist/dts/events/parsing.d.ts +0 -38
- package/dist/dts/events/payloads.d.ts +0 -93
- package/src/events/parsing.ts +0 -110
- package/src/events/payloads.ts +0 -125
package/dist/index.mjs
CHANGED
|
@@ -1,82 +1,93 @@
|
|
|
1
|
-
import { compareVersions as J, isRecord as
|
|
2
|
-
import { Logger as
|
|
3
|
-
import { EventEmitter as
|
|
4
|
-
import {
|
|
5
|
-
class
|
|
1
|
+
import { compareVersions as J, isRecord as p, withTimeout as L } from "@tma.js/utils";
|
|
2
|
+
import { Logger as N } from "@tma.js/logger";
|
|
3
|
+
import { EventEmitter as T } from "@tma.js/event-emitter";
|
|
4
|
+
import { json as s, string as o, rgb as j, toRecord as I, number as q, boolean as v } from "@tma.js/parsing";
|
|
5
|
+
class b extends Error {
|
|
6
6
|
constructor(t, r) {
|
|
7
|
-
super(`Method "${t}" is unsupported in the Mini Apps version ${r}.`), Object.setPrototypeOf(this,
|
|
7
|
+
super(`Method "${t}" is unsupported in the Mini Apps version ${r}.`), Object.setPrototypeOf(this, b.prototype);
|
|
8
8
|
}
|
|
9
9
|
}
|
|
10
|
-
class
|
|
10
|
+
class h extends Error {
|
|
11
11
|
constructor(t, r, n) {
|
|
12
|
-
super(`Parameter "${r}" in method "${t}" is unsupported in the Mini Apps version ${n}.`), Object.setPrototypeOf(this,
|
|
12
|
+
super(`Parameter "${r}" in method "${t}" is unsupported in the Mini Apps version ${n}.`), Object.setPrototypeOf(this, h.prototype);
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
function S() {
|
|
16
|
+
return s({
|
|
17
|
+
req_id: o(),
|
|
18
|
+
data: (e) => e === null ? e : o().optional().parse(e)
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
function G() {
|
|
22
|
+
return s({
|
|
23
|
+
req_id: o(),
|
|
24
|
+
result: (e) => e,
|
|
25
|
+
error: o().optional()
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
function H() {
|
|
29
|
+
return s({
|
|
30
|
+
slug: o(),
|
|
31
|
+
status: o()
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
function z() {
|
|
35
|
+
return s({ status: o() });
|
|
36
|
+
}
|
|
37
|
+
function B() {
|
|
38
|
+
return s({
|
|
39
|
+
button_id: (e) => e == null ? void 0 : o().parse(e)
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
function U() {
|
|
43
|
+
return s({
|
|
44
|
+
data: o().optional()
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
function F() {
|
|
48
|
+
return s({
|
|
49
|
+
theme_params: (e) => {
|
|
50
|
+
const t = j().optional();
|
|
51
|
+
return Object.entries(I(e)).reduce((r, [n, i]) => (r[n] = t.parse(i), r), {});
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
function K() {
|
|
56
|
+
return s({
|
|
57
|
+
height: q(),
|
|
58
|
+
width: (e) => e == null ? window.innerWidth : q().parse(e),
|
|
59
|
+
is_state_stable: v(),
|
|
60
|
+
is_expanded: v()
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
function Q() {
|
|
64
|
+
return s({ status: o() });
|
|
65
|
+
}
|
|
66
|
+
let P = "https://web.telegram.org";
|
|
67
|
+
const _ = new N("[Bridge]", !1);
|
|
68
|
+
function _e(e) {
|
|
18
69
|
if (e) {
|
|
19
|
-
|
|
70
|
+
_.enable();
|
|
20
71
|
return;
|
|
21
72
|
}
|
|
22
|
-
|
|
73
|
+
_.disable();
|
|
23
74
|
}
|
|
24
|
-
function
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
function
|
|
28
|
-
return
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
const i = I().optional(), U = k(), z = S(
|
|
34
|
-
(e) => e == null ? window.innerWidth : U.parse(e)
|
|
35
|
-
), B = c({
|
|
36
|
-
theme_params: c({
|
|
37
|
-
accent_text_color: i,
|
|
38
|
-
bg_color: i,
|
|
39
|
-
button_color: i,
|
|
40
|
-
button_text_color: i,
|
|
41
|
-
destructive_text_color: i,
|
|
42
|
-
header_bg_color: i,
|
|
43
|
-
hint_color: i,
|
|
44
|
-
link_color: i,
|
|
45
|
-
secondary_bg_color: i,
|
|
46
|
-
section_bg_color: i,
|
|
47
|
-
section_header_text_color: i,
|
|
48
|
-
subtitle_text_color: i,
|
|
49
|
-
text_color: i
|
|
50
|
-
})
|
|
51
|
-
}), F = c({
|
|
52
|
-
height: k(),
|
|
53
|
-
width: z(),
|
|
54
|
-
is_state_stable: P(),
|
|
55
|
-
is_expanded: P()
|
|
56
|
-
}), K = c({
|
|
57
|
-
button_id: s({ isEmpty: H }).optional()
|
|
58
|
-
}), Q = c({
|
|
59
|
-
data: s().optional()
|
|
60
|
-
}), X = c({
|
|
61
|
-
slug: s(),
|
|
62
|
-
status: s()
|
|
63
|
-
}), Y = c({
|
|
64
|
-
req_id: s(),
|
|
65
|
-
data: (e) => e === null ? e : s().optional().parse(e)
|
|
66
|
-
}), Z = c({ status: s() }), V = c({ status: s() }), D = c({
|
|
67
|
-
req_id: s(),
|
|
68
|
-
result: (e) => e,
|
|
69
|
-
error: s().optional()
|
|
70
|
-
}), O = c({
|
|
71
|
-
eventType: s(),
|
|
75
|
+
function ae(e) {
|
|
76
|
+
P = e;
|
|
77
|
+
}
|
|
78
|
+
function X() {
|
|
79
|
+
return P;
|
|
80
|
+
}
|
|
81
|
+
const Y = s({
|
|
82
|
+
eventType: o(),
|
|
72
83
|
eventData: (e) => e
|
|
73
84
|
});
|
|
74
|
-
function
|
|
85
|
+
function Z(e, t) {
|
|
75
86
|
window.dispatchEvent(new MessageEvent("message", {
|
|
76
87
|
data: JSON.stringify({ eventType: e, eventData: t })
|
|
77
88
|
}));
|
|
78
89
|
}
|
|
79
|
-
function
|
|
90
|
+
function D() {
|
|
80
91
|
const e = window;
|
|
81
92
|
"TelegramGameProxy_receiveEvent" in e || [
|
|
82
93
|
["TelegramGameProxy_receiveEvent"],
|
|
@@ -87,28 +98,28 @@ function te() {
|
|
|
87
98
|
// Android and iOS.
|
|
88
99
|
].forEach((t) => {
|
|
89
100
|
let r = e;
|
|
90
|
-
t.forEach((n,
|
|
91
|
-
if (
|
|
92
|
-
r[n] =
|
|
101
|
+
t.forEach((n, i, c) => {
|
|
102
|
+
if (i === c.length - 1) {
|
|
103
|
+
r[n] = Z;
|
|
93
104
|
return;
|
|
94
105
|
}
|
|
95
106
|
n in r || (r[n] = {}), r = r[n];
|
|
96
107
|
});
|
|
97
108
|
});
|
|
98
109
|
}
|
|
99
|
-
function
|
|
100
|
-
|
|
110
|
+
function V(e) {
|
|
111
|
+
D(), window.addEventListener("message", (t) => {
|
|
101
112
|
try {
|
|
102
|
-
const { eventType: r, eventData: n } =
|
|
113
|
+
const { eventType: r, eventData: n } = Y.parse(t.data);
|
|
103
114
|
e(r, n);
|
|
104
115
|
} catch {
|
|
105
116
|
}
|
|
106
117
|
});
|
|
107
118
|
}
|
|
108
|
-
const
|
|
109
|
-
function
|
|
110
|
-
const e = new
|
|
111
|
-
|
|
119
|
+
const g = "__telegram-cached-emitter__";
|
|
120
|
+
function O() {
|
|
121
|
+
const e = new T(), t = (r, ...n) => {
|
|
122
|
+
_.log("Emitting processed event:", r, ...n), e.emit(r, ...n);
|
|
112
123
|
};
|
|
113
124
|
return window.addEventListener("resize", () => {
|
|
114
125
|
t("viewport_changed", {
|
|
@@ -117,33 +128,33 @@ function ne() {
|
|
|
117
128
|
is_state_stable: !0,
|
|
118
129
|
is_expanded: !0
|
|
119
130
|
});
|
|
120
|
-
}),
|
|
121
|
-
|
|
131
|
+
}), V((r, n) => {
|
|
132
|
+
_.log("Received raw event:", r, n);
|
|
122
133
|
try {
|
|
123
134
|
switch (r) {
|
|
124
135
|
case "viewport_changed":
|
|
125
|
-
return t(r,
|
|
136
|
+
return t(r, K().parse(n));
|
|
126
137
|
case "theme_changed":
|
|
127
|
-
return t(r,
|
|
138
|
+
return t(r, F().parse(n));
|
|
128
139
|
case "popup_closed":
|
|
129
140
|
return (
|
|
130
141
|
// Sent on desktop.
|
|
131
|
-
n == null ? t(r, {}) : t(r,
|
|
142
|
+
n == null ? t(r, {}) : t(r, B().parse(n))
|
|
132
143
|
);
|
|
133
144
|
case "set_custom_style":
|
|
134
|
-
return t(r,
|
|
145
|
+
return t(r, o().parse(n));
|
|
135
146
|
case "qr_text_received":
|
|
136
|
-
return t(r,
|
|
147
|
+
return t(r, U().parse(n));
|
|
137
148
|
case "clipboard_text_received":
|
|
138
|
-
return t(r,
|
|
149
|
+
return t(r, S().parse(n));
|
|
139
150
|
case "invoice_closed":
|
|
140
|
-
return t(r,
|
|
151
|
+
return t(r, H().parse(n));
|
|
141
152
|
case "phone_requested":
|
|
142
|
-
return t("phone_requested",
|
|
153
|
+
return t("phone_requested", z().parse(n));
|
|
143
154
|
case "custom_method_invoked":
|
|
144
|
-
return t("custom_method_invoked",
|
|
155
|
+
return t("custom_method_invoked", G().parse(n));
|
|
145
156
|
case "write_access_requested":
|
|
146
|
-
return t("write_access_requested",
|
|
157
|
+
return t("write_access_requested", Q().parse(n));
|
|
147
158
|
case "main_button_pressed":
|
|
148
159
|
case "back_button_pressed":
|
|
149
160
|
case "settings_button_pressed":
|
|
@@ -153,39 +164,39 @@ function ne() {
|
|
|
153
164
|
default:
|
|
154
165
|
return t(r, n);
|
|
155
166
|
}
|
|
156
|
-
} catch (
|
|
157
|
-
|
|
167
|
+
} catch (i) {
|
|
168
|
+
_.error("Error processing event:", i);
|
|
158
169
|
}
|
|
159
170
|
}), e;
|
|
160
171
|
}
|
|
161
|
-
function
|
|
172
|
+
function d() {
|
|
162
173
|
const e = window;
|
|
163
|
-
return e[
|
|
174
|
+
return e[g] === void 0 && (e[g] = O()), e[g];
|
|
164
175
|
}
|
|
165
|
-
function
|
|
166
|
-
|
|
176
|
+
function A(e, t) {
|
|
177
|
+
d().off(e, t);
|
|
167
178
|
}
|
|
168
|
-
function
|
|
169
|
-
return
|
|
179
|
+
function ee(e, t) {
|
|
180
|
+
return d().on(e, t), () => A(e, t);
|
|
170
181
|
}
|
|
171
|
-
function
|
|
172
|
-
return
|
|
182
|
+
function pe(e, t) {
|
|
183
|
+
return d().once(e, t), () => A(e, t);
|
|
173
184
|
}
|
|
174
|
-
function
|
|
175
|
-
|
|
185
|
+
function te(e) {
|
|
186
|
+
d().unsubscribe(e);
|
|
176
187
|
}
|
|
177
|
-
function
|
|
178
|
-
return
|
|
188
|
+
function de(e) {
|
|
189
|
+
return d().subscribe(e), () => te(e);
|
|
179
190
|
}
|
|
180
|
-
function
|
|
191
|
+
function u(e, t) {
|
|
181
192
|
return J(e, t) <= 0;
|
|
182
193
|
}
|
|
183
|
-
function
|
|
194
|
+
function k(e, t, r) {
|
|
184
195
|
if (typeof r == "string") {
|
|
185
196
|
if (e === "web_app_open_link" && t === "try_instant_view")
|
|
186
|
-
return
|
|
197
|
+
return u("6.4", r);
|
|
187
198
|
if (e === "web_app_set_header_color" && t === "color")
|
|
188
|
-
return
|
|
199
|
+
return u("6.9", r);
|
|
189
200
|
}
|
|
190
201
|
switch (e) {
|
|
191
202
|
case "web_app_open_tg_link":
|
|
@@ -194,104 +205,113 @@ function v(e, t, r) {
|
|
|
194
205
|
case "web_app_set_background_color":
|
|
195
206
|
case "web_app_set_header_color":
|
|
196
207
|
case "web_app_trigger_haptic_feedback":
|
|
197
|
-
return
|
|
208
|
+
return u("6.1", t);
|
|
198
209
|
case "web_app_open_popup":
|
|
199
|
-
return
|
|
210
|
+
return u("6.2", t);
|
|
200
211
|
case "web_app_close_scan_qr_popup":
|
|
201
212
|
case "web_app_open_scan_qr_popup":
|
|
202
213
|
case "web_app_read_text_from_clipboard":
|
|
203
|
-
return
|
|
214
|
+
return u("6.4", t);
|
|
204
215
|
case "web_app_switch_inline_query":
|
|
205
|
-
return
|
|
216
|
+
return u("6.7", t);
|
|
206
217
|
case "web_app_invoke_custom_method":
|
|
207
218
|
case "web_app_request_write_access":
|
|
208
219
|
case "web_app_request_phone":
|
|
209
|
-
return
|
|
220
|
+
return u("6.9", t);
|
|
210
221
|
case "web_app_setup_settings_button":
|
|
211
|
-
return
|
|
222
|
+
return u("6.10", t);
|
|
212
223
|
default:
|
|
213
224
|
return !0;
|
|
214
225
|
}
|
|
215
226
|
}
|
|
216
|
-
function
|
|
227
|
+
function fe(e) {
|
|
217
228
|
return (t, r) => {
|
|
218
|
-
if (!
|
|
219
|
-
throw new
|
|
220
|
-
if (
|
|
229
|
+
if (!k(t, e))
|
|
230
|
+
throw new b(t, e);
|
|
231
|
+
if (p(r)) {
|
|
221
232
|
let n;
|
|
222
|
-
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 && !
|
|
223
|
-
throw new
|
|
233
|
+
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 && !k(t, n, e))
|
|
234
|
+
throw new h(t, n, e);
|
|
224
235
|
}
|
|
225
|
-
return
|
|
236
|
+
return W(t, r);
|
|
226
237
|
};
|
|
227
238
|
}
|
|
228
|
-
function
|
|
229
|
-
return "external" in e &&
|
|
239
|
+
function re(e) {
|
|
240
|
+
return "external" in e && p(e.external) && "notify" in e.external && typeof e.external.notify == "function";
|
|
230
241
|
}
|
|
231
|
-
function
|
|
232
|
-
return "TelegramWebviewProxy" in e &&
|
|
242
|
+
function ne(e) {
|
|
243
|
+
return "TelegramWebviewProxy" in e && p(e.TelegramWebviewProxy) && "postEvent" in e.TelegramWebviewProxy && typeof e.TelegramWebviewProxy.postEvent == "function";
|
|
233
244
|
}
|
|
234
|
-
function
|
|
245
|
+
function ie() {
|
|
235
246
|
try {
|
|
236
247
|
return window.self !== window.top;
|
|
237
248
|
} catch {
|
|
238
249
|
return !0;
|
|
239
250
|
}
|
|
240
251
|
}
|
|
241
|
-
function
|
|
242
|
-
let n = {},
|
|
243
|
-
t === void 0 && r === void 0 ? n = {} : t !== void 0 && r !== void 0 ? (n = r,
|
|
244
|
-
const { targetOrigin:
|
|
245
|
-
if (
|
|
252
|
+
function W(e, t, r) {
|
|
253
|
+
let n = {}, i;
|
|
254
|
+
t === void 0 && r === void 0 ? n = {} : t !== void 0 && r !== void 0 ? (n = r, i = t) : t !== void 0 && ("targetOrigin" in t ? n = t : i = t);
|
|
255
|
+
const { targetOrigin: c = X() } = n;
|
|
256
|
+
if (_.log(`Calling method "${e}"`, i), ie()) {
|
|
246
257
|
window.parent.postMessage(JSON.stringify({
|
|
247
258
|
eventType: e,
|
|
248
|
-
eventData:
|
|
249
|
-
}),
|
|
259
|
+
eventData: i
|
|
260
|
+
}), c);
|
|
250
261
|
return;
|
|
251
262
|
}
|
|
252
|
-
if (
|
|
253
|
-
window.external.notify(JSON.stringify({ eventType: e, eventData:
|
|
263
|
+
if (re(window)) {
|
|
264
|
+
window.external.notify(JSON.stringify({ eventType: e, eventData: i }));
|
|
254
265
|
return;
|
|
255
266
|
}
|
|
256
|
-
if (
|
|
257
|
-
window.TelegramWebviewProxy.postEvent(e, JSON.stringify(
|
|
267
|
+
if (ne(window)) {
|
|
268
|
+
window.TelegramWebviewProxy.postEvent(e, JSON.stringify(i));
|
|
258
269
|
return;
|
|
259
270
|
}
|
|
260
271
|
throw new Error(
|
|
261
272
|
"Unable to determine current environment and possible way to send event."
|
|
262
273
|
);
|
|
263
274
|
}
|
|
264
|
-
function
|
|
265
|
-
let
|
|
266
|
-
typeof t == "string" || Array.isArray(t) ? (w = Array.isArray(t) ? t : [t],
|
|
267
|
-
const { postEvent:
|
|
268
|
-
const
|
|
269
|
-
typeof
|
|
270
|
-
})),
|
|
275
|
+
function we(e, t, r, n) {
|
|
276
|
+
let i, c, w, l;
|
|
277
|
+
typeof t == "string" || Array.isArray(t) ? (w = Array.isArray(t) ? t : [t], i = r) : (c = t, w = Array.isArray(r) ? r : [r], i = n), p(c) && typeof c.req_id == "string" && (l = c.req_id);
|
|
278
|
+
const { postEvent: R = W, timeout: E } = i || {}, m = i && "capture" in i ? i.capture : null, y = new Promise(($, C) => {
|
|
279
|
+
const M = w.map((a) => ee(a, (f) => {
|
|
280
|
+
typeof l == "string" && (!p(f) || f.req_id !== l) || typeof m == "function" && !m(f) || (x(), $(f));
|
|
281
|
+
})), x = () => M.forEach((a) => a());
|
|
271
282
|
try {
|
|
272
|
-
|
|
273
|
-
} catch (
|
|
274
|
-
|
|
283
|
+
R(e, c);
|
|
284
|
+
} catch (a) {
|
|
285
|
+
x(), C(a);
|
|
275
286
|
}
|
|
276
287
|
});
|
|
277
|
-
return typeof
|
|
288
|
+
return typeof E == "number" ? L(y, E) : y;
|
|
278
289
|
}
|
|
279
290
|
export {
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
291
|
+
b as MethodUnsupportedError,
|
|
292
|
+
h as ParameterUnsupportedError,
|
|
293
|
+
S as clipboardTextReceived,
|
|
294
|
+
fe as createPostEvent,
|
|
295
|
+
G as customMethodInvoked,
|
|
296
|
+
re as hasExternalNotify,
|
|
297
|
+
ne as hasWebviewProxy,
|
|
298
|
+
H as invoiceClosed,
|
|
299
|
+
ie as isIframe,
|
|
300
|
+
A as off,
|
|
301
|
+
ee as on,
|
|
302
|
+
pe as once,
|
|
303
|
+
z as phoneRequested,
|
|
304
|
+
B as popupClosed,
|
|
305
|
+
W as postEvent,
|
|
306
|
+
U as qrTextReceived,
|
|
307
|
+
we as request,
|
|
308
|
+
_e as setDebug,
|
|
309
|
+
ae as setTargetOrigin,
|
|
310
|
+
de as subscribe,
|
|
311
|
+
k as supports,
|
|
312
|
+
F as themeChanged,
|
|
313
|
+
te as unsubscribe,
|
|
314
|
+
K as viewportChanged,
|
|
315
|
+
Q as writeAccessRequested
|
|
296
316
|
};
|
|
297
317
|
//# sourceMappingURL=index.mjs.map
|