2024-09-09 16:01:42 +08:00
|
|
|
|
import axios, { AxiosRequestConfig } from "axios";
|
2024-07-15 00:30:33 +08:00
|
|
|
|
import { logger } from "./util.log.js";
|
2024-06-15 02:17:34 +08:00
|
|
|
|
import { Logger } from "log4js";
|
2024-08-30 18:50:53 +08:00
|
|
|
|
|
|
|
|
|
|
export class HttpError extends Error {
|
|
|
|
|
|
status?: number;
|
|
|
|
|
|
statusText?: string;
|
2024-09-09 16:01:42 +08:00
|
|
|
|
code?: string;
|
|
|
|
|
|
request?: { url: string; method: string; params?: any; data?: any };
|
|
|
|
|
|
response?: { data: any };
|
|
|
|
|
|
cause?: any;
|
2024-08-30 18:50:53 +08:00
|
|
|
|
constructor(error: any) {
|
|
|
|
|
|
if (!error) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
super(error.message);
|
|
|
|
|
|
this.name = error.name;
|
2024-09-09 16:01:42 +08:00
|
|
|
|
this.code = error.code;
|
|
|
|
|
|
this.cause = error.cause;
|
|
|
|
|
|
|
|
|
|
|
|
this.status = error.response?.status;
|
|
|
|
|
|
this.statusText = error.response?.statusText;
|
2024-08-30 18:50:53 +08:00
|
|
|
|
this.request = {
|
2024-09-09 16:01:42 +08:00
|
|
|
|
url: error.config?.url,
|
|
|
|
|
|
method: error.config?.method,
|
|
|
|
|
|
params: error.config?.params,
|
|
|
|
|
|
data: error.config?.data,
|
2024-08-30 18:50:53 +08:00
|
|
|
|
};
|
|
|
|
|
|
this.response = {
|
2024-09-09 16:01:42 +08:00
|
|
|
|
data: error.response?.data,
|
2024-08-30 18:50:53 +08:00
|
|
|
|
};
|
2024-09-09 16:01:42 +08:00
|
|
|
|
|
|
|
|
|
|
delete error.response;
|
|
|
|
|
|
delete error.config;
|
|
|
|
|
|
delete error.request;
|
|
|
|
|
|
logger.error(error);
|
2024-08-30 18:50:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-11-07 23:31:20 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @description 创建请求实例
|
|
|
|
|
|
*/
|
2024-06-15 02:17:34 +08:00
|
|
|
|
export function createAxiosService({ logger }: { logger: Logger }) {
|
2022-11-07 23:31:20 +08:00
|
|
|
|
// 创建一个 axios 实例
|
|
|
|
|
|
const service = axios.create();
|
|
|
|
|
|
// 请求拦截
|
|
|
|
|
|
service.interceptors.request.use(
|
|
|
|
|
|
(config: any) => {
|
2024-09-09 16:01:42 +08:00
|
|
|
|
logger.info(`http request:${config.url},method:${config.method},params:${JSON.stringify(config.params)}`);
|
2022-11-07 23:31:20 +08:00
|
|
|
|
return config;
|
|
|
|
|
|
},
|
|
|
|
|
|
(error: Error) => {
|
|
|
|
|
|
// 发送失败
|
2024-06-15 02:17:34 +08:00
|
|
|
|
logger.error("接口请求失败:", error);
|
2022-11-07 23:31:20 +08:00
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
// 响应拦截
|
|
|
|
|
|
service.interceptors.response.use(
|
|
|
|
|
|
(response: any) => {
|
2024-06-15 02:17:34 +08:00
|
|
|
|
logger.info("http response:", JSON.stringify(response?.data));
|
2022-11-07 23:31:20 +08:00
|
|
|
|
return response.data;
|
|
|
|
|
|
},
|
|
|
|
|
|
(error: any) => {
|
|
|
|
|
|
// const status = _.get(error, 'response.status')
|
|
|
|
|
|
// switch (status) {
|
|
|
|
|
|
// case 400: error.message = '请求错误'; break
|
|
|
|
|
|
// case 401: error.message = '未授权,请登录'; break
|
|
|
|
|
|
// case 403: error.message = '拒绝访问'; break
|
|
|
|
|
|
// case 404: error.message = `请求地址出错: ${error.response.config.url}`; break
|
|
|
|
|
|
// case 408: error.message = '请求超时'; break
|
|
|
|
|
|
// case 500: error.message = '服务器内部错误'; break
|
|
|
|
|
|
// case 501: error.message = '服务未实现'; break
|
|
|
|
|
|
// case 502: error.message = '网关错误'; break
|
|
|
|
|
|
// case 503: error.message = '服务不可用'; break
|
|
|
|
|
|
// case 504: error.message = '网关超时'; break
|
|
|
|
|
|
// case 505: error.message = 'HTTP版本不受支持'; break
|
|
|
|
|
|
// default: break
|
|
|
|
|
|
// }
|
2024-08-30 18:50:53 +08:00
|
|
|
|
logger.error(
|
2024-09-09 16:01:42 +08:00
|
|
|
|
`请求出错:status:${error.response?.status},statusText:${error.response?.statusText},url:${error.config?.url},method:${error.config?.method}。`
|
2024-08-30 18:50:53 +08:00
|
|
|
|
);
|
2024-09-09 16:01:42 +08:00
|
|
|
|
logger.error("返回数据:", JSON.stringify(error.response?.data));
|
|
|
|
|
|
|
2024-09-09 10:17:40 +08:00
|
|
|
|
if (error instanceof AggregateError) {
|
|
|
|
|
|
logger.error(error);
|
|
|
|
|
|
}
|
2024-08-30 18:50:53 +08:00
|
|
|
|
const err = new HttpError(error);
|
|
|
|
|
|
return Promise.reject(err);
|
2022-11-07 23:31:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
return service;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-09 16:01:42 +08:00
|
|
|
|
export const http = createAxiosService({ logger }) as HttpClient;
|
|
|
|
|
|
export type HttpClientResponse<R> = any;
|
|
|
|
|
|
export type HttpClient = {
|
|
|
|
|
|
request<D = any, R = any>(config: AxiosRequestConfig<D>): Promise<HttpClientResponse<R>>;
|
|
|
|
|
|
};
|