vben-admin-thin-next/build/vite/proxy.ts

35 lines
839 B
TypeScript
Raw Normal View History

2021-02-09 23:47:14 +08:00
/**
* Used to parse the .env.development proxy configuration
*/
2021-03-26 21:16:08 +08:00
import type { ProxyOptions } from 'vite';
2021-01-09 23:28:52 +08:00
2020-09-28 20:19:10 +08:00
type ProxyItem = [string, string];
type ProxyList = ProxyItem[];
2021-03-26 21:16:08 +08:00
type ProxyTargetList = Record<string, ProxyOptions & { 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;
}