proxy.ts 717 B

1234567891011121314151617181920212223242526272829303132333435
  1. type ProxyItem = [string, string];
  2. type ProxyList = ProxyItem[];
  3. type ProxyTargetList = Record<
  4. string,
  5. {
  6. target: string;
  7. changeOrigin: boolean;
  8. rewrite: (path: string) => any;
  9. secure?: boolean;
  10. }
  11. >;
  12. const httpsRE = /^https:\/\//;
  13. /**
  14. * Generate proxy
  15. * @param list
  16. */
  17. export function createProxy(list: ProxyList = []) {
  18. const ret: ProxyTargetList = {};
  19. for (const [prefix, target] of list) {
  20. const isHttps = httpsRE.test(target);
  21. ret[prefix] = {
  22. target: target,
  23. changeOrigin: true,
  24. rewrite: (path) => path.replace(new RegExp(`^${prefix}`), ''),
  25. // https is require secure=false
  26. ...(isHttps ? { secure: false } : {}),
  27. };
  28. }
  29. return ret;
  30. }