utils.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. const hasBrowserEnv = typeof window !== 'undefined' && typeof document !== 'undefined';
  2. /**
  3. * Determine if we're running in a standard browser environment
  4. *
  5. * This allows axios to run in a web worker, and react-native.
  6. * Both environments support XMLHttpRequest, but not fully standard globals.
  7. *
  8. * web workers:
  9. * typeof window -> undefined
  10. * typeof document -> undefined
  11. *
  12. * react-native:
  13. * navigator.product -> 'ReactNative'
  14. * nativescript
  15. * navigator.product -> 'NativeScript' or 'NS'
  16. *
  17. * @returns {boolean}
  18. */
  19. const hasStandardBrowserEnv = (
  20. (product) => {
  21. return hasBrowserEnv && ['ReactNative', 'NativeScript', 'NS'].indexOf(product) < 0
  22. })(typeof navigator !== 'undefined' && navigator.product);
  23. /**
  24. * Determine if we're running in a standard browser webWorker environment
  25. *
  26. * Although the `isStandardBrowserEnv` method indicates that
  27. * `allows axios to run in a web worker`, the WebWorker will still be
  28. * filtered out due to its judgment standard
  29. * `typeof window !== 'undefined' && typeof document !== 'undefined'`.
  30. * This leads to a problem when axios post `FormData` in webWorker
  31. */
  32. const hasStandardBrowserWebWorkerEnv = (() => {
  33. return (
  34. typeof WorkerGlobalScope !== 'undefined' &&
  35. // eslint-disable-next-line no-undef
  36. self instanceof WorkerGlobalScope &&
  37. typeof self.importScripts === 'function'
  38. );
  39. })();
  40. const origin = hasBrowserEnv && window.location.href || 'http://localhost';
  41. export {
  42. hasBrowserEnv,
  43. hasStandardBrowserWebWorkerEnv,
  44. hasStandardBrowserEnv,
  45. origin
  46. }