api-client/functions/network.js

43 lines
1.4 KiB
JavaScript
Raw Normal View History

import AxiosStrategy from "./strategies/AxiosStrategy";
import ExtensionStrategy, {
hasExtensionInstalled
} from "./strategies/ExtensionStrategy";
import FirefoxStrategy from "./strategies/FirefoxStrategy";
2020-02-20 03:22:42 +00:00
import ChromeStrategy, {
hasChromeExtensionInstalled
} from "./strategies/ChromeStrategy";
2020-02-20 03:22:42 +00:00
const isExtensionsAllowed = ({ state }) =>
typeof state.postwoman.settings.EXTENSIONS_ENABLED === "undefined" ||
state.postwoman.settings.EXTENSIONS_ENABLED;
const runAppropriateStrategy = (req, store) => {
if (isExtensionsAllowed(store)) {
if (hasExtensionInstalled()) {
2020-02-16 14:48:10 +00:00
return ExtensionStrategy(req, store);
}
// The following strategies are deprecated and kept to support older version of the extensions
// Chrome Provides a chrome object for scripts to access
// Check its availability to say whether you are in Google Chrome
if (window.chrome && hasChromeExtensionInstalled()) {
2020-02-20 03:22:42 +00:00
return ChromeStrategy(req, store);
}
// The firefox plugin injects a function to send requests through it
// If that is available, then we can use the FirefoxStrategy
if (window.firefoxExtSendRequest) {
2020-02-04 19:01:26 +00:00
return FirefoxStrategy(req, store);
}
}
return AxiosStrategy(req, store);
2020-02-20 03:22:42 +00:00
};
2020-02-04 19:01:26 +00:00
const sendNetworkRequest = (req, store) =>
2020-02-20 03:22:42 +00:00
runAppropriateStrategy(req, store).finally(() =>
window.$nuxt.$loading.finish()
);
export { sendNetworkRequest };