🛣️ fix: Chat Stream Hangup (#4822)

Embedded sse.js code converted into an external
dependency.
Custom access token refresh logic moved to
useSSE.ts hook.

Closes #4820
This commit is contained in:
Thinger Soft 2024-12-04 04:35:31 +01:00 committed by GitHub
parent ebae494337
commit daa8e878d2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 64 additions and 275 deletions

View file

@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import axios, { AxiosRequestConfig, AxiosError } from 'axios';
import { setTokenHeader } from './headers-helpers';
import axios, { AxiosError, AxiosRequestConfig } from 'axios';
import * as endpoints from './api-endpoints';
import { setTokenHeader } from './headers-helpers';
async function _get<T>(url: string, options?: AxiosRequestConfig): Promise<T> {
const response = await axios.get(url, { ...options });
@ -65,6 +65,11 @@ let failedQueue: { resolve: (value?: any) => void; reject: (reason?: any) => voi
const refreshToken = (retry?: boolean) => _post(endpoints.refreshToken(retry));
const dispatchTokenUpdatedEvent = (token: string) => {
setTokenHeader(token);
window.dispatchEvent(new CustomEvent('tokenUpdated', { detail: token }));
};
const processQueue = (error: AxiosError | null, token: string | null = null) => {
failedQueue.forEach((prom) => {
if (error) {
@ -109,8 +114,7 @@ axios.interceptors.response.use(
if (token) {
originalRequest.headers['Authorization'] = 'Bearer ' + token;
setTokenHeader(token);
window.dispatchEvent(new CustomEvent('tokenUpdated', { detail: token }));
dispatchTokenUpdatedEvent(token);
processQueue(null, token);
return await axios(originalRequest);
} else {
@ -139,4 +143,5 @@ export default {
deleteWithOptions: _deleteWithOptions,
patch: _patch,
refreshToken,
dispatchTokenUpdatedEvent,
};