@tma.js/init-data 0.2.22 → 4.0.0-rc.0

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/src/serialize.ts DELETED
@@ -1,77 +0,0 @@
1
- import type { InitData, User } from './types.js';
2
-
3
- function setString(params: URLSearchParams, key: string, value: string | undefined) {
4
- if (value === undefined) {
5
- return;
6
- }
7
- params.set(key, value);
8
- }
9
-
10
- function setUser(params: URLSearchParams, key: string, value: User | undefined) {
11
- if (value === undefined) {
12
- return;
13
- }
14
- const {
15
- photoUrl,
16
- firstName,
17
- lastName,
18
- isPremium,
19
- languageCode,
20
- isBot,
21
- ...rest
22
- } = value;
23
-
24
- params.set(key, JSON.stringify({
25
- ...rest,
26
- photo_url: photoUrl,
27
- first_name: firstName,
28
- last_name: lastName,
29
- is_premium: isPremium,
30
- language_code: languageCode,
31
- is_bot: isBot,
32
- }));
33
- }
34
-
35
- /**
36
- * Converts init data to its initial representation.
37
- * @param value - init data.
38
- */
39
- export function serialize(value: InitData): string {
40
- const {
41
- authDate,
42
- canSendAfter,
43
- chat,
44
- chatInstance,
45
- chatType,
46
- hash,
47
- queryId,
48
- receiver,
49
- startParam,
50
- user,
51
- } = value;
52
-
53
- const params = new URLSearchParams();
54
-
55
- params.set('auth_date', (authDate.getTime() / 1000).toString());
56
- setString(params, 'chat_instance', chatInstance);
57
- setString(params, 'chat_type', chatType);
58
- setString(params, 'hash', hash);
59
- setString(params, 'query_id', queryId);
60
- setUser(params, 'receiver', receiver);
61
- setString(params, 'start_param', startParam);
62
- setUser(params, 'user', user);
63
-
64
- if (chat) {
65
- const { photoUrl, ...rest } = chat;
66
- params.set('chat', JSON.stringify({
67
- ...rest,
68
- photo_url: photoUrl,
69
- }));
70
- }
71
-
72
- if (canSendAfter) {
73
- params.set('can_send_after', canSendAfter.toString());
74
- }
75
-
76
- return params.toString();
77
- }
package/src/types.ts DELETED
@@ -1,164 +0,0 @@
1
- /**
2
- * Known type of chat.
3
- */
4
- export type ChatType =
5
- | 'sender'
6
- | 'private'
7
- | 'group'
8
- | 'supergroup'
9
- | 'channel'
10
- | string;
11
-
12
- /**
13
- * Describes Telegram Mini Apps [User](https://docs.telegram-mini-apps.com/launch-parameters/init-data#user) type.
14
- */
15
- export interface User {
16
- /**
17
- * True, if this user added the bot to the attachment menu.
18
- */
19
- addedToAttachmentMenu?: boolean;
20
-
21
- /**
22
- * True, if this user allowed the bot to message them.
23
- */
24
- allowsWriteToPm?: boolean;
25
-
26
- /**
27
- * First name of the user or bot.
28
- */
29
- firstName: string;
30
-
31
- /**
32
- * A unique identifier for the user or bot.
33
- */
34
- id: number;
35
-
36
- /**
37
- * True, if this user is a bot. Returned in the `receiver` field only.
38
- * @see InitData.receiver
39
- */
40
- isBot?: boolean;
41
-
42
- /**
43
- * True, if this user is a Telegram Premium user.
44
- */
45
- isPremium?: boolean;
46
-
47
- /**
48
- * Last name of the user or bot.
49
- */
50
- lastName?: string;
51
-
52
- /**
53
- * [IETF language tag](https://en.wikipedia.org/wiki/IETF_language_tag) of the user's language.
54
- * Returns in user field only.
55
- */
56
- languageCode?: string;
57
-
58
- /**
59
- * URL of the user’s profile photo. The photo can be in .jpeg or .svg
60
- * formats. Only returned for Mini Apps launched from the attachment menu.
61
- */
62
- photoUrl?: string;
63
-
64
- /**
65
- * Username of the user or bot.
66
- */
67
- username?: string;
68
- }
69
-
70
- /**
71
- * Describes Telegram Mini Apps [Chat](https://docs.telegram-mini-apps.com/launch-parameters/init-data#chat) type.
72
- */
73
- export interface Chat {
74
- /**
75
- * Unique identifier for this chat.
76
- */
77
- id: number;
78
-
79
- /**
80
- * URL of the chat’s photo. The photo can be in .jpeg or .svg formats.
81
- * Only returned for Mini Apps launched from the attachment menu.
82
- */
83
- photoUrl?: string;
84
-
85
- /**
86
- * Type of chat.
87
- */
88
- type: 'group' | 'supergroup' | 'channel' | string;
89
-
90
- /**
91
- * Title of the chat.
92
- */
93
- title: string;
94
-
95
- /**
96
- * Username of the chat.
97
- */
98
- username?: string;
99
- }
100
-
101
- /**
102
- * Describes Telegram Mini Apps [InitData](https://docs.telegram-mini-apps.com/launch-parameters/init-data#parameters-list)
103
- * type.
104
- */
105
- export interface InitData {
106
- /**
107
- * Init data generation date.
108
- */
109
- authDate: Date;
110
-
111
- /**
112
- * The number of seconds after which a message can be sent via the method [answerWebAppQuery](https://core.telegram.org/bots/api#answerwebappquery).
113
- */
114
- canSendAfter?: number;
115
-
116
- /**
117
- * An object containing data about the chat where the bot was launched via
118
- * the attachment menu. Returned for supergroups, channels and group
119
- * chats – only for Mini Apps launched via the attachment menu.
120
- */
121
- chat?: Chat;
122
-
123
- /**
124
- * The type of chat from which Mini App was opened.
125
- */
126
- chatType?: ChatType;
127
-
128
- /**
129
- * A global identifier indicating the chat from which Mini App was
130
- * opened. Returned only for applications opened by direct link.
131
- */
132
- chatInstance?: string;
133
-
134
- /**
135
- * A hash of all passed parameters, which the bot server can use to
136
- * check their [validity](https://core.telegram.org/bots/webapps#validating-data-received-via-the-web-app).
137
- */
138
- hash: string;
139
-
140
- /**
141
- * A unique identifier for the Mini App session, required for sending
142
- * messages via the [answerWebAppQuery](https://core.telegram.org/bots/api#answerwebappquery) method.
143
- */
144
- queryId?: string;
145
-
146
- /**
147
- * An object containing data about the chat partner of the current user in
148
- * the chat where the bot was launched via the attachment menu.
149
- * Returned only for private chats and only for Mini Apps launched
150
- * via the attachment menu.
151
- */
152
- receiver?: User;
153
-
154
- /**
155
- * The value of the `startattach` parameter, passed via link. Only returned for
156
- * Mini Apps when launched from the attachment menu via link.
157
- */
158
- startParam?: string;
159
-
160
- /**
161
- * An object containing data about the current user.
162
- */
163
- user?: User;
164
- }
package/src/user.ts DELETED
@@ -1,45 +0,0 @@
1
- import { json, string, boolean, number } from '@tma.js/parsing';
2
-
3
- import type { User } from './types.js';
4
-
5
- /**
6
- * Returns parser used to parse user data.
7
- */
8
- export function user() {
9
- return json<User>({
10
- addedToAttachmentMenu: {
11
- type: boolean().optional(),
12
- from: 'added_to_attachment_menu',
13
- },
14
- allowsWriteToPm: {
15
- type: boolean().optional(),
16
- from: 'allows_write_to_pm',
17
- },
18
- firstName: {
19
- type: string(),
20
- from: 'first_name',
21
- },
22
- id: number(),
23
- isBot: {
24
- type: boolean().optional(),
25
- from: 'is_bot',
26
- },
27
- isPremium: {
28
- type: boolean().optional(),
29
- from: 'is_premium',
30
- },
31
- languageCode: {
32
- type: string().optional(),
33
- from: 'language_code',
34
- },
35
- lastName: {
36
- type: string().optional(),
37
- from: 'last_name',
38
- },
39
- photoUrl: {
40
- type: string().optional(),
41
- from: 'photo_url',
42
- },
43
- username: string().optional(),
44
- }, 'User');
45
- }