@tma.js/init-data 0.2.18 → 0.2.20
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/README.md +3 -3
- package/dist/dts/chat.d.ts +10 -0
- package/dist/dts/index.d.ts +5 -1
- package/dist/dts/initData.d.ts +5 -0
- package/dist/dts/parse.d.ts +6 -0
- package/dist/dts/serialize.d.ts +6 -0
- package/dist/dts/types.d.ts +24 -24
- package/dist/dts/user.d.ts +5 -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 +132 -40
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/chat.ts +17 -0
- package/src/index.ts +5 -1
- package/src/initData.ts +41 -0
- package/src/parse.ts +10 -0
- package/src/serialize.ts +77 -0
- package/src/types.ts +26 -24
- package/src/user.ts +45 -0
- package/dist/dts/parsing.d.ts +0 -11
- package/src/parsing.ts +0 -72
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
[code-badge]: https://img.shields.io/badge/source-black?logo=github
|
|
6
6
|
|
|
7
|
-
[docs-link]: https://docs.telegram-mini-apps.com/
|
|
7
|
+
[docs-link]: https://docs.telegram-mini-apps.com/packages/typescript/tma-js-init-data/about
|
|
8
8
|
|
|
9
9
|
[docs-badge]: https://img.shields.io/badge/documentation-blue?logo=gitbook&logoColor=white
|
|
10
10
|
|
|
@@ -20,9 +20,9 @@
|
|
|
20
20
|
[![docs-badge]][docs-link]
|
|
21
21
|
[![code-badge]][code-link]
|
|
22
22
|
|
|
23
|
-
Package providing utilities connected with Telegram
|
|
23
|
+
Package providing utilities connected with Telegram Mini Apps init data. It
|
|
24
24
|
allows init data parsing, works in browser only.
|
|
25
25
|
|
|
26
26
|
This library is a part of TypeScript packages ecosystem around Telegram Web
|
|
27
27
|
Apps. You can learn more about this package in this
|
|
28
|
-
[documentation]
|
|
28
|
+
[documentation][docs-link].
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns parser used to parse chat data.
|
|
3
|
+
*/
|
|
4
|
+
export declare function chat(): import("@tma.js/parsing").ValueParser<{
|
|
5
|
+
id: number;
|
|
6
|
+
type: string;
|
|
7
|
+
title: string;
|
|
8
|
+
photoUrl: import("@tma.js/parsing").ParseResult<string, true>;
|
|
9
|
+
username: import("@tma.js/parsing").ParseResult<string, true>;
|
|
10
|
+
}, false>;
|
package/dist/dts/index.d.ts
CHANGED
package/dist/dts/types.d.ts
CHANGED
|
@@ -3,10 +3,17 @@
|
|
|
3
3
|
*/
|
|
4
4
|
export type ChatType = 'sender' | 'private' | 'group' | 'supergroup' | 'channel' | string;
|
|
5
5
|
/**
|
|
6
|
-
* Describes user
|
|
7
|
-
* @see https://docs.telegram-mini-apps.com/docs/launch-params/init-data#user
|
|
6
|
+
* Describes Telegram Mini Apps [User](https://docs.telegram-mini-apps.com/launch-parameters/init-data#user) type.
|
|
8
7
|
*/
|
|
9
8
|
export interface User {
|
|
9
|
+
/**
|
|
10
|
+
* True, if this user added the bot to the attachment menu.
|
|
11
|
+
*/
|
|
12
|
+
addedToAttachmentMenu?: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* True, if this user allowed the bot to message them.
|
|
15
|
+
*/
|
|
16
|
+
allowsWriteToPm?: boolean;
|
|
10
17
|
/**
|
|
11
18
|
* First name of the user or bot.
|
|
12
19
|
*/
|
|
@@ -29,14 +36,13 @@ export interface User {
|
|
|
29
36
|
*/
|
|
30
37
|
lastName?: string;
|
|
31
38
|
/**
|
|
32
|
-
* IETF language tag of the user's language.
|
|
33
|
-
*
|
|
34
|
-
* @see https://en.wikipedia.org/wiki/IETF_language_tag
|
|
39
|
+
* [IETF language tag](https://en.wikipedia.org/wiki/IETF_language_tag) of the user's language.
|
|
40
|
+
* Returns in user field only.
|
|
35
41
|
*/
|
|
36
42
|
languageCode?: string;
|
|
37
43
|
/**
|
|
38
44
|
* URL of the user’s profile photo. The photo can be in .jpeg or .svg
|
|
39
|
-
* formats. Only returned for
|
|
45
|
+
* formats. Only returned for Mini Apps launched from the attachment menu.
|
|
40
46
|
*/
|
|
41
47
|
photoUrl?: string;
|
|
42
48
|
/**
|
|
@@ -45,8 +51,7 @@ export interface User {
|
|
|
45
51
|
username?: string;
|
|
46
52
|
}
|
|
47
53
|
/**
|
|
48
|
-
* Describes chat
|
|
49
|
-
* @see https://docs.telegram-mini-apps.com/docs/launch-params/init-data#chat
|
|
54
|
+
* Describes Telegram Mini Apps [Chat](https://docs.telegram-mini-apps.com/launch-parameters/init-data#chat) type.
|
|
50
55
|
*/
|
|
51
56
|
export interface Chat {
|
|
52
57
|
/**
|
|
@@ -55,7 +60,7 @@ export interface Chat {
|
|
|
55
60
|
id: number;
|
|
56
61
|
/**
|
|
57
62
|
* URL of the chat’s photo. The photo can be in .jpeg or .svg formats.
|
|
58
|
-
* Only returned for
|
|
63
|
+
* Only returned for Mini Apps launched from the attachment menu.
|
|
59
64
|
*/
|
|
60
65
|
photoUrl?: string;
|
|
61
66
|
/**
|
|
@@ -72,8 +77,8 @@ export interface Chat {
|
|
|
72
77
|
username?: string;
|
|
73
78
|
}
|
|
74
79
|
/**
|
|
75
|
-
* Describes
|
|
76
|
-
*
|
|
80
|
+
* Describes Telegram Mini Apps [InitData](https://docs.telegram-mini-apps.com/launch-parameters/init-data#parameters-list)
|
|
81
|
+
* type.
|
|
77
82
|
*/
|
|
78
83
|
export interface InitData {
|
|
79
84
|
/**
|
|
@@ -81,14 +86,13 @@ export interface InitData {
|
|
|
81
86
|
*/
|
|
82
87
|
authDate: Date;
|
|
83
88
|
/**
|
|
84
|
-
* The number of seconds after which a message can be sent via the method answerWebAppQuery.
|
|
85
|
-
* @see https://core.telegram.org/bots/api#answerwebappquery
|
|
89
|
+
* The number of seconds after which a message can be sent via the method [answerWebAppQuery](https://core.telegram.org/bots/api#answerwebappquery).
|
|
86
90
|
*/
|
|
87
91
|
canSendAfter?: number;
|
|
88
92
|
/**
|
|
89
93
|
* An object containing data about the chat where the bot was launched via
|
|
90
94
|
* the attachment menu. Returned for supergroups, channels and group
|
|
91
|
-
* chats – only for
|
|
95
|
+
* chats – only for Mini Apps launched via the attachment menu.
|
|
92
96
|
*/
|
|
93
97
|
chat?: Chat;
|
|
94
98
|
/**
|
|
@@ -102,28 +106,24 @@ export interface InitData {
|
|
|
102
106
|
chatInstance?: string;
|
|
103
107
|
/**
|
|
104
108
|
* A hash of all passed parameters, which the bot server can use to
|
|
105
|
-
* check their validity.
|
|
106
|
-
*
|
|
107
|
-
* @see https://core.telegram.org/bots/webapps#validating-data-received-via-the-web-app
|
|
109
|
+
* check their [validity](https://core.telegram.org/bots/webapps#validating-data-received-via-the-web-app).
|
|
108
110
|
*/
|
|
109
111
|
hash: string;
|
|
110
112
|
/**
|
|
111
|
-
* A unique identifier for the
|
|
112
|
-
* messages via the answerWebAppQuery method.
|
|
113
|
-
*
|
|
114
|
-
* @see https://core.telegram.org/bots/api#answerwebappquery
|
|
113
|
+
* A unique identifier for the Mini App session, required for sending
|
|
114
|
+
* messages via the [answerWebAppQuery](https://core.telegram.org/bots/api#answerwebappquery) method.
|
|
115
115
|
*/
|
|
116
116
|
queryId?: string;
|
|
117
117
|
/**
|
|
118
118
|
* An object containing data about the chat partner of the current user in
|
|
119
119
|
* the chat where the bot was launched via the attachment menu.
|
|
120
|
-
* Returned only for private chats and only for
|
|
120
|
+
* Returned only for private chats and only for Mini Apps launched
|
|
121
121
|
* via the attachment menu.
|
|
122
122
|
*/
|
|
123
123
|
receiver?: User;
|
|
124
124
|
/**
|
|
125
|
-
* The value of the startattach parameter, passed via link. Only returned for
|
|
126
|
-
*
|
|
125
|
+
* The value of the `startattach` parameter, passed via link. Only returned for
|
|
126
|
+
* Mini Apps when launched from the attachment menu via link.
|
|
127
127
|
*/
|
|
128
128
|
startParam?: string;
|
|
129
129
|
/**
|
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("@tma.js/parsing");function _(){return t.json({id:t.number(),type:t.string(),title:t.string(),photoUrl:{type:t.string().optional(),from:"photo_url"},username:t.string().optional()})}function f(){return t.json({addedToAttachmentMenu:{type:t.boolean().optional(),from:"added_to_attachment_menu"},allowsWriteToPm:{type:t.boolean().optional(),from:"allows_write_to_pm"},firstName:{type:t.string(),from:"first_name"},id:t.number(),isBot:{type:t.boolean().optional(),from:"is_bot"},isPremium:{type:t.boolean().optional(),from:"is_premium"},languageCode:{type:t.string().optional(),from:"language_code"},lastName:{type:t.string().optional(),from:"last_name"},photoUrl:{type:t.string().optional(),from:"photo_url"},username:t.string().optional()})}function g(){return t.searchParams({authDate:{type:t.date(),from:"auth_date"},canSendAfter:{type:t.number().optional(),from:"can_send_after"},chat:_().optional(),chatInstance:{type:t.string().optional(),from:"chat_instance"},chatType:{type:t.string().optional(),from:"chat_type"},hash:t.string(),queryId:{type:t.string().optional(),from:"query_id"},receiver:f().optional(),startParam:{type:t.string().optional(),from:"start_param"},user:f().optional()})}function S(r){return g().parse(r)}function n(r,o,a){a!==void 0&&r.set(o,a)}function h(r,o,a){if(a===void 0)return;const{photoUrl:s,firstName:p,lastName:m,isPremium:u,languageCode:l,isBot:c,...i}=a;r.set(o,JSON.stringify({...i,photo_url:s,first_name:p,last_name:m,is_premium:u,language_code:l,is_bot:c}))}function P(r){const{chat:o,chatType:a,chatInstance:s,hash:p,user:m,queryId:u,receiver:l,startParam:c,canSendAfter:i,authDate:y}=r,e=new URLSearchParams;if(e.set("auth_date",(y.getTime()/1e3).toString()),n(e,"chat_instance",s),n(e,"chat_type",a),n(e,"hash",p),n(e,"query_id",u),h(e,"receiver",l),n(e,"start_param",c),h(e,"user",m),o){const{photoUrl:d,...b}=o;e.set("chat",JSON.stringify({...b,photo_url:d}))}return i&&e.set("can_send_after",i.toString()),e.toString()}exports.chat=_;exports.initData=g;exports.parse=S;exports.serialize=P;exports.user=f;
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/chat.ts","../src/user.ts","../src/initData.ts","../src/parse.ts","../src/serialize.ts"],"sourcesContent":["import { json, number, string } from '@tma.js/parsing';\n\n/**\n * Returns parser used to parse chat data.\n */\nexport function chat() {\n return json({\n id: number(),\n type: string(),\n title: string(),\n photoUrl: {\n type: string().optional(),\n from: 'photo_url',\n },\n username: string().optional(),\n });\n}\n","import { json, string, boolean, number } from '@tma.js/parsing';\n\nimport type { User } from './types.js';\n\n/**\n * Returns parser used to parse user data.\n */\nexport function user() {\n return json<User>({\n addedToAttachmentMenu: {\n type: boolean().optional(),\n from: 'added_to_attachment_menu',\n },\n allowsWriteToPm: {\n type: boolean().optional(),\n from: 'allows_write_to_pm',\n },\n firstName: {\n type: string(),\n from: 'first_name',\n },\n id: number(),\n isBot: {\n type: boolean().optional(),\n from: 'is_bot',\n },\n isPremium: {\n type: boolean().optional(),\n from: 'is_premium',\n },\n languageCode: {\n type: string().optional(),\n from: 'language_code',\n },\n lastName: {\n type: string().optional(),\n from: 'last_name',\n },\n photoUrl: {\n type: string().optional(),\n from: 'photo_url',\n },\n username: string().optional(),\n });\n}\n","import { string, number, date, searchParams } from '@tma.js/parsing';\n\nimport { user } from './user.js';\nimport { chat } from './chat.js';\nimport type { InitData } from './types.js';\n\n/**\n * Returns parser used to parse init data, presented as search params.\n */\nexport function initData() {\n return searchParams<InitData>({\n authDate: {\n type: date(),\n from: 'auth_date',\n },\n canSendAfter: {\n type: number().optional(),\n from: 'can_send_after',\n },\n chat: chat().optional(),\n chatInstance: {\n type: string().optional(),\n from: 'chat_instance',\n },\n chatType: {\n type: string().optional(),\n from: 'chat_type',\n },\n hash: string(),\n queryId: {\n type: string().optional(),\n from: 'query_id',\n },\n receiver: user().optional(),\n startParam: {\n type: string().optional(),\n from: 'start_param',\n },\n user: user().optional(),\n });\n}\n","import { initData } from './initData.js';\nimport type { InitData } from './types.js';\n\n/**\n * Parses incoming value as init data.\n * @param value - value to parse.\n */\nexport function parse(value: unknown): InitData {\n return initData().parse(value);\n}\n","import type { InitData, User } from './types.js';\n\nfunction setString(params: URLSearchParams, key: string, value: string | undefined) {\n if (value === undefined) {\n return;\n }\n params.set(key, value);\n}\n\nfunction setUser(params: URLSearchParams, key: string, value: User | undefined) {\n if (value === undefined) {\n return;\n }\n const {\n photoUrl,\n firstName,\n lastName,\n isPremium,\n languageCode,\n isBot,\n ...rest\n } = value;\n\n params.set(key, JSON.stringify({\n ...rest,\n photo_url: photoUrl,\n first_name: firstName,\n last_name: lastName,\n is_premium: isPremium,\n language_code: languageCode,\n is_bot: isBot,\n }));\n}\n\n/**\n * Converts init data to its initial representation.\n * @param value - init data.\n */\nexport function serialize(value: InitData): string {\n const {\n chat,\n chatType,\n chatInstance,\n hash,\n user,\n queryId,\n receiver,\n startParam,\n canSendAfter,\n authDate,\n } = value;\n\n const params = new URLSearchParams();\n\n params.set('auth_date', (authDate.getTime() / 1000).toString());\n setString(params, 'chat_instance', chatInstance);\n setString(params, 'chat_type', chatType);\n setString(params, 'hash', hash);\n setString(params, 'query_id', queryId);\n setUser(params, 'receiver', receiver);\n setString(params, 'start_param', startParam);\n setUser(params, 'user', user);\n\n if (chat) {\n const { photoUrl, ...rest } = chat;\n params.set('chat', JSON.stringify({\n ...rest,\n photo_url: photoUrl,\n }));\n }\n\n if (canSendAfter) {\n params.set('can_send_after', canSendAfter.toString());\n }\n\n return params.toString();\n}\n"],"names":["chat","json","number","string","user","boolean","initData","searchParams","date","parse","value","setString","params","key","setUser","photoUrl","firstName","lastName","isPremium","languageCode","isBot","rest","serialize","chatType","chatInstance","hash","queryId","receiver","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CAAI,EAExBJ,EAAM,CACR,KAAM,CAAE,SAAAe,EAAU,GAAGM,CAAA,EAASrB,EACvBY,EAAA,IAAI,OAAQ,KAAK,UAAU,CAChC,GAAGS,EACH,UAAWN,CACZ,CAAA,CAAC,CACJ,CAEA,OAAIc,GACFjB,EAAO,IAAI,iBAAkBiB,EAAa,SAAU,CAAA,EAG/CjB,EAAO,UAChB"}
|
package/dist/index.iife.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
this.tmajs=this.tmajs||{};this.tmajs.initData=function(
|
|
1
|
+
this.tmajs=this.tmajs||{};this.tmajs.initData=function(y){"use strict";class u extends Error{constructor(e,{type:n,error:i,field:r}={}){super(`Unable to parse ${r?`field "${r}"`:"value"} as ${n}`,{cause:{value:e,error:i}}),Object.setPrototypeOf(this,u.prototype)}}class b{constructor(e,n,i){this.parser=e,this.isOptional=n,this.isEmpty=i}parse(e){return this.isOptional&&this.isEmpty(e)?void 0:this.parser(e)}optional(){return this.isOptional=!0,this}}function S(t){return t===void 0}function h(){return new TypeError("Does not have any of expected types")}function _(t,{isEmpty:e=S}={}){return function({isEmpty:n=e}={}){return new b(t,!1,n)}}function v(t,e,n){const i={};for(const r in t){const o=t[r];if(!o)continue;let s,c;if("type"in o){const{type:f}=o;s=o.from||r,c=typeof f=="function"?f:f.parse.bind(f)}else s=r,c=typeof o=="function"?o:o.parse.bind(o);let l;const m=e(s);try{l=c(m)}catch(f){throw new u(m,{field:s,type:n,error:f})}l!==void 0&&(i[r]=l)}return i}const w=_(t=>{if(typeof t=="boolean")return t;const e=String(t);if(e==="1"||e==="true")return!0;if(e==="0"||e==="false")return!1;throw new u(t,{type:"boolean",error:h()})}),g=_(t=>{if(typeof t=="number")return t;if(typeof t=="string"){const e=Number(t);if(!Number.isNaN(e))return e}throw new u(t,{type:"number",error:h()})}),E=g(),T=_(t=>{if(t instanceof Date)return t;try{return new Date(E.parse(t)*1e3)}catch(e){throw new u(t,{type:"Date",error:e})}});function O(t,e={}){const{type:n,isEmpty:i=S}=e;return new b(r=>{let o=r;if(typeof o=="string")try{o=JSON.parse(o)}catch(s){throw new u(r,{type:n,error:s})}if(typeof o!="object"||o===null||Array.isArray(o))throw new u(r,{type:n,error:h()});return v(t,s=>o[s])},!1,i)}const a=_(t=>{if(typeof t=="string"||typeof t=="number")return t.toString();throw new u(t,{type:"string",error:h()})});a();function j(t,e={}){const{type:n,isEmpty:i=S}=e;return new b(r=>{if(typeof r!="string"&&!(r instanceof URLSearchParams))throw new u(r,{type:n,error:h()});const o=typeof r=="string"?new URLSearchParams(r):r;return v(t,s=>{const c=o.get(s);return c===null?void 0:c})},!1,i)}function P(){return O({id:g(),type:a(),title:a(),photoUrl:{type:a().optional(),from:"photo_url"},username:a().optional()})}function N(){return O({addedToAttachmentMenu:{type:w().optional(),from:"added_to_attachment_menu"},allowsWriteToPm:{type:w().optional(),from:"allows_write_to_pm"},firstName:{type:a(),from:"first_name"},id:g(),isBot:{type:w().optional(),from:"is_bot"},isPremium:{type:w().optional(),from:"is_premium"},languageCode:{type:a().optional(),from:"language_code"},lastName:{type:a().optional(),from:"last_name"},photoUrl:{type:a().optional(),from:"photo_url"},username:a().optional()})}function U(){return j({authDate:{type:T(),from:"auth_date"},canSendAfter:{type:g().optional(),from:"can_send_after"},chat:P().optional(),chatInstance:{type:a().optional(),from:"chat_instance"},chatType:{type:a().optional(),from:"chat_type"},hash:a(),queryId:{type:a().optional(),from:"query_id"},receiver:N().optional(),startParam:{type:a().optional(),from:"start_param"},user:N().optional()})}function A(t){return U().parse(t)}function d(t,e,n){n!==void 0&&t.set(e,n)}function D(t,e,n){if(n===void 0)return;const{photoUrl:i,firstName:r,lastName:o,isPremium:s,languageCode:c,isBot:l,...m}=n;t.set(e,JSON.stringify({...m,photo_url:i,first_name:r,last_name:o,is_premium:s,language_code:c,is_bot:l}))}function q(t){const{chat:e,chatType:n,chatInstance:i,hash:r,user:o,queryId:s,receiver:c,startParam:l,canSendAfter:m,authDate:f}=t,p=new URLSearchParams;if(p.set("auth_date",(f.getTime()/1e3).toString()),d(p,"chat_instance",i),d(p,"chat_type",n),d(p,"hash",r),d(p,"query_id",s),D(p,"receiver",c),d(p,"start_param",l),D(p,"user",o),e){const{photoUrl:I,...R}=e;p.set("chat",JSON.stringify({...R,photo_url:I}))}return m&&p.set("can_send_after",m.toString()),p.toString()}return y.chat=P,y.initData=U,y.parse=A,y.serialize=q,y.user=N,Object.defineProperty(y,Symbol.toStringTag,{value:"Module"}),y}({});
|
|
2
2
|
//# sourceMappingURL=index.iife.js.map
|
package/dist/index.iife.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.iife.js","sources":["../../parsing/dist/index.mjs","../src/parsing.ts"],"sourcesContent":["var g = Object.defineProperty;\nvar l = (r, t, e) => t in r ? g(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;\nvar d = (r, t, e) => (l(r, typeof t != \"symbol\" ? t + \"\" : t, e), e);\nimport { toRGB as A } from \"@tma.js/colors\";\nclass i extends Error {\n constructor(t, { type: e, error: o, field: n } = {}) {\n super(`Unable to parse ${n ? `field \"${n}\"` : \"value\"} as ${e}`, {\n cause: { value: t, error: o }\n }), Object.setPrototypeOf(this, i.prototype);\n }\n}\nclass u {\n constructor(t, e, o) {\n this.parser = t, this.isOptional = e, this.isEmpty = o;\n }\n parse(t) {\n return this.isOptional && this.isEmpty(t) ? void 0 : this.parser(t);\n }\n optional() {\n return this.isOptional = !0, this;\n }\n}\nfunction E(r) {\n if (Array.isArray(r))\n return r;\n if (typeof r == \"string\")\n try {\n const t = JSON.parse(r);\n if (Array.isArray(t))\n return t;\n } catch {\n }\n throw new i(r, { type: \"array\" });\n}\nclass O extends u {\n constructor(e, o, n) {\n super(E, o, n);\n d(this, \"itemParser\");\n this.itemParser = typeof e == \"function\" ? e : e.parse.bind(e);\n }\n parse(e) {\n const o = super.parse(e);\n return o === void 0 ? o : o.map(this.itemParser);\n }\n of(e) {\n return this.itemParser = typeof e == \"function\" ? e : e.parse.bind(e), this;\n }\n}\nfunction h(r) {\n return r === void 0;\n}\nfunction D() {\n return new O((r) => r, !1, h);\n}\nfunction f() {\n return new TypeError(\"Does not have any of expected types\");\n}\nfunction y(r, { isEmpty: t = h } = {}) {\n return function({ isEmpty: o = t } = {}) {\n return new u(r, !1, o);\n };\n}\nfunction b(r, t, e) {\n const o = {};\n for (const n in r) {\n const s = r[n];\n if (!s)\n continue;\n let a, p;\n if (\"type\" in s) {\n const { type: c } = s;\n a = s.from || n, p = typeof c == \"function\" ? c : c.parse.bind(c);\n } else\n a = n, p = typeof s == \"function\" ? s : s.parse.bind(s);\n let w;\n const m = t(a);\n try {\n w = p(m);\n } catch (c) {\n throw new i(m, {\n field: a,\n type: e,\n error: c\n });\n }\n w !== void 0 && (o[n] = w);\n }\n return o;\n}\nconst G = y((r) => {\n if (typeof r == \"boolean\")\n return r;\n throw new i(r, { type: \"boolean\", error: f() });\n}), V = y((r) => {\n if (typeof r == \"number\")\n return r;\n if (typeof r == \"string\") {\n const t = Number(r);\n if (!Number.isNaN(t))\n return t;\n }\n throw new i(r, { type: \"number\", error: f() });\n}), N = V(), R = y((r) => {\n if (r instanceof Date)\n return r;\n try {\n return new Date(N.parse(r) * 1e3);\n } catch (t) {\n throw new i(r, { type: \"Date\", error: t });\n }\n});\nfunction U(r, t = {}) {\n const {\n type: e,\n isEmpty: o = h\n } = t;\n return new u((n) => {\n let s = n;\n if (typeof s == \"string\")\n try {\n s = JSON.parse(s);\n } catch (a) {\n throw new i(n, { type: e, error: a });\n }\n if (typeof s != \"object\" || s === null || Array.isArray(s))\n throw new i(n, { type: e, error: f() });\n return b(r, (a) => s[a]);\n }, !1, o);\n}\nconst P = y((r) => {\n if (typeof r == \"string\" || typeof r == \"number\")\n return r.toString();\n throw new i(r, { type: \"string\", error: f() });\n}), S = P(), B = y((r) => {\n try {\n return A(S.parse(r));\n } catch (t) {\n throw new i(r, { type: \"RGB\", error: t });\n }\n});\nfunction $(r, t = {}) {\n const {\n type: e,\n isEmpty: o = h\n } = t;\n return new u((n) => {\n if (typeof n != \"string\" && !(n instanceof URLSearchParams))\n throw new i(n, { type: e, error: f() });\n const s = typeof n == \"string\" ? new URLSearchParams(n) : n;\n return b(r, (a) => {\n const p = s.get(a);\n return p === null ? void 0 : p;\n });\n }, !1, o);\n}\nexport {\n O as ArrayValueParser,\n i as ParsingError,\n u as ValueParser,\n D as array,\n G as boolean,\n y as createValueParserGen,\n R as date,\n U as json,\n V as number,\n B as rgb,\n $ as searchParams,\n P as string\n};\n//# sourceMappingURL=index.mjs.map\n","import {\n searchParams,\n json,\n string,\n number,\n boolean,\n date,\n type AnyParser,\n} from '@tma.js/parsing';\n\nimport type { InitData, User } from './types.js';\n\nfunction field<P extends AnyParser<any>>(parser: P, from?: string) {\n return {\n from,\n type: parser,\n };\n}\n\nconst str = string();\nconst strOptional = string().optional();\nconst num = number();\nconst numOptional = number().optional();\nconst boolOptional = boolean().optional();\n\nfunction user() {\n return json<User>({\n firstName: field(str, 'first_name'),\n id: num,\n isBot: field(boolOptional, 'is_bot'),\n isPremium: field(boolOptional, 'is_premium'),\n lastName: field(strOptional, 'last_name'),\n languageCode: field(strOptional, 'language_code'),\n photoUrl: field(strOptional, 'photo_url'),\n username: strOptional,\n });\n}\n\nfunction chat() {\n return json({\n id: num,\n type: str,\n title: str,\n photoUrl: field(strOptional, 'photo_url'),\n username: strOptional,\n });\n}\n\n/**\n * Parser used to parse init data, presented as search params.\n * @deprecated Use `parse` method instead.\n */\nexport const initData = searchParams<InitData>({\n authDate: field(date(), 'auth_date'),\n hash: str,\n user: user().optional(),\n receiver: user().optional(),\n chat: chat().optional(),\n chatType: field(strOptional, 'chat_type'),\n chatInstance: field(strOptional, 'chat_instance'),\n canSendAfter: field(numOptional, 'can_send_after'),\n queryId: field(strOptional, 'query_id'),\n startParam: field(strOptional, 'start_param'),\n});\n\n/**\n * Parses incoming value as init data.\n * @param value - value to parse.\n */\nexport function parse(value: string | URLSearchParams): InitData {\n return initData.parse(value);\n}\n"],"names":["i","t","e","o","n","u","h","r","f","y","b","s","a","p","c","w","m","G","V","N","R","U","P","$","field","parser","from","str","string","strOptional","num","number","numOptional","boolOptional","boolean","user","json","chat","initData","searchParams","date","parse","value"],"mappings":"uEAIA,MAAMA,UAAU,KAAM,CACpB,YAAYC,EAAG,CAAE,KAAMC,EAAG,MAAOC,EAAG,MAAOC,CAAG,EAAG,GAAI,CACnD,MAAM,mBAAmBA,EAAI,UAAUA,CAAC,IAAM,OAAO,OAAOF,CAAC,GAAI,CAC/D,MAAO,CAAE,MAAOD,EAAG,MAAOE,CAAG,CACnC,CAAK,EAAG,OAAO,eAAe,KAAMH,EAAE,SAAS,CAC5C,CACH,CACA,MAAMK,CAAE,CACN,YAAYJ,EAAGC,EAAGC,EAAG,CACnB,KAAK,OAASF,EAAG,KAAK,WAAaC,EAAG,KAAK,QAAUC,CACtD,CACD,MAAMF,EAAG,CACP,OAAO,KAAK,YAAc,KAAK,QAAQA,CAAC,EAAI,OAAS,KAAK,OAAOA,CAAC,CACnE,CACD,UAAW,CACT,OAAO,KAAK,WAAa,GAAI,IAC9B,CACH,CA2BA,SAASK,EAAEC,EAAG,CACZ,OAAOA,IAAM,MACf,CAIA,SAASC,GAAI,CACX,OAAO,IAAI,UAAU,qCAAqC,CAC5D,CACA,SAASC,EAAEF,EAAG,CAAE,QAASN,EAAIK,CAAG,EAAG,GAAI,CACrC,OAAO,SAAS,CAAE,QAASH,EAAIF,CAAC,EAAK,CAAA,EAAI,CACvC,OAAO,IAAII,EAAEE,EAAG,GAAIJ,CAAC,CACzB,CACA,CACA,SAASO,EAAEH,EAAGN,EAAGC,EAAG,CAClB,MAAMC,EAAI,CAAA,EACV,UAAWC,KAAKG,EAAG,CACjB,MAAMI,EAAIJ,EAAEH,CAAC,EACb,GAAI,CAACO,EACH,SACF,IAAIC,EAAGC,EACP,GAAI,SAAUF,EAAG,CACf,KAAM,CAAE,KAAMG,CAAG,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IAAS,WACvBE,EAAeC,IAAU,WAE/B,SAASC,GAAO,CACd,OAAOC,EAAW,CAChB,UAAWZ,EAAMG,EAAK,YAAY,EAClC,GAAIG,EACJ,MAAON,EAAMS,EAAc,QAAQ,EACnC,UAAWT,EAAMS,EAAc,YAAY,EAC3C,SAAUT,EAAMK,EAAa,WAAW,EACxC,aAAcL,EAAMK,EAAa,eAAe,EAChD,SAAUL,EAAMK,EAAa,WAAW,EACxC,SAAUA,CAAA,CACX,CACH,CAEA,SAASQ,GAAO,CACd,OAAOD,EAAK,CACV,GAAIN,EACJ,KAAMH,EACN,MAAOA,EACP,SAAUH,EAAMK,EAAa,WAAW,EACxC,SAAUA,CAAA,CACX,CACH,CAMa,MAAAS,EAAWC,EAAuB,CAC7C,SAAUf,EAAMgB,EAAK,EAAG,WAAW,EACnC,KAAMb,EACN,KAAMQ,EAAK,EAAE,SAAS,EACtB,SAAUA,EAAK,EAAE,SAAS,EAC1B,KAAME,EAAK,EAAE,SAAS,EACtB,SAAUb,EAAMK,EAAa,WAAW,EACxC,aAAcL,EAAMK,EAAa,eAAe,EAChD,aAAcL,EAAMQ,EAAa,gBAAgB,EACjD,QAASR,EAAMK,EAAa,UAAU,EACtC,WAAYL,EAAMK,EAAa,aAAa,CAC9C,CAAC,EAMM,SAASY,EAAMC,EAA2C,CACxD,OAAAJ,EAAS,MAAMI,CAAK,CAC7B"}
|
|
1
|
+
{"version":3,"file":"index.iife.js","sources":["../../parsing/dist/index.mjs","../src/chat.ts","../src/user.ts","../src/initData.ts","../src/parse.ts","../src/serialize.ts"],"sourcesContent":["var g = Object.defineProperty;\nvar l = (r, t, e) => t in r ? g(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;\nvar d = (r, t, e) => (l(r, typeof t != \"symbol\" ? t + \"\" : t, e), e);\nimport { toRGB as A } from \"@tma.js/colors\";\nclass i extends Error {\n constructor(t, { type: e, error: o, field: n } = {}) {\n super(`Unable to parse ${n ? `field \"${n}\"` : \"value\"} as ${e}`, {\n cause: { value: t, error: o }\n }), Object.setPrototypeOf(this, i.prototype);\n }\n}\nclass u {\n constructor(t, e, o) {\n this.parser = t, this.isOptional = e, this.isEmpty = o;\n }\n parse(t) {\n return this.isOptional && this.isEmpty(t) ? void 0 : this.parser(t);\n }\n optional() {\n return this.isOptional = !0, this;\n }\n}\nfunction E(r) {\n if (Array.isArray(r))\n return r;\n if (typeof r == \"string\")\n try {\n const t = JSON.parse(r);\n if (Array.isArray(t))\n return t;\n } catch {\n }\n throw new i(r, { type: \"array\" });\n}\nclass S extends u {\n constructor(e, o, n) {\n super(E, o, n);\n d(this, \"itemParser\");\n this.itemParser = typeof e == \"function\" ? e : e.parse.bind(e);\n }\n parse(e) {\n const o = super.parse(e);\n return o === void 0 ? o : o.map(this.itemParser);\n }\n of(e) {\n return this.itemParser = typeof e == \"function\" ? e : e.parse.bind(e), this;\n }\n}\nfunction h(r) {\n return r === void 0;\n}\nfunction D() {\n return new S((r) => r, !1, h);\n}\nfunction f() {\n return new TypeError(\"Does not have any of expected types\");\n}\nfunction y(r, { isEmpty: t = h } = {}) {\n return function({ isEmpty: o = t } = {}) {\n return new u(r, !1, o);\n };\n}\nfunction b(r, t, e) {\n const o = {};\n for (const n in r) {\n const s = r[n];\n if (!s)\n continue;\n let a, c;\n if (\"type\" in s) {\n const { type: p } = s;\n a = s.from || n, c = typeof p == \"function\" ? p : p.parse.bind(p);\n } else\n a = n, c = typeof s == \"function\" ? s : s.parse.bind(s);\n let w;\n const m = t(a);\n try {\n w = c(m);\n } catch (p) {\n throw new i(m, {\n field: a,\n type: e,\n error: p\n });\n }\n w !== void 0 && (o[n] = w);\n }\n return o;\n}\nconst G = y((r) => {\n if (typeof r == \"boolean\")\n return r;\n const t = String(r);\n if (t === \"1\" || t === \"true\")\n return !0;\n if (t === \"0\" || t === \"false\")\n return !1;\n throw new i(r, { type: \"boolean\", error: f() });\n}), O = y((r) => {\n if (typeof r == \"number\")\n return r;\n if (typeof r == \"string\") {\n const t = Number(r);\n if (!Number.isNaN(t))\n return t;\n }\n throw new i(r, { type: \"number\", error: f() });\n}), V = O(), R = y((r) => {\n if (r instanceof Date)\n return r;\n try {\n return new Date(V.parse(r) * 1e3);\n } catch (t) {\n throw new i(r, { type: \"Date\", error: t });\n }\n});\nfunction U(r, t = {}) {\n const {\n type: e,\n isEmpty: o = h\n } = t;\n return new u((n) => {\n let s = n;\n if (typeof s == \"string\")\n try {\n s = JSON.parse(s);\n } catch (a) {\n throw new i(n, { type: e, error: a });\n }\n if (typeof s != \"object\" || s === null || Array.isArray(s))\n throw new i(n, { type: e, error: f() });\n return b(r, (a) => s[a]);\n }, !1, o);\n}\nconst N = y((r) => {\n if (typeof r == \"string\" || typeof r == \"number\")\n return r.toString();\n throw new i(r, { type: \"string\", error: f() });\n}), P = N(), B = y((r) => {\n try {\n return A(P.parse(r));\n } catch (t) {\n throw new i(r, { type: \"RGB\", error: t });\n }\n});\nfunction $(r, t = {}) {\n const {\n type: e,\n isEmpty: o = h\n } = t;\n return new u((n) => {\n if (typeof n != \"string\" && !(n instanceof URLSearchParams))\n throw new i(n, { type: e, error: f() });\n const s = typeof n == \"string\" ? new URLSearchParams(n) : n;\n return b(r, (a) => {\n const c = s.get(a);\n return c === null ? void 0 : c;\n });\n }, !1, o);\n}\nexport {\n S as ArrayValueParser,\n i as ParsingError,\n u as ValueParser,\n D as array,\n G as boolean,\n y as createValueParserGen,\n R as date,\n U as json,\n O as number,\n B as rgb,\n $ as searchParams,\n N as string\n};\n//# sourceMappingURL=index.mjs.map\n","import { json, number, string } from '@tma.js/parsing';\n\n/**\n * Returns parser used to parse chat data.\n */\nexport function chat() {\n return json({\n id: number(),\n type: string(),\n title: string(),\n photoUrl: {\n type: string().optional(),\n from: 'photo_url',\n },\n username: string().optional(),\n });\n}\n","import { json, string, boolean, number } from '@tma.js/parsing';\n\nimport type { User } from './types.js';\n\n/**\n * Returns parser used to parse user data.\n */\nexport function user() {\n return json<User>({\n addedToAttachmentMenu: {\n type: boolean().optional(),\n from: 'added_to_attachment_menu',\n },\n allowsWriteToPm: {\n type: boolean().optional(),\n from: 'allows_write_to_pm',\n },\n firstName: {\n type: string(),\n from: 'first_name',\n },\n id: number(),\n isBot: {\n type: boolean().optional(),\n from: 'is_bot',\n },\n isPremium: {\n type: boolean().optional(),\n from: 'is_premium',\n },\n languageCode: {\n type: string().optional(),\n from: 'language_code',\n },\n lastName: {\n type: string().optional(),\n from: 'last_name',\n },\n photoUrl: {\n type: string().optional(),\n from: 'photo_url',\n },\n username: string().optional(),\n });\n}\n","import { string, number, date, searchParams } from '@tma.js/parsing';\n\nimport { user } from './user.js';\nimport { chat } from './chat.js';\nimport type { InitData } from './types.js';\n\n/**\n * Returns parser used to parse init data, presented as search params.\n */\nexport function initData() {\n return searchParams<InitData>({\n authDate: {\n type: date(),\n from: 'auth_date',\n },\n canSendAfter: {\n type: number().optional(),\n from: 'can_send_after',\n },\n chat: chat().optional(),\n chatInstance: {\n type: string().optional(),\n from: 'chat_instance',\n },\n chatType: {\n type: string().optional(),\n from: 'chat_type',\n },\n hash: string(),\n queryId: {\n type: string().optional(),\n from: 'query_id',\n },\n receiver: user().optional(),\n startParam: {\n type: string().optional(),\n from: 'start_param',\n },\n user: user().optional(),\n });\n}\n","import { initData } from './initData.js';\nimport type { InitData } from './types.js';\n\n/**\n * Parses incoming value as init data.\n * @param value - value to parse.\n */\nexport function parse(value: unknown): InitData {\n return initData().parse(value);\n}\n","import type { InitData, User } from './types.js';\n\nfunction setString(params: URLSearchParams, key: string, value: string | undefined) {\n if (value === undefined) {\n return;\n }\n params.set(key, value);\n}\n\nfunction setUser(params: URLSearchParams, key: string, value: User | undefined) {\n if (value === undefined) {\n return;\n }\n const {\n photoUrl,\n firstName,\n lastName,\n isPremium,\n languageCode,\n isBot,\n ...rest\n } = value;\n\n params.set(key, JSON.stringify({\n ...rest,\n photo_url: photoUrl,\n first_name: firstName,\n last_name: lastName,\n is_premium: isPremium,\n language_code: languageCode,\n is_bot: isBot,\n }));\n}\n\n/**\n * Converts init data to its initial representation.\n * @param value - init data.\n */\nexport function serialize(value: InitData): string {\n const {\n chat,\n chatType,\n chatInstance,\n hash,\n user,\n queryId,\n receiver,\n startParam,\n canSendAfter,\n authDate,\n } = value;\n\n const params = new URLSearchParams();\n\n params.set('auth_date', (authDate.getTime() / 1000).toString());\n setString(params, 'chat_instance', chatInstance);\n setString(params, 'chat_type', chatType);\n setString(params, 'hash', hash);\n setString(params, 'query_id', queryId);\n setUser(params, 'receiver', receiver);\n setString(params, 'start_param', startParam);\n setUser(params, 'user', user);\n\n if (chat) {\n const { photoUrl, ...rest } = chat;\n params.set('chat', JSON.stringify({\n ...rest,\n photo_url: photoUrl,\n }));\n }\n\n if (canSendAfter) {\n params.set('can_send_after', canSendAfter.toString());\n }\n\n return params.toString();\n}\n"],"names":["i","t","e","o","n","u","h","r","f","y","b","s","a","p","w","G","O","V","R","U","N","$","chat","json","number","string","user","boolean","initData","searchParams","date","parse","value","setString","params","key","setUser","photoUrl","firstName","lastName","isPremium","languageCode","isBot","rest","serialize","chatType","chatInstance","hash","queryId","receiver","startParam","canSendAfter","authDate"],"mappings":"uEAIA,MAAMA,UAAU,KAAM,CACpB,YAAYC,EAAG,CAAE,KAAMC,EAAG,MAAOC,EAAG,MAAOC,CAAG,EAAG,GAAI,CACnD,MAAM,mBAAmBA,EAAI,UAAUA,CAAC,IAAM,OAAO,OAAOF,CAAC,GAAI,CAC/D,MAAO,CAAE,MAAOD,EAAG,MAAOE,CAAG,CACnC,CAAK,EAAG,OAAO,eAAe,KAAMH,EAAE,SAAS,CAC5C,CACH,CACA,MAAMK,CAAE,CACN,YAAYJ,EAAGC,EAAGC,EAAG,CACnB,KAAK,OAASF,EAAG,KAAK,WAAaC,EAAG,KAAK,QAAUC,CACtD,CACD,MAAMF,EAAG,CACP,OAAO,KAAK,YAAc,KAAK,QAAQA,CAAC,EAAI,OAAS,KAAK,OAAOA,CAAC,CACnE,CACD,UAAW,CACT,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,EAAO,IAAI,aAAckB,EAAS,UAAY,KAAM,UAAU,EACpDnB,EAAAC,EAAQ,gBAAiBY,CAAY,EACrCb,EAAAC,EAAQ,YAAaW,CAAQ,EAC7BZ,EAAAC,EAAQ,OAAQa,CAAI,EACpBd,EAAAC,EAAQ,WAAYc,CAAO,EAC7BZ,EAAAF,EAAQ,WAAYe,CAAQ,EAC1BhB,EAAAC,EAAQ,cAAegB,CAAU,EACnCd,EAAAF,EAAQ,OAAQR,CAAI,EAExBJ,EAAM,CACR,KAAM,CAAE,SAAAe,EAAU,GAAGM,CAAA,EAASrB,EACvBY,EAAA,IAAI,OAAQ,KAAK,UAAU,CAChC,GAAGS,EACH,UAAWN,CACZ,CAAA,CAAC,CACJ,CAEA,OAAIc,GACFjB,EAAO,IAAI,iBAAkBiB,EAAa,SAAU,CAAA,EAG/CjB,EAAO,UAChB"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,49 +1,141 @@
|
|
|
1
|
-
import {
|
|
2
|
-
function
|
|
3
|
-
return {
|
|
4
|
-
|
|
5
|
-
type:
|
|
6
|
-
|
|
1
|
+
import { json as d, number as h, string as t, boolean as s, searchParams as N, date as U } from "@tma.js/parsing";
|
|
2
|
+
function T() {
|
|
3
|
+
return d({
|
|
4
|
+
id: h(),
|
|
5
|
+
type: t(),
|
|
6
|
+
title: t(),
|
|
7
|
+
photoUrl: {
|
|
8
|
+
type: t().optional(),
|
|
9
|
+
from: "photo_url"
|
|
10
|
+
},
|
|
11
|
+
username: t().optional()
|
|
12
|
+
});
|
|
7
13
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
function _() {
|
|
15
|
+
return d({
|
|
16
|
+
addedToAttachmentMenu: {
|
|
17
|
+
type: s().optional(),
|
|
18
|
+
from: "added_to_attachment_menu"
|
|
19
|
+
},
|
|
20
|
+
allowsWriteToPm: {
|
|
21
|
+
type: s().optional(),
|
|
22
|
+
from: "allows_write_to_pm"
|
|
23
|
+
},
|
|
24
|
+
firstName: {
|
|
25
|
+
type: t(),
|
|
26
|
+
from: "first_name"
|
|
27
|
+
},
|
|
28
|
+
id: h(),
|
|
29
|
+
isBot: {
|
|
30
|
+
type: s().optional(),
|
|
31
|
+
from: "is_bot"
|
|
32
|
+
},
|
|
33
|
+
isPremium: {
|
|
34
|
+
type: s().optional(),
|
|
35
|
+
from: "is_premium"
|
|
36
|
+
},
|
|
37
|
+
languageCode: {
|
|
38
|
+
type: t().optional(),
|
|
39
|
+
from: "language_code"
|
|
40
|
+
},
|
|
41
|
+
lastName: {
|
|
42
|
+
type: t().optional(),
|
|
43
|
+
from: "last_name"
|
|
44
|
+
},
|
|
45
|
+
photoUrl: {
|
|
46
|
+
type: t().optional(),
|
|
47
|
+
from: "photo_url"
|
|
48
|
+
},
|
|
49
|
+
username: t().optional()
|
|
19
50
|
});
|
|
20
51
|
}
|
|
21
|
-
function
|
|
22
|
-
return
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
52
|
+
function b() {
|
|
53
|
+
return N({
|
|
54
|
+
authDate: {
|
|
55
|
+
type: U(),
|
|
56
|
+
from: "auth_date"
|
|
57
|
+
},
|
|
58
|
+
canSendAfter: {
|
|
59
|
+
type: h().optional(),
|
|
60
|
+
from: "can_send_after"
|
|
61
|
+
},
|
|
62
|
+
chat: T().optional(),
|
|
63
|
+
chatInstance: {
|
|
64
|
+
type: t().optional(),
|
|
65
|
+
from: "chat_instance"
|
|
66
|
+
},
|
|
67
|
+
chatType: {
|
|
68
|
+
type: t().optional(),
|
|
69
|
+
from: "chat_type"
|
|
70
|
+
},
|
|
71
|
+
hash: t(),
|
|
72
|
+
queryId: {
|
|
73
|
+
type: t().optional(),
|
|
74
|
+
from: "query_id"
|
|
75
|
+
},
|
|
76
|
+
receiver: _().optional(),
|
|
77
|
+
startParam: {
|
|
78
|
+
type: t().optional(),
|
|
79
|
+
from: "start_param"
|
|
80
|
+
},
|
|
81
|
+
user: _().optional()
|
|
28
82
|
});
|
|
29
83
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
84
|
+
function w(a) {
|
|
85
|
+
return b().parse(a);
|
|
86
|
+
}
|
|
87
|
+
function n(a, r, o) {
|
|
88
|
+
o !== void 0 && a.set(r, o);
|
|
89
|
+
}
|
|
90
|
+
function y(a, r, o) {
|
|
91
|
+
if (o === void 0)
|
|
92
|
+
return;
|
|
93
|
+
const {
|
|
94
|
+
photoUrl: p,
|
|
95
|
+
firstName: m,
|
|
96
|
+
lastName: u,
|
|
97
|
+
isPremium: c,
|
|
98
|
+
languageCode: l,
|
|
99
|
+
isBot: f,
|
|
100
|
+
...i
|
|
101
|
+
} = o;
|
|
102
|
+
a.set(r, JSON.stringify({
|
|
103
|
+
...i,
|
|
104
|
+
photo_url: p,
|
|
105
|
+
first_name: m,
|
|
106
|
+
last_name: u,
|
|
107
|
+
is_premium: c,
|
|
108
|
+
language_code: l,
|
|
109
|
+
is_bot: f
|
|
110
|
+
}));
|
|
111
|
+
}
|
|
112
|
+
function I(a) {
|
|
113
|
+
const {
|
|
114
|
+
chat: r,
|
|
115
|
+
chatType: o,
|
|
116
|
+
chatInstance: p,
|
|
117
|
+
hash: m,
|
|
118
|
+
user: u,
|
|
119
|
+
queryId: c,
|
|
120
|
+
receiver: l,
|
|
121
|
+
startParam: f,
|
|
122
|
+
canSendAfter: i,
|
|
123
|
+
authDate: g
|
|
124
|
+
} = a, e = new URLSearchParams();
|
|
125
|
+
if (e.set("auth_date", (g.getTime() / 1e3).toString()), n(e, "chat_instance", p), n(e, "chat_type", o), n(e, "hash", m), n(e, "query_id", c), y(e, "receiver", l), n(e, "start_param", f), y(e, "user", u), r) {
|
|
126
|
+
const { photoUrl: S, ...P } = r;
|
|
127
|
+
e.set("chat", JSON.stringify({
|
|
128
|
+
...P,
|
|
129
|
+
photo_url: S
|
|
130
|
+
}));
|
|
131
|
+
}
|
|
132
|
+
return i && e.set("can_send_after", i.toString()), e.toString();
|
|
44
133
|
}
|
|
45
134
|
export {
|
|
46
|
-
|
|
47
|
-
|
|
135
|
+
T as chat,
|
|
136
|
+
b as initData,
|
|
137
|
+
w as parse,
|
|
138
|
+
I as serialize,
|
|
139
|
+
_ as user
|
|
48
140
|
};
|
|
49
141
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../src/
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../src/chat.ts","../src/user.ts","../src/initData.ts","../src/parse.ts","../src/serialize.ts"],"sourcesContent":["import { json, number, string } from '@tma.js/parsing';\n\n/**\n * Returns parser used to parse chat data.\n */\nexport function chat() {\n return json({\n id: number(),\n type: string(),\n title: string(),\n photoUrl: {\n type: string().optional(),\n from: 'photo_url',\n },\n username: string().optional(),\n });\n}\n","import { json, string, boolean, number } from '@tma.js/parsing';\n\nimport type { User } from './types.js';\n\n/**\n * Returns parser used to parse user data.\n */\nexport function user() {\n return json<User>({\n addedToAttachmentMenu: {\n type: boolean().optional(),\n from: 'added_to_attachment_menu',\n },\n allowsWriteToPm: {\n type: boolean().optional(),\n from: 'allows_write_to_pm',\n },\n firstName: {\n type: string(),\n from: 'first_name',\n },\n id: number(),\n isBot: {\n type: boolean().optional(),\n from: 'is_bot',\n },\n isPremium: {\n type: boolean().optional(),\n from: 'is_premium',\n },\n languageCode: {\n type: string().optional(),\n from: 'language_code',\n },\n lastName: {\n type: string().optional(),\n from: 'last_name',\n },\n photoUrl: {\n type: string().optional(),\n from: 'photo_url',\n },\n username: string().optional(),\n });\n}\n","import { string, number, date, searchParams } from '@tma.js/parsing';\n\nimport { user } from './user.js';\nimport { chat } from './chat.js';\nimport type { InitData } from './types.js';\n\n/**\n * Returns parser used to parse init data, presented as search params.\n */\nexport function initData() {\n return searchParams<InitData>({\n authDate: {\n type: date(),\n from: 'auth_date',\n },\n canSendAfter: {\n type: number().optional(),\n from: 'can_send_after',\n },\n chat: chat().optional(),\n chatInstance: {\n type: string().optional(),\n from: 'chat_instance',\n },\n chatType: {\n type: string().optional(),\n from: 'chat_type',\n },\n hash: string(),\n queryId: {\n type: string().optional(),\n from: 'query_id',\n },\n receiver: user().optional(),\n startParam: {\n type: string().optional(),\n from: 'start_param',\n },\n user: user().optional(),\n });\n}\n","import { initData } from './initData.js';\nimport type { InitData } from './types.js';\n\n/**\n * Parses incoming value as init data.\n * @param value - value to parse.\n */\nexport function parse(value: unknown): InitData {\n return initData().parse(value);\n}\n","import type { InitData, User } from './types.js';\n\nfunction setString(params: URLSearchParams, key: string, value: string | undefined) {\n if (value === undefined) {\n return;\n }\n params.set(key, value);\n}\n\nfunction setUser(params: URLSearchParams, key: string, value: User | undefined) {\n if (value === undefined) {\n return;\n }\n const {\n photoUrl,\n firstName,\n lastName,\n isPremium,\n languageCode,\n isBot,\n ...rest\n } = value;\n\n params.set(key, JSON.stringify({\n ...rest,\n photo_url: photoUrl,\n first_name: firstName,\n last_name: lastName,\n is_premium: isPremium,\n language_code: languageCode,\n is_bot: isBot,\n }));\n}\n\n/**\n * Converts init data to its initial representation.\n * @param value - init data.\n */\nexport function serialize(value: InitData): string {\n const {\n chat,\n chatType,\n chatInstance,\n hash,\n user,\n queryId,\n receiver,\n startParam,\n canSendAfter,\n authDate,\n } = value;\n\n const params = new URLSearchParams();\n\n params.set('auth_date', (authDate.getTime() / 1000).toString());\n setString(params, 'chat_instance', chatInstance);\n setString(params, 'chat_type', chatType);\n setString(params, 'hash', hash);\n setString(params, 'query_id', queryId);\n setUser(params, 'receiver', receiver);\n setString(params, 'start_param', startParam);\n setUser(params, 'user', user);\n\n if (chat) {\n const { photoUrl, ...rest } = chat;\n params.set('chat', JSON.stringify({\n ...rest,\n photo_url: photoUrl,\n }));\n }\n\n if (canSendAfter) {\n params.set('can_send_after', canSendAfter.toString());\n }\n\n return params.toString();\n}\n"],"names":["chat","json","number","string","user","boolean","initData","searchParams","date","parse","value","setString","params","key","setUser","photoUrl","firstName","lastName","isPremium","languageCode","isBot","rest","serialize","chatType","chatInstance","hash","queryId","receiver","startParam","canSendAfter","authDate"],"mappings":";AAKO,SAASA,IAAO;AACrB,SAAOC,EAAK;AAAA,IACV,IAAIC,EAAO;AAAA,IACX,MAAMC,EAAO;AAAA,IACb,OAAOA,EAAO;AAAA,IACd,UAAU;AAAA,MACR,MAAMA,EAAO,EAAE,SAAS;AAAA,MACxB,MAAM;AAAA,IACR;AAAA,IACA,UAAUA,EAAO,EAAE,SAAS;AAAA,EAAA,CAC7B;AACH;ACTO,SAASC,IAAO;AACrB,SAAOH,EAAW;AAAA,IAChB,uBAAuB;AAAA,MACrB,MAAMI,EAAQ,EAAE,SAAS;AAAA,MACzB,MAAM;AAAA,IACR;AAAA,IACA,iBAAiB;AAAA,MACf,MAAMA,EAAQ,EAAE,SAAS;AAAA,MACzB,MAAM;AAAA,IACR;AAAA,IACA,WAAW;AAAA,MACT,MAAMF,EAAO;AAAA,MACb,MAAM;AAAA,IACR;AAAA,IACA,IAAID,EAAO;AAAA,IACX,OAAO;AAAA,MACL,MAAMG,EAAQ,EAAE,SAAS;AAAA,MACzB,MAAM;AAAA,IACR;AAAA,IACA,WAAW;AAAA,MACT,MAAMA,EAAQ,EAAE,SAAS;AAAA,MACzB,MAAM;AAAA,IACR;AAAA,IACA,cAAc;AAAA,MACZ,MAAMF,EAAO,EAAE,SAAS;AAAA,MACxB,MAAM;AAAA,IACR;AAAA,IACA,UAAU;AAAA,MACR,MAAMA,EAAO,EAAE,SAAS;AAAA,MACxB,MAAM;AAAA,IACR;AAAA,IACA,UAAU;AAAA,MACR,MAAMA,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U;AAAA,IAC7B,GAAGQ;AAAA,IACH,WAAWN;AAAA,IACX,YAAYC;AAAA,IACZ,WAAWC;AAAA,IACX,YAAYC;AAAA,IACZ,eAAeC;AAAA,IACf,QAAQC;AAAA,EACT,CAAA,CAAC;AACJ;AAMO,SAASE,EAAUZ,GAAyB;AAC3C,QAAA;AAAA,IACJ,MAAAV;AAAA,IACA,UAAAuB;AAAA,IACA,cAAAC;AAAA,IACA,MAAAC;AAAA,IACA,MAAArB;AAAA,IACA,SAAAsB;AAAA,IACA,UAAAC;AAAA,IACA,YAAAC;AAAA,IACA,cAAAC;AAAA,IACA,UAAAC;AAAA,EACE,IAAApB,GAEEE,IAAS,IAAI;AAWnB,MATAA,EAAO,IAAI,cAAckB,EAAS,YAAY,KAAM,UAAU,GACpDnB,EAAAC,GAAQ,iBAAiBY,CAAY,GACrCb,EAAAC,GAAQ,aAAaW,CAAQ,GAC7BZ,EAAAC,GAAQ,QAAQa,CAAI,GACpBd,EAAAC,GAAQ,YAAYc,CAAO,GAC7BZ,EAAAF,GAAQ,YAAYe,CAAQ,GAC1BhB,EAAAC,GAAQ,eAAegB,CAAU,GACnCd,EAAAF,GAAQ,QAAQR,CAAI,GAExBJ,GAAM;AACR,UAAM,EAAE,UAAAe,GAAU,GAAGM,EAAA,IAASrB;AACvB,IAAAY,EAAA,IAAI,QAAQ,KAAK,UAAU;AAAA,MAChC,GAAGS;AAAA,MACH,WAAWN;AAAA,IACZ,CAAA,CAAC;AAAA,EACJ;AAEA,SAAIc,KACFjB,EAAO,IAAI,kBAAkBiB,EAAa,SAAU,CAAA,GAG/CjB,EAAO;AAChB;"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tma.js/init-data",
|
|
3
|
-
"version": "0.2.
|
|
4
|
-
"description": "Frontend TypeScript library to make work with
|
|
3
|
+
"version": "0.2.20",
|
|
4
|
+
"description": "Frontend TypeScript library to make work with Mini Apps init data easier.",
|
|
5
5
|
"author": "Vladislav Kibenko <wolfram.deus@gmail.com>",
|
|
6
6
|
"homepage": "https://github.com/Telegram-Mini-Apps/tma.js#readme",
|
|
7
7
|
"repository": {
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
}
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@tma.js/parsing": "0.1.
|
|
40
|
+
"@tma.js/parsing": "0.1.3"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"tsconfig": "0.0.2",
|
package/src/chat.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { json, number, string } from '@tma.js/parsing';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Returns parser used to parse chat data.
|
|
5
|
+
*/
|
|
6
|
+
export function chat() {
|
|
7
|
+
return json({
|
|
8
|
+
id: number(),
|
|
9
|
+
type: string(),
|
|
10
|
+
title: string(),
|
|
11
|
+
photoUrl: {
|
|
12
|
+
type: string().optional(),
|
|
13
|
+
from: 'photo_url',
|
|
14
|
+
},
|
|
15
|
+
username: string().optional(),
|
|
16
|
+
});
|
|
17
|
+
}
|
package/src/index.ts
CHANGED
package/src/initData.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { string, number, date, searchParams } from '@tma.js/parsing';
|
|
2
|
+
|
|
3
|
+
import { user } from './user.js';
|
|
4
|
+
import { chat } from './chat.js';
|
|
5
|
+
import type { InitData } from './types.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Returns parser used to parse init data, presented as search params.
|
|
9
|
+
*/
|
|
10
|
+
export function initData() {
|
|
11
|
+
return searchParams<InitData>({
|
|
12
|
+
authDate: {
|
|
13
|
+
type: date(),
|
|
14
|
+
from: 'auth_date',
|
|
15
|
+
},
|
|
16
|
+
canSendAfter: {
|
|
17
|
+
type: number().optional(),
|
|
18
|
+
from: 'can_send_after',
|
|
19
|
+
},
|
|
20
|
+
chat: chat().optional(),
|
|
21
|
+
chatInstance: {
|
|
22
|
+
type: string().optional(),
|
|
23
|
+
from: 'chat_instance',
|
|
24
|
+
},
|
|
25
|
+
chatType: {
|
|
26
|
+
type: string().optional(),
|
|
27
|
+
from: 'chat_type',
|
|
28
|
+
},
|
|
29
|
+
hash: string(),
|
|
30
|
+
queryId: {
|
|
31
|
+
type: string().optional(),
|
|
32
|
+
from: 'query_id',
|
|
33
|
+
},
|
|
34
|
+
receiver: user().optional(),
|
|
35
|
+
startParam: {
|
|
36
|
+
type: string().optional(),
|
|
37
|
+
from: 'start_param',
|
|
38
|
+
},
|
|
39
|
+
user: user().optional(),
|
|
40
|
+
});
|
|
41
|
+
}
|
package/src/parse.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { initData } from './initData.js';
|
|
2
|
+
import type { InitData } from './types.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Parses incoming value as init data.
|
|
6
|
+
* @param value - value to parse.
|
|
7
|
+
*/
|
|
8
|
+
export function parse(value: unknown): InitData {
|
|
9
|
+
return initData().parse(value);
|
|
10
|
+
}
|
package/src/serialize.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
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
|
+
chat,
|
|
42
|
+
chatType,
|
|
43
|
+
chatInstance,
|
|
44
|
+
hash,
|
|
45
|
+
user,
|
|
46
|
+
queryId,
|
|
47
|
+
receiver,
|
|
48
|
+
startParam,
|
|
49
|
+
canSendAfter,
|
|
50
|
+
authDate,
|
|
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
CHANGED
|
@@ -10,10 +10,19 @@ export type ChatType =
|
|
|
10
10
|
| string;
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
* Describes user
|
|
14
|
-
* @see https://docs.telegram-mini-apps.com/docs/launch-params/init-data#user
|
|
13
|
+
* Describes Telegram Mini Apps [User](https://docs.telegram-mini-apps.com/launch-parameters/init-data#user) type.
|
|
15
14
|
*/
|
|
16
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
|
+
|
|
17
26
|
/**
|
|
18
27
|
* First name of the user or bot.
|
|
19
28
|
*/
|
|
@@ -41,15 +50,14 @@ export interface User {
|
|
|
41
50
|
lastName?: string;
|
|
42
51
|
|
|
43
52
|
/**
|
|
44
|
-
* IETF language tag of the user's language.
|
|
45
|
-
*
|
|
46
|
-
* @see https://en.wikipedia.org/wiki/IETF_language_tag
|
|
53
|
+
* [IETF language tag](https://en.wikipedia.org/wiki/IETF_language_tag) of the user's language.
|
|
54
|
+
* Returns in user field only.
|
|
47
55
|
*/
|
|
48
56
|
languageCode?: string;
|
|
49
57
|
|
|
50
58
|
/**
|
|
51
59
|
* URL of the user’s profile photo. The photo can be in .jpeg or .svg
|
|
52
|
-
* formats. Only returned for
|
|
60
|
+
* formats. Only returned for Mini Apps launched from the attachment menu.
|
|
53
61
|
*/
|
|
54
62
|
photoUrl?: string;
|
|
55
63
|
|
|
@@ -60,8 +68,7 @@ export interface User {
|
|
|
60
68
|
}
|
|
61
69
|
|
|
62
70
|
/**
|
|
63
|
-
* Describes chat
|
|
64
|
-
* @see https://docs.telegram-mini-apps.com/docs/launch-params/init-data#chat
|
|
71
|
+
* Describes Telegram Mini Apps [Chat](https://docs.telegram-mini-apps.com/launch-parameters/init-data#chat) type.
|
|
65
72
|
*/
|
|
66
73
|
export interface Chat {
|
|
67
74
|
/**
|
|
@@ -71,7 +78,7 @@ export interface Chat {
|
|
|
71
78
|
|
|
72
79
|
/**
|
|
73
80
|
* URL of the chat’s photo. The photo can be in .jpeg or .svg formats.
|
|
74
|
-
* Only returned for
|
|
81
|
+
* Only returned for Mini Apps launched from the attachment menu.
|
|
75
82
|
*/
|
|
76
83
|
photoUrl?: string;
|
|
77
84
|
|
|
@@ -92,8 +99,8 @@ export interface Chat {
|
|
|
92
99
|
}
|
|
93
100
|
|
|
94
101
|
/**
|
|
95
|
-
* Describes
|
|
96
|
-
*
|
|
102
|
+
* Describes Telegram Mini Apps [InitData](https://docs.telegram-mini-apps.com/launch-parameters/init-data#parameters-list)
|
|
103
|
+
* type.
|
|
97
104
|
*/
|
|
98
105
|
export interface InitData {
|
|
99
106
|
/**
|
|
@@ -102,15 +109,14 @@ export interface InitData {
|
|
|
102
109
|
authDate: Date;
|
|
103
110
|
|
|
104
111
|
/**
|
|
105
|
-
* The number of seconds after which a message can be sent via the method answerWebAppQuery.
|
|
106
|
-
* @see https://core.telegram.org/bots/api#answerwebappquery
|
|
112
|
+
* The number of seconds after which a message can be sent via the method [answerWebAppQuery](https://core.telegram.org/bots/api#answerwebappquery).
|
|
107
113
|
*/
|
|
108
114
|
canSendAfter?: number;
|
|
109
115
|
|
|
110
116
|
/**
|
|
111
117
|
* An object containing data about the chat where the bot was launched via
|
|
112
118
|
* the attachment menu. Returned for supergroups, channels and group
|
|
113
|
-
* chats – only for
|
|
119
|
+
* chats – only for Mini Apps launched via the attachment menu.
|
|
114
120
|
*/
|
|
115
121
|
chat?: Chat;
|
|
116
122
|
|
|
@@ -127,31 +133,27 @@ export interface InitData {
|
|
|
127
133
|
|
|
128
134
|
/**
|
|
129
135
|
* A hash of all passed parameters, which the bot server can use to
|
|
130
|
-
* check their validity.
|
|
131
|
-
*
|
|
132
|
-
* @see https://core.telegram.org/bots/webapps#validating-data-received-via-the-web-app
|
|
136
|
+
* check their [validity](https://core.telegram.org/bots/webapps#validating-data-received-via-the-web-app).
|
|
133
137
|
*/
|
|
134
138
|
hash: string;
|
|
135
139
|
|
|
136
140
|
/**
|
|
137
|
-
* A unique identifier for the
|
|
138
|
-
* messages via the answerWebAppQuery method.
|
|
139
|
-
*
|
|
140
|
-
* @see https://core.telegram.org/bots/api#answerwebappquery
|
|
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.
|
|
141
143
|
*/
|
|
142
144
|
queryId?: string;
|
|
143
145
|
|
|
144
146
|
/**
|
|
145
147
|
* An object containing data about the chat partner of the current user in
|
|
146
148
|
* the chat where the bot was launched via the attachment menu.
|
|
147
|
-
* Returned only for private chats and only for
|
|
149
|
+
* Returned only for private chats and only for Mini Apps launched
|
|
148
150
|
* via the attachment menu.
|
|
149
151
|
*/
|
|
150
152
|
receiver?: User;
|
|
151
153
|
|
|
152
154
|
/**
|
|
153
|
-
* The value of the startattach parameter, passed via link. Only returned for
|
|
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.
|
|
155
157
|
*/
|
|
156
158
|
startParam?: string;
|
|
157
159
|
|
package/src/user.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
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
|
+
});
|
|
45
|
+
}
|
package/dist/dts/parsing.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { InitData } from './types.js';
|
|
2
|
-
/**
|
|
3
|
-
* Parser used to parse init data, presented as search params.
|
|
4
|
-
* @deprecated Use `parse` method instead.
|
|
5
|
-
*/
|
|
6
|
-
export declare const initData: import("@tma.js/parsing").ValueParser<InitData, false>;
|
|
7
|
-
/**
|
|
8
|
-
* Parses incoming value as init data.
|
|
9
|
-
* @param value - value to parse.
|
|
10
|
-
*/
|
|
11
|
-
export declare function parse(value: string | URLSearchParams): InitData;
|
package/src/parsing.ts
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
searchParams,
|
|
3
|
-
json,
|
|
4
|
-
string,
|
|
5
|
-
number,
|
|
6
|
-
boolean,
|
|
7
|
-
date,
|
|
8
|
-
type AnyParser,
|
|
9
|
-
} from '@tma.js/parsing';
|
|
10
|
-
|
|
11
|
-
import type { InitData, User } from './types.js';
|
|
12
|
-
|
|
13
|
-
function field<P extends AnyParser<any>>(parser: P, from?: string) {
|
|
14
|
-
return {
|
|
15
|
-
from,
|
|
16
|
-
type: parser,
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const str = string();
|
|
21
|
-
const strOptional = string().optional();
|
|
22
|
-
const num = number();
|
|
23
|
-
const numOptional = number().optional();
|
|
24
|
-
const boolOptional = boolean().optional();
|
|
25
|
-
|
|
26
|
-
function user() {
|
|
27
|
-
return json<User>({
|
|
28
|
-
firstName: field(str, 'first_name'),
|
|
29
|
-
id: num,
|
|
30
|
-
isBot: field(boolOptional, 'is_bot'),
|
|
31
|
-
isPremium: field(boolOptional, 'is_premium'),
|
|
32
|
-
lastName: field(strOptional, 'last_name'),
|
|
33
|
-
languageCode: field(strOptional, 'language_code'),
|
|
34
|
-
photoUrl: field(strOptional, 'photo_url'),
|
|
35
|
-
username: strOptional,
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function chat() {
|
|
40
|
-
return json({
|
|
41
|
-
id: num,
|
|
42
|
-
type: str,
|
|
43
|
-
title: str,
|
|
44
|
-
photoUrl: field(strOptional, 'photo_url'),
|
|
45
|
-
username: strOptional,
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Parser used to parse init data, presented as search params.
|
|
51
|
-
* @deprecated Use `parse` method instead.
|
|
52
|
-
*/
|
|
53
|
-
export const initData = searchParams<InitData>({
|
|
54
|
-
authDate: field(date(), 'auth_date'),
|
|
55
|
-
hash: str,
|
|
56
|
-
user: user().optional(),
|
|
57
|
-
receiver: user().optional(),
|
|
58
|
-
chat: chat().optional(),
|
|
59
|
-
chatType: field(strOptional, 'chat_type'),
|
|
60
|
-
chatInstance: field(strOptional, 'chat_instance'),
|
|
61
|
-
canSendAfter: field(numOptional, 'can_send_after'),
|
|
62
|
-
queryId: field(strOptional, 'query_id'),
|
|
63
|
-
startParam: field(strOptional, 'start_param'),
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Parses incoming value as init data.
|
|
68
|
-
* @param value - value to parse.
|
|
69
|
-
*/
|
|
70
|
-
export function parse(value: string | URLSearchParams): InitData {
|
|
71
|
-
return initData.parse(value);
|
|
72
|
-
}
|