mirror of
https://github.com/BTMuli/TeyvatGuide.git
synced 2025-12-16 09:58:13 +08:00
83 lines
2.1 KiB
TypeScript
83 lines
2.1 KiB
TypeScript
/**
|
|
* @file utils/TGHttp.ts
|
|
* @description 封装HTTP请求
|
|
* @since Beta v0.5.1
|
|
*/
|
|
|
|
import { fetch } from "@tauri-apps/plugin-http";
|
|
|
|
/**
|
|
* @description 请求参数
|
|
* @since Beta v0.5.1
|
|
* @property {"GET"|"POST"} method 请求方法
|
|
* @property {Record<string,string>} headers 请求头
|
|
* @property {Record<string,string>} query 请求参数
|
|
* @property {string} body 请求体
|
|
* @property {boolean} isBlob 是否为Blob
|
|
* @return TGHttpParams
|
|
*/
|
|
type TGHttpParams = {
|
|
method: "GET" | "POST";
|
|
headers?: Record<string, string>;
|
|
query?: Record<string, any>;
|
|
body?: string;
|
|
isBlob?: boolean;
|
|
};
|
|
|
|
/**
|
|
* @description 发送请求
|
|
* @since Beta v0.5.1
|
|
* @template T
|
|
* @param {string} url 请求地址
|
|
* @param {TGHttpParams} options 请求参数
|
|
* @returns {Promise<T>} 请求结果
|
|
*/
|
|
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 = {
|
|
method: options.method,
|
|
headers: httpHeaders,
|
|
};
|
|
if (options.body) {
|
|
fetchOptions.body = options.body;
|
|
}
|
|
if (options.query) {
|
|
const query = new URLSearchParams(options.query).toString();
|
|
url += `?${query}`;
|
|
}
|
|
console.log("fetch url: ", url);
|
|
console.log("fetch options: ", options);
|
|
return await fetch(url, fetchOptions)
|
|
.then((res) => {
|
|
if (res.ok) {
|
|
const data = options.isBlob ? res.arrayBuffer() : res.json();
|
|
if (fullResponse) {
|
|
return { data, resp: res };
|
|
}
|
|
return data;
|
|
}
|
|
throw new Error(`HTTP error! status: ${res.status}`);
|
|
})
|
|
.catch((err) => {
|
|
console.error("HTTP error: ", err);
|
|
return err;
|
|
});
|
|
}
|
|
|
|
export default TGHttp;
|