@elqnt/chat 3.3.0 → 3.5.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.
@@ -4,6 +4,18 @@
4
4
  import { useCallback, useEffect, useRef, useState } from "react";
5
5
 
6
6
  // transport/types.ts
7
+ async function resolveTransportToken(cfg) {
8
+ if (!cfg) return void 0;
9
+ if (cfg.getToken) {
10
+ try {
11
+ const t = await cfg.getToken();
12
+ return t ?? void 0;
13
+ } catch {
14
+ return void 0;
15
+ }
16
+ }
17
+ return cfg.token ?? void 0;
18
+ }
7
19
  function createLogger(debug = false) {
8
20
  return {
9
21
  debug: debug ? console.log.bind(console, "[chat]") : () => {
@@ -84,9 +96,12 @@ function createSSETransport(options = {}) {
84
96
  }
85
97
  const url = `${config.baseUrl}/${endpoint}`;
86
98
  logger.debug(`POST ${endpoint}`, body);
99
+ const token = await resolveTransportToken(config);
100
+ const headers = { "Content-Type": "application/json" };
101
+ if (token) headers["Authorization"] = `Bearer ${token}`;
87
102
  const response = await fetch(url, {
88
103
  method: "POST",
89
- headers: { "Content-Type": "application/json" },
104
+ headers,
90
105
  body: JSON.stringify(body)
91
106
  });
92
107
  if (!response.ok) {
@@ -213,6 +228,7 @@ function createSSETransport(options = {}) {
213
228
  reconnectTimeout = void 0;
214
229
  }
215
230
  state = retryCount > 0 ? "reconnecting" : "connecting";
231
+ const connectToken = await resolveTransportToken(cfg);
216
232
  return new Promise((resolve, reject) => {
217
233
  const connectionStart = Date.now();
218
234
  const streamParams = new URLSearchParams({
@@ -221,6 +237,7 @@ function createSSETransport(options = {}) {
221
237
  clientType: cfg.clientType
222
238
  });
223
239
  if (cfg.chatKey) streamParams.set("chatId", cfg.chatKey);
240
+ if (connectToken) streamParams.set("token", connectToken);
224
241
  const url = `${cfg.baseUrl}/stream?${streamParams.toString()}`;
225
242
  logger.debug("Connecting to:", url);
226
243
  const es = new EventSource(url);
@@ -474,9 +491,12 @@ function createFetchSSETransport(options = {}) {
474
491
  }
475
492
  const url = `${config.baseUrl}/${endpoint}`;
476
493
  logger.debug(`POST ${endpoint}`, body);
494
+ const token = await resolveTransportToken(config);
495
+ const headers = { "Content-Type": "application/json" };
496
+ if (token) headers["Authorization"] = `Bearer ${token}`;
477
497
  const response = await customFetch(url, {
478
498
  method: "POST",
479
- headers: { "Content-Type": "application/json" },
499
+ headers,
480
500
  body: JSON.stringify(body)
481
501
  });
482
502
  if (!response.ok) {
@@ -519,15 +539,19 @@ function createFetchSSETransport(options = {}) {
519
539
  clientType: cfg.clientType
520
540
  });
521
541
  if (cfg.chatKey) streamParams.set("chatId", cfg.chatKey);
542
+ const token = await resolveTransportToken(cfg);
543
+ if (token) streamParams.set("token", token);
522
544
  const url = `${cfg.baseUrl}/stream?${streamParams.toString()}`;
523
545
  logger.debug("Connecting to:", url);
524
546
  abortController = new AbortController();
547
+ const streamHeaders = {
548
+ Accept: "text/event-stream",
549
+ "Cache-Control": "no-cache"
550
+ };
551
+ if (token) streamHeaders["Authorization"] = `Bearer ${token}`;
525
552
  const response = await customFetch(url, {
526
553
  method: "GET",
527
- headers: {
528
- Accept: "text/event-stream",
529
- "Cache-Control": "no-cache"
530
- },
554
+ headers: streamHeaders,
531
555
  signal: abortController.signal
532
556
  });
533
557
  if (!response.ok) {
@@ -841,7 +865,9 @@ function useChat(options) {
841
865
  onConnectionChange,
842
866
  autoConnect = false,
843
867
  retryConfig,
844
- debug = false
868
+ debug = false,
869
+ token,
870
+ getToken
845
871
  } = options;
846
872
  const [connectionState, setConnectionState] = useState("disconnected");
847
873
  const [currentChat, setCurrentChat] = useState(null);
@@ -860,10 +886,14 @@ function useChat(options) {
860
886
  const onMessageRef = useRef(onMessage);
861
887
  const onErrorRef = useRef(onError);
862
888
  const typingTimeoutRef = useRef(null);
889
+ const getTokenRef = useRef(getToken);
890
+ const tokenRef = useRef(token);
863
891
  useEffect(() => {
864
892
  onMessageRef.current = onMessage;
865
893
  onErrorRef.current = onError;
866
- }, [onMessage, onError]);
894
+ getTokenRef.current = getToken;
895
+ tokenRef.current = token;
896
+ }, [onMessage, onError, getToken, token]);
867
897
  useEffect(() => {
868
898
  if (typeof transportOption === "object") {
869
899
  transportRef.current = transportOption;
@@ -946,7 +976,10 @@ function useChat(options) {
946
976
  userId,
947
977
  clientType,
948
978
  chatKey: chatKey || void 0,
949
- debug
979
+ debug,
980
+ // Always resolve via the ref so a long-lived/reconnecting stream picks
981
+ // up a freshly-minted token rather than a stale captured one.
982
+ getToken: () => getTokenRef.current ? getTokenRef.current() : tokenRef.current
950
983
  });
951
984
  setConnectionState("connected");
952
985
  setError(null);