@botpress/webchat-client 0.2.0 → 0.3.1
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/.turbo/turbo-build.log +6 -6
- package/dist/index.d.ts +165 -13
- package/dist/webchat-client.js +5 -3
- package/dist/webchat-client2.js +118 -82
- package/dist/webchat-client3.js +28 -20
- package/package.json +6 -3
- package/src/client/client.ts +111 -28
- package/src/client/types/signals/index.ts +2 -0
- package/src/client/types/signals/initialize.ts +18 -0
- package/src/index.ts +2 -0
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
> @botpress/webchat-client@0.
|
|
2
|
+
> @botpress/webchat-client@0.3.1 build /home/runner/work/genisys/genisys/packages/webchat-client
|
|
3
3
|
> openapi-ts && vite build
|
|
4
4
|
|
|
5
5
|
⏳ Generating from raw OpenAPI specification
|
|
@@ -11,19 +11,19 @@ rendering chunks...
|
|
|
11
11
|
|
|
12
12
|
[vite:dts] Start generate declaration files...
|
|
13
13
|
computing gzip size...
|
|
14
|
-
[2mdist/[22m[36mwebchat-client.js [39m[1m[2m 0.17 kB[22m[1m[22m[2m │ gzip: 0.13 kB[22m
|
|
15
14
|
[2mdist/[22m[36mwebchat-client8.js [39m[1m[2m 0.19 kB[22m[1m[22m[2m │ gzip: 0.16 kB[22m
|
|
15
|
+
[2mdist/[22m[36mwebchat-client.js [39m[1m[2m 0.25 kB[22m[1m[22m[2m │ gzip: 0.16 kB[22m
|
|
16
16
|
[2mdist/[22m[36mwebchat-client4.js [39m[1m[2m 0.29 kB[22m[1m[22m[2m │ gzip: 0.18 kB[22m
|
|
17
17
|
[2mdist/[22m[36mwebchat-client6.js [39m[1m[2m 0.30 kB[22m[1m[22m[2m │ gzip: 0.22 kB[22m
|
|
18
18
|
[2mdist/[22m[36mwebchat-client7.js [39m[1m[2m 0.45 kB[22m[1m[22m[2m │ gzip: 0.26 kB[22m
|
|
19
19
|
[2mdist/[22m[36mwebchat-client9.js [39m[1m[2m 2.63 kB[22m[1m[22m[2m │ gzip: 1.15 kB[22m
|
|
20
|
-
[2mdist/[22m[36mwebchat-client3.js [39m[1m[2m 3.
|
|
21
|
-
[2mdist/[22m[36mwebchat-client2.js [39m[1m[2m
|
|
20
|
+
[2mdist/[22m[36mwebchat-client3.js [39m[1m[2m 3.50 kB[22m[1m[22m[2m │ gzip: 0.60 kB[22m
|
|
21
|
+
[2mdist/[22m[36mwebchat-client2.js [39m[1m[2m 5.66 kB[22m[1m[22m[2m │ gzip: 1.60 kB[22m
|
|
22
22
|
[2mdist/[22m[36mwebchat-client10.js [39m[1m[2m 8.58 kB[22m[1m[22m[2m │ gzip: 2.91 kB[22m
|
|
23
23
|
[2mdist/[22m[36mwebchat-client5.js [39m[1m[2m10.93 kB[22m[1m[22m[2m │ gzip: 3.47 kB[22m
|
|
24
24
|
[vite:dts] Start rollup declaration files...
|
|
25
25
|
Analysis will use the bundled TypeScript version 5.4.2
|
|
26
26
|
*** The target project appears to use TypeScript 5.6.2 which is newer than the bundled compiler engine; consider upgrading API Extractor.
|
|
27
|
-
[vite:dts] Declaration files built in
|
|
27
|
+
[vite:dts] Declaration files built in 4664ms.
|
|
28
28
|
|
|
29
|
-
[32m✓ built in
|
|
29
|
+
[32m✓ built in 4.95s[39m
|
package/dist/index.d.ts
CHANGED
|
@@ -186,17 +186,16 @@ export declare type Client = {
|
|
|
186
186
|
getMessage: (params: MessageIdParam) => Promise<ClientTypes_2.GetMessageResponse>;
|
|
187
187
|
deleteMessage: (params: MessageIdParam) => Promise<ClientTypes_2.DeleteMessageResponse>;
|
|
188
188
|
createFile: (params: ClientTypes_2.CreateFileBody) => Promise<ClientTypes_2.CreateFileResponse>;
|
|
189
|
-
createUser: (params: ClientTypes_2.CreateUserBody) => Promise<ClientTypes_2.CreateUserResponse>;
|
|
190
189
|
getUser: () => Promise<ClientTypes_2.GetUserResponse>;
|
|
191
190
|
updateUser: (params: ClientTypes_2.CreateUserBody) => Promise<ClientTypes_2.UpdateUserResponse>;
|
|
192
191
|
deleteUser: (params: UserIdParam) => Promise<ClientTypes_2.DeleteUserResponse>;
|
|
193
192
|
createEvent: (params: ClientTypes_2.CreateEventBody) => Promise<ClientTypes_2.CreateEventResponse>;
|
|
194
193
|
getEvent: (params: EventIdParam) => Promise<ClientTypes_2.GetEventResponse>;
|
|
195
|
-
listenConversation: (params: ConversationIdParam) => EventEmitter<
|
|
194
|
+
listenConversation: (params: ConversationIdParam) => EventEmitter<EventMap>['on'];
|
|
196
195
|
addMessageFeedback: (params: MessageIdParam & ClientTypes_2.AddMessageFeedbackBody) => Promise<ClientTypes_2.AddMessageFeedbackResponse>;
|
|
197
196
|
removeMessageFeedback: (params: MessageIdParam) => Promise<ClientTypes_2.RemoveMessageFeedbackResponse>;
|
|
198
197
|
generateUserKey: (params: ClientTypes_2.GenerateUserKeyBody & {
|
|
199
|
-
|
|
198
|
+
adminSecret: string;
|
|
200
199
|
}) => Promise<ClientTypes_2.GenerateUserKeyResponse>;
|
|
201
200
|
};
|
|
202
201
|
|
|
@@ -220,6 +219,7 @@ declare namespace ClientTypes_2 {
|
|
|
220
219
|
Message,
|
|
221
220
|
Event_2 as Event,
|
|
222
221
|
File_2 as File,
|
|
222
|
+
Perf,
|
|
223
223
|
CreateConversationBody,
|
|
224
224
|
AddParticipantBody,
|
|
225
225
|
CreateMessageBody,
|
|
@@ -231,6 +231,7 @@ declare namespace ClientTypes_2 {
|
|
|
231
231
|
UpdateUserBody,
|
|
232
232
|
CreateEventBody,
|
|
233
233
|
CreateFileBody,
|
|
234
|
+
SendPerfMetricsBody,
|
|
234
235
|
DeleteConversationData,
|
|
235
236
|
DeleteConversationErrors,
|
|
236
237
|
DeleteConversationError,
|
|
@@ -356,6 +357,16 @@ declare namespace ClientTypes_2 {
|
|
|
356
357
|
CreateFileError,
|
|
357
358
|
CreateFileResponses,
|
|
358
359
|
CreateFileResponse,
|
|
360
|
+
InitializeConversationData,
|
|
361
|
+
InitializeConversationErrors,
|
|
362
|
+
InitializeConversationError,
|
|
363
|
+
InitializeConversationResponses,
|
|
364
|
+
InitializeConversationResponse,
|
|
365
|
+
SendPerfMetricsData,
|
|
366
|
+
SendPerfMetricsErrors,
|
|
367
|
+
SendPerfMetricsError,
|
|
368
|
+
SendPerfMetricsResponses,
|
|
369
|
+
SendPerfMetricsResponse,
|
|
359
370
|
ClientOptions,
|
|
360
371
|
Options,
|
|
361
372
|
deleteConversation,
|
|
@@ -382,7 +393,9 @@ declare namespace ClientTypes_2 {
|
|
|
382
393
|
generateUserKey_2 as generateUserKey,
|
|
383
394
|
getEvent,
|
|
384
395
|
createEvent,
|
|
385
|
-
createFile
|
|
396
|
+
createFile,
|
|
397
|
+
initializeConversation,
|
|
398
|
+
sendPerfMetrics_2 as sendPerfMetrics
|
|
386
399
|
}
|
|
387
400
|
}
|
|
388
401
|
|
|
@@ -489,10 +502,11 @@ declare type ConversationIdParam = {
|
|
|
489
502
|
conversationId: string;
|
|
490
503
|
};
|
|
491
504
|
|
|
492
|
-
export declare const createClient: ({ userKey, clientId, apiUrl, }: {
|
|
505
|
+
export declare const createClient: ({ userKey, clientId, apiUrl, customHeaders, }: {
|
|
493
506
|
userKey: string;
|
|
494
507
|
clientId: string;
|
|
495
508
|
apiUrl?: string;
|
|
509
|
+
customHeaders?: Record<string, string>;
|
|
496
510
|
}) => Client;
|
|
497
511
|
|
|
498
512
|
/**
|
|
@@ -888,11 +902,12 @@ export declare type CreateMessageResponses = {
|
|
|
888
902
|
};
|
|
889
903
|
};
|
|
890
904
|
|
|
891
|
-
export declare const createUser: ({ clientId, apiUrl,
|
|
905
|
+
export declare const createUser: ({ clientId, apiUrl, adminSecret, customHeaders, ...body }: {
|
|
892
906
|
clientId: string;
|
|
893
907
|
apiUrl?: string;
|
|
894
|
-
|
|
895
|
-
|
|
908
|
+
adminSecret?: string;
|
|
909
|
+
customHeaders?: Record<string, string>;
|
|
910
|
+
} & ClientTypes_2.CreateUserBody) => Promise<{
|
|
896
911
|
user: ClientTypes_2.User;
|
|
897
912
|
key: string;
|
|
898
913
|
}>;
|
|
@@ -935,7 +950,7 @@ export declare type CreateUserData = {
|
|
|
935
950
|
/**
|
|
936
951
|
* Admin Key for managing users
|
|
937
952
|
*/
|
|
938
|
-
'x-admin-
|
|
953
|
+
'x-admin-secret'?: string;
|
|
939
954
|
};
|
|
940
955
|
path?: never;
|
|
941
956
|
query?: never;
|
|
@@ -1166,6 +1181,10 @@ declare type EventIdParam = {
|
|
|
1166
1181
|
eventId: string;
|
|
1167
1182
|
};
|
|
1168
1183
|
|
|
1184
|
+
declare type EventMap = {
|
|
1185
|
+
[E in Event_3 as E['type']]: E['data'];
|
|
1186
|
+
};
|
|
1187
|
+
|
|
1169
1188
|
declare type File_2 = {
|
|
1170
1189
|
id: string;
|
|
1171
1190
|
botId: string;
|
|
@@ -1186,8 +1205,8 @@ declare type File_2 = {
|
|
|
1186
1205
|
};
|
|
1187
1206
|
export { File_2 as File }
|
|
1188
1207
|
|
|
1189
|
-
export declare const generateUserKey: ({
|
|
1190
|
-
|
|
1208
|
+
export declare const generateUserKey: ({ adminSecret, expiresAt, userId, apiUrl, clientId, }: {
|
|
1209
|
+
adminSecret: string;
|
|
1191
1210
|
expiresAt: number;
|
|
1192
1211
|
userId: string;
|
|
1193
1212
|
apiUrl?: string;
|
|
@@ -1228,7 +1247,7 @@ export declare type GenerateUserKeyData = {
|
|
|
1228
1247
|
/**
|
|
1229
1248
|
* Admin Key for managing users
|
|
1230
1249
|
*/
|
|
1231
|
-
'x-admin-
|
|
1250
|
+
'x-admin-secret'?: string;
|
|
1232
1251
|
};
|
|
1233
1252
|
path?: never;
|
|
1234
1253
|
query?: never;
|
|
@@ -1501,6 +1520,79 @@ export declare type GetUserResponses = {
|
|
|
1501
1520
|
};
|
|
1502
1521
|
};
|
|
1503
1522
|
|
|
1523
|
+
declare type Initialize = {
|
|
1524
|
+
type: 'init';
|
|
1525
|
+
data: {
|
|
1526
|
+
conversation: GetConversationResponse['conversation'];
|
|
1527
|
+
messages: ListConversationMessagesResponse['messages'];
|
|
1528
|
+
participants: ListParticipantsResponse['participants'];
|
|
1529
|
+
user: GetUserResponse['user'] & {
|
|
1530
|
+
userKey: string;
|
|
1531
|
+
};
|
|
1532
|
+
};
|
|
1533
|
+
};
|
|
1534
|
+
|
|
1535
|
+
export declare const initialize: ({ conversationId, userToken, clientId, apiUrl, adminSecret, }: {
|
|
1536
|
+
conversationId?: string;
|
|
1537
|
+
userToken?: string;
|
|
1538
|
+
apiUrl?: string;
|
|
1539
|
+
clientId: string;
|
|
1540
|
+
adminSecret?: string;
|
|
1541
|
+
}) => <U extends "custom" | "error" | "message_created" | "message_updated" | "typing_started" | "typing_stopped" | "webchat_config" | "webchat_visibility" | "participant_added" | "participant_removed" | "message_status_changed" | "init" | "open">(event: U, callback: (args: EventMap[U]) => void) => () => void;
|
|
1542
|
+
|
|
1543
|
+
/**
|
|
1544
|
+
* Creates a SSE stream to receive messages and events. The first event will be a payload containing the conversation details.
|
|
1545
|
+
*/
|
|
1546
|
+
declare const initializeConversation: <ThrowOnError extends boolean = false>(options?: Options<InitializeConversationData, ThrowOnError>) => RequestResult<{
|
|
1547
|
+
[key: string]: never;
|
|
1548
|
+
}, {
|
|
1549
|
+
[key: string]: never;
|
|
1550
|
+
}, ThrowOnError>;
|
|
1551
|
+
|
|
1552
|
+
export declare type InitializeConversationData = {
|
|
1553
|
+
body?: never;
|
|
1554
|
+
headers?: {
|
|
1555
|
+
/**
|
|
1556
|
+
* Authentication Key
|
|
1557
|
+
*/
|
|
1558
|
+
'x-user-key'?: string;
|
|
1559
|
+
};
|
|
1560
|
+
path?: never;
|
|
1561
|
+
query?: {
|
|
1562
|
+
/**
|
|
1563
|
+
* User id (if not provided with a user key a new user will be created)
|
|
1564
|
+
*/
|
|
1565
|
+
userId?: string;
|
|
1566
|
+
/**
|
|
1567
|
+
* Conversation id
|
|
1568
|
+
*/
|
|
1569
|
+
conversationId?: string;
|
|
1570
|
+
};
|
|
1571
|
+
url: '/initialize';
|
|
1572
|
+
};
|
|
1573
|
+
|
|
1574
|
+
export declare type InitializeConversationError = InitializeConversationErrors[keyof InitializeConversationErrors];
|
|
1575
|
+
|
|
1576
|
+
export declare type InitializeConversationErrors = {
|
|
1577
|
+
/**
|
|
1578
|
+
* Returns nothing but a stream
|
|
1579
|
+
*/
|
|
1580
|
+
default: {
|
|
1581
|
+
[key: string]: never;
|
|
1582
|
+
};
|
|
1583
|
+
};
|
|
1584
|
+
|
|
1585
|
+
export declare type InitializeConversationResponse = InitializeConversationResponses[keyof InitializeConversationResponses];
|
|
1586
|
+
|
|
1587
|
+
export declare type InitializeConversationResponses = {
|
|
1588
|
+
/**
|
|
1589
|
+
* Returns nothing but a stream
|
|
1590
|
+
*/
|
|
1591
|
+
200: {
|
|
1592
|
+
[key: string]: never;
|
|
1593
|
+
};
|
|
1594
|
+
};
|
|
1595
|
+
|
|
1504
1596
|
export declare type IntegrationError = {
|
|
1505
1597
|
id: string;
|
|
1506
1598
|
code: number;
|
|
@@ -2611,6 +2703,12 @@ declare type ParticipantRemoved = {
|
|
|
2611
2703
|
};
|
|
2612
2704
|
};
|
|
2613
2705
|
|
|
2706
|
+
export declare type Perf = {
|
|
2707
|
+
duration: number;
|
|
2708
|
+
event: string;
|
|
2709
|
+
properties: {};
|
|
2710
|
+
};
|
|
2711
|
+
|
|
2614
2712
|
declare type QuerySerializer = (query: Record<string, unknown>) => string;
|
|
2615
2713
|
|
|
2616
2714
|
declare interface QuerySerializerOptions {
|
|
@@ -2819,6 +2917,60 @@ export declare type SelectMessageResponses = {
|
|
|
2819
2917
|
};
|
|
2820
2918
|
};
|
|
2821
2919
|
|
|
2920
|
+
export declare const sendPerfMetrics: (clientId: string, body: ClientTypes_2.SendPerfMetricsBody, apiUrl?: string, customHeaders?: Record<string, string>) => Promise<{
|
|
2921
|
+
[key: string]: never;
|
|
2922
|
+
}>;
|
|
2923
|
+
|
|
2924
|
+
/**
|
|
2925
|
+
* Sends performance metrics to the server.
|
|
2926
|
+
*/
|
|
2927
|
+
declare const sendPerfMetrics_2: <ThrowOnError extends boolean = false>(options?: Options<SendPerfMetricsData, ThrowOnError>) => RequestResult<{
|
|
2928
|
+
[key: string]: never;
|
|
2929
|
+
}, {
|
|
2930
|
+
[key: string]: never;
|
|
2931
|
+
}, ThrowOnError>;
|
|
2932
|
+
|
|
2933
|
+
/**
|
|
2934
|
+
* Performance metrics
|
|
2935
|
+
*/
|
|
2936
|
+
export declare type SendPerfMetricsBody = {
|
|
2937
|
+
duration: number;
|
|
2938
|
+
event: string;
|
|
2939
|
+
properties: {};
|
|
2940
|
+
};
|
|
2941
|
+
|
|
2942
|
+
export declare type SendPerfMetricsData = {
|
|
2943
|
+
/**
|
|
2944
|
+
* Performance metrics
|
|
2945
|
+
*/
|
|
2946
|
+
body?: SendPerfMetricsBody;
|
|
2947
|
+
path?: never;
|
|
2948
|
+
query?: never;
|
|
2949
|
+
url: '/p';
|
|
2950
|
+
};
|
|
2951
|
+
|
|
2952
|
+
export declare type SendPerfMetricsError = SendPerfMetricsErrors[keyof SendPerfMetricsErrors];
|
|
2953
|
+
|
|
2954
|
+
export declare type SendPerfMetricsErrors = {
|
|
2955
|
+
/**
|
|
2956
|
+
* Returns nothing
|
|
2957
|
+
*/
|
|
2958
|
+
default: {
|
|
2959
|
+
[key: string]: never;
|
|
2960
|
+
};
|
|
2961
|
+
};
|
|
2962
|
+
|
|
2963
|
+
export declare type SendPerfMetricsResponse = SendPerfMetricsResponses[keyof SendPerfMetricsResponses];
|
|
2964
|
+
|
|
2965
|
+
export declare type SendPerfMetricsResponses = {
|
|
2966
|
+
/**
|
|
2967
|
+
* Returns nothing
|
|
2968
|
+
*/
|
|
2969
|
+
200: {
|
|
2970
|
+
[key: string]: never;
|
|
2971
|
+
};
|
|
2972
|
+
};
|
|
2973
|
+
|
|
2822
2974
|
declare interface SerializerOptions<T> {
|
|
2823
2975
|
/**
|
|
2824
2976
|
* @default true
|
|
@@ -2827,7 +2979,7 @@ declare interface SerializerOptions<T> {
|
|
|
2827
2979
|
style: T;
|
|
2828
2980
|
}
|
|
2829
2981
|
|
|
2830
|
-
declare type Signal = MessageCreated | MessageUpdated | WebchatVisibility | WebchatConfig | Custom | TypingStarted | TypingStopped | ParticipantAdded | ParticipantRemoved | MessageStatusChanged;
|
|
2982
|
+
declare type Signal = MessageCreated | MessageUpdated | WebchatVisibility | WebchatConfig | Custom | TypingStarted | TypingStopped | ParticipantAdded | ParticipantRemoved | MessageStatusChanged | Initialize;
|
|
2831
2983
|
|
|
2832
2984
|
declare interface TDataShape {
|
|
2833
2985
|
body?: unknown;
|
package/dist/webchat-client.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { createClient as t, createUser as
|
|
1
|
+
import { createClient as t, createUser as i, generateUserKey as a, initialize as n, sendPerfMetrics as s } from "./webchat-client2.js";
|
|
2
2
|
export {
|
|
3
3
|
t as createClient,
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
i as createUser,
|
|
5
|
+
a as generateUserKey,
|
|
6
|
+
n as initialize,
|
|
7
|
+
s as sendPerfMetrics
|
|
6
8
|
};
|
package/dist/webchat-client2.js
CHANGED
|
@@ -1,111 +1,147 @@
|
|
|
1
|
-
import { createUser as
|
|
2
|
-
import { client as
|
|
3
|
-
import { EventSource as
|
|
4
|
-
import { throwErrorInterceptor as
|
|
5
|
-
import { createEventEmitter as
|
|
6
|
-
const n = (a) => a.data, v = {
|
|
1
|
+
import { createUser as x, generateUserKey as y, sendPerfMetrics as k, removeMessageFeedback as F, addMessageFeedback as T, getEvent as R, createEvent as w, deleteUser as I, updateUser as K, getUser as O, createFile as S, deleteMessage as _, getMessage as q, updateMessage as N, createMessage as z, listParticipants as A, getParticipant as J, removeParticipant as j, addParticipant as D, listConversationMessages as L, listConversations as V, deleteConversation as X, getConversation as Y, createConversation as B } from "./webchat-client3.js";
|
|
2
|
+
import { client as l } from "./webchat-client4.js";
|
|
3
|
+
import { EventSource as G } from "./webchat-client5.js";
|
|
4
|
+
import { throwErrorInterceptor as b } from "./webchat-client6.js";
|
|
5
|
+
import { createEventEmitter as C } from "./webchat-client7.js";
|
|
6
|
+
const n = (a) => a.data, $ = 10, M = 3e3, H = "60000", v = {
|
|
7
7
|
throwOnError: !0
|
|
8
|
-
},
|
|
8
|
+
}, se = async ({
|
|
9
9
|
clientId: a,
|
|
10
|
-
apiUrl:
|
|
11
|
-
|
|
10
|
+
apiUrl: c,
|
|
11
|
+
adminSecret: i,
|
|
12
|
+
customHeaders: o,
|
|
13
|
+
...s
|
|
12
14
|
}) => {
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
const
|
|
15
|
+
const d = `${c || l.getConfig().baseUrl}/${a}`;
|
|
16
|
+
l.interceptors.response._fns.length === 0 && l.interceptors.response.use(b);
|
|
17
|
+
const t = {
|
|
16
18
|
...v,
|
|
17
|
-
baseUrl:
|
|
19
|
+
baseUrl: d
|
|
20
|
+
}, h = {
|
|
21
|
+
...i ? { "x-admin-secret": i } : {},
|
|
22
|
+
...o
|
|
18
23
|
};
|
|
19
|
-
return
|
|
20
|
-
...
|
|
21
|
-
|
|
22
|
-
|
|
24
|
+
return x({
|
|
25
|
+
...t,
|
|
26
|
+
headers: Object.keys(h).length > 0 ? h : void 0,
|
|
27
|
+
body: s
|
|
23
28
|
}).then(n);
|
|
24
|
-
},
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
apiUrl:
|
|
29
|
-
|
|
29
|
+
}, ne = ({
|
|
30
|
+
conversationId: a,
|
|
31
|
+
userToken: c,
|
|
32
|
+
clientId: i,
|
|
33
|
+
apiUrl: o,
|
|
34
|
+
adminSecret: s
|
|
30
35
|
}) => {
|
|
31
|
-
|
|
36
|
+
let d = 0, t = null;
|
|
37
|
+
const h = C(), e = () => {
|
|
38
|
+
t && t.close();
|
|
39
|
+
const g = `${`${o || l.getConfig().baseUrl}/${i}`}/initialize${a ? `?conversationId=${a}` : ""}`, p = {
|
|
40
|
+
...s ? { "x-admin-secret": s } : {},
|
|
41
|
+
...c ? { "x-user-key": c } : {}
|
|
42
|
+
};
|
|
43
|
+
t = P(g, p), t.onopen = () => h.emit("open"), t.onerror = (u) => {
|
|
44
|
+
d < $ ? (d++, setTimeout(() => e(), M)) : h.emit("error", u);
|
|
45
|
+
}, t.onmessage = (u) => {
|
|
46
|
+
const m = E(u.data);
|
|
47
|
+
h.emit(m.type, m.data);
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
return e(), h.on;
|
|
51
|
+
}, re = async ({
|
|
52
|
+
adminSecret: a,
|
|
53
|
+
expiresAt: c,
|
|
54
|
+
userId: i,
|
|
55
|
+
apiUrl: o,
|
|
56
|
+
clientId: s
|
|
57
|
+
}) => {
|
|
58
|
+
const d = { "x-admin-secret": a }, t = `${o || l.getConfig().baseUrl}/${s}`, h = {
|
|
32
59
|
...v,
|
|
33
|
-
baseUrl:
|
|
60
|
+
baseUrl: t
|
|
34
61
|
};
|
|
35
|
-
return y({ ...
|
|
36
|
-
},
|
|
62
|
+
return y({ ...h, headers: d, body: { id: i, expiresAt: c } }).then(n);
|
|
63
|
+
}, ae = async (a, c, i, o) => {
|
|
64
|
+
const s = `${i || l.getConfig().baseUrl}/${a}`, d = {
|
|
65
|
+
...v,
|
|
66
|
+
baseUrl: s
|
|
67
|
+
};
|
|
68
|
+
return k({ ...d, body: c, headers: o }).then(n);
|
|
69
|
+
}, oe = ({
|
|
37
70
|
userKey: a,
|
|
38
|
-
clientId:
|
|
39
|
-
apiUrl:
|
|
71
|
+
clientId: c,
|
|
72
|
+
apiUrl: i,
|
|
73
|
+
customHeaders: o
|
|
40
74
|
}) => {
|
|
41
|
-
const
|
|
42
|
-
"x-user-key": a
|
|
43
|
-
|
|
75
|
+
const s = {
|
|
76
|
+
"x-user-key": a,
|
|
77
|
+
...o
|
|
78
|
+
}, d = `${i || l.getConfig().baseUrl}/${c}`, t = {
|
|
44
79
|
...v,
|
|
45
80
|
baseUrl: d
|
|
46
81
|
};
|
|
47
|
-
return
|
|
48
|
-
createConversation: () =>
|
|
49
|
-
getConversation: ({ conversationId: e }) =>
|
|
50
|
-
deleteConversation: ({ conversationId: e }) =>
|
|
51
|
-
listConversations: ({ nextToken: e }) =>
|
|
52
|
-
listConversationMessages: ({ conversationId: e, nextToken: r }) =>
|
|
53
|
-
...
|
|
54
|
-
headers:
|
|
82
|
+
return l.interceptors.response._fns.length === 0 && l.interceptors.response.use(b), {
|
|
83
|
+
createConversation: () => B({ ...t, headers: s, body: {} }).then(n),
|
|
84
|
+
getConversation: ({ conversationId: e }) => Y({ ...t, headers: s, path: { id: e } }).then(n),
|
|
85
|
+
deleteConversation: ({ conversationId: e }) => X({ ...t, headers: s, path: { id: e } }).then(n),
|
|
86
|
+
listConversations: ({ nextToken: e }) => V({ ...t, headers: s, query: { nextToken: e } }).then(n),
|
|
87
|
+
listConversationMessages: ({ conversationId: e, nextToken: r }) => L({
|
|
88
|
+
...t,
|
|
89
|
+
headers: s,
|
|
55
90
|
path: { id: e },
|
|
56
91
|
query: { nextToken: r }
|
|
57
92
|
}).then(n),
|
|
58
|
-
addParticipant: ({ conversationId: e, userId: r }) =>
|
|
59
|
-
removeParticipant: ({ conversationId: e, userId: r }) =>
|
|
60
|
-
getParticipant: ({ conversationId: e, userId: r }) =>
|
|
61
|
-
listParticipants: ({ conversationId: e, nextToken: r }) => A({ ...
|
|
62
|
-
createMessage: (e) =>
|
|
63
|
-
updateMessage: ({ messageId: e, ...r }) =>
|
|
64
|
-
getMessage: ({ messageId: e }) =>
|
|
65
|
-
deleteMessage: ({ messageId: e }) =>
|
|
66
|
-
createFile: (e) =>
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
generateUserKey: ({ adminKey: e, ...r }) => y({ ...s, headers: { "x-admin-key": e }, body: r }).then(n),
|
|
93
|
+
addParticipant: ({ conversationId: e, userId: r }) => D({ ...t, headers: s, path: { id: e }, body: { userId: r } }).then(n),
|
|
94
|
+
removeParticipant: ({ conversationId: e, userId: r }) => j({ ...t, headers: s, path: { id: e, userId: r } }).then(n),
|
|
95
|
+
getParticipant: ({ conversationId: e, userId: r }) => J({ ...t, headers: s, path: { id: e, userId: r } }).then(n),
|
|
96
|
+
listParticipants: ({ conversationId: e, nextToken: r }) => A({ ...t, headers: s, path: { id: e }, query: { nextToken: r } }).then(n),
|
|
97
|
+
createMessage: (e) => z({ ...t, headers: s, body: e }).then(n),
|
|
98
|
+
updateMessage: ({ messageId: e, ...r }) => N({ ...t, headers: s, path: { id: e }, body: r }).then(n),
|
|
99
|
+
getMessage: ({ messageId: e }) => q({ ...t, headers: s, path: { id: e } }).then(n),
|
|
100
|
+
deleteMessage: ({ messageId: e }) => _({ ...t, headers: s, path: { id: e } }).then(n),
|
|
101
|
+
createFile: (e) => S({ ...t, headers: s, body: e }).then(n),
|
|
102
|
+
getUser: () => O({ ...t, headers: s }).then(n),
|
|
103
|
+
updateUser: (e) => K({ ...t, headers: s, body: e }).then(n),
|
|
104
|
+
deleteUser: () => I({ ...t, headers: s }).then(n),
|
|
105
|
+
createEvent: (e) => w({ ...t, headers: s, body: e }).then(n),
|
|
106
|
+
getEvent: ({ eventId: e }) => R({ ...t, headers: s, path: { id: e } }).then(n),
|
|
107
|
+
addMessageFeedback: ({ messageId: e, ...r }) => T({ ...t, headers: s, path: { id: e }, body: r }).then(n),
|
|
108
|
+
removeMessageFeedback: ({ messageId: e }) => F({ ...t, headers: s, path: { id: e } }).then(n),
|
|
109
|
+
generateUserKey: ({ adminSecret: e, ...r }) => y({ ...t, headers: { "x-admin-secret": e }, body: r }).then(n),
|
|
76
110
|
listenConversation: ({ conversationId: e }) => {
|
|
77
|
-
let
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
...t,
|
|
87
|
-
timeout: "60_000"
|
|
88
|
-
}
|
|
89
|
-
})
|
|
90
|
-
}), h.onopen = () => l.emit("open"), h.onerror = (g) => {
|
|
91
|
-
u < 10 ? (u++, setTimeout(() => f(), 3e3)) : l.emit("error", g);
|
|
92
|
-
}, h.onmessage = (g) => {
|
|
93
|
-
const o = D(g.data);
|
|
94
|
-
l.emit(o.type, o.data);
|
|
111
|
+
let r = 0, g = null;
|
|
112
|
+
const p = C(), u = () => {
|
|
113
|
+
g && g.close();
|
|
114
|
+
const m = `${d}/conversations/${e}/listen`;
|
|
115
|
+
g = P(m, s), g.onopen = () => p.emit("open"), g.onerror = (f) => {
|
|
116
|
+
r < $ ? (r++, setTimeout(() => u(), M)) : p.emit("error", f);
|
|
117
|
+
}, g.onmessage = (f) => {
|
|
118
|
+
const U = E(f.data);
|
|
119
|
+
p.emit(U.type, U.data);
|
|
95
120
|
};
|
|
96
121
|
};
|
|
97
|
-
return
|
|
122
|
+
return u(), p.on;
|
|
98
123
|
}
|
|
99
124
|
};
|
|
100
|
-
},
|
|
125
|
+
}, E = (a) => {
|
|
101
126
|
try {
|
|
102
127
|
return JSON.parse(a);
|
|
103
128
|
} catch {
|
|
104
129
|
return a;
|
|
105
130
|
}
|
|
106
|
-
}
|
|
131
|
+
}, P = (a, c) => new G(a, {
|
|
132
|
+
fetch: (i, o) => fetch(i, {
|
|
133
|
+
...o,
|
|
134
|
+
headers: {
|
|
135
|
+
...o == null ? void 0 : o.headers,
|
|
136
|
+
...c,
|
|
137
|
+
timeout: H
|
|
138
|
+
}
|
|
139
|
+
})
|
|
140
|
+
});
|
|
107
141
|
export {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
142
|
+
oe as createClient,
|
|
143
|
+
se as createUser,
|
|
144
|
+
re as generateUserKey,
|
|
145
|
+
ne as initialize,
|
|
146
|
+
ae as sendPerfMetrics
|
|
111
147
|
};
|
package/dist/webchat-client3.js
CHANGED
|
@@ -47,54 +47,54 @@ const t = (e) => (e.client ?? r).delete({
|
|
|
47
47
|
"Content-Type": "application/json",
|
|
48
48
|
...e == null ? void 0 : e.headers
|
|
49
49
|
}
|
|
50
|
-
}),
|
|
50
|
+
}), y = (e) => (e.client ?? r).post({
|
|
51
51
|
url: "/messages",
|
|
52
52
|
...e,
|
|
53
53
|
headers: {
|
|
54
54
|
"Content-Type": "application/json",
|
|
55
55
|
...e == null ? void 0 : e.headers
|
|
56
56
|
}
|
|
57
|
-
}),
|
|
57
|
+
}), j = (e) => (e.client ?? r).delete({
|
|
58
58
|
url: "/messages/{id}/feedback",
|
|
59
59
|
...e
|
|
60
|
-
}),
|
|
60
|
+
}), T = (e) => (e.client ?? r).put({
|
|
61
61
|
url: "/messages/{id}/feedback",
|
|
62
62
|
...e,
|
|
63
63
|
headers: {
|
|
64
64
|
"Content-Type": "application/json",
|
|
65
65
|
...e == null ? void 0 : e.headers
|
|
66
66
|
}
|
|
67
|
-
}),
|
|
67
|
+
}), M = (e) => (e.client ?? r).delete({
|
|
68
68
|
url: "/users/me",
|
|
69
69
|
...e
|
|
70
|
-
}),
|
|
70
|
+
}), i = (e) => (e.client ?? r).get({
|
|
71
71
|
url: "/users/me",
|
|
72
72
|
...e
|
|
73
|
-
}),
|
|
73
|
+
}), f = (e) => (e.client ?? r).put({
|
|
74
74
|
url: "/users/me",
|
|
75
75
|
...e,
|
|
76
76
|
headers: {
|
|
77
77
|
"Content-Type": "application/json",
|
|
78
78
|
...e == null ? void 0 : e.headers
|
|
79
79
|
}
|
|
80
|
-
}),
|
|
80
|
+
}), k = (e) => ((e == null ? void 0 : e.client) ?? r).post({
|
|
81
81
|
url: "/users",
|
|
82
82
|
...e,
|
|
83
83
|
headers: {
|
|
84
84
|
"Content-Type": "application/json",
|
|
85
85
|
...e == null ? void 0 : e.headers
|
|
86
86
|
}
|
|
87
|
-
}),
|
|
87
|
+
}), P = (e) => ((e == null ? void 0 : e.client) ?? r).post({
|
|
88
88
|
url: "/users/key",
|
|
89
89
|
...e,
|
|
90
90
|
headers: {
|
|
91
91
|
"Content-Type": "application/json",
|
|
92
92
|
...e == null ? void 0 : e.headers
|
|
93
93
|
}
|
|
94
|
-
}),
|
|
94
|
+
}), U = (e) => (e.client ?? r).get({
|
|
95
95
|
url: "/events/{id}",
|
|
96
96
|
...e
|
|
97
|
-
}),
|
|
97
|
+
}), b = (e) => (e.client ?? r).post({
|
|
98
98
|
url: "/events",
|
|
99
99
|
...e,
|
|
100
100
|
headers: {
|
|
@@ -108,29 +108,37 @@ const t = (e) => (e.client ?? r).delete({
|
|
|
108
108
|
"Content-Type": "application/json",
|
|
109
109
|
...e == null ? void 0 : e.headers
|
|
110
110
|
}
|
|
111
|
+
}), E = (e) => ((e == null ? void 0 : e.client) ?? r).post({
|
|
112
|
+
url: "/p",
|
|
113
|
+
...e,
|
|
114
|
+
headers: {
|
|
115
|
+
"Content-Type": "application/json",
|
|
116
|
+
...e == null ? void 0 : e.headers
|
|
117
|
+
}
|
|
111
118
|
});
|
|
112
119
|
export {
|
|
113
|
-
|
|
120
|
+
T as addMessageFeedback,
|
|
114
121
|
d as addParticipant,
|
|
115
122
|
n as createConversation,
|
|
116
|
-
|
|
123
|
+
b as createEvent,
|
|
117
124
|
F as createFile,
|
|
118
|
-
|
|
119
|
-
|
|
125
|
+
y as createMessage,
|
|
126
|
+
k as createUser,
|
|
120
127
|
t as deleteConversation,
|
|
121
128
|
v as deleteMessage,
|
|
122
|
-
|
|
123
|
-
|
|
129
|
+
M as deleteUser,
|
|
130
|
+
P as generateUserKey,
|
|
124
131
|
c as getConversation,
|
|
125
|
-
|
|
132
|
+
U as getEvent,
|
|
126
133
|
C as getMessage,
|
|
127
134
|
h as getParticipant,
|
|
128
|
-
|
|
135
|
+
i as getUser,
|
|
129
136
|
s as listConversationMessages,
|
|
130
137
|
l as listConversations,
|
|
131
138
|
u as listParticipants,
|
|
132
|
-
|
|
139
|
+
j as removeMessageFeedback,
|
|
133
140
|
g as removeParticipant,
|
|
141
|
+
E as sendPerfMetrics,
|
|
134
142
|
m as updateMessage,
|
|
135
|
-
|
|
143
|
+
f as updateUser
|
|
136
144
|
};
|
package/package.json
CHANGED
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@botpress/webchat-client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"sideEffects": false,
|
|
8
|
+
"repository": {
|
|
9
|
+
"url": "https://github.com/botpress/genisys"
|
|
10
|
+
},
|
|
8
11
|
"scripts": {
|
|
9
12
|
"build": "openapi-ts && vite build",
|
|
10
13
|
"check:lint": "eslint .",
|
|
11
14
|
"fix:lint": "eslint --fix .",
|
|
12
|
-
"check:type": "tsc --noEmit"
|
|
15
|
+
"check:type": "openapi-ts && tsc --noEmit"
|
|
13
16
|
},
|
|
14
17
|
"types": "./dist/index.d.ts",
|
|
15
18
|
"module": "./dist/webchat-client.js",
|
|
@@ -26,7 +29,7 @@
|
|
|
26
29
|
"eventsource": "^3.0.6"
|
|
27
30
|
},
|
|
28
31
|
"devDependencies": {
|
|
29
|
-
"@bpinternal/webchat-http-client": "0.
|
|
32
|
+
"@bpinternal/webchat-http-client": "0.6.0",
|
|
30
33
|
"@hey-api/client-fetch": "^0.10.0",
|
|
31
34
|
"@hey-api/openapi-ts": "^0.66.4",
|
|
32
35
|
"@repo/eslint-config": "workspace:*",
|
package/src/client/client.ts
CHANGED
|
@@ -39,6 +39,14 @@ type Event =
|
|
|
39
39
|
data: any
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
+
type EventMap = {
|
|
43
|
+
[E in Event as E['type']]: E['data']
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const MAX_RETRIES = 10
|
|
47
|
+
const RETRY_INTERVAL = 3000 // ms
|
|
48
|
+
const TIMEOUT = '60000' // ms
|
|
49
|
+
|
|
42
50
|
export type Client = {
|
|
43
51
|
createConversation: () => Promise<ClientTypes.CreateConversationResponse>
|
|
44
52
|
getConversation: (params: ConversationIdParam) => Promise<ClientTypes.GetConversationResponse>
|
|
@@ -56,19 +64,18 @@ export type Client = {
|
|
|
56
64
|
getMessage: (params: MessageIdParam) => Promise<ClientTypes.GetMessageResponse>
|
|
57
65
|
deleteMessage: (params: MessageIdParam) => Promise<ClientTypes.DeleteMessageResponse>
|
|
58
66
|
createFile: (params: ClientTypes.CreateFileBody) => Promise<ClientTypes.CreateFileResponse>
|
|
59
|
-
createUser: (params: ClientTypes.CreateUserBody) => Promise<ClientTypes.CreateUserResponse>
|
|
60
67
|
getUser: () => Promise<ClientTypes.GetUserResponse>
|
|
61
68
|
updateUser: (params: ClientTypes.CreateUserBody) => Promise<ClientTypes.UpdateUserResponse>
|
|
62
69
|
deleteUser: (params: UserIdParam) => Promise<ClientTypes.DeleteUserResponse>
|
|
63
70
|
createEvent: (params: ClientTypes.CreateEventBody) => Promise<ClientTypes.CreateEventResponse>
|
|
64
71
|
getEvent: (params: EventIdParam) => Promise<ClientTypes.GetEventResponse>
|
|
65
|
-
listenConversation: (params: ConversationIdParam) => EventEmitter<
|
|
72
|
+
listenConversation: (params: ConversationIdParam) => EventEmitter<EventMap>['on']
|
|
66
73
|
addMessageFeedback: (
|
|
67
74
|
params: MessageIdParam & ClientTypes.AddMessageFeedbackBody
|
|
68
75
|
) => Promise<ClientTypes.AddMessageFeedbackResponse>
|
|
69
76
|
removeMessageFeedback: (params: MessageIdParam) => Promise<ClientTypes.RemoveMessageFeedbackResponse>
|
|
70
77
|
generateUserKey: (
|
|
71
|
-
params: ClientTypes.GenerateUserKeyBody & {
|
|
78
|
+
params: ClientTypes.GenerateUserKeyBody & { adminSecret: string }
|
|
72
79
|
) => Promise<ClientTypes.GenerateUserKeyResponse>
|
|
73
80
|
}
|
|
74
81
|
|
|
@@ -79,12 +86,15 @@ const baseConfigs = {
|
|
|
79
86
|
export const createUser = async ({
|
|
80
87
|
clientId,
|
|
81
88
|
apiUrl,
|
|
82
|
-
|
|
89
|
+
adminSecret,
|
|
90
|
+
customHeaders,
|
|
91
|
+
...body
|
|
83
92
|
}: {
|
|
84
93
|
clientId: string
|
|
85
94
|
apiUrl?: string
|
|
86
|
-
|
|
87
|
-
|
|
95
|
+
adminSecret?: string
|
|
96
|
+
customHeaders?: Record<string, string>
|
|
97
|
+
} & ClientTypes.CreateUserBody) => {
|
|
88
98
|
const baseUrl = `${apiUrl || baseClient.getConfig().baseUrl}/${clientId}`
|
|
89
99
|
|
|
90
100
|
// @ts-expect-error - this is a private property
|
|
@@ -97,29 +107,84 @@ export const createUser = async ({
|
|
|
97
107
|
baseUrl,
|
|
98
108
|
}
|
|
99
109
|
|
|
110
|
+
const headers = {
|
|
111
|
+
...(adminSecret ? { 'x-admin-secret': adminSecret } : {}),
|
|
112
|
+
...customHeaders,
|
|
113
|
+
}
|
|
114
|
+
|
|
100
115
|
return sdk
|
|
101
116
|
.createUser({
|
|
102
117
|
...configs,
|
|
103
|
-
|
|
104
|
-
|
|
118
|
+
headers: Object.keys(headers).length > 0 ? headers : undefined,
|
|
119
|
+
body,
|
|
105
120
|
})
|
|
106
121
|
.then(extractData)
|
|
107
122
|
}
|
|
108
123
|
|
|
124
|
+
export const initialize = ({
|
|
125
|
+
conversationId,
|
|
126
|
+
userToken,
|
|
127
|
+
clientId,
|
|
128
|
+
apiUrl,
|
|
129
|
+
adminSecret,
|
|
130
|
+
}: {
|
|
131
|
+
conversationId?: string
|
|
132
|
+
userToken?: string
|
|
133
|
+
apiUrl?: string
|
|
134
|
+
clientId: string
|
|
135
|
+
adminSecret?: string
|
|
136
|
+
}) => {
|
|
137
|
+
let retryCount = 0
|
|
138
|
+
let eventSource: EventSource | null = null
|
|
139
|
+
const eventEmitter = createEventEmitter<EventMap>()
|
|
140
|
+
|
|
141
|
+
const connect = () => {
|
|
142
|
+
if (eventSource) {
|
|
143
|
+
eventSource.close()
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const baseUrl = `${apiUrl || baseClient.getConfig().baseUrl}/${clientId}`
|
|
147
|
+
const url = `${baseUrl}/initialize${conversationId ? `?conversationId=${conversationId}` : ''}`
|
|
148
|
+
|
|
149
|
+
const headers = {
|
|
150
|
+
...(adminSecret ? { 'x-admin-secret': adminSecret } : {}),
|
|
151
|
+
...(userToken ? { 'x-user-key': userToken } : {}),
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
eventSource = createNewEventSource(url, headers)
|
|
155
|
+
|
|
156
|
+
eventSource.onopen = () => eventEmitter.emit('open')
|
|
157
|
+
eventSource.onerror = (err) => {
|
|
158
|
+
if (retryCount < MAX_RETRIES) {
|
|
159
|
+
retryCount++
|
|
160
|
+
setTimeout(() => connect(), RETRY_INTERVAL)
|
|
161
|
+
} else {
|
|
162
|
+
eventEmitter.emit('error', err)
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
eventSource.onmessage = (ev) => {
|
|
166
|
+
const event = safeJsonParse(ev.data) as Signal
|
|
167
|
+
eventEmitter.emit(event.type, event.data)
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
connect()
|
|
171
|
+
return eventEmitter.on
|
|
172
|
+
}
|
|
173
|
+
|
|
109
174
|
export const generateUserKey = async ({
|
|
110
|
-
|
|
175
|
+
adminSecret,
|
|
111
176
|
expiresAt,
|
|
112
177
|
userId,
|
|
113
178
|
apiUrl,
|
|
114
179
|
clientId,
|
|
115
180
|
}: {
|
|
116
|
-
|
|
181
|
+
adminSecret: string
|
|
117
182
|
expiresAt: number
|
|
118
183
|
userId: string
|
|
119
184
|
apiUrl?: string
|
|
120
185
|
clientId: string
|
|
121
186
|
}) => {
|
|
122
|
-
const headers = { 'x-admin-
|
|
187
|
+
const headers = { 'x-admin-secret': adminSecret }
|
|
123
188
|
|
|
124
189
|
const baseUrl = `${apiUrl || baseClient.getConfig().baseUrl}/${clientId}`
|
|
125
190
|
const configs = {
|
|
@@ -129,17 +194,34 @@ export const generateUserKey = async ({
|
|
|
129
194
|
return sdk.generateUserKey({ ...configs, headers, body: { id: userId, expiresAt } }).then(extractData)
|
|
130
195
|
}
|
|
131
196
|
|
|
197
|
+
export const sendPerfMetrics = async (
|
|
198
|
+
clientId: string,
|
|
199
|
+
body: ClientTypes.SendPerfMetricsBody,
|
|
200
|
+
apiUrl?: string,
|
|
201
|
+
customHeaders?: Record<string, string>
|
|
202
|
+
) => {
|
|
203
|
+
const baseUrl = `${apiUrl || baseClient.getConfig().baseUrl}/${clientId}`
|
|
204
|
+
const configs = {
|
|
205
|
+
...baseConfigs,
|
|
206
|
+
baseUrl,
|
|
207
|
+
}
|
|
208
|
+
return sdk.sendPerfMetrics({ ...configs, body, headers: customHeaders }).then(extractData)
|
|
209
|
+
}
|
|
210
|
+
|
|
132
211
|
export const createClient = ({
|
|
133
212
|
userKey,
|
|
134
213
|
clientId,
|
|
135
214
|
apiUrl,
|
|
215
|
+
customHeaders,
|
|
136
216
|
}: {
|
|
137
217
|
userKey: string
|
|
138
218
|
clientId: string
|
|
139
219
|
apiUrl?: string
|
|
220
|
+
customHeaders?: Record<string, string>
|
|
140
221
|
}): Client => {
|
|
141
222
|
const headers = {
|
|
142
223
|
'x-user-key': userKey,
|
|
224
|
+
...customHeaders,
|
|
143
225
|
}
|
|
144
226
|
|
|
145
227
|
const baseUrl = `${apiUrl || baseClient.getConfig().baseUrl}/${clientId}`
|
|
@@ -187,7 +269,6 @@ export const createClient = ({
|
|
|
187
269
|
deleteMessage: ({ messageId }) =>
|
|
188
270
|
sdk.deleteMessage({ ...configs, headers, path: { id: messageId } }).then(extractData),
|
|
189
271
|
createFile: (body) => sdk.createFile({ ...configs, headers, body }).then(extractData),
|
|
190
|
-
createUser: (body) => sdk.createUser({ ...configs, headers, body }).then(extractData),
|
|
191
272
|
getUser: () => sdk.getUser({ ...configs, headers }).then(extractData),
|
|
192
273
|
updateUser: (body) => sdk.updateUser({ ...configs, headers, body }).then(extractData),
|
|
193
274
|
deleteUser: () => sdk.deleteUser({ ...configs, headers }).then(extractData),
|
|
@@ -197,14 +278,12 @@ export const createClient = ({
|
|
|
197
278
|
sdk.addMessageFeedback({ ...configs, headers, path: { id: messageId }, body }).then(extractData),
|
|
198
279
|
removeMessageFeedback: ({ messageId }) =>
|
|
199
280
|
sdk.removeMessageFeedback({ ...configs, headers, path: { id: messageId } }).then(extractData),
|
|
200
|
-
generateUserKey: ({
|
|
201
|
-
sdk.generateUserKey({ ...configs, headers: { 'x-admin-
|
|
281
|
+
generateUserKey: ({ adminSecret, ...body }) =>
|
|
282
|
+
sdk.generateUserKey({ ...configs, headers: { 'x-admin-secret': adminSecret }, body }).then(extractData),
|
|
202
283
|
listenConversation: ({ conversationId }) => {
|
|
203
|
-
const MAX_RETRIES = 10
|
|
204
|
-
const RETRY_INTERVAL = 3000 // ms
|
|
205
284
|
let retryCount = 0
|
|
206
285
|
let eventSource: EventSource | null = null
|
|
207
|
-
const eventEmitter = createEventEmitter<
|
|
286
|
+
const eventEmitter = createEventEmitter<EventMap>()
|
|
208
287
|
|
|
209
288
|
const connect = () => {
|
|
210
289
|
if (eventSource) {
|
|
@@ -213,17 +292,7 @@ export const createClient = ({
|
|
|
213
292
|
|
|
214
293
|
const url = `${baseUrl}/conversations/${conversationId}/listen`
|
|
215
294
|
|
|
216
|
-
eventSource =
|
|
217
|
-
fetch: (input, init) =>
|
|
218
|
-
fetch(input, {
|
|
219
|
-
...init,
|
|
220
|
-
headers: {
|
|
221
|
-
...init?.headers,
|
|
222
|
-
...headers,
|
|
223
|
-
timeout: '60_000',
|
|
224
|
-
},
|
|
225
|
-
}),
|
|
226
|
-
})
|
|
295
|
+
eventSource = createNewEventSource(url, headers)
|
|
227
296
|
|
|
228
297
|
eventSource.onopen = () => eventEmitter.emit('open')
|
|
229
298
|
eventSource.onerror = (err) => {
|
|
@@ -254,3 +323,17 @@ const safeJsonParse = (data: any) => {
|
|
|
254
323
|
return data
|
|
255
324
|
}
|
|
256
325
|
}
|
|
326
|
+
|
|
327
|
+
const createNewEventSource = (url: string, headers: Record<string, string>) => {
|
|
328
|
+
return new EventSource(url, {
|
|
329
|
+
fetch: (input, init) =>
|
|
330
|
+
fetch(input, {
|
|
331
|
+
...init,
|
|
332
|
+
headers: {
|
|
333
|
+
...init?.headers,
|
|
334
|
+
...headers,
|
|
335
|
+
timeout: TIMEOUT,
|
|
336
|
+
},
|
|
337
|
+
}),
|
|
338
|
+
})
|
|
339
|
+
}
|
|
@@ -8,6 +8,7 @@ import type { WebchatVisibility } from './webchatVisibility'
|
|
|
8
8
|
import type { ParticipantAdded } from './participantAdded'
|
|
9
9
|
import type { ParticipantRemoved } from './participantRemoved'
|
|
10
10
|
import type { MessageStatusChanged } from './messageStatusChanged'
|
|
11
|
+
import type { Initialize } from './initialize'
|
|
11
12
|
|
|
12
13
|
export type Signal =
|
|
13
14
|
| MessageCreated
|
|
@@ -20,3 +21,4 @@ export type Signal =
|
|
|
20
21
|
| ParticipantAdded
|
|
21
22
|
| ParticipantRemoved
|
|
22
23
|
| MessageStatusChanged
|
|
24
|
+
| Initialize
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
GetConversationResponse,
|
|
3
|
+
GetUserResponse,
|
|
4
|
+
ListConversationMessagesResponse,
|
|
5
|
+
ListParticipantsResponse,
|
|
6
|
+
} from '../../../gen/client'
|
|
7
|
+
|
|
8
|
+
export type Initialize = {
|
|
9
|
+
type: 'init'
|
|
10
|
+
data: {
|
|
11
|
+
conversation: GetConversationResponse['conversation']
|
|
12
|
+
messages: ListConversationMessagesResponse['messages']
|
|
13
|
+
participants: ListParticipantsResponse['participants']
|
|
14
|
+
user: GetUserResponse['user'] & {
|
|
15
|
+
userKey: string
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|