Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring proxy handling to be done in strategies #500

Merged
5 changes: 0 additions & 5 deletions functions/network.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import AxiosStrategy from "./strategies/AxiosStrategy";
import ProxyStrategy from "./strategies/ProxyStrategy";
import FirefoxStrategy from "./strategies/FirefoxStrategy";


Expand All @@ -10,10 +9,6 @@ const runAppropriateStrategy = (req, store) => {
return FirefoxStrategy(req, store);
}

if (store.state.postwoman.settings.PROXY_ENABLED) {
return ProxyStrategy(req, store);
}

return AxiosStrategy(req, store);
}

Expand Down
17 changes: 16 additions & 1 deletion functions/strategies/AxiosStrategy.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import axios from "axios";

const axiosStrategy = async (req, _store) => {
const axiosWithProxy = async (req, store) => {
const { data } = await axios.post(
store.state.postwoman.settings.PROXY_URL ||
"https://postwoman.apollotv.xyz/",
req
);
return data;
}

const axiosWithoutProxy = async (req, _store) => {
const res = await axios(req);
return res;
};

const axiosStrategy = (req, store) => {
if (store.state.postwoman.settings.PROXY_ENABLED)
return axiosWithProxy(req, store);
else return axiosWithoutProxy(req, store);
AndrewBastin marked this conversation as resolved.
Show resolved Hide resolved
AndrewBastin marked this conversation as resolved.
Show resolved Hide resolved
}

export default axiosStrategy;
25 changes: 24 additions & 1 deletion functions/strategies/FirefoxStrategy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
const firefoxStrategy = (req, _store) => new Promise((resolve, reject) => {

const firefoxWithProxy = (req, store) => new Promise((resolve, reject) => {
const eventListener = (event) => {
window.removeEventListener("firefoxExtSendRequestComplete", event);

if (event.detail.error) reject(JSON.parse(event.detail.error));
else resolve(JSON.parse(event.detail.response));
jamesgeorge007 marked this conversation as resolved.
Show resolved Hide resolved
};

window.addEventListener("firefoxExtSendRequestComplete", eventListener);

window.firefoxExtSendRequest({
method: "post",
url: store.state.postwoman.settings.PROXY_URL || "https://postwoman.apollotv.xyz/",
data: req
});
});

const firefoxWithoutProxy = (req, _store) => new Promise((resolve, reject) => {
const eventListener = (event) => {
window.removeEventListener("firefoxExtSendRequestComplete", eventListener);

Expand All @@ -12,4 +29,10 @@ const firefoxStrategy = (req, _store) => new Promise((resolve, reject) => {
window.firefoxExtSendRequest(req);
});

const firefoxStrategy = (req, store) => {
if (store.state.postwoman.settings.PROXY_ENABLED)
return firefoxWithProxy(req, store);
else return firefoxWithoutProxy(req, store);
jamesgeorge007 marked this conversation as resolved.
Show resolved Hide resolved
AndrewBastin marked this conversation as resolved.
Show resolved Hide resolved
}

export default firefoxStrategy;
12 changes: 0 additions & 12 deletions functions/strategies/ProxyStrategy.js

This file was deleted.