Merge pull request #487 from AndrewBastin/refactor/network-strategy

Network Strategies
This commit is contained in:
Andrew Bastin
2020-01-14 20:22:42 -05:00
committed by GitHub
5 changed files with 39 additions and 28 deletions

13
functions/network.js Normal file
View File

@@ -0,0 +1,13 @@
import AxiosStrategy from "./strategies/AxiosStrategy";
import ProxyStrategy from "./strategies/ProxyStrategy";
const sendNetworkRequest = (req, store) => {
if (store.state.postwoman.settings.PROXY_ENABLED) {
return ProxyStrategy(req, store);
}
return AxiosStrategy(req, store);
}
export { sendNetworkRequest };

View File

@@ -0,0 +1,9 @@
import axios from "axios";
const axiosStrategy = async (req, _store) => {
const res = await axios(req);
window.$nuxt.$loading.finish();
return res;
};
export default axiosStrategy;

View File

@@ -0,0 +1,13 @@
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
);
window.$nuxt.$loading.finish();
return data;
};
export default proxyStrategy;

View File

@@ -391,6 +391,7 @@ import axios from "axios";
import * as gql from "graphql";
import textareaAutoHeight from "../directives/textareaAutoHeight";
import AceEditor from "../components/ace-editor";
import { sendNetworkRequest } from "../functions/network";
export default {
directives: {
@@ -690,21 +691,7 @@ export default {
data: JSON.stringify({ query: gqlQueryString, variables })
};
const reqConfig = this.$store.state.postwoman.settings.PROXY_ENABLED
? {
method: "post",
url:
this.$store.state.postwoman.settings.PROXY_URL ||
`https://postwoman.apollotv.xyz/`,
data: reqOptions
}
: reqOptions;
const res = await axios(reqConfig);
const data = this.$store.state.postwoman.settings.PROXY_ENABLED
? res.data
: res;
const data = await sendNetworkRequest(reqOptions, this.$store);
this.responseString = JSON.stringify(data.data, null, 2);

View File

@@ -1135,6 +1135,7 @@ import getEnvironmentVariablesFromScript from "../functions/preRequest";
import parseTemplateString from "../functions/templating";
import AceEditor from "../components/ace-editor";
import { tokenRequest, oauthRedirect } from "../assets/js/oauth";
import { sendNetworkRequest } from "../functions/network";
const statusCategories = [
{
@@ -1992,20 +1993,8 @@ export default {
if (typeof requestOptions.data === "string") {
requestOptions.data = parseTemplateString(requestOptions.data);
}
const config = this.$store.state.postwoman.settings.PROXY_ENABLED
? {
method: "POST",
url:
this.$store.state.postwoman.settings.PROXY_URL ||
"https://postwoman.apollotv.xyz/",
data: requestOptions
}
: requestOptions;
const response = await this.$axios(config);
return this.$store.state.postwoman.settings.PROXY_ENABLED
? response.data
: response;
return await sendNetworkRequest(requestOptions, this.$store);
},
async sendRequest() {
this.$toast.clear();