refactor: http requests

This commit is contained in:
手瓜一十雪 2024-05-14 15:31:53 +08:00
parent 745cb0175c
commit caa2fca4e8
2 changed files with 7 additions and 9 deletions

View File

@ -30,16 +30,16 @@ export class RequestUtil {
}
// 请求和回复都是JSON data传原始内容 自动编码json
static async HttpGetJson<T>(url: string, method: string = 'GET', data?: any, headers: Record<string, string> = {}) {
static async HttpGetJson<T>(url: string, method: string = 'GET', data?: any, headers: Record<string, string> = {}): Promise<T> {
let option = new URL(url);
const protocol = url.startsWith('https://') ? https : http;
const options = {
hostname: url.replace(/^(https?:\/\/)?(.*?)(\/.*)?$/, '$2'),
port: url.startsWith('https://') ? 443 : 80,
path: url.replace(/^(https?:\/\/[^\/]+)?(.*?)(\/.*)?$/, '$3'),
hostname: option.hostname,
port: option.port,
path: option.href,
method,
headers,
};
return new Promise((resolve, reject) => {
const req = protocol.request(options, (res: any) => {
let responseBody = '';
@ -64,11 +64,9 @@ export class RequestUtil {
req.on('error', (error: any) => {
reject(error);
});
if (method === 'POST' || method === 'PUT' || method === 'PATCH') {
req.write(JSON.stringify(data));
}
req.end();
});
}

View File

@ -22,8 +22,8 @@ export async function postLoginStatus() {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0'
});
resolve(true);
} catch {
reject('umami post failed')
} catch (e: any) {
reject(e);
}
});
}