using sse: schema to fetch in App

This commit is contained in:
lloydzhou
2024-09-28 01:19:39 +08:00
parent 23f2b6213c
commit d84d51b475
4 changed files with 149 additions and 40 deletions

View File

@@ -2,8 +2,7 @@ import { useEffect, useState } from "react";
import { showToast } from "./components/ui-lib";
import Locale from "./locales";
import { RequestMessage } from "./client/api";
import { ServiceProvider, REQUEST_TIMEOUT_MS } from "./constant";
import { fetch as tauriFetch, ResponseType } from "@tauri-apps/api/http";
import { ServiceProvider } from "./constant";
export function trimTopic(topic: string) {
// Fix an issue where double quotes still show in the Indonesian language
@@ -292,30 +291,50 @@ export function fetch(
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);
const tauriUri = window.__TAURI__.convertFileSrc(url, "sse");
return window.fetch(tauriUri, options).then((r) => {
// 1. create response,
// TODO using event to get status and statusText and headers
const { status, statusText } = r;
const { readable, writable } = new TransformStream();
const res = new Response(readable, { status, statusText });
// 2. call fetch_read_body multi times, and write to Response.body
const writer = writable.getWriter();
let unlisten;
window.__TAURI__.event
.listen("sse-response", (e) => {
const { id, payload } = e;
console.log("event", id, payload);
writer.ready.then(() => {
if (payload !== 0) {
writer.write(new Uint8Array(payload));
} else {
writer.releaseLock();
writable.close();
unlisten && unlisten();
}
});
})
.then((u) => (unlisten = u));
return res;
});
}
return window.fetch(url, options);
}
if (undefined !== window) {
window.tauriFetch = fetch;
}
export function adapter(config: Record<string, unknown>) {
const { baseURL, url, params, ...rest } = config;
const path = baseURL ? `${baseURL}${url}` : url;
const fetchUrl = params
? `${path}?${new URLSearchParams(params as any).toString()}`
: path;
return fetch(fetchUrl as string, { ...rest, responseType: "text" });
return fetch(fetchUrl as string, rest)
.then((res) => res.text())
.then((data) => ({ data }));
}
export function safeLocalStorage(): {