proxy.ts 781 B

12345678910111213141516171819202122232425262728293031
  1. import type { ServerOptions } from 'http-proxy';
  2. type ProxyItem = [string, string];
  3. type ProxyList = ProxyItem[];
  4. type ProxyTargetList = Record<string, ServerOptions & { rewrite: (path: string) => string }>;
  5. const httpsRE = /^https:\/\//;
  6. /**
  7. * Generate proxy
  8. * @param list
  9. */
  10. export function createProxy(list: ProxyList = []) {
  11. const ret: ProxyTargetList = {};
  12. for (const [prefix, target] of list) {
  13. const isHttps = httpsRE.test(target);
  14. // https://github.com/http-party/node-http-proxy#options
  15. ret[prefix] = {
  16. target: target,
  17. changeOrigin: true,
  18. ws: true,
  19. rewrite: (path) => path.replace(new RegExp(`^${prefix}`), ''),
  20. // https is require secure=false
  21. ...(isHttps ? { secure: false } : {}),
  22. };
  23. }
  24. return ret;
  25. }