完善细节
This commit is contained in:
@@ -138,6 +138,32 @@ function getFetchConfig() {
|
||||
};
|
||||
}
|
||||
|
||||
async function authorizedFetch(path: string, init: RequestInit) {
|
||||
const request = () => {
|
||||
const { authorization, baseURL } = getFetchConfig();
|
||||
const headers = new Headers(init.headers);
|
||||
if (authorization) {
|
||||
headers.set('Authorization', authorization);
|
||||
} else {
|
||||
headers.delete('Authorization');
|
||||
}
|
||||
return fetch(`${baseURL}${path}`, { ...init, headers });
|
||||
};
|
||||
|
||||
let response = await request();
|
||||
if (response.status !== 401) return response;
|
||||
|
||||
try {
|
||||
// 标准请求客户端会使用项目现有的 refresh token 机制更新令牌。
|
||||
await getCubeReportScope();
|
||||
} catch {
|
||||
return response;
|
||||
}
|
||||
await response.body?.cancel();
|
||||
response = await request();
|
||||
return response;
|
||||
}
|
||||
|
||||
async function responseError(response: Response) {
|
||||
const text = await response.text();
|
||||
try {
|
||||
@@ -272,12 +298,10 @@ export async function stopCubeReportTask(taskId: string) {
|
||||
}
|
||||
|
||||
export async function uploadCubeReportFile(file: File) {
|
||||
const { authorization, baseURL } = getFetchConfig();
|
||||
const form = new FormData();
|
||||
form.append('file', file);
|
||||
const response = await fetch(`${baseURL}/llm/cube-report/files/upload`, {
|
||||
const response = await authorizedFetch('/llm/cube-report/files/upload', {
|
||||
method: 'POST',
|
||||
headers: { Authorization: authorization },
|
||||
body: form,
|
||||
});
|
||||
if (!response.ok) throw new Error(await responseError(response));
|
||||
@@ -291,14 +315,13 @@ export async function downloadCubeReportFile(
|
||||
conversationId: string,
|
||||
file: CubeReportFile,
|
||||
) {
|
||||
const { authorization, baseURL } = getFetchConfig();
|
||||
const params = new URLSearchParams({
|
||||
conversationId,
|
||||
asAttachment: 'true',
|
||||
});
|
||||
const response = await fetch(
|
||||
`${baseURL}/llm/cube-report/files/${file.id}/preview?${params}`,
|
||||
{ headers: { Authorization: authorization } },
|
||||
const response = await authorizedFetch(
|
||||
`/llm/cube-report/files/${file.id}/preview?${params}`,
|
||||
{},
|
||||
);
|
||||
if (!response.ok) throw new Error(await responseError(response));
|
||||
const blob = await response.blob();
|
||||
@@ -314,12 +337,10 @@ export async function streamCubeReportMessage(
|
||||
payload: SendCubeReportMessage,
|
||||
onEvent: (event: CubeReportStreamEvent) => void,
|
||||
) {
|
||||
const { authorization, baseURL } = getFetchConfig();
|
||||
const response = await fetch(`${baseURL}/llm/cube-report/messages/stream`, {
|
||||
const response = await authorizedFetch('/llm/cube-report/messages/stream', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Accept: 'text/event-stream',
|
||||
Authorization: authorization,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
|
||||
Reference in New Issue
Block a user