bm-webapp-sdk 0.1.4 → 0.1.5
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,
|
|
1
|
+
(()=>{"use strict";var t={d:(e,i)=>{for(var s in i)t.o(i,s)&&!t.o(e,s)&&Object.defineProperty(e,s,{enumerable:!0,get:i[s]})},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:()=>s});const i={retryDelayOnTimeout:2e3,requestTimeout:6e4,responseVar:"response",apiEndpoint:"https://console.bot-marketing.com/api/public"};class s{constructor(t){this.options={...i,...t}}isActive(){var t;return"active"===(null===(t=this.data)||void 0===t?void 0:t.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(){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}`)}}async request(t,e,i){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(i)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,i,s){const r=await fetch(`${this.options.apiEndpoint}/${e}`,{method:t,signal:s,..."POST"===t?{body:JSON.stringify(i),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 i=()=>{this.currentRequest.retryTimeoutId=setTimeout((async()=>{await this.refreshData(),this.data.isTransitioning?i():t(this)}),this.options.retryDelayOnTimeout)};i()}))}}window.BotMarketing=e})();
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,69 @@
|
|
|
1
|
-
|
|
1
|
+
export 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
|
+
export type TData = {
|
|
13
|
+
[key: string]: any;
|
|
14
|
+
};
|
|
15
|
+
export 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
|
+
export interface INode {
|
|
26
|
+
id: number;
|
|
27
|
+
title: string;
|
|
28
|
+
typeName: "basic" | "event" | "eventListener" | "condition" | "conditionBranch";
|
|
29
|
+
waitingForEvents?: INodeEventsDecl;
|
|
30
|
+
}
|
|
31
|
+
export type TScenarioSessionStatus = "active" | "stopped" | "ended" | "unknownError" | "tunnelDisabled" | "expired" | "userDisabled" | "stepDeleted" | "transitionsLimit" | "parentStopped";
|
|
32
|
+
export 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 interface ICurrentRequest {
|
|
42
|
+
controller: AbortController | null;
|
|
43
|
+
timeoutId: number;
|
|
44
|
+
retryTimeoutId?: number;
|
|
45
|
+
reject?: (reason?: any) => void;
|
|
46
|
+
}
|
|
47
|
+
export default class WebAppHandler {
|
|
48
|
+
readonly options: HandlerOptions;
|
|
49
|
+
data?: ScenarioSession;
|
|
50
|
+
private currentRequest?;
|
|
51
|
+
constructor(options: HandlerOptions);
|
|
52
|
+
isActive(): boolean;
|
|
53
|
+
init(): Promise<WebAppHandler>;
|
|
54
|
+
refreshData(): Promise<WebAppHandler>;
|
|
55
|
+
request(code: string, params?: TData, async?: boolean): Promise<WebAppHandler>;
|
|
56
|
+
abort(): void;
|
|
57
|
+
isTelegramInitAvailable(): boolean;
|
|
58
|
+
protected initNewSession(): Promise<WebAppHandler>;
|
|
59
|
+
protected makeRequest(method: string, path: string, json?: TData, signal?: AbortSignal): Promise<any>;
|
|
60
|
+
private waitUntilSessionFinishesTransition;
|
|
61
|
+
}
|
|
62
|
+
|
|
2
63
|
export { WebAppHandler };
|
|
3
64
|
declare global {
|
|
4
65
|
interface Window {
|
|
5
66
|
Telegram: any;
|
|
6
67
|
}
|
|
7
68
|
}
|
|
69
|
+
|
|
@@ -9,7 +9,8 @@ export default class WebAppHandler {
|
|
|
9
9
|
this.options = { ...DEFAULTS, ...options };
|
|
10
10
|
}
|
|
11
11
|
isActive() {
|
|
12
|
-
|
|
12
|
+
var _a;
|
|
13
|
+
return ((_a = this.data) === null || _a === void 0 ? void 0 : _a.statusName) === "active";
|
|
13
14
|
}
|
|
14
15
|
async init() {
|
|
15
16
|
if (this.data)
|
|
@@ -28,7 +29,8 @@ export default class WebAppHandler {
|
|
|
28
29
|
}
|
|
29
30
|
}
|
|
30
31
|
async refreshData() {
|
|
31
|
-
|
|
32
|
+
var _a;
|
|
33
|
+
const sessionId = ((_a = this.data) === null || _a === void 0 ? void 0 : _a.hash) || this.options.sessionHash;
|
|
32
34
|
if (!sessionId)
|
|
33
35
|
throw new Error('Cannot refresh data: session id is missing');
|
|
34
36
|
try {
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bm-webapp-sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "",
|
|
5
|
-
"main": "
|
|
6
|
-
"types": "
|
|
5
|
+
"main": "src/index.ts",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"develop": "webpack-dev-server --mode development",
|
|
9
9
|
"build": "webpack --mode production"
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"repository": "https://gitlab.com/b4494/bm-webapp-sdk",
|
|
12
12
|
"files": [
|
|
13
13
|
"/dist",
|
|
14
|
-
"/
|
|
14
|
+
"/es2019"
|
|
15
15
|
],
|
|
16
16
|
"keywords": [],
|
|
17
17
|
"author": "lazychaser@gmail.com",
|
package/src/index.ts
ADDED
|
File without changes
|
|
File without changes
|
/package/{esm → es2019}/index.js
RENAMED
|
File without changes
|