using stream fetch replace old tauri http fetch

This commit is contained in:
lloydzhou 2024-09-30 02:28:19 +08:00
parent af49ed4fdc
commit f42488d4cb
1 changed files with 19 additions and 15 deletions

View File

@ -2,8 +2,9 @@ import { useEffect, useState } from "react";
import { showToast } from "./components/ui-lib"; import { showToast } from "./components/ui-lib";
import Locale from "./locales"; import Locale from "./locales";
import { RequestMessage } from "./client/api"; import { RequestMessage } from "./client/api";
import { ServiceProvider, REQUEST_TIMEOUT_MS } from "./constant"; import { ServiceProvider } from "./constant";
import { fetch as tauriFetch, ResponseType } from "@tauri-apps/api/http"; // import { fetch as tauriFetch, ResponseType } from "@tauri-apps/api/http";
import { fetch as tauriStreamFetch } from "./utils/stream";
export function trimTopic(topic: string) { export function trimTopic(topic: string) {
// Fix an issue where double quotes still show in the Indonesian language // Fix an issue where double quotes still show in the Indonesian language
@ -292,19 +293,22 @@ export function fetch(
options?: Record<string, unknown>, options?: Record<string, unknown>,
): Promise<any> { ): Promise<any> {
if (window.__TAURI__) { if (window.__TAURI__) {
const payload = options?.body || options?.data; return tauriStreamFetch(url, options)
return tauriFetch(url, { .then((res) => res.text())
...options, .then((data) => ({ data }));
body: // const payload = options?.body || options?.data;
payload && // return tauriFetch(url, {
({ // ...options,
type: "Text", // body:
payload, // payload &&
} as any), // ({
timeout: ((options?.timeout as number) || REQUEST_TIMEOUT_MS) / 1000, // type: "Text",
responseType: // payload,
options?.responseType == "text" ? ResponseType.Text : ResponseType.JSON, // } as any),
} as any); // timeout: ((options?.timeout as number) || REQUEST_TIMEOUT_MS) / 1000,
// responseType:
// options?.responseType == "text" ? ResponseType.Text : ResponseType.JSON,
// } as any);
} }
return window.fetch(url, options); return window.fetch(url, options);
} }