import type { ProxyOptions } from "vite"; type ProxyItem = [string, string]; type ProxyList = ProxyItem[]; type ProxyTargetList = Record; /** * 创建代理,用于解析 .env.development 代理配置 * @param list */ export function createProxy(list: ProxyList = []) { const ret: ProxyTargetList = {}; for (const [prefix, target] of list) { const httpsRE = /^https:\/\//; const isHttps = httpsRE.test(target); // https://github.com/http-party/node-http-proxy#options ret[prefix] = { target: target, changeOrigin: true, ws: true, rewrite: path => path.replace(new RegExp(`^${prefix}`), ""), // https is require secure=false ...(isHttps ? { secure: false } : {}), configure: (proxy, options) => { proxy.on("proxyReq", (proxyReq, req, res, options) => { console.log("---------------------------------proxyReq----------------------------------"); console.log(proxyReq.path); }); proxy.on("proxyRes", (proxyRes, req, res) => { console.log("---------------------------------proxyRes----------------------------------"); console.log("RAW Response from the target", JSON.stringify(proxyRes.headers, null, 2)); }); proxy.on("error", function (err, req, res) { console.log("error : ", err); }); } }; } return ret; }