utils.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. const hasBrowserEnv = typeof window !== 'undefined' && typeof document !== 'undefined';
  2. const _navigator = (typeof navigator === 'object' && navigator) || undefined;
  3. /**
  4. * Determine if we're running in a standard browser environment
  5. *
  6. * This allows axios to run in a web worker, and react-native.
  7. * Both environments support XMLHttpRequest, but not fully standard globals.
  8. *
  9. * web workers:
  10. * typeof window -> undefined
  11. * typeof document -> undefined
  12. *
  13. * react-native:
  14. * navigator.product -> 'ReactNative'
  15. * nativescript
  16. * navigator.product -> 'NativeScript' or 'NS'
  17. *
  18. * @returns {boolean}
  19. */
  20. const hasStandardBrowserEnv =
  21. hasBrowserEnv &&
  22. (!_navigator || ['ReactNative', 'NativeScript', 'NS'].indexOf(_navigator.product) < 0);
  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. _navigator as navigator,
  46. origin,
  47. };