vben
2021-01-19 ca4f1a8faf7d588c0d57d0dc81f4dc04cd757380
提交 | 用户 | age
99ac30 1 import type { ServerOptions } from 'http-proxy';
V 2
2f6253 3 type ProxyItem = [string, string];
4
5 type ProxyList = ProxyItem[];
6
99ac30 7 type ProxyTargetList = Record<string, ServerOptions & { rewrite: (path: string) => string }>;
bd7b53 8
V 9 const httpsRE = /^https:\/\//;
2f1fbf 10
V 11 /**
12  * Generate proxy
13  * @param list
14  */
526e6c 15 export function createProxy(list: ProxyList = []) {
bd7b53 16   const ret: ProxyTargetList = {};
2f6253 17   for (const [prefix, target] of list) {
bd7b53 18     const isHttps = httpsRE.test(target);
710158 19
99ac30 20     // https://github.com/http-party/node-http-proxy#options
2f6253 21     ret[prefix] = {
22       target: target,
23       changeOrigin: true,
99ac30 24       ws: true,
bd7b53 25       rewrite: (path) => path.replace(new RegExp(`^${prefix}`), ''),
2f1fbf 26       // https is require secure=false
710158 27       ...(isHttps ? { secure: false } : {}),
2f6253 28     };
29   }
30   return ret;
31 }