bm-webapp-sdk 0.1.9 → 0.1.10

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.
@@ -1 +1 @@
1
- (()=>{"use strict";var t={d:(e,s)=>{for(var i in s)t.o(s,i)&&!t.o(e,i)&&Object.defineProperty(e,i,{enumerable:!0,get:s[i]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};t.r(e),t.d(e,{WebAppHandler:()=>i});const s={retryDelayOnTimeout:2e3,requestTimeout:6e4,apiEndpoint:"https://console.bot-marketing.com/api/public"};class i{constructor(t){this.options={...s,...t}}isActive(){var t;return"active"===(null===(t=this.data)||void 0===t?void 0:t.statusName)}getVariable(t){var e;return((null===(e=this.data)||void 0===e?void 0:e.variables)||{})[t]}async init(){if(this.data)throw new Error("Handler already initialized");if(this.options.sessionHash)return await this.refreshData(),!this.isActive()&&this.isTelegramInitAvailable()?this.initNewSession():this;if(this.isTelegramInitAvailable())return this.initNewSession();throw new Error("Failed to init handler. You must specify `sessionHash`, or `scenarioCode` when working with Telegram")}async refreshData(){var t;const e=(null===(t=this.data)||void 0===t?void 0:t.hash)||this.options.sessionHash;if(!e)throw new Error("Cannot refresh data: session id is missing");try{return this.data=await this.makeRequest("GET",`tunnelSessions/${e}`),this}catch(t){throw new Error(`Failed to get session data: ${t.message}`)}}requestResponse(){return this.options.responseVar?"function"==typeof this.options.responseVar?this.options.responseVar(this):(this.data.variables||{})[this.options.responseVar]:this}async request(t,e,s){if(this.currentRequest)throw new Error("Please wait until pending request is finished");if(this.data||await this.init(),!this.isActive())throw new Error("Cannot make request: session is not active");if(s)return this.makeRequest("POST",`tunnelSessions/${this.data.hash}/request`,{code:t,params:e}),this;try{return this.currentRequest={controller:new AbortController,timeoutId:setTimeout((()=>this.abort()),this.options.requestTimeout)},this.data=await this.makeRequest("POST",`tunnelSessions/${this.data.hash}/request`,{code:t,params:e},this.currentRequest.controller.signal),this.currentRequest=null,this.requestResponse()}catch(t){if("request_timeout"===t.message)return this.waitUntilSessionFinishesTransition();throw this.currentRequest=null,t}}abort(){this.currentRequest&&(this.currentRequest.controller?this.currentRequest.controller.abort("Request timeout"):(clearTimeout(this.currentRequest.retryTimeoutId),this.currentRequest.reject("Request timeout"),this.currentRequest=null))}isTelegramInitAvailable(){return void 0!==window.Telegram&&void 0!==this.options.scenarioCode}async initNewSession(){try{return this.data=await this.makeRequest("POST",`tunnel/${this.options.scenarioCode}/initWebApp`,{initData:Telegram.WebApp.initData,botTokenParamName:this.options.botTokenParamName,variables:this.options.initVariables}),this}catch(t){throw new Error(`Failed to init new session: ${t.message}`)}}async makeRequest(t,e,s,i){const r=await fetch(`${this.options.apiEndpoint}/${e}`,{method:t,signal:i,..."POST"===t?{body:JSON.stringify(s),headers:{"Content-Type":"application/json",Accept:"application/json"}}:{headers:{Accept:"application/json"}}});if(r.status>=400){const t=await r.json();throw new Error(t.message||`Http Error ${r.status}`)}return r.json()}waitUntilSessionFinishesTransition(){return new Promise(((t,e)=>{this.currentRequest.reject=e;const s=()=>{this.currentRequest.retryTimeoutId=setTimeout((async()=>{if(await this.refreshData(),this.data.isTransitioning)s();else try{t(this.requestResponse())}catch(t){e(t)}}),this.options.retryDelayOnTimeout)};s()}))}}window.BotMarketing=e})();
1
+ (()=>{"use strict";var t={d:(e,s)=>{for(var i in s)t.o(s,i)&&!t.o(e,i)&&Object.defineProperty(e,i,{enumerable:!0,get:s[i]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};t.r(e),t.d(e,{WebAppHandler:()=>i});const s={retryDelayOnTimeout:2e3,requestTimeout:6e4,apiEndpoint:"https://console.bot-marketing.com/api/public"};class i{constructor(t){this.options={...s,...t}}isActive(){var t;return"active"===(null===(t=this.data)||void 0===t?void 0:t.statusName)}getVariable(t){var e;return((null===(e=this.data)||void 0===e?void 0:e.variables)||{})[t]}async init(){if(this.data)throw new Error("Handler already initialized");if(this.options.sessionHash)return await this.refreshData(),!this.isActive()&&this.isTelegramInitAvailable()?this.initNewSession():this;if(this.isTelegramInitAvailable())return this.initNewSession();throw new Error("Failed to init handler. You must specify `sessionHash`, or `scenarioCode` when working with Telegram")}async refreshData(){var t;const e=(null===(t=this.data)||void 0===t?void 0:t.hash)||this.options.sessionHash;if(!e)throw new Error("Cannot refresh data: session id is missing");try{return this.data=await this.makeRequest("GET",`tunnelSessions/${e}`),this}catch(t){throw new Error(`Failed to get session data: ${t.message}`)}}requestResponse(){return this.options.responseVar?"function"==typeof this.options.responseVar?this.options.responseVar(this):(this.data.variables||{})[this.options.responseVar]:this}async request(t,e,s){if(this.currentRequest)throw new Error("Please wait until pending request is finished");if(this.data||await this.init(),!this.isActive())throw new Error("Cannot make request: session is not active");if(s)return this.makeRequest("POST",`tunnelSessions/${this.data.hash}/request`,{code:t,params:e}),this;try{return this.currentRequest={controller:new AbortController,timeoutId:setTimeout((()=>this.abort()),this.options.requestTimeout)},this.data=await this.makeRequest("POST",`tunnelSessions/${this.data.hash}/request`,{code:t,params:e},this.currentRequest.controller.signal),this.currentRequest=null,this.requestResponse()}catch(t){if("request_timeout"===t.message)return this.waitUntilSessionFinishesTransition();throw this.currentRequest=null,t}}abort(){this.currentRequest&&(this.currentRequest.controller?this.currentRequest.controller.abort("Request timeout"):(clearTimeout(this.currentRequest.retryTimeoutId),this.currentRequest.reject("Request timeout"),this.currentRequest=null))}isTelegramInitAvailable(){return void 0!==window.Telegram&&void 0!==this.options.scenarioCode}async initNewSession(){try{return this.data=await this.makeRequest("POST",`tunnel/${this.options.scenarioCode}/initWebApp`,{initData:Telegram.WebApp.initData,botTokenParamName:this.options.botTokenParamName,variables:this.options.initVariables}),this}catch(t){throw new Error(`Failed to init new session: ${t.message}`)}}async makeRequest(t,e,s,i){const r=await fetch(`${this.options.apiEndpoint}/${e}`,{method:t,signal:i,..."POST"===t?{body:JSON.stringify(s),headers:{"Content-Type":"application/json",Accept:"application/json"}}:{headers:{Accept:"application/json"}}});if(r.status>=400){const t=await r.json();throw new Error(t.message||`Http Error ${r.status}`)}return r.json()}waitUntilSessionFinishesTransition(){return new Promise(((t,e)=>{this.currentRequest.reject=e;const s=()=>{this.currentRequest.retryTimeoutId=setTimeout((async()=>{if(await this.refreshData(),this.data.isTransitioning)s();else{this.currentRequest=null;try{t(this.requestResponse())}catch(t){e(t)}}}),this.options.retryDelayOnTimeout)};s()}))}}window.BotMarketing=e})();
@@ -141,6 +141,7 @@ export default class WebAppHandler {
141
141
  resetTimeout();
142
142
  }
143
143
  else {
144
+ this.currentRequest = null;
144
145
  try {
145
146
  resolve(this.requestResponse());
146
147
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bm-webapp-sdk",
3
- "version": "0.1.9",
3
+ "version": "0.1.10",
4
4
  "description": "",
5
5
  "main": "src/index.ts",
6
6
  "types": "dist/index.d.ts",