@botonic/webchat-core 2.23.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/CHANGELOG.md +247 -0
- package/PR-LOCALE-SUPPORT.md +156 -0
- package/README.md +11 -0
- package/package.json +29 -0
- package/src/index.d.ts +11 -0
- package/src/index.js +15 -0
- package/src/index.js.map +1 -0
- package/src/lib/controllers/index.d.ts +2 -0
- package/src/lib/controllers/index.js +3 -0
- package/src/lib/controllers/index.js.map +1 -0
- package/src/lib/controllers/public.d.ts +383 -0
- package/src/lib/controllers/public.js +194 -0
- package/src/lib/controllers/public.js.map +1 -0
- package/src/lib/controllers/webchat.d.ts +414 -0
- package/src/lib/controllers/webchat.js +255 -0
- package/src/lib/controllers/webchat.js.map +1 -0
- package/src/lib/domain/index.d.ts +50 -0
- package/src/lib/domain/index.js +11 -0
- package/src/lib/domain/index.js.map +1 -0
- package/src/lib/infra/clients/hubtype-api.d.ts +53 -0
- package/src/lib/infra/clients/hubtype-api.js +189 -0
- package/src/lib/infra/clients/hubtype-api.js.map +1 -0
- package/src/lib/infra/clients/index.d.ts +3 -0
- package/src/lib/infra/clients/index.js +4 -0
- package/src/lib/infra/clients/index.js.map +1 -0
- package/src/lib/infra/clients/realtime.d.ts +92 -0
- package/src/lib/infra/clients/realtime.js +277 -0
- package/src/lib/infra/clients/realtime.js.map +1 -0
- package/src/lib/infra/clients/types.d.ts +21 -0
- package/src/lib/infra/clients/types.js +2 -0
- package/src/lib/infra/clients/types.js.map +1 -0
- package/src/lib/infra/repositories/index.d.ts +1 -0
- package/src/lib/infra/repositories/index.js +2 -0
- package/src/lib/infra/repositories/index.js.map +1 -0
- package/src/lib/infra/repositories/storage/base-storage.d.ts +23 -0
- package/src/lib/infra/repositories/storage/base-storage.js +102 -0
- package/src/lib/infra/repositories/storage/base-storage.js.map +1 -0
- package/src/lib/infra/repositories/storage/browser-storage.d.ts +9 -0
- package/src/lib/infra/repositories/storage/browser-storage.js +46 -0
- package/src/lib/infra/repositories/storage/browser-storage.js.map +1 -0
- package/src/lib/infra/repositories/storage/factory.d.ts +2 -0
- package/src/lib/infra/repositories/storage/factory.js +20 -0
- package/src/lib/infra/repositories/storage/factory.js.map +1 -0
- package/src/lib/infra/repositories/storage/in-memory.d.ts +9 -0
- package/src/lib/infra/repositories/storage/in-memory.js +21 -0
- package/src/lib/infra/repositories/storage/in-memory.js.map +1 -0
- package/src/lib/infra/repositories/storage/index.d.ts +7 -0
- package/src/lib/infra/repositories/storage/index.js +7 -0
- package/src/lib/infra/repositories/storage/index.js.map +1 -0
- package/src/lib/infra/repositories/storage/local-storage.d.ts +4 -0
- package/src/lib/infra/repositories/storage/local-storage.js +7 -0
- package/src/lib/infra/repositories/storage/local-storage.js.map +1 -0
- package/src/lib/infra/repositories/storage/session-storage.d.ts +4 -0
- package/src/lib/infra/repositories/storage/session-storage.js +7 -0
- package/src/lib/infra/repositories/storage/session-storage.js.map +1 -0
- package/src/lib/infra/repositories/storage/types.d.ts +30 -0
- package/src/lib/infra/repositories/storage/types.js +2 -0
- package/src/lib/infra/repositories/storage/types.js.map +1 -0
- package/src/lib/machines/message.d.ts +200 -0
- package/src/lib/machines/message.js +210 -0
- package/src/lib/machines/message.js.map +1 -0
- package/src/lib/machines/overlay.d.ts +299 -0
- package/src/lib/machines/overlay.js +217 -0
- package/src/lib/machines/overlay.js.map +1 -0
- package/src/lib/machines/webchat.d.ts +641 -0
- package/src/lib/machines/webchat.js +768 -0
- package/src/lib/machines/webchat.js.map +1 -0
- package/src/lib/services/webchat-service.d.ts +125 -0
- package/src/lib/services/webchat-service.js +339 -0
- package/src/lib/services/webchat-service.js.map +1 -0
- package/src/lib/utils/locale-detection.d.ts +2 -0
- package/src/lib/utils/locale-detection.js +20 -0
- package/src/lib/utils/locale-detection.js.map +1 -0
- package/src/lib/utils/timezone-to-country.d.ts +1 -0
- package/src/lib/utils/timezone-to-country.js +105 -0
- package/src/lib/utils/timezone-to-country.js.map +1 -0
- package/src/lib/webview/constants.d.ts +10 -0
- package/src/lib/webview/constants.js +10 -0
- package/src/lib/webview/constants.js.map +1 -0
- package/src/lib/webview/index.d.ts +3 -0
- package/src/lib/webview/index.js +4 -0
- package/src/lib/webview/index.js.map +1 -0
- package/src/lib/webview/types.d.ts +35 -0
- package/src/lib/webview/types.js +16 -0
- package/src/lib/webview/types.js.map +1 -0
- package/src/lib/webview/utils.d.ts +9 -0
- package/src/lib/webview/utils.js +33 -0
- package/src/lib/webview/utils.js.map +1 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { HubtypeUser, HubtypeUserLocale } from '@botonic/shared';
|
|
2
|
+
import { StorageConfig } from '../infra/repositories';
|
|
3
|
+
import { WebchatConnectionConfig } from '../machines/webchat';
|
|
4
|
+
export declare enum ConnectionStatus {
|
|
5
|
+
Connecting = "connecting",
|
|
6
|
+
Connected = "connected",
|
|
7
|
+
Disconnected = "disconnected",
|
|
8
|
+
Failed = "failed",
|
|
9
|
+
Unavailable = "unavailable",
|
|
10
|
+
Reconnecting = "reconnecting"
|
|
11
|
+
}
|
|
12
|
+
export interface ConfirmCloseConfig {
|
|
13
|
+
title?: string;
|
|
14
|
+
message: string;
|
|
15
|
+
confirmText?: string;
|
|
16
|
+
cancelText?: string;
|
|
17
|
+
}
|
|
18
|
+
export type ConfirmCloseOption = boolean | string | ConfirmCloseConfig;
|
|
19
|
+
/**
|
|
20
|
+
* Base properties shared by all overlay types
|
|
21
|
+
*/
|
|
22
|
+
export interface OverlayStateBase {
|
|
23
|
+
isOpen: boolean;
|
|
24
|
+
title: string;
|
|
25
|
+
icon?: string;
|
|
26
|
+
allowClose: boolean;
|
|
27
|
+
confirmClose?: ConfirmCloseOption;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Webview overlay - renders external content in an iframe
|
|
31
|
+
*/
|
|
32
|
+
export interface WebviewOverlayState extends OverlayStateBase {
|
|
33
|
+
type: 'webview';
|
|
34
|
+
url: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Cover overlay - renders a registered React component inline
|
|
38
|
+
*/
|
|
39
|
+
export interface CoverOverlayState extends OverlayStateBase {
|
|
40
|
+
type: 'cover';
|
|
41
|
+
componentId: string;
|
|
42
|
+
props?: Record<string, unknown>;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Discriminated union of all overlay types
|
|
46
|
+
*/
|
|
47
|
+
export type OverlayState = WebviewOverlayState | CoverOverlayState;
|
|
48
|
+
export type WebchatConfig = WebchatConnectionConfig & HubtypeUserLocale & Pick<HubtypeUser, 'extra_data'> & {
|
|
49
|
+
storage?: StorageConfig;
|
|
50
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// Moved temporarily into domain, check if this really needs to be here
|
|
2
|
+
export var ConnectionStatus;
|
|
3
|
+
(function (ConnectionStatus) {
|
|
4
|
+
ConnectionStatus["Connecting"] = "connecting";
|
|
5
|
+
ConnectionStatus["Connected"] = "connected";
|
|
6
|
+
ConnectionStatus["Disconnected"] = "disconnected";
|
|
7
|
+
ConnectionStatus["Failed"] = "failed";
|
|
8
|
+
ConnectionStatus["Unavailable"] = "unavailable";
|
|
9
|
+
ConnectionStatus["Reconnecting"] = "reconnecting";
|
|
10
|
+
})(ConnectionStatus || (ConnectionStatus = {}));
|
|
11
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../../libs/botonic/webchat-core/src/lib/domain/index.ts"],"names":[],"mappings":"AAKA,uEAAuE;AACvE,MAAM,CAAN,IAAY,gBAOX;AAPD,WAAY,gBAAgB;IAC1B,6CAAyB,CAAA;IACzB,2CAAuB,CAAA;IACvB,iDAA6B,CAAA;IAC7B,qCAAiB,CAAA;IACjB,+CAA2B,CAAA;IAC3B,iDAA6B,CAAA;AAC/B,CAAC,EAPW,gBAAgB,KAAhB,gBAAgB,QAO3B"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { HubtypeUser, WebchatMessage } from '@botonic/shared';
|
|
2
|
+
import type { PusherAuthResponse, WebchatSettingsResponse } from './types';
|
|
3
|
+
export interface HubtypeApiClientConfig {
|
|
4
|
+
appId: string;
|
|
5
|
+
apiUrl?: string;
|
|
6
|
+
timeout?: number;
|
|
7
|
+
}
|
|
8
|
+
export interface ApiResponse<T = any> {
|
|
9
|
+
success: boolean;
|
|
10
|
+
data?: T;
|
|
11
|
+
error?: string;
|
|
12
|
+
status: number;
|
|
13
|
+
}
|
|
14
|
+
export interface WebchatRealtimeAuthContext {
|
|
15
|
+
userId?: string;
|
|
16
|
+
lastMessageId?: string;
|
|
17
|
+
lastMessageUpdateDate?: string;
|
|
18
|
+
}
|
|
19
|
+
export declare class HubtypeApiClient {
|
|
20
|
+
private config;
|
|
21
|
+
private baseApiUrl;
|
|
22
|
+
private _authEndpointUrl;
|
|
23
|
+
private _webchatWebhooksEndpointUrl;
|
|
24
|
+
private _webchatSettingsUrl;
|
|
25
|
+
constructor(config: HubtypeApiClientConfig);
|
|
26
|
+
/**
|
|
27
|
+
* Get the auth endpoint URL
|
|
28
|
+
*/
|
|
29
|
+
get authEndpointUrl(): string;
|
|
30
|
+
get webchatWebhooksEndpointUrl(): string;
|
|
31
|
+
pusherCustomAuthorize(socketId: string, channelName: string, authContext?: WebchatRealtimeAuthContext): Promise<PusherAuthResponse>;
|
|
32
|
+
/**
|
|
33
|
+
* Fetch webchat settings (Pusher config) from the backend.
|
|
34
|
+
* Response shape: { pusher: { key, cluster, status_channel? } }
|
|
35
|
+
*/
|
|
36
|
+
getWebchatSettings(): Promise<WebchatSettingsResponse>;
|
|
37
|
+
/**
|
|
38
|
+
* Post a message to the backend - matches deprecated implementation exactly
|
|
39
|
+
*/
|
|
40
|
+
postMessage(message: WebchatMessage, user: HubtypeUser): Promise<ApiResponse>;
|
|
41
|
+
/**
|
|
42
|
+
* Send a postback (button click) to the backend - matches deprecated format
|
|
43
|
+
*/
|
|
44
|
+
postPostback(payload: string, user: HubtypeUser, buttonText?: string): Promise<ApiResponse>;
|
|
45
|
+
/**
|
|
46
|
+
* Make HTTP request with proper error handling and timeout
|
|
47
|
+
*/
|
|
48
|
+
private makeRequest;
|
|
49
|
+
/**
|
|
50
|
+
* Update configuration
|
|
51
|
+
*/
|
|
52
|
+
updateConfig(newConfig: Partial<HubtypeApiClientConfig>): void;
|
|
53
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { __awaiter } from "tslib";
|
|
2
|
+
import { MessageSentBy, WebchatMessageFactory, } from '@botonic/shared';
|
|
3
|
+
/**
|
|
4
|
+
* Hubtype API client for webchat functionality
|
|
5
|
+
* Handles HTTP requests to Hubtype backend for webchat operations
|
|
6
|
+
*/
|
|
7
|
+
const WEBCHAT_SETTINGS_PATH = '/webhooks/webchat/v2/settings/';
|
|
8
|
+
export class HubtypeApiClient {
|
|
9
|
+
constructor(config) {
|
|
10
|
+
this.config = config;
|
|
11
|
+
this.baseApiUrl = config.apiUrl || 'https://api.hubtype.com';
|
|
12
|
+
this._authEndpointUrl = `${this.baseApiUrl}/webhooks/webchat/v2/${config.appId}/auth/`;
|
|
13
|
+
this._webchatWebhooksEndpointUrl = `${this.baseApiUrl}/webhooks/webchat/v2/${config.appId}/`;
|
|
14
|
+
this._webchatSettingsUrl = `${this.baseApiUrl}${WEBCHAT_SETTINGS_PATH}`;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Get the auth endpoint URL
|
|
18
|
+
*/
|
|
19
|
+
get authEndpointUrl() {
|
|
20
|
+
return this._authEndpointUrl;
|
|
21
|
+
}
|
|
22
|
+
get webchatWebhooksEndpointUrl() {
|
|
23
|
+
return this._webchatWebhooksEndpointUrl;
|
|
24
|
+
}
|
|
25
|
+
pusherCustomAuthorize(socketId, channelName, authContext) {
|
|
26
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
27
|
+
const authEndpointUrl = this._authEndpointUrl;
|
|
28
|
+
const headers = {
|
|
29
|
+
'Content-Type': 'application/json',
|
|
30
|
+
};
|
|
31
|
+
if (authContext === null || authContext === void 0 ? void 0 : authContext.userId) {
|
|
32
|
+
headers['X-Botonic-User-Id'] = authContext.userId;
|
|
33
|
+
}
|
|
34
|
+
if (authContext === null || authContext === void 0 ? void 0 : authContext.lastMessageId) {
|
|
35
|
+
headers['X-Botonic-Last-Message-Id'] = authContext.lastMessageId;
|
|
36
|
+
}
|
|
37
|
+
if (authContext === null || authContext === void 0 ? void 0 : authContext.lastMessageUpdateDate) {
|
|
38
|
+
headers['X-Botonic-Last-Message-Update-Date'] =
|
|
39
|
+
authContext.lastMessageUpdateDate;
|
|
40
|
+
}
|
|
41
|
+
const response = yield fetch(authEndpointUrl, {
|
|
42
|
+
method: 'POST',
|
|
43
|
+
headers,
|
|
44
|
+
body: JSON.stringify({
|
|
45
|
+
socket_id: socketId,
|
|
46
|
+
channel_name: channelName,
|
|
47
|
+
}),
|
|
48
|
+
});
|
|
49
|
+
if (!response.ok) {
|
|
50
|
+
throw new Error(`Auth request failed: ${response.status}`);
|
|
51
|
+
}
|
|
52
|
+
return yield response.json();
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Fetch webchat settings (Pusher config) from the backend.
|
|
57
|
+
* Response shape: { pusher: { key, cluster, status_channel? } }
|
|
58
|
+
*/
|
|
59
|
+
getWebchatSettings() {
|
|
60
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
61
|
+
var _a, _b;
|
|
62
|
+
const response = yield fetch(this._webchatSettingsUrl, {
|
|
63
|
+
method: 'GET',
|
|
64
|
+
headers: { Accept: 'application/json' },
|
|
65
|
+
});
|
|
66
|
+
if (!response.ok) {
|
|
67
|
+
throw new Error(`Webchat settings request failed: ${response.status} ${response.statusText}`);
|
|
68
|
+
}
|
|
69
|
+
const data = (yield response.json());
|
|
70
|
+
if (!((_a = data === null || data === void 0 ? void 0 : data.pusher) === null || _a === void 0 ? void 0 : _a.key) || !((_b = data === null || data === void 0 ? void 0 : data.pusher) === null || _b === void 0 ? void 0 : _b.cluster)) {
|
|
71
|
+
throw new Error('Invalid webchat settings response: missing pusher.key or pusher.cluster');
|
|
72
|
+
}
|
|
73
|
+
return data;
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Post a message to the backend - matches deprecated implementation exactly
|
|
78
|
+
*/
|
|
79
|
+
postMessage(message, user) {
|
|
80
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
81
|
+
console.log('incoming message', message);
|
|
82
|
+
try {
|
|
83
|
+
const hubtypeMessage = {
|
|
84
|
+
message: message,
|
|
85
|
+
sender: user,
|
|
86
|
+
};
|
|
87
|
+
const response = yield this.makeRequest('POST', '', hubtypeMessage);
|
|
88
|
+
return {
|
|
89
|
+
success: response.ok,
|
|
90
|
+
data: response.ok ? yield response.json() : null,
|
|
91
|
+
error: response.ok
|
|
92
|
+
? undefined
|
|
93
|
+
: `HTTP ${response.status}: ${response.statusText}`,
|
|
94
|
+
status: response.status,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
console.error('Error posting message:', error);
|
|
99
|
+
return {
|
|
100
|
+
success: false,
|
|
101
|
+
error: error instanceof Error ? error.message : 'Unknown error',
|
|
102
|
+
status: 0,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Send a postback (button click) to the backend - matches deprecated format
|
|
109
|
+
*/
|
|
110
|
+
postPostback(payload, user, buttonText) {
|
|
111
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
112
|
+
try {
|
|
113
|
+
// Create postback message in same format as deprecated implementation
|
|
114
|
+
const postbackMessage = WebchatMessageFactory.createPostback({
|
|
115
|
+
payload: buttonText || '',
|
|
116
|
+
metadata: {
|
|
117
|
+
sent_by: MessageSentBy.User,
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
const hubtypeMessage = {
|
|
121
|
+
message: postbackMessage,
|
|
122
|
+
sender: user,
|
|
123
|
+
};
|
|
124
|
+
const response = yield this.makeRequest('POST', '', hubtypeMessage);
|
|
125
|
+
return {
|
|
126
|
+
success: response.ok,
|
|
127
|
+
data: response.ok ? yield response.json() : null,
|
|
128
|
+
error: response.ok
|
|
129
|
+
? undefined
|
|
130
|
+
: `HTTP ${response.status}: ${response.statusText}`,
|
|
131
|
+
status: response.status,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
console.error('Error posting postback:', error);
|
|
136
|
+
return {
|
|
137
|
+
success: false,
|
|
138
|
+
error: error instanceof Error ? error.message : 'Unknown error',
|
|
139
|
+
status: 0,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Make HTTP request with proper error handling and timeout
|
|
146
|
+
*/
|
|
147
|
+
makeRequest(method, endpoint, body) {
|
|
148
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
149
|
+
const url = `${this._webchatWebhooksEndpointUrl}${endpoint}`;
|
|
150
|
+
const timeout = this.config.timeout || 10000; // 10 seconds default
|
|
151
|
+
const controller = new AbortController();
|
|
152
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
153
|
+
try {
|
|
154
|
+
const response = yield fetch(url, {
|
|
155
|
+
method,
|
|
156
|
+
headers: {
|
|
157
|
+
'Content-Type': 'application/json',
|
|
158
|
+
Accept: 'application/json',
|
|
159
|
+
},
|
|
160
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
161
|
+
signal: controller.signal,
|
|
162
|
+
});
|
|
163
|
+
clearTimeout(timeoutId);
|
|
164
|
+
return response;
|
|
165
|
+
}
|
|
166
|
+
catch (error) {
|
|
167
|
+
clearTimeout(timeoutId);
|
|
168
|
+
if (error instanceof Error && error.name === 'AbortError') {
|
|
169
|
+
throw new Error(`Request timeout after ${timeout}ms`);
|
|
170
|
+
}
|
|
171
|
+
throw error;
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Update configuration
|
|
177
|
+
*/
|
|
178
|
+
updateConfig(newConfig) {
|
|
179
|
+
this.config = Object.assign(Object.assign({}, this.config), newConfig);
|
|
180
|
+
if (newConfig.appId || newConfig.apiUrl) {
|
|
181
|
+
const apiUrl = this.config.apiUrl ||
|
|
182
|
+
(typeof window !== 'undefined' &&
|
|
183
|
+
window.VITE_HUBTYPE_API_URL) ||
|
|
184
|
+
'https://api.hubtype.com';
|
|
185
|
+
this._webchatWebhooksEndpointUrl = `${apiUrl}/v2/webhooks/webchat/${this.config.appId}/`;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
//# sourceMappingURL=hubtype-api.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hubtype-api.js","sourceRoot":"","sources":["../../../../../../../../libs/botonic/webchat-core/src/lib/infra/clients/hubtype-api.ts"],"names":[],"mappings":";AAAA,OAAO,EAEL,aAAa,EAGb,qBAAqB,GACtB,MAAM,iBAAiB,CAAA;AAuBxB;;;GAGG;AACH,MAAM,qBAAqB,GAAG,gCAAgC,CAAA;AAE9D,MAAM,OAAO,gBAAgB;IAO3B,YAAY,MAA8B;QACxC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QACpB,IAAI,CAAC,UAAU,GAAG,MAAM,CAAC,MAAM,IAAI,yBAAyB,CAAA;QAC5D,IAAI,CAAC,gBAAgB,GAAG,GAAG,IAAI,CAAC,UAAU,wBAAwB,MAAM,CAAC,KAAK,QAAQ,CAAA;QACtF,IAAI,CAAC,2BAA2B,GAAG,GAAG,IAAI,CAAC,UAAU,wBAAwB,MAAM,CAAC,KAAK,GAAG,CAAA;QAC5F,IAAI,CAAC,mBAAmB,GAAG,GAAG,IAAI,CAAC,UAAU,GAAG,qBAAqB,EAAE,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Y,EAAE,CAAC;oBAC1D,MAAM,IAAI,KAAK,CAAC,yBAAyB,OAAO,IAAI,CAAC,CAAA;gBACvD,CAAC;gBACD,MAAM,KAAK,CAAA;YACb,CAAC;QACH,CAAC;KAAA;IAED;;OAEG;IACH,YAAY,CAAC,SAA0C;QACrD,IAAI,CAAC,MAAM,mCAAQ,IAAI,CAAC,MAAM,GAAK,SAAS,CAAE,CAAA;QAC9C,IAAI,SAAS,CAAC,KAAK,IAAI,SAAS,CAAC,MAAM,EAAE,CAAC;YACxC,MAAM,MAAM,GACV,IAAI,CAAC,MAAM,CAAC,MAAM;gBAClB,CAAC,OAAO,MAAM,KAAK,WAAW;oBAC3B,MAAc,CAAC,oBAAoB,CAAC;gBACvC,yBAAyB,CAAA;YAC3B,IAAI,CAAC,2BAA2B,GAAG,GAAG,MAAM,wBAAwB,IAAI,CAAC,MAAM,CAAC,KAAK,GAAG,CAAA;QAC1F,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../../../libs/botonic/webchat-core/src/lib/infra/clients/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAC1B,cAAc,SAAS,CAAA"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { Channel } from 'pusher-js';
|
|
2
|
+
import { ConnectionStatus } from '../../domain';
|
|
3
|
+
import { WebchatRealtimeAuthContext } from './hubtype-api';
|
|
4
|
+
import type { PusherAuthResponse, PusherCustomAuthorizerFunction } from './types';
|
|
5
|
+
export interface RealtimeClientConfig {
|
|
6
|
+
pusherAppKey: string;
|
|
7
|
+
pusherCluster: string;
|
|
8
|
+
customAuthorizer: PusherCustomAuthorizerFunction;
|
|
9
|
+
activityTimeout?: number;
|
|
10
|
+
pongTimeout?: number;
|
|
11
|
+
maxReconnectionAttempts?: number;
|
|
12
|
+
reconnectionDelay?: number;
|
|
13
|
+
enableHeartbeat?: boolean;
|
|
14
|
+
onAuthData?: (pusherAuthResponse: PusherAuthResponse) => void;
|
|
15
|
+
}
|
|
16
|
+
export interface RealtimeChannel {
|
|
17
|
+
name: string;
|
|
18
|
+
channel: Channel;
|
|
19
|
+
isSubscribed: boolean;
|
|
20
|
+
}
|
|
21
|
+
export interface ChannelEventCallback {
|
|
22
|
+
(data: any): void;
|
|
23
|
+
}
|
|
24
|
+
export interface ConnectionState {
|
|
25
|
+
status: ConnectionStatus;
|
|
26
|
+
lastConnected?: Date;
|
|
27
|
+
connectionAttempts: number;
|
|
28
|
+
lastError?: any;
|
|
29
|
+
}
|
|
30
|
+
export interface States {
|
|
31
|
+
previous: ConnectionStatus;
|
|
32
|
+
current: ConnectionStatus;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Improved platform-agnostic realtime client
|
|
36
|
+
* Handles WebSocket connections via Pusher with encryption
|
|
37
|
+
*/
|
|
38
|
+
export declare class RealtimeClient {
|
|
39
|
+
private authContext?;
|
|
40
|
+
private pusher?;
|
|
41
|
+
private subscribedChannels;
|
|
42
|
+
private eventCallbacks;
|
|
43
|
+
isInitialized: boolean;
|
|
44
|
+
private _connectionState;
|
|
45
|
+
private maxReconnectionAttempts;
|
|
46
|
+
private reconnectionDelay;
|
|
47
|
+
private reconnectionTimer?;
|
|
48
|
+
private heartbeatInterval?;
|
|
49
|
+
private enableHeartbeat;
|
|
50
|
+
private onConnectionStateChange?;
|
|
51
|
+
initialize(config: RealtimeClientConfig, authContext?: WebchatRealtimeAuthContext): void;
|
|
52
|
+
private setupConnectionHandlers;
|
|
53
|
+
private updateConnectionState;
|
|
54
|
+
private scheduleReconnection;
|
|
55
|
+
private attemptReconnection;
|
|
56
|
+
private startHeartbeat;
|
|
57
|
+
private stopHeartbeat;
|
|
58
|
+
/**
|
|
59
|
+
* Subscribe to a channel - matches original pattern
|
|
60
|
+
*/
|
|
61
|
+
subscribe(channelName: string, subscriptionCallback?: () => void): Channel | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* Bind an event to a channel - matches original pattern
|
|
64
|
+
*/
|
|
65
|
+
bind(channelName: string, eventName: string, callback: (data: any) => void): void;
|
|
66
|
+
/**
|
|
67
|
+
* Unbind an event from a channel - matches original pattern
|
|
68
|
+
*/
|
|
69
|
+
unbind(channelName: string, eventName: string, callback: (data: any) => void): void;
|
|
70
|
+
/**
|
|
71
|
+
* Bind event listener to a channel
|
|
72
|
+
*/
|
|
73
|
+
bindChannelEvent(channelName: string, eventName: string, callback: ChannelEventCallback): void;
|
|
74
|
+
/**
|
|
75
|
+
* Unbind event listener from a channel
|
|
76
|
+
*/
|
|
77
|
+
unbindChannelEvent(channelName: string, eventName: string, callback?: ChannelEventCallback): void;
|
|
78
|
+
/**
|
|
79
|
+
* Unsubscribe from a channel
|
|
80
|
+
*/
|
|
81
|
+
unsubscribeFromChannel(channelName: string): void;
|
|
82
|
+
get connectionState(): string;
|
|
83
|
+
get isConnected(): boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Update the last message ID in auth context for reconnection
|
|
86
|
+
*/
|
|
87
|
+
updateLastMessageId(messageId: string): void;
|
|
88
|
+
/**
|
|
89
|
+
* Disconnect and cleanup
|
|
90
|
+
*/
|
|
91
|
+
disconnect(): void;
|
|
92
|
+
}
|