2024-10-08 19:02:51 +08:00
|
|
|
|
import axios, { AxiosRequestConfig } from 'axios';
|
|
|
|
|
|
import { logger } from './util.log.js';
|
|
|
|
|
|
import { Logger } from 'log4js';
|
|
|
|
|
|
import { HttpProxyAgent } from 'http-proxy-agent';
|
|
|
|
|
|
import { HttpsProxyAgent } from 'https-proxy-agent';
|
|
|
|
|
|
import nodeHttp from 'http';
|
|
|
|
|
|
import * as https from 'node:https';
|
2024-10-22 18:46:29 +08:00
|
|
|
|
import { merge } from 'lodash-es';
|
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;
|
2024-09-30 00:22:50 +08:00
|
|
|
|
request?: { baseURL: string; url: string; method: string; params?: any; data?: any };
|
2024-09-09 16:01:42 +08:00
|
|
|
|
response?: { data: any };
|
|
|
|
|
|
cause?: any;
|
2024-08-30 18:50:53 +08:00
|
|
|
|
constructor(error: any) {
|
|
|
|
|
|
if (!error) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
super(error.message);
|
2024-10-03 01:29:12 +08:00
|
|
|
|
|
2024-10-08 19:02:51 +08:00
|
|
|
|
if (error?.message?.indexOf('ssl3_get_record:wrong version number') >= 0) {
|
|
|
|
|
|
this.message = 'http协议错误,服务端要求http协议,请检查是否使用了https请求';
|
2024-10-03 01:29:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-30 18:50:53 +08:00
|
|
|
|
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-30 00:22:50 +08:00
|
|
|
|
baseURL: error.config?.baseURL,
|
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
|
|
|
|
};
|
2024-10-22 01:01:04 +08:00
|
|
|
|
let url = error.config?.url;
|
|
|
|
|
|
if (error.config?.baseURL) {
|
|
|
|
|
|
url = error.config?.baseURL + url;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (url) {
|
2024-10-22 18:46:29 +08:00
|
|
|
|
this.message = `${this.message} : url=${url}`;
|
2024-10-22 01:01:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
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;
|
2024-09-19 14:23:15 +08:00
|
|
|
|
// logger.error(error);
|
2024-08-30 18:50:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-09-19 14:23:15 +08:00
|
|
|
|
|
|
|
|
|
|
export const HttpCommonError = HttpError;
|
2024-10-12 16:49:49 +08:00
|
|
|
|
|
|
|
|
|
|
let defaultAgents = createAgent();
|
|
|
|
|
|
|
|
|
|
|
|
export function setGlobalProxy(opts: { httpProxy?: string; httpsProxy?: string }) {
|
|
|
|
|
|
logger.info('setGlobalProxy:', opts);
|
2024-10-26 23:24:26 +08:00
|
|
|
|
defaultAgents = createAgent(opts);
|
2024-10-12 16:49:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-22 01:01:04 +08:00
|
|
|
|
export function getGlobalAgents() {
|
|
|
|
|
|
return defaultAgents;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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();
|
2024-09-14 10:28:06 +08:00
|
|
|
|
|
2022-11-07 23:31:20 +08:00
|
|
|
|
// 请求拦截
|
|
|
|
|
|
service.interceptors.request.use(
|
|
|
|
|
|
(config: any) => {
|
2024-10-21 11:15:41 +08:00
|
|
|
|
logger.info(`http request:${config.url},method:${config.method}`);
|
2024-10-26 18:01:06 +08:00
|
|
|
|
if (config.logParams !== false && config.params) {
|
2024-10-21 11:15:41 +08:00
|
|
|
|
logger.info(`params:${JSON.stringify(config.params)}`);
|
|
|
|
|
|
}
|
2024-09-10 11:58:58 +08:00
|
|
|
|
if (config.timeout == null) {
|
2024-09-10 17:39:41 +08:00
|
|
|
|
config.timeout = 15000;
|
2024-09-10 11:58:58 +08:00
|
|
|
|
}
|
2024-09-22 00:33:09 +08:00
|
|
|
|
let agents = defaultAgents;
|
|
|
|
|
|
if (config.skipSslVerify) {
|
2024-10-08 19:02:51 +08:00
|
|
|
|
logger.info('跳过SSL验证');
|
2024-09-22 00:33:09 +08:00
|
|
|
|
agents = createAgent({ rejectUnauthorized: false } as any);
|
|
|
|
|
|
}
|
|
|
|
|
|
delete config.skipSslVerify;
|
|
|
|
|
|
config.httpsAgent = agents.httpsAgent;
|
|
|
|
|
|
config.httpAgent = agents.httpAgent;
|
2024-10-22 18:46:29 +08:00
|
|
|
|
|
|
|
|
|
|
// const agent = new https.Agent({
|
|
|
|
|
|
// rejectUnauthorized: false // 允许自签名证书
|
|
|
|
|
|
// });
|
|
|
|
|
|
// config.httpsAgent = agent;
|
2024-10-03 22:03:20 +08:00
|
|
|
|
config.proxy = false; //必须 否则还会走一层代理,
|
2022-11-07 23:31:20 +08:00
|
|
|
|
return config;
|
|
|
|
|
|
},
|
|
|
|
|
|
(error: Error) => {
|
|
|
|
|
|
// 发送失败
|
2024-10-08 19:02:51 +08:00
|
|
|
|
logger.error('接口请求失败:', error);
|
2022-11-07 23:31:20 +08:00
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
// 响应拦截
|
|
|
|
|
|
service.interceptors.response.use(
|
|
|
|
|
|
(response: any) => {
|
2024-10-21 11:15:41 +08:00
|
|
|
|
if (response?.config?.logRes !== false) {
|
|
|
|
|
|
logger.info(`http response : status=${response?.status},data=${JSON.stringify(response?.data)}`);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
logger.info('http response status:', response?.status);
|
|
|
|
|
|
}
|
2022-11-07 23:31:20 +08:00
|
|
|
|
return response.data;
|
|
|
|
|
|
},
|
|
|
|
|
|
(error: any) => {
|
2024-09-10 17:39:41 +08:00
|
|
|
|
const status = error.response?.status;
|
|
|
|
|
|
switch (status) {
|
|
|
|
|
|
case 400:
|
2024-10-08 19:02:51 +08:00
|
|
|
|
error.message = '请求错误';
|
2024-09-10 17:39:41 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case 401:
|
2024-10-08 19:02:51 +08:00
|
|
|
|
error.message = '未授权,请登录';
|
2024-09-10 17:39:41 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case 403:
|
2024-10-08 19:02:51 +08:00
|
|
|
|
error.message = '拒绝访问';
|
2024-09-10 17:39:41 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case 404:
|
|
|
|
|
|
error.message = `请求地址出错: ${error.response.config.url}`;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 408:
|
2024-10-08 19:02:51 +08:00
|
|
|
|
error.message = '请求超时';
|
2024-09-10 17:39:41 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case 500:
|
2024-10-08 19:02:51 +08:00
|
|
|
|
error.message = '服务器内部错误';
|
2024-09-10 17:39:41 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case 501:
|
2024-10-08 19:02:51 +08:00
|
|
|
|
error.message = '服务未实现';
|
2024-09-10 17:39:41 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case 502:
|
2024-10-08 19:02:51 +08:00
|
|
|
|
error.message = '网关错误';
|
2024-09-10 17:39:41 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case 503:
|
2024-10-08 19:02:51 +08:00
|
|
|
|
error.message = '服务不可用';
|
2024-09-10 17:39:41 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case 504:
|
2024-10-08 19:02:51 +08:00
|
|
|
|
error.message = '网关超时';
|
2024-09-10 17:39:41 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case 505:
|
2024-10-08 19:02:51 +08:00
|
|
|
|
error.message = 'HTTP版本不受支持';
|
2024-09-10 17:39:41 +08:00
|
|
|
|
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-10-08 19:02:51 +08:00
|
|
|
|
logger.error('返回数据:', JSON.stringify(error.response?.data));
|
2024-09-24 11:11:08 +08:00
|
|
|
|
if (error.response?.data) {
|
|
|
|
|
|
error.message = error.response.data.message || error.response.data.msg || error.response.data.error || error.response.data;
|
|
|
|
|
|
}
|
2024-09-09 10:17:40 +08:00
|
|
|
|
if (error instanceof AggregateError) {
|
2024-10-08 19:02:51 +08:00
|
|
|
|
logger.error('AggregateError', error);
|
2024-09-09 10:17:40 +08:00
|
|
|
|
}
|
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;
|
2024-10-26 18:01:06 +08:00
|
|
|
|
export type HttpRequestConfig<D = any> = {
|
2024-09-14 10:28:06 +08:00
|
|
|
|
skipSslVerify?: boolean;
|
2024-09-30 18:00:51 +08:00
|
|
|
|
skipCheckRes?: boolean;
|
2024-10-21 11:15:41 +08:00
|
|
|
|
logParams?: boolean;
|
|
|
|
|
|
logRes?: boolean;
|
2024-09-14 10:28:06 +08:00
|
|
|
|
} & AxiosRequestConfig<D>;
|
2024-09-09 16:01:42 +08:00
|
|
|
|
export type HttpClient = {
|
2024-09-14 10:28:06 +08:00
|
|
|
|
request<D = any, R = any>(config: HttpRequestConfig<D>): Promise<HttpClientResponse<R>>;
|
2024-09-09 16:01:42 +08:00
|
|
|
|
};
|
2024-09-14 10:28:06 +08:00
|
|
|
|
|
2024-10-26 23:24:26 +08:00
|
|
|
|
export type CreateAgentOptions = {
|
|
|
|
|
|
httpProxy?: string;
|
|
|
|
|
|
httpsProxy?: string;
|
|
|
|
|
|
} & nodeHttp.AgentOptions;
|
|
|
|
|
|
export function createAgent(opts: CreateAgentOptions = {}) {
|
2024-09-22 00:33:09 +08:00
|
|
|
|
let httpAgent, httpsAgent;
|
2024-10-26 23:24:26 +08:00
|
|
|
|
const httpProxy = opts.httpProxy || process.env.HTTP_PROXY || process.env.http_proxy;
|
2024-09-22 00:33:09 +08:00
|
|
|
|
if (httpProxy) {
|
2024-10-08 19:02:51 +08:00
|
|
|
|
logger.info('use httpProxy:', httpProxy);
|
2024-09-22 00:33:09 +08:00
|
|
|
|
httpAgent = new HttpProxyAgent(httpProxy, opts as any);
|
2024-10-22 18:46:29 +08:00
|
|
|
|
merge(httpAgent.options, opts);
|
2024-10-03 22:03:20 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
httpAgent = new nodeHttp.Agent(opts);
|
2024-09-22 00:33:09 +08:00
|
|
|
|
}
|
2024-10-26 23:24:26 +08:00
|
|
|
|
const httpsProxy = opts.httpsProxy || process.env.HTTPS_PROXY || process.env.https_proxy;
|
2024-09-22 00:33:09 +08:00
|
|
|
|
if (httpsProxy) {
|
2024-10-08 19:02:51 +08:00
|
|
|
|
logger.info('use httpsProxy:', httpsProxy);
|
2024-09-22 00:33:09 +08:00
|
|
|
|
httpsAgent = new HttpsProxyAgent(httpsProxy, opts as any);
|
2024-10-22 18:46:29 +08:00
|
|
|
|
merge(httpsAgent.options, opts);
|
2024-10-03 22:03:20 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
httpsAgent = new https.Agent(opts);
|
2024-09-22 00:33:09 +08:00
|
|
|
|
}
|
2024-09-14 10:28:06 +08:00
|
|
|
|
return {
|
|
|
|
|
|
httpAgent,
|
2024-09-22 00:33:09 +08:00
|
|
|
|
httpsAgent,
|
2024-09-14 10:28:06 +08:00
|
|
|
|
};
|
|
|
|
|
|
}
|