feat: 更新网络请求工具函数
All checks were successful
CI Monitor CI/CD / build-image (push) Successful in 30s
CI Monitor CI/CD / deploy (push) Successful in 37s

This commit is contained in:
zhaoyingbo 2024-07-12 07:42:34 +00:00
parent f66bf4b39c
commit 153f15d839
2 changed files with 149 additions and 64 deletions

View File

@ -1,29 +1,27 @@
interface NetGetParams {
interface NetRequestParams {
url: string;
method: string;
params?: any;
data?: any;
headers?: any;
queryParams?: any;
payload?: any;
additionalHeaders?: any;
}
/**
*
* @param response
* @param method
* @param data
*
* @param response -
* @param method - 使HTTP方法
* @param headers -
* @param requestBody -
* @param responseBody -
* @returns
*/
const logResponse = async (
const logResponse = (
response: Response,
method: string,
data: any,
headers: any
headers: any,
requestBody: any,
responseBody: any
) => {
let responseData = null;
try {
responseData = await response.json();
} catch (error) {
responseData = "parse to json error";
}
const responseLog = {
ok: response.ok,
status: response.status,
@ -32,70 +30,156 @@ const logResponse = async (
method: method,
requestHeaders: headers,
responseHeaders: response.headers,
requestBody: data,
responseBody: responseData as any,
requestBody,
responseBody,
};
console.log("🚀 ~ responseLog:", JSON.stringify(responseLog, null, 2));
return responseLog;
};
const netTool = <T = any>({
/**
* Promise
* @param url - URL
* @param method - 使HTTP方法
* @param queryParams - URL中的查询参数
* @param payload -
* @param additionalHeaders -
* @returns Promise
* @throws
*/
const netTool = async <T = any>({
url,
method,
params,
data,
headers,
}: NetGetParams): Promise<T> => {
queryParams,
payload,
additionalHeaders,
}: NetRequestParams): Promise<T> => {
// 拼接完整的URL
let fullUrl = url;
if (params) {
if (typeof params === "string") {
fullUrl = `${url}?${params}`;
if (queryParams) {
if (typeof queryParams === "string") {
fullUrl = `${url}?${queryParams}`;
} else {
const queryString = new URLSearchParams(params).toString();
fullUrl = `${url}?${queryString}`;
const queryString = new URLSearchParams(queryParams).toString();
if (queryString) fullUrl = `${url}?${queryString}`;
}
}
return fetch(fullUrl, {
// 设置请求头
const headers = {
"Content-Type": "application/json",
...additionalHeaders,
};
// 发送请求
const res = await fetch(fullUrl, {
method,
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json",
...headers,
},
})
.then((response) => logResponse(response, method, data, headers))
.then((responseLog) => {
if (!responseLog.ok) {
if (responseLog?.responseBody?.msg) {
throw new Error(responseLog.responseBody.msg);
}
throw new Error("网络响应异常");
}
if (responseLog.responseBody === "parse to json error") {
throw new Error("解析响应数据异常");
}
return responseLog.responseBody as T;
});
body: JSON.stringify(payload),
headers,
});
// 获取响应数据
let resData: any = null;
try {
resData = await res.json();
} catch (error) {
resData = "解析为JSON时出错";
}
// 记录响应
logResponse(res, method, headers, payload, resData);
if (!res.ok) {
if (resData?.msg) {
throw new Error(resData.msg);
}
throw new Error("网络响应异常");
}
if (resData === "解析为JSON时出错") {
throw new Error("解析响应数据异常");
}
return resData as T;
};
netTool.get = <T = any>(url: string, params?: any, headers?: any): Promise<T> =>
netTool({ url, method: "get", params, headers });
/**
* GET请求并返回一个解析为响应数据的Promise
*
* @param url - URL
* @param queryParams - URL中的查询参数
* @param additionalHeaders -
* @returns Promise
*/
netTool.get = <T = any>(
url: string,
queryParams?: any,
additionalHeaders?: any
): Promise<T> =>
netTool({ url, method: "get", queryParams, additionalHeaders });
/**
* POST请求并返回一个解析为响应数据的Promise
*
* @param url - URL
* @param payload -
* @param queryParams - URL中的查询参数
* @param additionalHeaders -
* @returns Promise
*/
netTool.post = <T = any>(
url: string,
data?: any,
params?: any,
headers?: any
): Promise<T> => netTool({ url, method: "post", data, params, headers });
payload?: any,
queryParams?: any,
additionalHeaders?: any
): Promise<T> =>
netTool({ url, method: "post", payload, queryParams, additionalHeaders });
netTool.put = <T = any>(url: string, data: any, headers?: any): Promise<T> =>
netTool({ url, method: "put", data, headers });
/**
* PUT请求并返回一个解析为响应数据的Promise
*
* @param url - URL
* @param payload -
* @param queryParams - URL中的查询参数
* @param additionalHeaders -
* @returns Promise
*/
netTool.put = <T = any>(
url: string,
payload: any,
queryParams?: any,
additionalHeaders?: any
): Promise<T> =>
netTool({ url, method: "put", payload, queryParams, additionalHeaders });
netTool.del = <T = any>(url: string, data: any, headers?: any): Promise<T> =>
netTool({ url, method: "delete", data, headers });
/**
* DELETE请求并返回一个解析为响应数据的Promise
*
* @param url - URL
* @param payload -
* @param queryParams - URL中的查询参数
* @param additionalHeaders -
* @returns Promise
*/
netTool.del = <T = any>(
url: string,
payload: any,
queryParams?: any,
additionalHeaders?: any
): Promise<T> =>
netTool({ url, method: "delete", payload, queryParams, additionalHeaders });
netTool.patch = <T = any>(url: string, data: any, headers?: any): Promise<T> =>
netTool({ url, method: "patch", data, headers });
/**
* PATCH请求并返回一个解析为响应数据的Promise
*
* @param url - URL
* @param payload -
* @param queryParams - URL中的查询参数
* @param additionalHeaders -
* @returns Promise
*/
netTool.patch = <T = any>(
url: string,
payload: any,
queryParams?: any,
additionalHeaders?: any
): Promise<T> =>
netTool({ url, method: "patch", payload, queryParams, additionalHeaders });
export default netTool;

View File

@ -5,7 +5,10 @@ const API_KEY = "1dfz4wlpbbgiky0";
const sendMessage = async (body: any) => {
const URL = "https://egg.imoaix.cn/message";
try {
const res = await netTool.post(URL, body);
const res = await netTool.post(URL, {
api_key: API_KEY,
...body,
});
return res;
} catch {
return null;
@ -14,7 +17,6 @@ const sendMessage = async (body: any) => {
sendMessage.byGroupId = async (group_id: string, content: string) => {
return sendMessage({
api_key: API_KEY,
group_id,
msg_type: "interactive",
content,
@ -23,7 +25,6 @@ sendMessage.byGroupId = async (group_id: string, content: string) => {
sendMessage.byChatId = async (chat_id: string, content: string) => {
return sendMessage({
api_key: API_KEY,
receive_id: chat_id,
receive_id_type: "chat_id",
msg_type: "interactive",