Blame view

build/vite/proxy.ts 781 Bytes
vben authored
1
2
import type { ServerOptions } from 'http-proxy';
陈文彬 authored
3
4
5
6
type ProxyItem = [string, string];

type ProxyList = ProxyItem[];
vben authored
7
type ProxyTargetList = Record<string, ServerOptions & { rewrite: (path: string) => string }>;
8
9

const httpsRE = /^https:\/\//;
vben authored
10
11
12
13
14

/**
 * Generate proxy
 * @param list
 */
nebv authored
15
export function createProxy(list: ProxyList = []) {
16
  const ret: ProxyTargetList = {};
陈文彬 authored
17
  for (const [prefix, target] of list) {
18
    const isHttps = httpsRE.test(target);
vben authored
19
vben authored
20
    // https://github.com/http-party/node-http-proxy#options
陈文彬 authored
21
22
23
    ret[prefix] = {
      target: target,
      changeOrigin: true,
vben authored
24
      ws: true,
25
      rewrite: (path) => path.replace(new RegExp(`^${prefix}`), ''),
vben authored
26
      // https is require secure=false
vben authored
27
      ...(isHttps ? { secure: false } : {}),
陈文彬 authored
28
29
30
31
    };
  }
  return ret;
}