mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2026-02-12 07:50:25 +00:00
refactor: umami
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
export class RequestUtil {
|
||||
//适用于获取服务器下发cookies时获取
|
||||
static async HttpGetCookies(url: string, method: string = 'GET'): Promise<Map<string, string>> {
|
||||
const response = await fetch(url, { method: method });
|
||||
if (!response.ok) {
|
||||
@@ -18,6 +19,7 @@ export class RequestUtil {
|
||||
|
||||
return result;
|
||||
}
|
||||
// 请求和回复都是JSON data传原始内容 自动编码json
|
||||
static async HttpGetJson<T>(url: string, method: string = 'GET', data?: any, headers: Record<string, string> = {}):
|
||||
Promise<T> {
|
||||
let body: BodyInit | undefined = undefined;
|
||||
@@ -45,4 +47,28 @@ export class RequestUtil {
|
||||
throw new Error(`Failed to fetch JSON: ${error.message}`);
|
||||
}
|
||||
}
|
||||
// 请求返回都是原始内容
|
||||
static async HttpGetText(url: string, method: string = 'GET', data?: any, headers: Record<string, string> = {}): Promise<string> {
|
||||
let requestInit: RequestInit = { method: method };
|
||||
if (method.toUpperCase() === 'POST' && data !== undefined) {
|
||||
if (headers) {
|
||||
headers['Content-Type'] = 'application/json';
|
||||
requestInit.headers = new Headers(headers);
|
||||
} else {
|
||||
requestInit.headers = new Headers({ 'Content-Type': 'application/json' });
|
||||
}
|
||||
} else {
|
||||
requestInit.headers = new Headers(headers);
|
||||
}
|
||||
try {
|
||||
const response = await fetch(url, { ...requestInit, body: data });
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
const jsonResult = await response.text();
|
||||
return jsonResult;
|
||||
} catch (error: any) {
|
||||
throw new Error(`Failed to fetch JSON: ${error.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user