From 1a8407a78245a121cb79f4bb14309a5d52fbb26e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Wed, 15 May 2024 21:13:41 +0800 Subject: [PATCH] refactor: requests --- src/common/utils/helper.ts | 4 ++-- src/common/utils/request.ts | 12 ++++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/common/utils/helper.ts b/src/common/utils/helper.ts index 3f21cbf3..56059d4f 100644 --- a/src/common/utils/helper.ts +++ b/src/common/utils/helper.ts @@ -66,7 +66,7 @@ export function cacheFunc(ttl: number, customKey: string = '') { return descriptor; }; } -function isValidOldConfig(config: any) { +export function isValidOldConfig(config: any) { if (typeof config !== 'object') { return false; } @@ -110,7 +110,7 @@ function isValidOldConfig(config: any) { } return true; } -function migrateConfig(oldConfig: any) { +export function migrateConfig(oldConfig: any) { const newConfig = { http: { enable: oldConfig.enableHttp, diff --git a/src/common/utils/request.ts b/src/common/utils/request.ts index b53722ad..8ae463d2 100644 --- a/src/common/utils/request.ts +++ b/src/common/utils/request.ts @@ -30,7 +30,7 @@ export class RequestUtil { } // 请求和回复都是JSON data传原始内容 自动编码json - static async HttpGetJson(url: string, method: string = 'GET', data?: any, headers: Record = {}, isJsonRet: boolean = true): Promise { + static async HttpGetJson(url: string, method: string = 'GET', data?: any, headers: Record = {}, isJsonRet: boolean = true, isArgJson: boolean = true): Promise { let option = new URL(url); const protocol = url.startsWith('https://') ? https : http; const options = { @@ -69,7 +69,12 @@ export class RequestUtil { reject(error); }); if (method === 'POST' || method === 'PUT' || method === 'PATCH') { - req.write(JSON.stringify(data)); + if (isArgJson) { + req.write(JSON.stringify(data)); + } else { + req.write(data); + } + } req.end(); }); @@ -77,7 +82,6 @@ export class RequestUtil { // 请求返回都是原始内容 static async HttpGetText(url: string, method: string = 'GET', data?: any, headers: Record = {}) { - //console.log(url); - return this.HttpGetJson(url, method, data, headers, false); + return this.HttpGetJson(url, method, data, headers, false, false); } } \ No newline at end of file