utils.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 = hasBrowserEnv &&
  21. (!_navigator || ['ReactNative', 'NativeScript', 'NS'].indexOf(_navigator.product) < 0);
  22. /**
  23. * Determine if we're running in a standard browser webWorker environment
  24. *
  25. * Although the `isStandardBrowserEnv` method indicates that
  26. * `allows axios to run in a web worker`, the WebWorker will still be
  27. * filtered out due to its judgment standard
  28. * `typeof window !== 'undefined' && typeof document !== 'undefined'`.
  29. * This leads to a problem when axios post `FormData` in webWorker
  30. */
  31. const hasStandardBrowserWebWorkerEnv = (() => {
  32. return (
  33. typeof WorkerGlobalScope !== 'undefined' &&
  34. // eslint-disable-next-line no-undef
  35. self instanceof WorkerGlobalScope &&
  36. typeof self.importScripts === 'function'
  37. );
  38. })();
  39. const origin = hasBrowserEnv && window.location.href || 'http://localhost';
  40. export {
  41. hasBrowserEnv,
  42. hasStandardBrowserWebWorkerEnv,
  43. hasStandardBrowserEnv,
  44. _navigator as navigator,
  45. origin
  46. }