bm-webapp-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/WebAppHandler.d.ts +56 -0
- package/dist/bm-webapp-sdk.min.js +1 -0
- package/dist/index.d.ts +8 -0
- package/package.json +26 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
interface HandlerOptions {
|
|
2
|
+
sessionHash?: string;
|
|
3
|
+
scenarioCode?: string;
|
|
4
|
+
initVariables?: TData;
|
|
5
|
+
botTokenParamName?: string;
|
|
6
|
+
userApiToken?: string;
|
|
7
|
+
retryDelayOnTimeout?: number;
|
|
8
|
+
requestTimeout?: number;
|
|
9
|
+
responseVar?: string;
|
|
10
|
+
apiEndpoint?: string;
|
|
11
|
+
}
|
|
12
|
+
type TData = {
|
|
13
|
+
[key: string]: any;
|
|
14
|
+
};
|
|
15
|
+
interface INodeEventsDecl {
|
|
16
|
+
inbox: boolean;
|
|
17
|
+
outbox: boolean;
|
|
18
|
+
anyInbox: boolean;
|
|
19
|
+
dialogClosed: boolean;
|
|
20
|
+
invoicePaid: boolean;
|
|
21
|
+
externalRequest: boolean;
|
|
22
|
+
buttons: boolean;
|
|
23
|
+
customEvents: boolean;
|
|
24
|
+
}
|
|
25
|
+
interface INode {
|
|
26
|
+
id: number;
|
|
27
|
+
title: string;
|
|
28
|
+
typeName: "basic" | "event" | "eventListener" | "condition" | "conditionBranch";
|
|
29
|
+
waitingForEvents?: INodeEventsDecl;
|
|
30
|
+
}
|
|
31
|
+
type TScenarioSessionStatus = "active" | "stopped" | "ended" | "unknownError" | "tunnelDisabled" | "expired" | "userDisabled" | "stepDeleted" | "transitionsLimit" | "parentStopped";
|
|
32
|
+
interface ScenarioSession {
|
|
33
|
+
id: number;
|
|
34
|
+
hash: string;
|
|
35
|
+
statusName: TScenarioSessionStatus;
|
|
36
|
+
ref: string | null;
|
|
37
|
+
variables: TData;
|
|
38
|
+
isTransitioning: boolean;
|
|
39
|
+
currentNode: INode | null;
|
|
40
|
+
}
|
|
41
|
+
export default class WebAppHandler {
|
|
42
|
+
readonly options: HandlerOptions;
|
|
43
|
+
data?: ScenarioSession;
|
|
44
|
+
private currentRequest?;
|
|
45
|
+
constructor(options: HandlerOptions);
|
|
46
|
+
isActive(): boolean;
|
|
47
|
+
init(): Promise<WebAppHandler>;
|
|
48
|
+
refreshData(): Promise<WebAppHandler>;
|
|
49
|
+
request(code: string, params?: TData, async?: boolean): Promise<WebAppHandler>;
|
|
50
|
+
abort(): void;
|
|
51
|
+
isTelegramInitAvailable(): boolean;
|
|
52
|
+
protected initNewSession(): Promise<WebAppHandler>;
|
|
53
|
+
protected makeRequest(method: string, path: string, json?: TData, signal?: AbortSignal): Promise<any>;
|
|
54
|
+
private waitUntilSessionFinishesTransition;
|
|
55
|
+
}
|
|
56
|
+
export {};
|
|
@@ -0,0 +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,responseVar:"response",apiEndpoint:"https://console.bot-marketing.com/api/public"};class i{constructor(t){this.options={...s,...t}}isActive(){return"active"===this.data?.statusName}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 `sessionId`, or `scenarioId` when working with Telegram")}async refreshData(){const t=this.data?.hash||this.options.sessionHash;if(!t)throw new Error("Cannot refresh data: session id is missing");try{return this.data=await this.makeRequest("GET",`tunnelSessions/${t}`),this}catch(t){throw new Error(`Failed to get session data: ${t.message}`)}}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}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()=>{await this.refreshData(),this.data.isTransitioning?s():t(this)}),this.options.retryDelayOnTimeout)};s()}))}}window.BotMarketing=e})();
|
package/dist/index.d.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "bm-webapp-sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/bundle.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"develop": "webpack-dev-server --mode development",
|
|
9
|
+
"build": "webpack --mode production"
|
|
10
|
+
},
|
|
11
|
+
"repository": "https://gitlab.com/b4494/bm-webapp-sdk",
|
|
12
|
+
"files": [
|
|
13
|
+
"/dist"
|
|
14
|
+
],
|
|
15
|
+
"keywords": [],
|
|
16
|
+
"author": "lazychaser@gmail.com",
|
|
17
|
+
"license": "ISC",
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"telegram-webapps-types": "^1.0.5",
|
|
20
|
+
"terser-webpack-plugin": "^5.3.10",
|
|
21
|
+
"ts-loader": "^9.5.1",
|
|
22
|
+
"typescript": "^5.3.3",
|
|
23
|
+
"webpack-cli": "^5.1.4",
|
|
24
|
+
"webpack-dev-server": "^5.0.2"
|
|
25
|
+
}
|
|
26
|
+
}
|