Skip to content

Commit c02f0bc

Browse files
authored
Merge pull request #653 from Telegram-Mini-Apps/feature/v3-docs
New packages' major versions' docs.
2 parents e67898f + d035dc5 commit c02f0bc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+5421
-15
lines changed

.changeset/dull-bobcats-sell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@telegram-apps/bridge": patch
3+
---
4+
5+
Actualize README.

.changeset/khaki-oranges-speak.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@telegram-apps/sdk": patch
3+
---
4+
5+
Actualize ConfigureOptions.postEvent comments.

apps/docs/.vitepress/packages.ts

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ export const packagesLinksGenerator = (prefix: string = '') => {
4343
section('TypeScript', {
4444
'@telegram-apps/signals': 'telegram-apps-signals',
4545
'@telegram-apps/bridge': ['telegram-apps-bridge', {
46+
'@2.x': ['2-x', {
47+
'Methods': 'methods',
48+
'Events': 'events',
49+
'Launch Parameters': 'launch-parameters',
50+
'Environment': 'environment',
51+
'Globals': 'globals',
52+
'Advanced': 'advanced',
53+
}],
4654
'Methods': 'methods',
4755
'Events': 'events',
4856
'Environment': 'environment',
@@ -116,28 +124,69 @@ export const packagesLinksGenerator = (prefix: string = '') => {
116124
scope('uncategorized'),
117125
])],
118126
}],
127+
'@3.x': ['3-x', {
128+
'Initializing': 'initializing',
129+
'Scopes': 'scopes',
130+
'Usage Tips': 'usage-tips',
131+
'Components': [
132+
{ url: 'components', page: false },
133+
fromEntries([
134+
scope('back-button'),
135+
scope('biometry'),
136+
scope('closing-behavior'),
137+
scope('cloud-storage'),
138+
scope('haptic-feedback'),
139+
scope('init-data'),
140+
scope('invoice'),
141+
scope('main-button'),
142+
scope('mini-app'),
143+
scope('popup'),
144+
scope('qr-scanner', 'QR Scanner'),
145+
scope('secondary-button'),
146+
scope('settings-button'),
147+
scope('swipe-behavior'),
148+
scope('theme-params'),
149+
scope('viewport'),
150+
]),
151+
],
152+
'Utilities': [{ url: 'utils', page: false }, fromEntries([
153+
scope('emoji-status'),
154+
scope('home-screen'),
155+
scope('links'),
156+
scope('privacy'),
157+
scope('uncategorized'),
158+
])],
159+
}],
119160
}],
120161
'@telegram-apps/sdk-react': [{
121162
url: 'telegram-apps-sdk-react',
122163
page: false,
123164
}, {
124165
'@1.x': '1-x',
125166
'@2.x': '2-x',
167+
'@3.x': '3-x',
126168
}],
127169
'@telegram-apps/sdk-solid': [{
128170
url: 'telegram-apps-sdk-solid',
129171
page: false,
130172
}, {
131173
'@1.x': '1-x',
132174
'@2.x': '2-x',
175+
'@3.x': '3-x',
176+
}],
177+
'@telegram-apps/sdk-vue': [{ url: 'telegram-apps-sdk-vue' }, {
178+
'@2.x': '2-x',
179+
}],
180+
'@telegram-apps/sdk-svelte': [{ url: 'telegram-apps-sdk-svelte' }, {
181+
'@2.x': '2-x',
133182
}],
134-
'@telegram-apps/sdk-vue': '/telegram-apps-sdk-vue',
135-
'@telegram-apps/sdk-svelte': '/telegram-apps-sdk-svelte',
136183
'@telegram-apps/solid-router-integration': '/telegram-apps-solid-router-integration',
137184
'@telegram-apps/react-router-integration': '/telegram-apps-react-router-integration',
138185
}),
139186
section('Node', {
140-
'@telegram-apps/init-data-node': 'telegram-apps-init-data-node',
187+
'@telegram-apps/init-data-node': [{ url: 'telegram-apps-init-data-node' }, {
188+
'@2.x': '2-x',
189+
}],
141190
}),
142191
section('GoLang', {
143192
'init-data-golang': 'init-data-golang',

apps/docs/packages/telegram-apps-bridge.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# @telegram-apps/bridge
22

3+
> [!TIP] Newer version available
4+
> `@telegram-apps/[email protected]` is [out](./telegram-apps-bridge/2-x)!
5+
36
<p style="display: flex; gap: 8px; min-height: 20px">
47
<a href="https://npmjs.com/package/@telegram-apps/bridge">
58
<img src="https://img.shields.io/npm/v/@telegram-apps/bridge?logo=npm"/>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# @telegram-apps/bridge@2
2+
3+
<p style="display: flex; gap: 8px; min-height: 20px">
4+
<a href="https://npmjs.com/package/@telegram-apps/bridge">
5+
<img src="https://img.shields.io/npm/v/@telegram-apps/bridge?logo=npm"/>
6+
</a>
7+
<img src="https://img.shields.io/bundlephobia/minzip/@telegram-apps/bridge"/>
8+
<a href="https://github.com/Telegram-Mini-Apps/telegram-apps/tree/master/packages/bridge">
9+
<img src="https://img.shields.io/badge/source-black?logo=github"/>
10+
</a>
11+
</p>
12+
13+
The lowest level communication layer with Telegram Mini Apps.
14+
15+
This package provides fundamental utilities and types for developing applications on the Telegram
16+
Mini Apps platform.
17+
18+
While a developer can use this package alone, it's recommended to use a higher-level package
19+
like [@telegram-apps/sdk](telegram-apps-sdk/2-x).
20+
21+
## Installation
22+
23+
::: code-group
24+
25+
```bash [pnpm]
26+
pnpm i @telegram-apps/bridge
27+
```
28+
29+
```bash [npm]
30+
npm i @telegram-apps/bridge
31+
```
32+
33+
```bash [yarn]
34+
yarn add @telegram-apps/bridge
35+
```
36+
37+
:::
38+
39+
## Usage
40+
41+
Here’s a basic example of how to use this package. For more details, refer to the next articles in
42+
the documentation.
43+
44+
```ts
45+
import { on, postEvent } from '@telegram-apps/bridge';
46+
47+
// Show the back button, wait for it to be clicked once,
48+
// then hide it.
49+
postEvent('web_app_setup_back_button', { is_visible: true });
50+
51+
const off = on('back_button_pressed', () => {
52+
postEvent('web_app_setup_back_button', { is_visible: false });
53+
off();
54+
});
55+
```
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# Advanced
2+
3+
This article covers advanced usage of the bridge.
4+
5+
## Calling Method, Receiving Event
6+
7+
The `request` function should be used when a developer needs to call a Telegram Mini Apps method and
8+
receive a specific event.
9+
10+
For example, to call
11+
the [web_app_request_viewport](../../../platform/methods.md#web-app-request-viewport) method and catch
12+
the [viewport_changed](../../../platform/events.md#viewport-changed) event for actual viewport data:
13+
14+
```typescript
15+
import { request } from '@telegram-apps/bridge';
16+
17+
await request('web_app_request_viewport', 'viewport_changed');
18+
// {
19+
// is_state_stable: true,
20+
// is_expanded: false,
21+
// height: 320
22+
// };
23+
```
24+
25+
If the Telegram Mini Apps method accepts parameters, they should be passed in the `params` property
26+
of the third argument:
27+
28+
```typescript
29+
const { button_id } = await request('web_app_open_popup', 'popup_closed', {
30+
params: {
31+
title: 'Caution',
32+
message: 'Should we delete your account?',
33+
buttons: [
34+
{ id: 'yes', type: 'ok' },
35+
{ id: 'no', type: 'cancel' },
36+
],
37+
},
38+
});
39+
```
40+
41+
It is also allowed to track several events at the same time:
42+
43+
```typescript
44+
await request(
45+
'web_app_open_scan_qr_popup',
46+
['qr_text_received', 'scan_qr_popup_closed'],
47+
);
48+
49+
// The result will either be the qr_text_received
50+
// or scan_qr_popup_closed event payload.
51+
```
52+
53+
This function allows passing additional options such as `postEvent`, `abortSignal`, `timeout`,
54+
and `capture`.
55+
56+
### `postEvent`
57+
58+
The `postEvent` option allows a developer to override the method used to call the Telegram Mini Apps
59+
method.
60+
61+
```typescript
62+
request('web_app_request_viewport', 'viewport_changed', {
63+
postEvent() {
64+
console.log('Hey, I am not going to do anything. Live with that');
65+
},
66+
});
67+
```
68+
69+
### `abortSignal`
70+
71+
To abort the returned promise externally, the `abortSignal` option is used.
72+
73+
```ts
74+
const controller = new AbortController();
75+
76+
request('web_app_request_viewport', 'viewport_changed', {
77+
abortSignal: controller.signal,
78+
});
79+
80+
setTimeout(() => {
81+
controller.abort(new Error('Not going to wait anymore'));
82+
}, 500);
83+
```
84+
85+
### `timeout`
86+
87+
The `timeout` option assigns a timeout to the request.
88+
89+
```typescript
90+
import { request } from '@telegram-apps/bridge';
91+
92+
try {
93+
await request(
94+
'web_app_invoke_custom_method',
95+
'custom_method_invoked',
96+
{
97+
timeout: 5000,
98+
params: {
99+
req_id: '1',
100+
method: 'deleteStorageValues',
101+
params: { keys: ['a'] },
102+
},
103+
},
104+
);
105+
} catch (e) {
106+
console.error(e); // e.name will be 'TimeoutError'
107+
}
108+
```
109+
110+
#### `capture`
111+
112+
The `capture` property is a function that allows developers to determine if an occurred Mini Apps
113+
event should be captured and returned from the `request` function:
114+
115+
```typescript
116+
const slug = 'jjKSJnm1k23lodd';
117+
118+
request('web_app_open_invoice', 'invoice_closed', {
119+
params: { slug },
120+
capture(data) {
121+
return slug === data.slug;
122+
},
123+
});
124+
```
125+
126+
By default, the `request` function captures the first event with the required name. In this case,
127+
the function will capture the event only if it has the expected slug, specific for the
128+
[invoice_closed](../../../platform/events.md#invoice-closed) event.
129+
130+
When passing an array of events, the `capture` function will receive an object with
131+
the `event: EventName` and `payload?: EventPayload` properties.
132+
133+
## Invoking Custom Methods
134+
135+
Custom methods are those methods which can be used with
136+
the [web_app_invoke_custom_method](../../../platform/methods.md#web-app-invoke-custom-method) Mini
137+
Apps
138+
method.
139+
140+
The `invokeCustomMethod` function simplifies the usage of such methods by reusing the `request`
141+
function.
142+
143+
Here’s an example without using this function:
144+
145+
```typescript
146+
const reqId = 'ABC';
147+
148+
request('web_app_invoke_custom_method', 'custom_method_invoked', {
149+
params: {
150+
req_id: reqId,
151+
method: 'deleteStorageValues',
152+
params: { keys: ['a'] },
153+
},
154+
capture(data) {
155+
return data.req_id === reqId;
156+
}
157+
})
158+
.then(({ data, error }) => {
159+
if (error) {
160+
throw new Error(error);
161+
}
162+
return data;
163+
})
164+
.then(data => {
165+
console.log('We got some data', data);
166+
});
167+
```
168+
169+
Now, rewritten using the `invokeCustomMethod` function:
170+
171+
```typescript
172+
import { invokeCustomMethod } from '@telegram-apps/bridge';
173+
174+
invokeCustomMethod('deleteStorageValues', { keys: ['a'] }, 'ABC')
175+
.then(data => {
176+
console.log('We got some data', data);
177+
});
178+
```
179+
180+
Internally, it just encapsulates a specific logic related to the methods, so a developer shouldn't
181+
do it.
182+
183+
Unlike the `request` function, the `invokeCustomMethod` function parses the result and checks if it
184+
contains the `error` property. If it does, the function throws the corresponding error; otherwise,
185+
the `result` property is returned.

0 commit comments

Comments
 (0)