@tma.js/init-data 0.2.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Vladislav Kibenko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # @tma.js/init-data
2
+
3
+ [code-link]: https://github.com/Telegram-Mini-Apps/tma.js/tree/master/packages/init-data
4
+
5
+ [code-badge]: https://img.shields.io/badge/source-black?logo=github
6
+
7
+ [docs-link]: https://docs.telegram-mini-apps.com/docs/libraries/tma-js-init-data
8
+
9
+ [docs-badge]: https://img.shields.io/badge/documentation-blue?logo=gitbook&logoColor=white
10
+
11
+ [npm-link]: https://npmjs.com/package/@tma.js/init-data
12
+
13
+ [npm-badge]: https://img.shields.io/npm/v/@tma.js/init-data?logo=npm
14
+
15
+ [size-badge]: https://img.shields.io/bundlephobia/minzip/@tma.js/init-data
16
+
17
+
18
+ [![npm-badge]][npm-link]
19
+ ![size-badge]
20
+ [![docs-badge]][docs-link]
21
+ [![code-badge]][code-link]
22
+
23
+ Package providing utilities connected with Telegram Web Apps init data. It
24
+ allows init data parsing, works in browser only.
25
+
26
+ This library is a part of TypeScript packages ecosystem around Telegram Web
27
+ Apps. You can learn more about this package in this
28
+ [documentation](https://docs.telegram-mini-apps.com/docs/libraries/tma-js-init-data).
@@ -0,0 +1,2 @@
1
+ this["@tma"]=this["@tma"]||{},this["@tma"]["js/init-data"]=function(t){"use strict";class e{parser;isOptional;getDefault;isEmpty;static create(t){return new e(t,!1,void 0)}constructor(t,e,r,n=(t=>void 0===t)){this.parser=t,this.isOptional=e,this.getDefault=r,this.isEmpty=n}parse(t){return this.isOptional&&this.isEmpty(t)?this.getDefault?this.getDefault():void 0:this.parser(t)}optional(t){return this.isOptional=!0,t&&(this.isEmpty=t),this}default(t){return this.getDefault=t,this}}function r(t){return()=>e.create(t)}function n(t){return r((e=>{if(typeof e!==t)throw new TypeError(`Unable to parse value ${JSON.stringify(e)} as ${t}.`);return e}))}function o(t,e){const r={};for(const n in t){const o=t[n];if(!o)continue;let i,a,s,p;if("type"in o){const{from:t=n,type:e}=o;i=t,a="function"==typeof e?e:e.parse.bind(e),s="required"in o&&o.required}else i=n,s=!1,a="function"==typeof o?o:o.parse.bind(o);try{p=a(e(i))}catch(t){throw new Error(`Unable to parse field "${i}"`,{cause:t})}(void 0!==p||s)&&(r[n]=p)}return r}const i=n("string"),a=n("number"),s=n("boolean"),p=r((t=>{const e=i().parse(t);if(e){const t=Number(e),r=new Date(t.toString()===e?1e3*t:e);if("Invalid Date"!==r.toString())return r}throw new TypeError(`Unable to parse value "${t}" as Date.`)}));function u(t){return e.create((e=>{let r=e;if("string"==typeof r)try{r=JSON.parse(r)}catch(t){throw new TypeError("Value is not JSON object converted to string.")}if("object"!=typeof r||null===r||Array.isArray(r))throw new TypeError("Value is not JSON object.");return o(t,(t=>r[t]))}))}function l(t,e){return{from:e,type:t}}const c=u({firstName:l(i(),"first_name"),id:a(),isBot:l(s().optional(),"is_bot"),isPremium:l(s().optional(),"is_premium"),lastName:l(i().optional(),"last_name"),languageCode:l(i().optional(),"language_code"),photoUrl:l(i().optional(),"photo_url"),username:i().optional()}),f=(h={authDate:l(p(),"auth_date"),hash:i(),user:c.optional(),receiver:c.optional(),chat:u({id:a(),type:i(),title:i(),photoUrl:l(i().optional(),"photo_url"),username:i().optional()}).optional(),canSendAfter:l(p().optional(),"can_send_after"),queryId:l(i().optional(),"query_id"),startParam:l(i().optional(),"start_param")},e.create((t=>{if("string"!=typeof t&&!(t instanceof URLSearchParams))throw new TypeError("Value has not processable type.");const e="string"==typeof t?new URLSearchParams(t):t;return o(h,(t=>{const r=e.get(t);return null===r?void 0:r}))})));var h;return t.initData=f,t}({});
2
+ //# sourceMappingURL=browser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"browser.js","sources":["../../../parsing/dist/lib/index.mjs","../../src/parsing.ts"],"sourcesContent":["import{toRGB as t}from\"@tma.js/colors\";class r{parser;isOptional;getDefault;isEmpty;static create(t){return new r(t,!1,void 0)}constructor(t,r,e,n=(t=>void 0===t)){this.parser=t,this.isOptional=r,this.getDefault=e,this.isEmpty=n}parse(t){return this.isOptional&&this.isEmpty(t)?this.getDefault?this.getDefault():void 0:this.parser(t)}optional(t){return this.isOptional=!0,t&&(this.isEmpty=t),this}default(t){return this.getDefault=t,this}}function e(t){const r=\"function\"==typeof t?t:t.parse.bind(t);return t=>function(t){if(Array.isArray(t))return t;if(\"string\"==typeof t)try{const r=JSON.parse(t);if(Array.isArray(r))return r}catch(t){}throw new Error(`Passed value does not satisfy any of known formats: ${JSON.stringify(t)}`)}(t).map(r)}class n extends r{constructor(t,r,n,o=(t=>void 0===t)){super(e(t),r,n,o)}of(t){return this.parser=e(t),this}}function o(t){return()=>r.create(t)}function s(t){return o((r=>{if(typeof r!==t)throw new TypeError(`Unable to parse value ${JSON.stringify(r)} as ${t}.`);return r}))}function i(t,r){const e={};for(const n in t){const o=t[n];if(!o)continue;let s,i,a,u;if(\"type\"in o){const{from:t=n,type:r}=o;s=t,i=\"function\"==typeof r?r:r.parse.bind(r),a=\"required\"in o&&o.required}else s=n,a=!1,i=\"function\"==typeof o?o:o.parse.bind(o);try{u=i(r(s))}catch(t){throw new Error(`Unable to parse field \"${s}\"`,{cause:t})}(void 0!==u||a)&&(e[n]=u)}return e}const a=s(\"string\"),u=s(\"number\"),c=s(\"boolean\"),f=o((t=>{const r=a().parse(t);if(r){const t=Number(r),e=new Date(t.toString()===r?1e3*t:r);if(\"Invalid Date\"!==e.toString())return e}throw new TypeError(`Unable to parse value \"${t}\" as Date.`)})),p=o((r=>{const e=a().parse(r);try{return t(e)}catch(t){throw new TypeError(`Unable to parse value \"${e}\" as RGB.`,{cause:t})}}));function l(t){return r.create((r=>{let e=r;if(\"string\"==typeof e)try{e=JSON.parse(e)}catch(t){throw new TypeError(\"Value is not JSON object converted to string.\")}if(\"object\"!=typeof e||null===e||Array.isArray(e))throw new TypeError(\"Value is not JSON object.\");return i(t,(t=>e[t]))}))}function y(t){return r.create((r=>{if(\"string\"!=typeof r&&!(r instanceof URLSearchParams))throw new TypeError(\"Value has not processable type.\");const e=\"string\"==typeof r?new URLSearchParams(r):r;return i(t,(t=>{const r=e.get(t);return null===r?void 0:r}))}))}function h(){return new n((t=>t),!1,void 0)}export{n as ArrayValueParser,r as ValueParser,h as array,c as boolean,o as createParserGenerator,f as date,l as json,u as number,p as rgb,y as searchParams,a as string};\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 user = json<User>({\n firstName: field(string(), 'first_name'),\n id: number(),\n isBot: field(boolean().optional(), 'is_bot'),\n isPremium: field(boolean().optional(), 'is_premium'),\n lastName: field(string().optional(), 'last_name'),\n languageCode: field(string().optional(), 'language_code'),\n photoUrl: field(string().optional(), 'photo_url'),\n username: string().optional(),\n});\n\n/**\n * Parser used to parse init data, presented as search params.\n */\nexport const initData = searchParams<InitData>({\n authDate: field(date(), 'auth_date'),\n hash: string(),\n user: user.optional(),\n receiver: user.optional(),\n chat: json<InitData['chat']>({\n id: number(),\n type: string(),\n title: string(),\n photoUrl: field(string().optional(), 'photo_url'),\n username: string().optional(),\n }).optional(),\n canSendAfter: field(date().optional(), 'can_send_after'),\n queryId: field(string().optional(), 'query_id'),\n startParam: field(string().optional(), 'start_param'),\n});\n"],"names":["r","parser","isOptional","getDefault","isEmpty","static","t","constructor","e","n","this","parse","optional","default","o","create","s","TypeError","JSON","stringify","i","a","u","from","type","bind","required","Error","cause","c","f","Number","Date","toString","l","Array","isArray","field","user","json","firstName","string","id","number","isBot","boolean","isPremium","lastName","languageCode","photoUrl","username","initData","authDate","date","hash","receiver","chat","title","canSendAfter","queryId","startParam","URLSearchParams","get"],"mappings":"oFAAuC,MAAMA,EAAEC,OAAOC,WAAWC,WAAWC,QAAQC,cAAcC,GAAG,OAAO,IAAIN,EAAEM,GAAE,OAAG,EAAO,CAACC,YAAYD,EAAEN,EAAEQ,EAAEC,EAAG,CAAAH,QAAG,IAASA,IAAII,KAAKT,OAAOK,EAAEI,KAAKR,WAAWF,EAAEU,KAAKP,WAAWK,EAAEE,KAAKN,QAAQK,CAAC,CAACE,MAAML,GAAG,OAAOI,KAAKR,YAAYQ,KAAKN,QAAQE,GAAGI,KAAKP,WAAWO,KAAKP,kBAAa,EAAOO,KAAKT,OAAOK,EAAE,CAACM,SAASN,GAAG,OAAOI,KAAKR,YAAW,EAAGI,IAAII,KAAKN,QAAQE,GAAGI,IAAI,CAACG,QAAQP,GAAG,OAAOI,KAAKP,WAAWG,EAAEI,IAAI,EAA6Z,SAASI,EAAER,GAAG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E,GAAG,KAAlQ,IAAWM"}
@@ -0,0 +1,2 @@
1
+ "use strict";var t=require("@tma.js/parsing");function a(t,a){return{from:a,type:t}}const o=t.json({firstName:a(t.string(),"first_name"),id:t.number(),isBot:a(t.boolean().optional(),"is_bot"),isPremium:a(t.boolean().optional(),"is_premium"),lastName:a(t.string().optional(),"last_name"),languageCode:a(t.string().optional(),"language_code"),photoUrl:a(t.string().optional(),"photo_url"),username:t.string().optional()}),n=t.searchParams({authDate:a(t.date(),"auth_date"),hash:t.string(),user:o.optional(),receiver:o.optional(),chat:t.json({id:t.number(),type:t.string(),title:t.string(),photoUrl:a(t.string().optional(),"photo_url"),username:t.string().optional()}).optional(),canSendAfter:a(t.date().optional(),"can_send_after"),queryId:a(t.string().optional(),"query_id"),startParam:a(t.string().optional(),"start_param")});exports.initData=n;
2
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sources":["../../src/parsing.ts"],"sourcesContent":["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 user = json<User>({\n firstName: field(string(), 'first_name'),\n id: number(),\n isBot: field(boolean().optional(), 'is_bot'),\n isPremium: field(boolean().optional(), 'is_premium'),\n lastName: field(string().optional(), 'last_name'),\n languageCode: field(string().optional(), 'language_code'),\n photoUrl: field(string().optional(), 'photo_url'),\n username: string().optional(),\n});\n\n/**\n * Parser used to parse init data, presented as search params.\n */\nexport const initData = searchParams<InitData>({\n authDate: field(date(), 'auth_date'),\n hash: string(),\n user: user.optional(),\n receiver: user.optional(),\n chat: json<InitData['chat']>({\n id: number(),\n type: string(),\n title: string(),\n photoUrl: field(string().optional(), 'photo_url'),\n username: string().optional(),\n }).optional(),\n canSendAfter: field(date().optional(), 'can_send_after'),\n queryId: field(string().optional(), 'query_id'),\n startParam: field(string().optional(), 'start_param'),\n});\n"],"names":["field","parser","from","type","user","json","firstName","string","id","number","isBot","boolean","optional","isPremium","lastName","languageCode","photoUrl","username","initData","searchParams","authDate","date","hash","receiver","chat","title","canSendAfter","queryId","startParam"],"mappings":"8CAYA,SAASA,EAAgCC,EAAWC,GAClD,MAAO,CACLA,OACAC,KAAMF,EAEV,CAEA,MAAMG,EAAOC,EAAAA,KAAW,CACtBC,UAAWN,EAAMO,WAAU,cAC3BC,GAAIC,EAAAA,SACJC,MAAOV,EAAMW,EAAAA,UAAUC,WAAY,UACnCC,UAAWb,EAAMW,EAAAA,UAAUC,WAAY,cACvCE,SAAUd,EAAMO,EAAAA,SAASK,WAAY,aACrCG,aAAcf,EAAMO,EAAAA,SAASK,WAAY,iBACzCI,SAAUhB,EAAMO,EAAAA,SAASK,WAAY,aACrCK,SAAUV,EAAAA,SAASK,aAMRM,EAAWC,EAAAA,aAAuB,CAC7CC,SAAUpB,EAAMqB,SAAQ,aACxBC,KAAMf,EAAAA,SACNH,KAAMA,EAAKQ,WACXW,SAAUnB,EAAKQ,WACfY,KAAMnB,EAAAA,KAAuB,CAC3BG,GAAIC,EAAAA,SACJN,KAAMI,EAAAA,SACNkB,MAAOlB,EAAAA,SACPS,SAAUhB,EAAMO,EAAAA,SAASK,WAAY,aACrCK,SAAUV,EAAAA,SAASK,aAClBA,WACHc,aAAc1B,EAAMqB,EAAAA,OAAOT,WAAY,kBACvCe,QAAS3B,EAAMO,EAAAA,SAASK,WAAY,YACpCgB,WAAY5B,EAAMO,EAAAA,SAASK,WAAY"}
@@ -0,0 +1,2 @@
1
+ import{json as o,string as t,number as a,boolean as e,searchParams as i,date as n}from"@tma.js/parsing";function r(o,t){return{from:t,type:o}}const p=o({firstName:r(t(),"first_name"),id:a(),isBot:r(e().optional(),"is_bot"),isPremium:r(e().optional(),"is_premium"),lastName:r(t().optional(),"last_name"),languageCode:r(t().optional(),"language_code"),photoUrl:r(t().optional(),"photo_url"),username:t().optional()}),l=i({authDate:r(n(),"auth_date"),hash:t(),user:p.optional(),receiver:p.optional(),chat:o({id:a(),type:t(),title:t(),photoUrl:r(t().optional(),"photo_url"),username:t().optional()}).optional(),canSendAfter:r(n().optional(),"can_send_after"),queryId:r(t().optional(),"query_id"),startParam:r(t().optional(),"start_param")});export{l as initData};
2
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sources":["../../src/parsing.ts"],"sourcesContent":["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 user = json<User>({\n firstName: field(string(), 'first_name'),\n id: number(),\n isBot: field(boolean().optional(), 'is_bot'),\n isPremium: field(boolean().optional(), 'is_premium'),\n lastName: field(string().optional(), 'last_name'),\n languageCode: field(string().optional(), 'language_code'),\n photoUrl: field(string().optional(), 'photo_url'),\n username: string().optional(),\n});\n\n/**\n * Parser used to parse init data, presented as search params.\n */\nexport const initData = searchParams<InitData>({\n authDate: field(date(), 'auth_date'),\n hash: string(),\n user: user.optional(),\n receiver: user.optional(),\n chat: json<InitData['chat']>({\n id: number(),\n type: string(),\n title: string(),\n photoUrl: field(string().optional(), 'photo_url'),\n username: string().optional(),\n }).optional(),\n canSendAfter: field(date().optional(), 'can_send_after'),\n queryId: field(string().optional(), 'query_id'),\n startParam: field(string().optional(), 'start_param'),\n});\n"],"names":["field","parser","from","type","user","json","firstName","string","id","number","isBot","boolean","optional","isPremium","lastName","languageCode","photoUrl","username","initData","searchParams","authDate","date","hash","receiver","chat","title","canSendAfter","queryId","startParam"],"mappings":"wGAYA,SAASA,EAAgCC,EAAWC,GAClD,MAAO,CACLA,OACAC,KAAMF,EAEV,CAEA,MAAMG,EAAOC,EAAW,CACtBC,UAAWN,EAAMO,IAAU,cAC3BC,GAAIC,IACJC,MAAOV,EAAMW,IAAUC,WAAY,UACnCC,UAAWb,EAAMW,IAAUC,WAAY,cACvCE,SAAUd,EAAMO,IAASK,WAAY,aACrCG,aAAcf,EAAMO,IAASK,WAAY,iBACzCI,SAAUhB,EAAMO,IAASK,WAAY,aACrCK,SAAUV,IAASK,aAMRM,EAAWC,EAAuB,CAC7CC,SAAUpB,EAAMqB,IAAQ,aACxBC,KAAMf,IACNH,KAAMA,EAAKQ,WACXW,SAAUnB,EAAKQ,WACfY,KAAMnB,EAAuB,CAC3BG,GAAIC,IACJN,KAAMI,IACNkB,MAAOlB,IACPS,SAAUhB,EAAMO,IAASK,WAAY,aACrCK,SAAUV,IAASK,aAClBA,WACHc,aAAc1B,EAAMqB,IAAOT,WAAY,kBACvCe,QAAS3B,EAAMO,IAASK,WAAY,YACpCgB,WAAY5B,EAAMO,IAASK,WAAY"}
@@ -0,0 +1,2 @@
1
+ export * from './parsing.js';
2
+ export * from './types.js';
@@ -0,0 +1,5 @@
1
+ import type { InitData } from './types.js';
2
+ /**
3
+ * Parser used to parse init data, presented as search params.
4
+ */
5
+ export declare const initData: import("@tma.js/parsing").ValueParser<InitData, false, undefined>;
@@ -0,0 +1,120 @@
1
+ /**
2
+ * Describes user information.
3
+ * @see https://docs.telegram-mini-apps.com/docs/launch-params/init-data#user
4
+ */
5
+ export interface User {
6
+ /**
7
+ * First name of the user or bot.
8
+ */
9
+ firstName: string;
10
+ /**
11
+ * A unique identifier for the user or bot.
12
+ */
13
+ id: number;
14
+ /**
15
+ * True, if this user is a bot. Returned in the `receiver` field only.
16
+ * @see InitData.receiver
17
+ */
18
+ isBot?: boolean;
19
+ /**
20
+ * True, if this user is a Telegram Premium user.
21
+ */
22
+ isPremium?: boolean;
23
+ /**
24
+ * Last name of the user or bot.
25
+ */
26
+ lastName?: string;
27
+ /**
28
+ * IETF language tag of the user's language. Returns in user field only.
29
+ * TODO: Specify expected values.
30
+ * @see https://en.wikipedia.org/wiki/IETF_language_tag
31
+ */
32
+ languageCode?: string;
33
+ /**
34
+ * URL of the user’s profile photo. The photo can be in .jpeg or .svg
35
+ * formats. Only returned for Web Apps launched from the attachment menu.
36
+ */
37
+ photoUrl?: string;
38
+ /**
39
+ * Username of the user or bot.
40
+ */
41
+ username?: string;
42
+ }
43
+ /**
44
+ * Describes chat information.
45
+ * @see https://docs.telegram-mini-apps.com/docs/launch-params/init-data#chat
46
+ */
47
+ export interface Chat {
48
+ /**
49
+ * Unique identifier for this chat.
50
+ */
51
+ id: number;
52
+ /**
53
+ * URL of the chat’s photo. The photo can be in .jpeg or .svg formats.
54
+ * Only returned for Web Apps launched from the attachment menu.
55
+ */
56
+ photoUrl?: string;
57
+ /**
58
+ * Type of chat.
59
+ */
60
+ type: 'group' | 'supergroup' | 'channel' | string;
61
+ /**
62
+ * Title of the chat.
63
+ */
64
+ title: string;
65
+ /**
66
+ * Username of the chat.
67
+ */
68
+ username?: string;
69
+ }
70
+ /**
71
+ * Describes parsed initial data sent from Web App.
72
+ * @see https://docs.telegram-mini-apps.com/docs/launch-params/init-data#parameters-list
73
+ */
74
+ export interface InitData {
75
+ /**
76
+ * Init data generation date.
77
+ */
78
+ authDate: Date;
79
+ /**
80
+ * Date after which a message can be sent via the answerWebAppQuery method.
81
+ * @see https://core.telegram.org/bots/api#answerwebappquery
82
+ */
83
+ canSendAfter?: Date;
84
+ /**
85
+ * An object containing data about the chat where the bot was launched via
86
+ * the attachment menu. Returned for supergroups, channels and group
87
+ * chats – only for Web Apps launched via the attachment menu.
88
+ */
89
+ chat?: Chat;
90
+ /**
91
+ * A hash of all passed parameters, which the bot server can use to
92
+ * check their validity.
93
+ *
94
+ * @see https://core.telegram.org/bots/webapps#validating-data-received-via-the-web-app
95
+ */
96
+ hash: string;
97
+ /**
98
+ * A unique identifier for the Web App session, required for sending
99
+ * messages via the answerWebAppQuery method.
100
+ *
101
+ * @see https://core.telegram.org/bots/api#answerwebappquery
102
+ */
103
+ queryId?: string;
104
+ /**
105
+ * An object containing data about the chat partner of the current user in
106
+ * the chat where the bot was launched via the attachment menu.
107
+ * Returned only for private chats and only for Web Apps launched
108
+ * via the attachment menu.
109
+ */
110
+ receiver?: User;
111
+ /**
112
+ * The value of the startattach parameter, passed via link. Only returned for
113
+ * Web Apps when launched from the attachment menu via link.
114
+ */
115
+ startParam?: string;
116
+ /**
117
+ * An object containing data about the current user.
118
+ */
119
+ user?: User;
120
+ }
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@tma.js/init-data",
3
+ "version": "0.2.14",
4
+ "description": "Frontend TypeScript library to make work with Web Apps init data easier.",
5
+ "author": "Vladislav Kibenko <wolfram.deus@gmail.com>",
6
+ "homepage": "https://github.com/Telegram-Mini-Apps/tma.js#readme",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git@github.com:Telegram-Mini-Apps/tma.js.git",
10
+ "directory": "packages/init-data"
11
+ },
12
+ "bugs": {
13
+ "url": "https://github.com/Telegram-Mini-Apps/tma.js/issues"
14
+ },
15
+ "keywords": [
16
+ "telegram-mini-apps",
17
+ "init-data",
18
+ "typescript"
19
+ ],
20
+ "license": "MIT",
21
+ "type": "module",
22
+ "sideEffects": false,
23
+ "files": [
24
+ "dist",
25
+ "src"
26
+ ],
27
+ "main": "dist/lib/index.cjs",
28
+ "browser": "dist/lib/browser.js",
29
+ "module": "dist/lib/index.mjs",
30
+ "types": "dist/types/index.d.ts",
31
+ "exports": {
32
+ ".": {
33
+ "types": "./dist/types/index.d.ts",
34
+ "import": "./dist/lib/index.mjs",
35
+ "require": "./dist/lib/index.cjs",
36
+ "default": "./dist/lib/index.cjs"
37
+ },
38
+ "./*": {
39
+ "types": "./dist/types/*.d.ts",
40
+ "import": "./dist/lib/*.mjs",
41
+ "require": "./dist/lib/*.cjs",
42
+ "default": "./dist/lib/*.cjs"
43
+ }
44
+ },
45
+ "dependencies": {
46
+ "@tma.js/parsing": "0.0.2"
47
+ },
48
+ "devDependencies": {
49
+ "tsconfig": "0.0.2",
50
+ "eslint-config-custom": "0.1.0",
51
+ "jest-config-custom": "0.1.0"
52
+ },
53
+ "publishConfig": {
54
+ "access": "public"
55
+ },
56
+ "scripts": {
57
+ "test": "jest",
58
+ "lint": "eslint -c .eslintrc.cjs src/**/* __tests__/**/*",
59
+ "build": "rimraf dist && rollup --config rollup.config.js"
60
+ }
61
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './parsing.js';
2
+ export * from './types.js';
package/src/parsing.ts ADDED
@@ -0,0 +1,49 @@
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 user = json<User>({
21
+ firstName: field(string(), 'first_name'),
22
+ id: number(),
23
+ isBot: field(boolean().optional(), 'is_bot'),
24
+ isPremium: field(boolean().optional(), 'is_premium'),
25
+ lastName: field(string().optional(), 'last_name'),
26
+ languageCode: field(string().optional(), 'language_code'),
27
+ photoUrl: field(string().optional(), 'photo_url'),
28
+ username: string().optional(),
29
+ });
30
+
31
+ /**
32
+ * Parser used to parse init data, presented as search params.
33
+ */
34
+ export const initData = searchParams<InitData>({
35
+ authDate: field(date(), 'auth_date'),
36
+ hash: string(),
37
+ user: user.optional(),
38
+ receiver: user.optional(),
39
+ chat: json<InitData['chat']>({
40
+ id: number(),
41
+ type: string(),
42
+ title: string(),
43
+ photoUrl: field(string().optional(), 'photo_url'),
44
+ username: string().optional(),
45
+ }).optional(),
46
+ canSendAfter: field(date().optional(), 'can_send_after'),
47
+ queryId: field(string().optional(), 'query_id'),
48
+ startParam: field(string().optional(), 'start_param'),
49
+ });
package/src/types.ts ADDED
@@ -0,0 +1,140 @@
1
+ /**
2
+ * Describes user information.
3
+ * @see https://docs.telegram-mini-apps.com/docs/launch-params/init-data#user
4
+ */
5
+ export interface User {
6
+ /**
7
+ * First name of the user or bot.
8
+ */
9
+ firstName: string;
10
+
11
+ /**
12
+ * A unique identifier for the user or bot.
13
+ */
14
+ id: number;
15
+
16
+ /**
17
+ * True, if this user is a bot. Returned in the `receiver` field only.
18
+ * @see InitData.receiver
19
+ */
20
+ isBot?: boolean;
21
+
22
+ /**
23
+ * True, if this user is a Telegram Premium user.
24
+ */
25
+ isPremium?: boolean;
26
+
27
+ /**
28
+ * Last name of the user or bot.
29
+ */
30
+ lastName?: string;
31
+
32
+ /**
33
+ * IETF language tag of the user's language. Returns in user field only.
34
+ * TODO: Specify expected values.
35
+ * @see https://en.wikipedia.org/wiki/IETF_language_tag
36
+ */
37
+ languageCode?: string;
38
+
39
+ /**
40
+ * URL of the user’s profile photo. The photo can be in .jpeg or .svg
41
+ * formats. Only returned for Web Apps launched from the attachment menu.
42
+ */
43
+ photoUrl?: string;
44
+
45
+ /**
46
+ * Username of the user or bot.
47
+ */
48
+ username?: string;
49
+ }
50
+
51
+ /**
52
+ * Describes chat information.
53
+ * @see https://docs.telegram-mini-apps.com/docs/launch-params/init-data#chat
54
+ */
55
+ export interface Chat {
56
+ /**
57
+ * Unique identifier for this chat.
58
+ */
59
+ id: number;
60
+
61
+ /**
62
+ * URL of the chat’s photo. The photo can be in .jpeg or .svg formats.
63
+ * Only returned for Web Apps launched from the attachment menu.
64
+ */
65
+ photoUrl?: string;
66
+
67
+ /**
68
+ * Type of chat.
69
+ */
70
+ type: 'group' | 'supergroup' | 'channel' | string;
71
+
72
+ /**
73
+ * Title of the chat.
74
+ */
75
+ title: string;
76
+
77
+ /**
78
+ * Username of the chat.
79
+ */
80
+ username?: string;
81
+ }
82
+
83
+ /**
84
+ * Describes parsed initial data sent from Web App.
85
+ * @see https://docs.telegram-mini-apps.com/docs/launch-params/init-data#parameters-list
86
+ */
87
+ export interface InitData {
88
+ /**
89
+ * Init data generation date.
90
+ */
91
+ authDate: Date;
92
+
93
+ /**
94
+ * Date after which a message can be sent via the answerWebAppQuery method.
95
+ * @see https://core.telegram.org/bots/api#answerwebappquery
96
+ */
97
+ canSendAfter?: Date;
98
+
99
+ /**
100
+ * An object containing data about the chat where the bot was launched via
101
+ * the attachment menu. Returned for supergroups, channels and group
102
+ * chats – only for Web Apps launched via the attachment menu.
103
+ */
104
+ chat?: Chat;
105
+
106
+ /**
107
+ * A hash of all passed parameters, which the bot server can use to
108
+ * check their validity.
109
+ *
110
+ * @see https://core.telegram.org/bots/webapps#validating-data-received-via-the-web-app
111
+ */
112
+ hash: string;
113
+
114
+ /**
115
+ * A unique identifier for the Web App session, required for sending
116
+ * messages via the answerWebAppQuery method.
117
+ *
118
+ * @see https://core.telegram.org/bots/api#answerwebappquery
119
+ */
120
+ queryId?: string;
121
+
122
+ /**
123
+ * An object containing data about the chat partner of the current user in
124
+ * the chat where the bot was launched via the attachment menu.
125
+ * Returned only for private chats and only for Web Apps launched
126
+ * via the attachment menu.
127
+ */
128
+ receiver?: User;
129
+
130
+ /**
131
+ * The value of the startattach parameter, passed via link. Only returned for
132
+ * Web Apps when launched from the attachment menu via link.
133
+ */
134
+ startParam?: string;
135
+
136
+ /**
137
+ * An object containing data about the current user.
138
+ */
139
+ user?: User;
140
+ }