更新依赖,优化认证流程;添加聊天框动画效果,优化消息处理逻辑
This commit is contained in:
+6
-2
@@ -15,9 +15,13 @@ const resolveUrl = (input: RequestInfo | URL) => {
|
||||
const isMetaProjectsRequest = (input: RequestInfo | URL) =>
|
||||
resolveUrl(input).includes("/api/v1/meta/projects");
|
||||
|
||||
export interface ApiFetchInit extends RequestInit {
|
||||
skipAuthRedirect?: boolean;
|
||||
}
|
||||
|
||||
export const apiFetch = async (
|
||||
input: RequestInfo | URL,
|
||||
init: RequestInit = {},
|
||||
init: ApiFetchInit = {},
|
||||
) => {
|
||||
const projectId = useProjectStore.getState().currentProjectId;
|
||||
const headers = new Headers(init.headers ?? {});
|
||||
@@ -31,7 +35,7 @@ export const apiFetch = async (
|
||||
|
||||
const response = await fetch(input, { ...init, headers });
|
||||
|
||||
if (response.status === 401 && typeof window !== "undefined") {
|
||||
if (response.status === 401 && typeof window !== "undefined" && !init.skipAuthRedirect) {
|
||||
useAuthStore.getState().setAccessToken(null);
|
||||
if (!isSigningOut) {
|
||||
isSigningOut = true;
|
||||
|
||||
@@ -78,4 +78,23 @@ describe("streamCopilotChat", () => {
|
||||
{ type: "error", message: "stream request failed", detail: "bad request" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("emits re-login message on unauthorized response", async () => {
|
||||
apiFetch.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 401,
|
||||
body: null,
|
||||
text: async () => "unauthorized",
|
||||
});
|
||||
|
||||
const events: Array<{ type: string; message?: string; detail?: string }> = [];
|
||||
await streamCopilotChat({
|
||||
message: "hi",
|
||||
onEvent: (event) => events.push(event),
|
||||
});
|
||||
|
||||
expect(events).toEqual([
|
||||
{ type: "error", message: "Login expired. Please sign in again.", detail: undefined },
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
+11
-2
@@ -49,14 +49,23 @@ export const streamCopilotChat = async ({
|
||||
message,
|
||||
conversation_id: conversationId,
|
||||
}),
|
||||
skipAuthRedirect: true,
|
||||
});
|
||||
|
||||
if (!response.ok || !response.body) {
|
||||
const detail = await response.text();
|
||||
let message = "stream request failed";
|
||||
|
||||
if (response.status === 403) {
|
||||
message = "Permission denied. Please contact administrator.";
|
||||
} else if (response.status === 401) {
|
||||
message = "Login expired. Please sign in again.";
|
||||
}
|
||||
|
||||
onEvent({
|
||||
type: "error",
|
||||
message: "stream request failed",
|
||||
detail,
|
||||
message,
|
||||
detail: (response.status === 403 || response.status === 401) ? undefined : detail,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user