index.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import URLSearchParams from './classes/URLSearchParams.js'
  2. import FormData from './classes/FormData.js'
  3. import Blob from './classes/Blob.js'
  4. /**
  5. * Determine if we're running in a standard browser environment
  6. *
  7. * This allows axios to run in a web worker, and react-native.
  8. * Both environments support XMLHttpRequest, but not fully standard globals.
  9. *
  10. * web workers:
  11. * typeof window -> undefined
  12. * typeof document -> undefined
  13. *
  14. * react-native:
  15. * navigator.product -> 'ReactNative'
  16. * nativescript
  17. * navigator.product -> 'NativeScript' or 'NS'
  18. *
  19. * @returns {boolean}
  20. */
  21. const isStandardBrowserEnv = (() => {
  22. let product;
  23. if (typeof navigator !== 'undefined' && (
  24. (product = navigator.product) === 'ReactNative' ||
  25. product === 'NativeScript' ||
  26. product === 'NS')
  27. ) {
  28. return false;
  29. }
  30. return typeof window !== 'undefined' && typeof document !== 'undefined';
  31. })();
  32. /**
  33. * Determine if we're running in a standard browser webWorker environment
  34. *
  35. * Although the `isStandardBrowserEnv` method indicates that
  36. * `allows axios to run in a web worker`, the WebWorker will still be
  37. * filtered out due to its judgment standard
  38. * `typeof window !== 'undefined' && typeof document !== 'undefined'`.
  39. * This leads to a problem when axios post `FormData` in webWorker
  40. */
  41. const isStandardBrowserWebWorkerEnv = (() => {
  42. return (
  43. typeof WorkerGlobalScope !== 'undefined' &&
  44. // eslint-disable-next-line no-undef
  45. self instanceof WorkerGlobalScope &&
  46. typeof self.importScripts === 'function'
  47. );
  48. })();
  49. export default {
  50. isBrowser: true,
  51. classes: {
  52. URLSearchParams,
  53. FormData,
  54. Blob
  55. },
  56. isStandardBrowserEnv,
  57. isStandardBrowserWebWorkerEnv,
  58. protocols: ['http', 'https', 'file', 'blob', 'url', 'data']
  59. };