| 12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- /**
- * Used to parse the .env.development proxy configuration
- */
- import type { ProxyOptions } from 'vite';
- type ProxyItem = [string, string];
- type ProxyList = ProxyItem[];
- type ProxyTargetList = Record<string, ProxyOptions & { rewrite: (path: string) => string }>;
- const httpsRE = /^https:\/\//;
- /**
- * Generate proxy
- * @param list
- */
- export function createProxy(list: ProxyList = []) {
- const ret: ProxyTargetList = {};
- for (const [prefix, target] of list) {
- const isHttps = httpsRE.test(target);
- // https://github.com/http-party/node-http-proxy#options
- ret[prefix] = {
- target: target,
- changeOrigin: true,
- ws: true,
- // logLevel: 'debug', // 打印转发日志
- rewrite: (path) => path.replace(new RegExp(`^${prefix}`), ''),
- // https is require secure=false
- ...(isHttps ? { secure: false } : {}),
- // configure: (proxy, options) => {
- // proxy.on('proxyReq', (proxyReq, req, res) => {
- // console.log(`[Proxy] ${req.method} ${req.url}`);
- // });
- // proxy.on('proxyRes', (proxyRes, req, res) => {
- // console.log(`[Proxy] ${res.statusCode} ${req.method} ${req.url}`);
- // });
- // }
- };
- }
- return ret;
- }
|