proxy.ts 792 B

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