Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

Added cancellable requests for AxiosStrategy

Andrew Bastinb2600d3ffdfe
diff --git a/functions/strategies/AxiosStrategy.js b/functions/strategies/AxiosStrategy.js
index 1deda3333..81361009f 100644
--- a/functions/strategies/AxiosStrategy.js
+++ b/functions/strategies/AxiosStrategy.js
@@ -1,39 +1,69 @@
import axios from "axios"
+let cancelSource = axios.CancelToken.source()
+
+export const cancelRunningAxiosRequest = () => {
+ cancelSource.cancel()
+
+ // Create a new cancel token
+ cancelSource = axios.CancelToken.source()
+}
+
const axiosWithProxy = async (req, { state }) => {
- const { data } = await axios.post(
- state.postwoman.settings.PROXY_URL || "https://postwoman.apollosoftware.xyz/",
- req
- )
- return data
+ try {
+ const { data } = await axios.post(
+ state.postwoman.settings.PROXY_URL || "https://postwoman.apollosoftware.xyz/",
+ req,
+ {
+ cancelToken: cancelSource.token,
+ }
+ )
+ return data
+ } catch (e) {
+ // Check if the throw is due to a cancellation
+ if (axios.isCancel(e)) {
+ throw "cancellation"
+ } else {
+ throw e
+ }
+ }
}
const axiosWithoutProxy = async (req, _store) => {
- const res = await axios({
- ...req,
- transformResponse: [
- (data, headers) => {
- // If the response has a JSON content type, try parsing it
- if (
- headers["content-type"] &&
- (headers["content-type"].startsWith("application/json") ||
- headers["content-type"].startsWith("application/vnd.api+json") ||
- headers["content-type"].startsWith("application/hal+json"))
- ) {
- try {
- const jsonData = JSON.parse(data)
- return jsonData
- } catch (e) {
- return data
+ try {
+ const res = await axios({
+ ...req,
+ cancelToken: cancelSource.token,
+ transformResponse: [
+ (data, headers) => {
+ // If the response has a JSON content type, try parsing it
+ if (
+ headers["content-type"] &&
+ (headers["content-type"].startsWith("application/json") ||
+ headers["content-type"].startsWith("application/vnd.api+json") ||
+ headers["content-type"].startsWith("application/hal+json"))
+ ) {
+ try {
+ const jsonData = JSON.parse(data)
+ return jsonData
+ } catch (e) {
+ return data
+ }
}
- }
-
- // Else return the string itself without any transformations
- return data
- },
- ],
- })
- return res
+
+ // Else return the string itself without any transformations
+ return data
+ },
+ ],
+ })
+ return res
+ } catch (e) {
+ if (axios.isCancel(e)) {
+ throw "cancellation"
+ } else {
+ throw e
+ }
+ }
}
const axiosStrategy = (req, store) => {