Merge branch 'master' into feature/post-request-tests
This commit is contained in:
24
functions/network.js
Normal file
24
functions/network.js
Normal file
@@ -0,0 +1,24 @@
|
||||
import AxiosStrategy from "./strategies/AxiosStrategy";
|
||||
import ProxyStrategy from "./strategies/ProxyStrategy";
|
||||
import FirefoxStrategy from "./strategies/FirefoxStrategy";
|
||||
|
||||
|
||||
const runAppropriateStrategy = (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) {
|
||||
return FirefoxStrategy(req, store);
|
||||
}
|
||||
|
||||
if (store.state.postwoman.settings.PROXY_ENABLED) {
|
||||
return ProxyStrategy(req, store);
|
||||
}
|
||||
|
||||
return AxiosStrategy(req, store);
|
||||
}
|
||||
|
||||
const sendNetworkRequest = (req, store) =>
|
||||
runAppropriateStrategy(req, store)
|
||||
.finally(() => window.$nuxt.$loading.finish());
|
||||
|
||||
export { sendNetworkRequest };
|
||||
8
functions/strategies/AxiosStrategy.js
Normal file
8
functions/strategies/AxiosStrategy.js
Normal file
@@ -0,0 +1,8 @@
|
||||
import axios from "axios";
|
||||
|
||||
const axiosStrategy = async (req, _store) => {
|
||||
const res = await axios(req);
|
||||
return res;
|
||||
};
|
||||
|
||||
export default axiosStrategy;
|
||||
15
functions/strategies/FirefoxStrategy.js
Normal file
15
functions/strategies/FirefoxStrategy.js
Normal file
@@ -0,0 +1,15 @@
|
||||
const firefoxStrategy = (req, _store) => new Promise((resolve, reject) => {
|
||||
|
||||
const eventListener = (event) => {
|
||||
window.removeEventListener("firefoxExtSendRequestComplete", eventListener);
|
||||
|
||||
if (event.detail.error) reject(JSON.parse(event.detail.error));
|
||||
else resolve(JSON.parse(event.detail.response));
|
||||
};
|
||||
|
||||
window.addEventListener("firefoxExtSendRequestComplete", eventListener);
|
||||
|
||||
window.firefoxExtSendRequest(req);
|
||||
});
|
||||
|
||||
export default firefoxStrategy;
|
||||
12
functions/strategies/ProxyStrategy.js
Normal file
12
functions/strategies/ProxyStrategy.js
Normal file
@@ -0,0 +1,12 @@
|
||||
import axios from "axios";
|
||||
|
||||
const proxyStrategy = async (req, store) => {
|
||||
const { data } = await axios.post(
|
||||
store.state.postwoman.settings.PROXY_URL ||
|
||||
"https://postwoman.apollotv.xyz/",
|
||||
req
|
||||
);
|
||||
return data;
|
||||
};
|
||||
|
||||
export default proxyStrategy;
|
||||
Reference in New Issue
Block a user