@telegram-apps/bridge
Advanced tools
Comparing version 1.7.0 to 1.7.1
@@ -1,2 +0,2 @@ | ||
this.tapps=this.tapps||{};this.tapps.bridge=function(s){"use strict";class d extends Error{constructor(t,n,r){super(typeof n=="object"?n.message:n||t,{cause:typeof n=="object"?n.cause:r}),this.type=t,Object.setPrototypeOf(this,d.prototype)}}function Q(e){return e.replace(/[A-Z]/g,t=>`_${t.toLowerCase()}`)}function Ne(e){return e.replace(/_[a-z]/g,t=>t[1].toUpperCase())}const De="ERR_INVALID_VALUE",Ce="ERR_UNEXPECTED_VALUE",Ue="ERR_UNEXPECTED_TYPE",x="ERR_PARSE";function ee(e,t){const n={};for(const r in e){const o=e[r];if(!o)continue;let a,i;typeof o=="function"?(a=r,i=o):[a,i]=o;try{const c=i(t(a));c!==void 0&&(n[r]=c)}catch(c){throw new d(x,`Parser for "${r}" property failed${a===r?"":`. Source field: "${a}"`}`,c)}}return n}function te(e){let t=e;if(typeof t=="string")try{t=JSON.parse(t)}catch(n){throw new d(De,{cause:n})}if(typeof t!="object"||!t||Array.isArray(t))throw new d(Ce);return t}function g(e,t){return n=>{const r=o=>{if(!(n&&o===void 0))try{return t(o)}catch(a){throw new d(x,{message:`"${e}" transformer failed to parse the value`,cause:a})}};return Object.assign(r,{isValid(o){try{return r(o),!0}catch{return!1}}})}}function w(e,t){return g(t||"object",n=>{const r=te(n);return ee(e,o=>r[o])})}function R(e){throw new d(Ue,`Unexpected value received: ${JSON.stringify(e)}`)}const A=g("boolean",e=>{if(typeof e=="boolean")return e;const t=String(e);if(t==="1"||t==="true")return!0;if(t==="0"||t==="false")return!1;R(e)}),l=g("string",e=>{if(typeof e=="string"||typeof e=="number")return e.toString();R(e)}),P=g("number",e=>{if(typeof e=="number")return e;if(typeof e=="string"){const t=Number(e);if(!Number.isNaN(t))return t}R(e)}),Oe=g("date",e=>e instanceof Date?e:new Date(P()(e)*1e3));function ne(e,t){return g(t||"searchParams",n=>{typeof n!="string"&&!(n instanceof URLSearchParams)&&R(n);const r=typeof n=="string"?new URLSearchParams(n):n;return ee(e,o=>{const a=r.get(o);return a===null?void 0:a})})}function W(e){for(const t in e)e[t]=[Q(t),e[t]];return e}const je=e=>{const t=P(),n=P(!0),r=l(),o=l(!0),a=A(!0),i=w(W({addedToAttachmentMenu:a,allowsWriteToPm:a,firstName:r,id:t,isBot:a,isPremium:a,languageCode:o,lastName:o,photoUrl:o,username:o}),"User")(!0);return ne(W({authDate:Oe(),canSendAfter:n,chat:w(W({id:t,type:r,title:r,photoUrl:o,username:o}),"Chat")(!0),chatInstance:o,chatType:o,hash:r,queryId:o,receiver:i,startParam:o,user:i}),"initData")(e)};function We(e){return/^#[\da-f]{6}$/i.test(e)}function ke(e){return/^#[\da-f]{3}$/i.test(e)}function Me(e){const t=e.replace(/\s/g,"").toLowerCase();if(We(t))return t;if(ke(t)){let r="#";for(let o=0;o<3;o+=1)r+=t[1+o].repeat(2);return r}const n=t.match(/^rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)$/)||t.match(/^rgba\((\d{1,3}),(\d{1,3}),(\d{1,3}),\d{1,3}\)$/);if(!n)throw new Error(`Value "${e}" does not satisfy any of known RGB formats.`);return n.slice(1).reduce((r,o)=>{const a=parseInt(o,10).toString(16);return r+(a.length===1?"0":"")+a},"#")}const Le=g("rgb",e=>Me(l()(e))),re=g("themeParams",e=>{const t=Le(!0);return Object.entries(te(e)).reduce((n,[r,o])=>(n[Ne(r)]=t(o),n),{})});function k(e){return JSON.stringify(Object.fromEntries(Object.entries(e).map(([t,n])=>[Q(t),n])))}const qe=e=>{const t=l(),n=l(!0),r=A(!0);return ne({botInline:["tgWebAppBotInline",r],defaultColors:["tgWebAppDefaultColors",re(!0)],fullscreen:["tgWebAppFullscreen",r],initData:["tgWebAppData",je(!0)],initDataRaw:["tgWebAppData",n],platform:["tgWebAppPlatform",t],showSettings:["tgWebAppShowSettings",r],startParam:["tgWebAppStartParam",n],themeParams:["tgWebAppThemeParams",re()],version:["tgWebAppVersion",t]},"launchParams")(e)};function Ie(e){const{initDataRaw:t,startParam:n,showSettings:r,botInline:o,fullscreen:a,defaultColors:i}=e,c=new URLSearchParams;return c.set("tgWebAppPlatform",e.platform),c.set("tgWebAppThemeParams",k(e.themeParams)),c.set("tgWebAppVersion",e.version),t&&c.set("tgWebAppData",t),n&&c.set("tgWebAppStartParam",n),typeof r=="boolean"&&c.set("tgWebAppShowSettings",r?"1":"0"),typeof o=="boolean"&&c.set("tgWebAppBotInline",o?"1":"0"),typeof a=="boolean"&&c.set("tgWebAppFullscreen",a?"1":"0"),i&&c.set("tgWebAppDefaultColors",k(i)),c.toString()}const oe=w({eventType:l(),eventData:e=>e},"miniAppsMessage"),ae=g("fn",e=>{if(typeof e=="function")return e;R(e)});function Fe(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}const He=w({TelegramWebviewProxy:w({postEvent:ae()})()});function M(e){return He().isValid(e)}function se(){try{return window.self!==window.top}catch{return!0}}var Je=Object.defineProperty,Be=(e,t,n)=>t in e?Je(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,ie=(e,t,n)=>Be(e,typeof t!="symbol"?t+"":t,n);class b extends Error{constructor(t,n,r){super(typeof n=="object"?n.message:n||t,{cause:typeof n=="object"?n.cause:r}),this.type=t,Object.setPrototypeOf(this,b.prototype)}}function L(e,t,n){return e.addEventListener(t,n),()=>e.removeEventListener(t,n)}function $(...e){const t=e.flat(1);return[t.push.bind(t),()=>{t.forEach(n=>{n()})}]}function Ge(e,t){return e instanceof b&&e.type===t}function N(e){return t=>Ge(t,e)}const q="ERR_ABORTED",I="ERR_CANCELED",F="ERR_TIMED_OUT";function H(e){return new b(q,{cause:e})}const Ve=N(F),Ke=N(q),Ze=N(I);function ce(e,t){return e.reject=t.reject,e}class m extends Promise{constructor(t,n){let r,o;typeof t=="function"?(r=t,o=n):o=t;let a,i;super((c,u)=>{o||(o={});const{abortSignal:f}=o;if(f&&f.aborted)return u(H(f.reason));const[p,h]=$(),E=v=>(...vt)=>(h(),v(...vt)),O=new AbortController,{signal:j}=O;i=E(v=>{O.abort(v),u(v)}),a=E(c),f&&p(L(f,"abort",()=>{i(H(f.reason))}));const{timeout:Y}=o;if(Y){const v=setTimeout(()=>{i(new b(F,`Timeout reached: ${Y}ms`))},Y);p(()=>{clearTimeout(v)})}r&&r(a,i,j)}),ie(this,"reject"),this.reject=i}static withFn(t,n){return new m((r,o,a)=>{try{const i=t(a);return i instanceof Promise?i.then(r,o):r(i)}catch(i){o(i)}},n)}static resolve(t){return new m(n=>{n(t)})}static reject(t){return new m((n,r)=>{r(t)})}cancel(){this.reject(new b(I))}catch(t){return this.then(void 0,t)}finally(t){return ce(super.finally(t),this)}then(t,n){return ce(super.then(t,n),this)}}function ue(e,t){return e.resolve=t.resolve,e}class T extends m{constructor(t,n){let r,o;typeof t=="function"?(r=t,o=n):o=t;let a;super((i,c,u)=>{a=i,r&&r(i,c,u)},o),ie(this,"resolve"),this.resolve=a}static withFn(t,n){return new T((r,o,a)=>m.withFn(t,{abortSignal:a}).then(r,o),n)}static resolve(t){return new T(n=>{n(t)})}static reject(t){return new T((n,r)=>{r(t)})}catch(t){return this.then(void 0,t)}finally(t){return ue(super.finally(t),this)}then(t,n){return ue(super.then(t,n),this)}}function ze(e,t){return new m(n=>{setTimeout(n,e)},{abortSignal:t})}function fe(e){return`tapps/${e}`}function _e(e,t){sessionStorage.setItem(fe(e),JSON.stringify(t))}function pe(e){const t=sessionStorage.getItem(fe(e));try{return t?JSON.parse(t):void 0}catch{}}function Xe(e){return e.replace(/[A-Z]/g,t=>`-${t.toLowerCase()}`)}function Ye(e){return e.replace(/[A-Z]/g,t=>`_${t.toLowerCase()}`)}function Qe(e){return e.replace(/_[a-z]/g,t=>t[1].toUpperCase())}function le(e,t){t||(t={});const{textColor:n,bgColor:r,shouldLog:o=!0}=t;function a(i,...c){if(!o||typeof o=="function"&&!o())return;const u="font-weight:bold;padding:0 5px;border-radius:5px";console[i](`%c${Intl.DateTimeFormat("en-GB",{hour:"2-digit",minute:"2-digit",second:"2-digit",fractionalSecondDigits:3,timeZone:"UTC"}).format(new Date)}%c / %c${e}`,`${u};background-color: lightblue;color:black`,"",`${u};${n?`color:${n};`:""}${r?`background-color:${r}`:""}`,...c)}return[function(...i){a("log",...i)},function(...i){a("error",...i)}]}function xe(e,t){document.documentElement.style.setProperty(e,t)}function et(e){document.documentElement.style.removeProperty(e)}function tt(e,t){t()}function y(e,t){t||(t={});const n=t.equals||Object.is;let r=[],o=e;const a=f=>{if(!n(o,f)){const p=o;o=f,tt(u,()=>{[...r].forEach(([h,E])=>{h(f,p),E&&c(h,!0)})})}};function i(f){const p=typeof f!="object"?{once:f}:f;return{once:p.once||!1,signal:p.signal||!1}}const c=(f,p)=>{const h=i(p),E=r.findIndex(([O,j])=>O===f&&j.once===h.once&&j.signal===h.signal);E>=0&&r.splice(E,1)},u=Object.assign(function(){return nt(u),o},{destroy(){r=[]},set:a,reset(){a(e)},sub(f,p){return r.push([f,i(p)]),()=>c(f,p)},unsub:c,unsubAll(){r=r.filter(f=>f[1].signal)}});return u}const J=[];function nt(e){J.length&&J[J.length-1].add(e)}const B=y(!1),[G,rt]=le("Bridge",{bgColor:"#9147ff",textColor:"white",shouldLog:B}),ot={clipboard_text_received:w({req_id:l(),data:e=>e===null?e:l(!0)(e)},"clipboard_text_received"),custom_method_invoked:w({req_id:l(),result:e=>e,error:l(!0)},"custom_method_invoked"),popup_closed:g("popup_closed",e=>e?w({button_id:t=>t==null?void 0:l()(t)})()(e):{}),viewport_changed:w({height:P(),width:e=>e==null?window.innerWidth:P()(e),is_state_stable:A(),is_expanded:A()},"viewport_changed")};function at(e){const t=window,[,n]=$(L(t,"message",r=>{if(r.source!==t.parent)return;let o;try{o=oe()(r.data)}catch{return}const{eventType:a,eventData:i}=o,c=ot[a];try{const u=c?c()(i):i;G("Event received:",u?{eventType:a,eventData:u}:{eventType:a}),e([a,u])}catch(u){rt([`An error occurred processing the "${a}" event from the Telegram application.`,"Please, file an issue here:","https://github.com/Telegram-Mini-Apps/telegram-apps/issues/new/choose"].join(` | ||
this.tapps=this.tapps||{};this.tapps.bridge=function(s){"use strict";class d extends Error{constructor(t,n,r){super(typeof n=="object"?n.message:n||t,{cause:typeof n=="object"?n.cause:r}),this.type=t,Object.setPrototypeOf(this,d.prototype)}}function Q(e){return e.replace(/[A-Z]/g,t=>`_${t.toLowerCase()}`)}function Ne(e){return e.replace(/_[a-z]/g,t=>t[1].toUpperCase())}const De="ERR_INVALID_VALUE",Ce="ERR_UNEXPECTED_VALUE",Ue="ERR_UNEXPECTED_TYPE",x="ERR_PARSE";function ee(e,t){const n={};for(const r in e){const o=e[r];if(!o)continue;let a,i;typeof o=="function"?(a=r,i=o):[a,i]=o;try{const c=i(t(a));c!==void 0&&(n[r]=c)}catch(c){throw new d(x,`Parser for "${r}" property failed${a===r?"":`. Source field: "${a}"`}`,c)}}return n}function te(e){let t=e;if(typeof t=="string")try{t=JSON.parse(t)}catch(n){throw new d(De,{cause:n})}if(typeof t!="object"||!t||Array.isArray(t))throw new d(Ce);return t}function g(e,t){return n=>{const r=o=>{if(!(n&&o===void 0))try{return t(o)}catch(a){throw new d(x,{message:`"${e}" transformer failed to parse the value`,cause:a})}};return Object.assign(r,{isValid(o){try{return r(o),!0}catch{return!1}}})}}function w(e,t){return g(t||"object",n=>{const r=te(n);return ee(e,o=>r[o])})}function R(e){throw new d(Ue,`Unexpected value received: ${JSON.stringify(e)}`)}const A=g("boolean",e=>{if(typeof e=="boolean")return e;const t=String(e);if(t==="1"||t==="true")return!0;if(t==="0"||t==="false")return!1;R(e)}),l=g("string",e=>{if(typeof e=="string"||typeof e=="number")return e.toString();R(e)}),P=g("number",e=>{if(typeof e=="number")return e;if(typeof e=="string"){const t=Number(e);if(!Number.isNaN(t))return t}R(e)}),Oe=g("date",e=>e instanceof Date?e:new Date(P()(e)*1e3));function ne(e,t){return g(t||"searchParams",n=>{typeof n!="string"&&!(n instanceof URLSearchParams)&&R(n);const r=typeof n=="string"?new URLSearchParams(n):n;return ee(e,o=>{const a=r.get(o);return a===null?void 0:a})})}function W(e){for(const t in e)e[t]=[Q(t),e[t]];return e}const je=e=>{const t=P(),n=P(!0),r=l(),o=l(!0),a=A(!0),i=w(W({addedToAttachmentMenu:a,allowsWriteToPm:a,firstName:r,id:t,isBot:a,isPremium:a,languageCode:o,lastName:o,photoUrl:o,username:o}),"User")(!0);return ne(W({authDate:Oe(),canSendAfter:n,chat:w(W({id:t,type:r,title:r,photoUrl:o,username:o}),"Chat")(!0),chatInstance:o,chatType:o,hash:r,queryId:o,receiver:i,startParam:o,signature:r,user:i}),"initData")(e)};function We(e){return/^#[\da-f]{6}$/i.test(e)}function ke(e){return/^#[\da-f]{3}$/i.test(e)}function Me(e){const t=e.replace(/\s/g,"").toLowerCase();if(We(t))return t;if(ke(t)){let r="#";for(let o=0;o<3;o+=1)r+=t[1+o].repeat(2);return r}const n=t.match(/^rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)$/)||t.match(/^rgba\((\d{1,3}),(\d{1,3}),(\d{1,3}),\d{1,3}\)$/);if(!n)throw new Error(`Value "${e}" does not satisfy any of known RGB formats.`);return n.slice(1).reduce((r,o)=>{const a=parseInt(o,10).toString(16);return r+(a.length===1?"0":"")+a},"#")}const Le=g("rgb",e=>Me(l()(e))),re=g("themeParams",e=>{const t=Le(!0);return Object.entries(te(e)).reduce((n,[r,o])=>(n[Ne(r)]=t(o),n),{})});function k(e){return JSON.stringify(Object.fromEntries(Object.entries(e).map(([t,n])=>[Q(t),n])))}const qe=e=>{const t=l(),n=l(!0),r=A(!0);return ne({botInline:["tgWebAppBotInline",r],defaultColors:["tgWebAppDefaultColors",re(!0)],fullscreen:["tgWebAppFullscreen",r],initData:["tgWebAppData",je(!0)],initDataRaw:["tgWebAppData",n],platform:["tgWebAppPlatform",t],showSettings:["tgWebAppShowSettings",r],startParam:["tgWebAppStartParam",n],themeParams:["tgWebAppThemeParams",re()],version:["tgWebAppVersion",t]},"launchParams")(e)};function Ie(e){const{initDataRaw:t,startParam:n,showSettings:r,botInline:o,fullscreen:a,defaultColors:i}=e,c=new URLSearchParams;return c.set("tgWebAppPlatform",e.platform),c.set("tgWebAppThemeParams",k(e.themeParams)),c.set("tgWebAppVersion",e.version),t&&c.set("tgWebAppData",t),n&&c.set("tgWebAppStartParam",n),typeof r=="boolean"&&c.set("tgWebAppShowSettings",r?"1":"0"),typeof o=="boolean"&&c.set("tgWebAppBotInline",o?"1":"0"),typeof a=="boolean"&&c.set("tgWebAppFullscreen",a?"1":"0"),i&&c.set("tgWebAppDefaultColors",k(i)),c.toString()}const oe=w({eventType:l(),eventData:e=>e},"miniAppsMessage"),ae=g("fn",e=>{if(typeof e=="function")return e;R(e)});function Fe(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}const He=w({TelegramWebviewProxy:w({postEvent:ae()})()});function M(e){return He().isValid(e)}function se(){try{return window.self!==window.top}catch{return!0}}var Je=Object.defineProperty,Be=(e,t,n)=>t in e?Je(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,ie=(e,t,n)=>Be(e,typeof t!="symbol"?t+"":t,n);class b extends Error{constructor(t,n,r){super(typeof n=="object"?n.message:n||t,{cause:typeof n=="object"?n.cause:r}),this.type=t,Object.setPrototypeOf(this,b.prototype)}}function L(e,t,n){return e.addEventListener(t,n),()=>e.removeEventListener(t,n)}function $(...e){const t=e.flat(1);return[t.push.bind(t),()=>{t.forEach(n=>{n()})}]}function Ge(e,t){return e instanceof b&&e.type===t}function N(e){return t=>Ge(t,e)}const q="ERR_ABORTED",I="ERR_CANCELED",F="ERR_TIMED_OUT";function H(e){return new b(q,{cause:e})}const Ve=N(F),Ke=N(q),Ze=N(I);function ce(e,t){return e.reject=t.reject,e}class m extends Promise{constructor(t,n){let r,o;typeof t=="function"?(r=t,o=n):o=t;let a,i;super((c,u)=>{o||(o={});const{abortSignal:f}=o;if(f&&f.aborted)return u(H(f.reason));const[p,h]=$(),E=v=>(...vt)=>(h(),v(...vt)),O=new AbortController,{signal:j}=O;i=E(v=>{O.abort(v),u(v)}),a=E(c),f&&p(L(f,"abort",()=>{i(H(f.reason))}));const{timeout:Y}=o;if(Y){const v=setTimeout(()=>{i(new b(F,`Timeout reached: ${Y}ms`))},Y);p(()=>{clearTimeout(v)})}r&&r(a,i,j)}),ie(this,"reject"),this.reject=i}static withFn(t,n){return new m((r,o,a)=>{try{const i=t(a);return i instanceof Promise?i.then(r,o):r(i)}catch(i){o(i)}},n)}static resolve(t){return new m(n=>{n(t)})}static reject(t){return new m((n,r)=>{r(t)})}cancel(){this.reject(new b(I))}catch(t){return this.then(void 0,t)}finally(t){return ce(super.finally(t),this)}then(t,n){return ce(super.then(t,n),this)}}function ue(e,t){return e.resolve=t.resolve,e}class T extends m{constructor(t,n){let r,o;typeof t=="function"?(r=t,o=n):o=t;let a;super((i,c,u)=>{a=i,r&&r(i,c,u)},o),ie(this,"resolve"),this.resolve=a}static withFn(t,n){return new T((r,o,a)=>m.withFn(t,{abortSignal:a}).then(r,o),n)}static resolve(t){return new T(n=>{n(t)})}static reject(t){return new T((n,r)=>{r(t)})}catch(t){return this.then(void 0,t)}finally(t){return ue(super.finally(t),this)}then(t,n){return ue(super.then(t,n),this)}}function ze(e,t){return new m(n=>{setTimeout(n,e)},{abortSignal:t})}function fe(e){return`tapps/${e}`}function _e(e,t){sessionStorage.setItem(fe(e),JSON.stringify(t))}function pe(e){const t=sessionStorage.getItem(fe(e));try{return t?JSON.parse(t):void 0}catch{}}function Xe(e){return e.replace(/[A-Z]/g,t=>`-${t.toLowerCase()}`)}function Ye(e){return e.replace(/[A-Z]/g,t=>`_${t.toLowerCase()}`)}function Qe(e){return e.replace(/_[a-z]/g,t=>t[1].toUpperCase())}function le(e,t){t||(t={});const{textColor:n,bgColor:r,shouldLog:o=!0}=t;function a(i,...c){if(!o||typeof o=="function"&&!o())return;const u="font-weight:bold;padding:0 5px;border-radius:5px";console[i](`%c${Intl.DateTimeFormat("en-GB",{hour:"2-digit",minute:"2-digit",second:"2-digit",fractionalSecondDigits:3,timeZone:"UTC"}).format(new Date)}%c / %c${e}`,`${u};background-color: lightblue;color:black`,"",`${u};${n?`color:${n};`:""}${r?`background-color:${r}`:""}`,...c)}return[function(...i){a("log",...i)},function(...i){a("error",...i)}]}function xe(e,t){document.documentElement.style.setProperty(e,t)}function et(e){document.documentElement.style.removeProperty(e)}function tt(e,t){t()}function y(e,t){t||(t={});const n=t.equals||Object.is;let r=[],o=e;const a=f=>{if(!n(o,f)){const p=o;o=f,tt(u,()=>{[...r].forEach(([h,E])=>{h(f,p),E&&c(h,!0)})})}};function i(f){const p=typeof f!="object"?{once:f}:f;return{once:p.once||!1,signal:p.signal||!1}}const c=(f,p)=>{const h=i(p),E=r.findIndex(([O,j])=>O===f&&j.once===h.once&&j.signal===h.signal);E>=0&&r.splice(E,1)},u=Object.assign(function(){return nt(u),o},{destroy(){r=[]},set:a,reset(){a(e)},sub(f,p){return r.push([f,i(p)]),()=>c(f,p)},unsub:c,unsubAll(){r=r.filter(f=>f[1].signal)}});return u}const J=[];function nt(e){J.length&&J[J.length-1].add(e)}const B=y(!1),[G,rt]=le("Bridge",{bgColor:"#9147ff",textColor:"white",shouldLog:B}),ot={clipboard_text_received:w({req_id:l(),data:e=>e===null?e:l(!0)(e)},"clipboard_text_received"),custom_method_invoked:w({req_id:l(),result:e=>e,error:l(!0)},"custom_method_invoked"),popup_closed:g("popup_closed",e=>e?w({button_id:t=>t==null?void 0:l()(t)})()(e):{}),viewport_changed:w({height:P(),width:e=>e==null?window.innerWidth:P()(e),is_state_stable:A(),is_expanded:A()},"viewport_changed")};function at(e){const t=window,[,n]=$(L(t,"message",r=>{if(r.source!==t.parent)return;let o;try{o=oe()(r.data)}catch{return}const{eventType:a,eventData:i}=o,c=ot[a];try{const u=c?c()(i):i;G("Event received:",u?{eventType:a,eventData:u}:{eventType:a}),e([a,u])}catch(u){rt([`An error occurred processing the "${a}" event from the Telegram application.`,"Please, file an issue here:","https://github.com/Telegram-Mini-Apps/telegram-apps/issues/new/choose"].join(` | ||
`),o,u)}}));return n}const D=y(),C=y();function be(){return C()||C.set(at(D.set)),D}const S=y({});function ge(e){let t=S()[e];return t||(t=y(void 0,{equals(){return!1}}),be().sub(n=>{n&&n[0]===e&&t.set(n[1])}),S.set({...S(),[e]:t})),t}function we(e,t,n){return ge(e).sub(t,n)}const me="ERR_METHOD_UNSUPPORTED",de="ERR_RETRIEVE_LP_FAILED",he="ERR_METHOD_PARAMETER_UNSUPPORTED",Ee="ERR_UNKNOWN_ENV",ye="ERR_INVOKE_CUSTOM_METHOD_RESPONSE",V=y("https://web.telegram.org");function K(e,t){G("Posting event:",t?{eventType:e,eventData:t}:{eventType:e});const n=window;if(M(n)){n.TelegramWebviewProxy.postEvent(e,JSON.stringify(t));return}const r=JSON.stringify({eventType:e,eventData:t});if(se())return n.parent.postMessage(r,V());const{external:o}=n;if(w({notify:ae()})().isValid(o)){o.notify(r);return}throw new b(Ee)}function Z(e,t,n){n||(n={});const{capture:r}=n,[o,a]=$();return new m(i=>{(Array.isArray(t)?t:[t]).forEach(c=>{o(we(c,u=>{(!r||(Array.isArray(t)?r({event:c,payload:u}):r(u)))&&i(u)}))}),(n.postEvent||K)(e,n.params)},n).finally(a)}function z(e){return qe()(e)}function ve(e){return z(e.replace(/^[^?#]*[?#]/,"").replace(/[?#]/g,"&"))}function st(){return ve(window.location.href)}function it(){const e=performance.getEntriesByType("navigation")[0];if(!e)throw new Error("Unable to get first navigation entry.");return ve(e.name)}const ct="launchParams";function ut(){return z(pe(ct)||"")}function Re(e){_e("launchParams",Ie(e))}function Pe(e){return e instanceof Error?e.message+(e.cause?` | ||
@@ -3,0 +3,0 @@ ${Pe(e.cause)}`:""):JSON.stringify(e)}function Te(){const e=[];for(const t of[st,it,ut])try{const n=t();return Re(n),n}catch(n){e.push(n)}throw new b(de,["Unable to retrieve launch parameters from any known source. Perhaps, you have opened your app outside Telegram?","📖 Refer to docs for more information:","https://docs.telegram-mini-apps.com/packages/telegram-apps-sdk/environment","Collected errors:",...e.map(t=>`— ${Pe(t)}`)].join(` |
@@ -158,2 +158,3 @@ class d extends Error { | ||
startParam: o, | ||
signature: r, | ||
user: a | ||
@@ -160,0 +161,0 @@ }), |
{ | ||
"name": "@telegram-apps/bridge", | ||
"version": "1.7.0", | ||
"version": "1.7.1", | ||
"description": "TypeScript package to provide communication layer between Mini App and Telegram application.", | ||
@@ -42,4 +42,4 @@ "author": "Vladislav Kibenko <wolfram.deus@gmail.com>", | ||
"@telegram-apps/toolkit": "^1.0.0", | ||
"@telegram-apps/transformers": "^1.1.0", | ||
"@telegram-apps/types": "^1.1.0" | ||
"@telegram-apps/transformers": "^1.2.0", | ||
"@telegram-apps/types": "^1.2.0" | ||
}, | ||
@@ -46,0 +46,0 @@ "devDependencies": { |
@@ -46,4 +46,5 @@ import { it, expect, afterEach, vi } from 'vitest'; | ||
}, | ||
signature: 'abc', | ||
}, | ||
initDataRaw: 'user=%7B%22id%22%3A99281932%2C%22first_name%22%3A%22Andrew%22%2C%22last_name%22%3A%22Rogue%22%2C%22username%22%3A%22rogue%22%2C%22language_code%22%3A%22en%22%2C%22is_premium%22%3Atrue%2C%22allows_write_to_pm%22%3Atrue%7D&chat_instance=8428209589180549439&chat_type=sender&start_param=debug&auth_date=1716922846&hash=89d6079ad6762351f38c6dbbc41bb53048019256a9443988af7a48bcad16ba31', | ||
initDataRaw: 'user=%7B%22id%22%3A99281932%2C%22first_name%22%3A%22Andrew%22%2C%22last_name%22%3A%22Rogue%22%2C%22username%22%3A%22rogue%22%2C%22language_code%22%3A%22en%22%2C%22is_premium%22%3Atrue%2C%22allows_write_to_pm%22%3Atrue%7D&chat_instance=8428209589180549439&chat_type=sender&start_param=debug&auth_date=1716922846&hash=89d6079ad6762351f38c6dbbc41bb53048019256a9443988af7a48bcad16ba31&signature=abc', | ||
version: '7.2', | ||
@@ -55,3 +56,3 @@ platform: 'tdesktop', | ||
const lpString = 'tgWebAppPlatform=tdesktop&tgWebAppThemeParams=%7B%22accent_text_color%22%3A%22%236ab2f2%22%2C%22bg_color%22%3A%22%2317212b%22%2C%22button_color%22%3A%22%235288c1%22%2C%22button_text_color%22%3A%22%23ffffff%22%2C%22destructive_text_color%22%3A%22%23ec3942%22%2C%22header_bg_color%22%3A%22%2317212b%22%2C%22hint_color%22%3A%22%23708499%22%2C%22link_color%22%3A%22%236ab3f3%22%2C%22secondary_bg_color%22%3A%22%23232e3c%22%2C%22section_bg_color%22%3A%22%2317212b%22%2C%22section_header_text_color%22%3A%22%236ab3f3%22%2C%22subtitle_text_color%22%3A%22%23708499%22%2C%22text_color%22%3A%22%23f5f5f5%22%7D&tgWebAppVersion=7.2&tgWebAppData=user%3D%257B%2522id%2522%253A99281932%252C%2522first_name%2522%253A%2522Andrew%2522%252C%2522last_name%2522%253A%2522Rogue%2522%252C%2522username%2522%253A%2522rogue%2522%252C%2522language_code%2522%253A%2522en%2522%252C%2522is_premium%2522%253Atrue%252C%2522allows_write_to_pm%2522%253Atrue%257D%26chat_instance%3D8428209589180549439%26chat_type%3Dsender%26start_param%3Ddebug%26auth_date%3D1716922846%26hash%3D89d6079ad6762351f38c6dbbc41bb53048019256a9443988af7a48bcad16ba31&tgWebAppShowSettings=0&tgWebAppBotInline=0'; | ||
const lpString = 'tgWebAppPlatform=tdesktop&tgWebAppThemeParams=%7B%22accent_text_color%22%3A%22%236ab2f2%22%2C%22bg_color%22%3A%22%2317212b%22%2C%22button_color%22%3A%22%235288c1%22%2C%22button_text_color%22%3A%22%23ffffff%22%2C%22destructive_text_color%22%3A%22%23ec3942%22%2C%22header_bg_color%22%3A%22%2317212b%22%2C%22hint_color%22%3A%22%23708499%22%2C%22link_color%22%3A%22%236ab3f3%22%2C%22secondary_bg_color%22%3A%22%23232e3c%22%2C%22section_bg_color%22%3A%22%2317212b%22%2C%22section_header_text_color%22%3A%22%236ab3f3%22%2C%22subtitle_text_color%22%3A%22%23708499%22%2C%22text_color%22%3A%22%23f5f5f5%22%7D&tgWebAppVersion=7.2&tgWebAppData=user%3D%257B%2522id%2522%253A99281932%252C%2522first_name%2522%253A%2522Andrew%2522%252C%2522last_name%2522%253A%2522Rogue%2522%252C%2522username%2522%253A%2522rogue%2522%252C%2522language_code%2522%253A%2522en%2522%252C%2522is_premium%2522%253Atrue%252C%2522allows_write_to_pm%2522%253Atrue%257D%26chat_instance%3D8428209589180549439%26chat_type%3Dsender%26start_param%3Ddebug%26auth_date%3D1716922846%26hash%3D89d6079ad6762351f38c6dbbc41bb53048019256a9443988af7a48bcad16ba31%26signature%3Dabc&tgWebAppShowSettings=0&tgWebAppBotInline=0'; | ||
@@ -58,0 +59,0 @@ it('should save passed launch parameters in session storage', () => { |
@@ -88,3 +88,3 @@ import { dispatchMiniAppsEvent, createWindow } from 'test-utils'; | ||
it('should reject promise if postEvent threw an error', () => { | ||
it('should reject promise if postEvent threw an error', async () => { | ||
const promise = request('web_app_request_phone', 'phone_requested', { | ||
@@ -95,3 +95,3 @@ postEvent() { | ||
}); | ||
void expect(promise).rejects.toStrictEqual(new Error('Nope!')); | ||
await expect(promise).rejects.toStrictEqual(new Error('Nope!')); | ||
}); | ||
@@ -101,3 +101,3 @@ }); | ||
describe('capture', () => { | ||
it('should capture an event in case, capture method returned true', () => { | ||
it('should capture an event in case, capture method returned true', async () => { | ||
const promise = request('web_app_request_phone', 'phone_requested', { | ||
@@ -104,0 +104,0 @@ timeout: 1000, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
476693
122
6090