import { IPlugin } from '../../intf/IPlugin' import { AxiosInstance } from 'axios' /** 启用本地动态代理 */ export const dynamicProxy = (proxyPath?: string): IPlugin => { return { pluginName: 'dynamic-proxy', handler: (axios: AxiosInstance, plugins?: Array) => { const { command } = process.env if (command !== 'serve') return if (plugins && plugins.find((pluginName: string) => pluginName === 'env')) { const { form, target } = axios.defaults.headers.common if (!form) throw new Error(`⚠️ headers中, 缺少 'referer' 配置, 动态代理启动失败!`) if (!target) throw new Error(`⚠️ headers中, 缺少 'target' 配置, 动态代理启动失败!`) if (form.includes(location.host)) return axios.defaults.baseURL = proxyPath ?? '/proxy' } else { console.warn('注意: 由于缺少 envs 配置, 动态代理能力未生效!') } } } }