mirror of
https://github.com/BTMuli/TeyvatGuide.git
synced 2026-04-22 21:59:49 +08:00
🔥 删除无用请求
This commit is contained in:
@@ -1,89 +0,0 @@
|
||||
/**
|
||||
* 封装HTTP请求
|
||||
* @since Beta v0.10.0
|
||||
* @deprecated 请使用 TGHttps
|
||||
*/
|
||||
|
||||
import { type ClientOptions, fetch } from "@tauri-apps/plugin-http";
|
||||
|
||||
import TGLogger from "./TGLogger.js";
|
||||
|
||||
/**
|
||||
* 请求参数
|
||||
* @since Beta v0.10.0
|
||||
* @deprecated 请使用 TGHttpsConfig 类型
|
||||
*/
|
||||
type TGHttpParams = {
|
||||
/** 请求方法 */
|
||||
method: "GET" | "POST";
|
||||
/** 请求头 */
|
||||
headers?: Record<string, string>;
|
||||
/** 请求参数 */
|
||||
query?: Record<string, any>;
|
||||
/** 请求体 */
|
||||
body?: string;
|
||||
/** 是否是Blob */
|
||||
isBlob?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* 发送请求
|
||||
* @since Beta v0.10.0
|
||||
* @deprecated 请使用 TGHttps.get() 或 TGHttps.post() 替代
|
||||
* @param url - 请求地址
|
||||
* @param options - 请求参数
|
||||
* @returns 请求结果
|
||||
*/
|
||||
async function TGHttp<T>(url: string, options: TGHttpParams): Promise<T>;
|
||||
async function TGHttp<T>(
|
||||
url: string,
|
||||
options: TGHttpParams,
|
||||
fullResponse: true,
|
||||
): Promise<{ data: Promise<T>; resp: Response }>;
|
||||
async function TGHttp<T>(
|
||||
url: string,
|
||||
options: TGHttpParams,
|
||||
fullResponse?: true,
|
||||
): Promise<T | { data: Promise<T>; resp: Response }> {
|
||||
const httpHeaders = new Headers();
|
||||
if (options.headers) {
|
||||
for (const key in options.headers) httpHeaders.append(key, options.headers[key]);
|
||||
}
|
||||
const fetchOptions: RequestInit & ClientOptions = {
|
||||
method: options.method,
|
||||
headers: httpHeaders,
|
||||
};
|
||||
if (options.body) fetchOptions.body = options.body;
|
||||
if (options.query) {
|
||||
const query = new URLSearchParams(options.query).toString();
|
||||
url += `?${query}`;
|
||||
}
|
||||
if (options.isBlob) {
|
||||
console.debug(`Fetch Blob: ${url}`);
|
||||
} else {
|
||||
console.debug(`Fetch URL: ${url}`);
|
||||
console.debug(options);
|
||||
}
|
||||
return await fetch(url, fetchOptions)
|
||||
.then(async (res) => {
|
||||
if (res.ok) {
|
||||
const data = options.isBlob ? await res.arrayBuffer() : await res.json();
|
||||
if (fullResponse) return { data, resp: res };
|
||||
return data;
|
||||
}
|
||||
await TGLogger.Error(`Fetch URL: ${url} ERROR: ${res.status} ${res.statusText}`);
|
||||
throw new Error(await res.text(), { cause: `${res.status} ${res.statusText}` });
|
||||
})
|
||||
.catch(async (err) => {
|
||||
if (err instanceof Error) {
|
||||
console.error(err.message);
|
||||
} else if (typeof err === "object") {
|
||||
await TGLogger.Error(`Error: ${JSON.stringify(err)}`);
|
||||
} else {
|
||||
await TGLogger.Error(`Error: ${err}`);
|
||||
}
|
||||
return err;
|
||||
});
|
||||
}
|
||||
|
||||
export default TGHttp;
|
||||
Reference in New Issue
Block a user