UNPKG

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