mirror of
https://github.com/BTMuli/TeyvatGuide.git
synced 2026-04-23 22:09:51 +08:00
🔥 移除 json-bigint,buffer请求解耦
This commit is contained in:
@@ -5,7 +5,6 @@
|
||||
*/
|
||||
|
||||
import { type ClientOptions, fetch } from "@tauri-apps/plugin-http";
|
||||
import JSONBig from "json-bigint";
|
||||
|
||||
import TGLogger from "./TGLogger.js";
|
||||
|
||||
@@ -25,8 +24,6 @@ type TGHttpParams = {
|
||||
body?: string;
|
||||
/** 是否是Blob */
|
||||
isBlob?: boolean;
|
||||
/** 是否有BigInt */
|
||||
hasBigInt?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -70,11 +67,7 @@ async function TGHttp<T>(
|
||||
return await fetch(url, fetchOptions)
|
||||
.then(async (res) => {
|
||||
if (res.ok) {
|
||||
const data = options.isBlob
|
||||
? await res.arrayBuffer()
|
||||
: options.hasBigInt
|
||||
? JSONBig.parse(await res.text())
|
||||
: await res.json();
|
||||
const data = options.isBlob ? await res.arrayBuffer() : await res.json();
|
||||
if (fullResponse) return { data, resp: res };
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
*/
|
||||
|
||||
import { type ClientOptions, fetch } from "@tauri-apps/plugin-http";
|
||||
import JSONBig from "json-bigint";
|
||||
|
||||
/**
|
||||
* 构建 URL 查询字符串
|
||||
@@ -40,26 +39,6 @@ function createHttpError(
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析响应数据
|
||||
* @since Beta v0.10.0
|
||||
* @param response - 原始响应
|
||||
* @param config - 请求配置
|
||||
* @returns 解析后的数据
|
||||
*/
|
||||
async function parseResponse<T>(
|
||||
response: Response,
|
||||
config: TGApp.App.Response.ReqConf,
|
||||
): Promise<T> {
|
||||
if (config.isBlob) {
|
||||
return <T>await response.arrayBuffer();
|
||||
}
|
||||
if (config.hasBigInt) {
|
||||
return <T>JSONBig.parse(await response.text());
|
||||
}
|
||||
return <T>await response.json();
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行 HTTP 请求
|
||||
* @since Beta v0.10.0
|
||||
@@ -104,11 +83,7 @@ async function request<T>(
|
||||
}
|
||||
|
||||
// 调试日志
|
||||
if (config.isBlob) {
|
||||
console.debug(`[TGHttps] Fetch Blob: ${finalUrl}`);
|
||||
} else {
|
||||
console.debug(`[TGHttps] ${method} ${finalUrl}`);
|
||||
}
|
||||
console.debug(`[TGHttps] ${method} ${finalUrl}`);
|
||||
|
||||
// 创建超时控制器
|
||||
const timeoutController = new AbortController();
|
||||
@@ -138,10 +113,7 @@ async function request<T>(
|
||||
data: errorText,
|
||||
});
|
||||
}
|
||||
|
||||
// 解析响应
|
||||
const data = await parseResponse<T>(rawResponse, config);
|
||||
|
||||
const data = <T>await rawResponse.json();
|
||||
return {
|
||||
data: data,
|
||||
status: rawResponse.status,
|
||||
@@ -194,6 +166,39 @@ const TGHttps = {
|
||||
config?: TGApp.App.Response.ReqConfParams,
|
||||
): Promise<TGApp.App.Response.Resp<T>> => request<T>("POST", url, config),
|
||||
|
||||
/**
|
||||
* 用于获取图像 ArrayBuffer
|
||||
* @since Beta v0.10.0
|
||||
* @remarks 目前需求较为简单,故不做额外处理
|
||||
* @param url - 图像地址
|
||||
* @returns ArrayBuffer
|
||||
*/
|
||||
async buffer(url: string): Promise<ArrayBuffer> {
|
||||
console.debug(`[TGHttps] Fetch Buffer: ${url}`);
|
||||
try {
|
||||
const rawResponse = await fetch(url);
|
||||
if (!rawResponse.ok) {
|
||||
const errorText = await rawResponse.text().catch(() => "Unknown error");
|
||||
throw createHttpError(`HTTP Error: ${rawResponse.status} ${rawResponse.statusText}`, {
|
||||
status: rawResponse.status,
|
||||
statusText: rawResponse.statusText,
|
||||
data: errorText,
|
||||
});
|
||||
}
|
||||
return await rawResponse.arrayBuffer();
|
||||
} catch (error) {
|
||||
let httpError: TGApp.App.Response.HttpErr;
|
||||
if (this.isHttpErr(error)) {
|
||||
httpError = error;
|
||||
} else if (error instanceof Error) {
|
||||
httpError = createHttpError(error.message, { cause: error });
|
||||
} else {
|
||||
httpError = createHttpError(String(error), { cause: error });
|
||||
}
|
||||
throw httpError;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 通用请求方法
|
||||
* @since Beta v0.10.0
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* 生成分享截图并保存到本地
|
||||
* @since Beta v0.9.0
|
||||
* @since Beta v0.10.0
|
||||
*/
|
||||
|
||||
import showSnackbar from "@comp/func/snackbar.js";
|
||||
@@ -13,7 +13,7 @@ import { platform } from "@tauri-apps/plugin-os";
|
||||
import html2canvas from "html2canvas";
|
||||
import { storeToRefs } from "pinia";
|
||||
|
||||
import TGHttp from "./TGHttp.js";
|
||||
import TGHttps from "./TGHttps.js";
|
||||
import TGLogger from "./TGLogger.js";
|
||||
import { bytesToSize } from "./toolFunc.js";
|
||||
|
||||
@@ -50,12 +50,13 @@ export async function saveCanvasImg(
|
||||
|
||||
/**
|
||||
* 将图片保存到本地
|
||||
* @since Beta v0.5.0
|
||||
* @since Beta v0.10.0
|
||||
* @todo format param
|
||||
* @param url - 图片链接
|
||||
* @returns 图片元素
|
||||
*/
|
||||
export async function saveImgLocal(url: string): Promise<string> {
|
||||
const res = await TGHttp<Uint8Array>(url, { method: "GET", isBlob: true });
|
||||
const res = await TGHttps.buffer(url);
|
||||
const buffer = new Uint8Array(res);
|
||||
const blob = new Blob([buffer], { type: "image/png" });
|
||||
return URL.createObjectURL(blob);
|
||||
@@ -63,12 +64,13 @@ export async function saveImgLocal(url: string): Promise<string> {
|
||||
|
||||
/**
|
||||
* 返回图片 buffer
|
||||
* @since Beta v0.9.0
|
||||
* @since Beta v0.10.0
|
||||
* @deprecated 使用 TGHttps.buffer
|
||||
* @param url - 图片链接
|
||||
* @returns 图片 buffer
|
||||
*/
|
||||
export async function getImageBuffer(url: string): Promise<ArrayBuffer> {
|
||||
return await TGHttp<ArrayBuffer>(url, { method: "GET", isBlob: true });
|
||||
return await TGHttp.buffer(url);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user