diff --git a/functions/network.js b/functions/network.js new file mode 100644 index 000000000..f4ad8ec66 --- /dev/null +++ b/functions/network.js @@ -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 }; diff --git a/functions/strategies/AxiosStrategy.js b/functions/strategies/AxiosStrategy.js new file mode 100644 index 000000000..de74ec377 --- /dev/null +++ b/functions/strategies/AxiosStrategy.js @@ -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; diff --git a/functions/strategies/ProxyStrategy.js b/functions/strategies/ProxyStrategy.js new file mode 100644 index 000000000..945482f48 --- /dev/null +++ b/functions/strategies/ProxyStrategy.js @@ -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; diff --git a/pages/graphql.vue b/pages/graphql.vue index 98f6e427c..5abbde18e 100644 --- a/pages/graphql.vue +++ b/pages/graphql.vue @@ -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); diff --git a/pages/index.vue b/pages/index.vue index 1036c0c03..003f925a2 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -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();