mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-27 04:31:27 +08:00
- Changed cacheAxios from undefined to null for better initialization. - Updated proxy handling to use ProxyAgent, ensuring axios instance is recreated when the proxy changes. - Simplified axios instance creation by directly using the current proxy agent.
30 lines
883 B
TypeScript
30 lines
883 B
TypeScript
import { AxiosInstance, default as axios_ } from 'axios'
|
|
import { ProxyAgent } from 'proxy-agent'
|
|
|
|
import { proxyManager } from './ProxyManager'
|
|
|
|
class AxiosProxy {
|
|
private cacheAxios: AxiosInstance | null = null
|
|
private proxyAgent: ProxyAgent | null = null
|
|
|
|
get axios(): AxiosInstance {
|
|
const currentProxyAgent = proxyManager.getProxyAgent()
|
|
|
|
// 如果代理发生变化或尚未初始化,则重新创建 axios 实例
|
|
if (this.cacheAxios === null || (currentProxyAgent !== null && this.proxyAgent !== currentProxyAgent)) {
|
|
this.proxyAgent = currentProxyAgent
|
|
|
|
// 创建带有代理配置的 axios 实例
|
|
this.cacheAxios = axios_.create({
|
|
proxy: false,
|
|
httpAgent: currentProxyAgent || undefined,
|
|
httpsAgent: currentProxyAgent || undefined
|
|
})
|
|
}
|
|
|
|
return this.cacheAxios
|
|
}
|
|
}
|
|
|
|
export default new AxiosProxy()
|