Merge pull request #487 from AndrewBastin/refactor/network-strategy
Network Strategies
This commit is contained in:
13
functions/network.js
Normal file
13
functions/network.js
Normal 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 };
|
||||||
9
functions/strategies/AxiosStrategy.js
Normal file
9
functions/strategies/AxiosStrategy.js
Normal 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;
|
||||||
13
functions/strategies/ProxyStrategy.js
Normal file
13
functions/strategies/ProxyStrategy.js
Normal 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;
|
||||||
@@ -391,6 +391,7 @@ import axios from "axios";
|
|||||||
import * as gql from "graphql";
|
import * as gql from "graphql";
|
||||||
import textareaAutoHeight from "../directives/textareaAutoHeight";
|
import textareaAutoHeight from "../directives/textareaAutoHeight";
|
||||||
import AceEditor from "../components/ace-editor";
|
import AceEditor from "../components/ace-editor";
|
||||||
|
import { sendNetworkRequest } from "../functions/network";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
directives: {
|
directives: {
|
||||||
@@ -690,21 +691,7 @@ export default {
|
|||||||
data: JSON.stringify({ query: gqlQueryString, variables })
|
data: JSON.stringify({ query: gqlQueryString, variables })
|
||||||
};
|
};
|
||||||
|
|
||||||
const reqConfig = this.$store.state.postwoman.settings.PROXY_ENABLED
|
const data = await sendNetworkRequest(reqOptions, this.$store);
|
||||||
? {
|
|
||||||
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;
|
|
||||||
|
|
||||||
this.responseString = JSON.stringify(data.data, null, 2);
|
this.responseString = JSON.stringify(data.data, null, 2);
|
||||||
|
|
||||||
|
|||||||
@@ -1135,6 +1135,7 @@ import getEnvironmentVariablesFromScript from "../functions/preRequest";
|
|||||||
import parseTemplateString from "../functions/templating";
|
import parseTemplateString from "../functions/templating";
|
||||||
import AceEditor from "../components/ace-editor";
|
import AceEditor from "../components/ace-editor";
|
||||||
import { tokenRequest, oauthRedirect } from "../assets/js/oauth";
|
import { tokenRequest, oauthRedirect } from "../assets/js/oauth";
|
||||||
|
import { sendNetworkRequest } from "../functions/network";
|
||||||
|
|
||||||
const statusCategories = [
|
const statusCategories = [
|
||||||
{
|
{
|
||||||
@@ -1992,20 +1993,8 @@ export default {
|
|||||||
if (typeof requestOptions.data === "string") {
|
if (typeof requestOptions.data === "string") {
|
||||||
requestOptions.data = parseTemplateString(requestOptions.data);
|
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 await sendNetworkRequest(requestOptions, this.$store);
|
||||||
return this.$store.state.postwoman.settings.PROXY_ENABLED
|
|
||||||
? response.data
|
|
||||||
: response;
|
|
||||||
},
|
},
|
||||||
async sendRequest() {
|
async sendRequest() {
|
||||||
this.$toast.clear();
|
this.$toast.clear();
|
||||||
|
|||||||
Reference in New Issue
Block a user