revert plugin runtime using tarui/api/http, not using fetch_stream

This commit is contained in:
lloydzhou 2024-09-30 00:58:50 +08:00
parent b5f6e5a598
commit 5141145e4d
1 changed files with 26 additions and 10 deletions

View File

@ -2,8 +2,8 @@ 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 } from "./constant"; import { ServiceProvider, REQUEST_TIMEOUT_MS } from "./constant";
import { fetch } from "./utils/stream"; import { fetch as tauriFetch, ResponseType } from "@tauri-apps/api/http";
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
@ -287,19 +287,35 @@ export function showPlugins(provider: ServiceProvider, model: string) {
return false; return false;
} }
export function fetch(
url: string,
options?: Record<string, unknown>,
): Promise<any> {
if (window.__TAURI__) {
const payload = options?.body || options?.data;
return tauriFetch(url, {
...options,
body:
payload &&
({
type: "Text",
payload,
} 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);
}
export function adapter(config: Record<string, unknown>) { export function adapter(config: Record<string, unknown>) {
const { baseURL, url, params, method, data, ...rest } = config; const { baseURL, url, params, ...rest } = config;
const path = baseURL ? `${baseURL}${url}` : url; const path = baseURL ? `${baseURL}${url}` : url;
const fetchUrl = params const fetchUrl = params
? `${path}?${new URLSearchParams(params as any).toString()}` ? `${path}?${new URLSearchParams(params as any).toString()}`
: path; : path;
return fetch(fetchUrl as string, { return fetch(fetchUrl as string, { ...rest, responseType: "text" });
...rest,
method,
body: method.toUpperCase() == "GET" ? undefined : data,
})
.then((res) => res.text())
.then((data) => ({ data }));
} }
export function safeLocalStorage(): { export function safeLocalStorage(): {