bm-webapp-sdk 0.1.3 → 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.
- package/dist/bm-webapp-sdk.min.js +1 -1
- package/dist/index.d.ts +63 -1
- package/es2019/WebAppHandler.d.ts +61 -0
- package/es2019/WebAppHandler.js +140 -0
- package/es2019/index.d.ts +7 -0
- package/package.json +5 -4
- package/src/index.ts +9 -0
- /package/{esm → es2019}/index.js +0 -0
|
@@ -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
|
+
|
|
@@ -0,0 +1,61 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
const DEFAULTS = {
|
|
2
|
+
retryDelayOnTimeout: 2000,
|
|
3
|
+
requestTimeout: 60000,
|
|
4
|
+
responseVar: 'response',
|
|
5
|
+
apiEndpoint: 'https://console.bot-marketing.com/api/public',
|
|
6
|
+
};
|
|
7
|
+
export default class WebAppHandler {
|
|
8
|
+
constructor(options) {
|
|
9
|
+
this.options = { ...DEFAULTS, ...options };
|
|
10
|
+
}
|
|
11
|
+
isActive() {
|
|
12
|
+
var _a;
|
|
13
|
+
return ((_a = this.data) === null || _a === void 0 ? void 0 : _a.statusName) === "active";
|
|
14
|
+
}
|
|
15
|
+
async init() {
|
|
16
|
+
if (this.data)
|
|
17
|
+
throw new Error("Handler already initialized");
|
|
18
|
+
if (this.options.sessionHash) {
|
|
19
|
+
await this.refreshData();
|
|
20
|
+
if (!this.isActive() && this.isTelegramInitAvailable())
|
|
21
|
+
return this.initNewSession();
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
else if (this.isTelegramInitAvailable()) {
|
|
25
|
+
return this.initNewSession();
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
throw new Error('Failed to init handler. You must specify `sessionId`, or `scenarioId` when working with Telegram');
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
async refreshData() {
|
|
32
|
+
var _a;
|
|
33
|
+
const sessionId = ((_a = this.data) === null || _a === void 0 ? void 0 : _a.hash) || this.options.sessionHash;
|
|
34
|
+
if (!sessionId)
|
|
35
|
+
throw new Error('Cannot refresh data: session id is missing');
|
|
36
|
+
try {
|
|
37
|
+
this.data = await this.makeRequest("GET", `tunnelSessions/${sessionId}`);
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
throw new Error(`Failed to get session data: ${error.message}`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
async request(code, params, async) {
|
|
45
|
+
if (this.currentRequest)
|
|
46
|
+
throw new Error('Please wait until pending request is finished');
|
|
47
|
+
if (!this.data)
|
|
48
|
+
await this.init();
|
|
49
|
+
if (!this.isActive())
|
|
50
|
+
throw new Error('Cannot make request: session is not active');
|
|
51
|
+
if (async) {
|
|
52
|
+
// noinspection ES6MissingAwait
|
|
53
|
+
this.makeRequest("POST", `tunnelSessions/${this.data.hash}/request`, { code, params });
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
try {
|
|
57
|
+
this.currentRequest = {
|
|
58
|
+
controller: new AbortController(),
|
|
59
|
+
timeoutId: setTimeout(() => this.abort(), this.options.requestTimeout),
|
|
60
|
+
};
|
|
61
|
+
this.data = await this.makeRequest("POST", `tunnelSessions/${this.data.hash}/request`, { code, params, }, this.currentRequest.controller.signal);
|
|
62
|
+
this.currentRequest = null;
|
|
63
|
+
return this;
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
if (error.message === 'request_timeout')
|
|
67
|
+
return this.waitUntilSessionFinishesTransition();
|
|
68
|
+
this.currentRequest = null;
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
abort() {
|
|
73
|
+
if (!this.currentRequest)
|
|
74
|
+
return;
|
|
75
|
+
// Main request is still running, we can just abort it
|
|
76
|
+
// The rest is done inside request method
|
|
77
|
+
if (this.currentRequest.controller) {
|
|
78
|
+
this.currentRequest.controller.abort('Request timeout');
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
// At this point we are trying to wait until session is finished transitioning
|
|
82
|
+
clearTimeout(this.currentRequest.retryTimeoutId);
|
|
83
|
+
this.currentRequest.reject('Request timeout');
|
|
84
|
+
this.currentRequest = null;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
isTelegramInitAvailable() {
|
|
88
|
+
return window.Telegram !== undefined && this.options.scenarioCode !== undefined;
|
|
89
|
+
}
|
|
90
|
+
async initNewSession() {
|
|
91
|
+
try {
|
|
92
|
+
this.data = await this.makeRequest("POST", `tunnel/${this.options.scenarioCode}/initWebApp`, {
|
|
93
|
+
initData: Telegram.WebApp.initData,
|
|
94
|
+
botTokenParamName: this.options.botTokenParamName,
|
|
95
|
+
variables: this.options.initVariables,
|
|
96
|
+
});
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
throw new Error(`Failed to init new session: ${error.message}`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
async makeRequest(method, path, json, signal) {
|
|
104
|
+
const resp = await fetch(`${this.options.apiEndpoint}/${path}`, {
|
|
105
|
+
method: method,
|
|
106
|
+
signal,
|
|
107
|
+
...(method === "POST" ? {
|
|
108
|
+
body: JSON.stringify(json),
|
|
109
|
+
headers: {
|
|
110
|
+
"Content-Type": "application/json",
|
|
111
|
+
"Accept": "application/json",
|
|
112
|
+
},
|
|
113
|
+
} : {
|
|
114
|
+
headers: { "Accept": "application/json" },
|
|
115
|
+
}),
|
|
116
|
+
});
|
|
117
|
+
if (resp.status >= 400) {
|
|
118
|
+
const data = await resp.json();
|
|
119
|
+
throw new Error(data.message || `Http Error ${resp.status}`);
|
|
120
|
+
}
|
|
121
|
+
return resp.json();
|
|
122
|
+
}
|
|
123
|
+
waitUntilSessionFinishesTransition() {
|
|
124
|
+
return new Promise((resolve, reject) => {
|
|
125
|
+
this.currentRequest.reject = reject;
|
|
126
|
+
const resetTimeout = () => {
|
|
127
|
+
this.currentRequest.retryTimeoutId = setTimeout(async () => {
|
|
128
|
+
await this.refreshData();
|
|
129
|
+
if (this.data.isTransitioning) {
|
|
130
|
+
resetTimeout();
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
resolve(this);
|
|
134
|
+
}
|
|
135
|
+
}, this.options.retryDelayOnTimeout);
|
|
136
|
+
};
|
|
137
|
+
resetTimeout();
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
}
|
package/package.json
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
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"
|
|
10
10
|
},
|
|
11
11
|
"repository": "https://gitlab.com/b4494/bm-webapp-sdk",
|
|
12
12
|
"files": [
|
|
13
|
-
"/dist"
|
|
13
|
+
"/dist",
|
|
14
|
+
"/es2019"
|
|
14
15
|
],
|
|
15
16
|
"keywords": [],
|
|
16
17
|
"author": "lazychaser@gmail.com",
|
package/src/index.ts
ADDED
/package/{esm → es2019}/index.js
RENAMED
|
File without changes
|