gf-vben-admin/build/vite/proxy.ts

32 lines
781 B
TypeScript
Raw Normal View History

2021-01-09 23:28:52 +08:00
import type { ServerOptions } from 'http-proxy';
2020-09-28 20:19:10 +08:00
type ProxyItem = [string, string];
type ProxyList = ProxyItem[];
2021-01-09 23:28:52 +08:00
type ProxyTargetList = Record<string, ServerOptions & { rewrite: (path: string) => string }>;
const httpsRE = /^https:\/\//;
2020-10-29 23:01:11 +08:00
/**
* Generate proxy
* @param list
*/
2020-10-11 01:18:38 +08:00
export function createProxy(list: ProxyList = []) {
const ret: ProxyTargetList = {};
2020-09-28 20:19:10 +08:00
for (const [prefix, target] of list) {
const isHttps = httpsRE.test(target);
2020-10-18 21:55:21 +08:00
2021-01-09 23:28:52 +08:00
// https://github.com/http-party/node-http-proxy#options
2020-09-28 20:19:10 +08:00
ret[prefix] = {
target: target,
changeOrigin: true,
2021-01-09 23:28:52 +08:00
ws: true,
rewrite: (path) => path.replace(new RegExp(`^${prefix}`), ''),
2020-10-29 23:01:11 +08:00
// https is require secure=false
2020-10-18 21:55:21 +08:00
...(isHttps ? { secure: false } : {}),
2020-09-28 20:19:10 +08:00
};
}
return ret;
}