AxiosStrategy.js (hoppscotch-2.0.0) | : | AxiosStrategy.js (hoppscotch-2.1.0) | ||
---|---|---|---|---|
import axios from "axios" | import axios from "axios" | |||
import { v4 } from "uuid" | ||||
import { decodeB64StringToArrayBuffer } from "../utils/b64" | import { decodeB64StringToArrayBuffer } from "../utils/b64" | |||
import { settingsStore } from "~/newstore/settings" | import { settingsStore } from "~/newstore/settings" | |||
import { JsonFormattedError } from "~/helpers/utils/JsonFormattedError" | ||||
let cancelSource = axios.CancelToken.source() | let cancelSource = axios.CancelToken.source() | |||
export const cancelRunningAxiosRequest = () => { | export const cancelRunningAxiosRequest = () => { | |||
cancelSource.cancel() | cancelSource.cancel() | |||
// Create a new cancel token | // Create a new cancel token | |||
cancelSource = axios.CancelToken.source() | cancelSource = axios.CancelToken.source() | |||
} | } | |||
const axiosWithProxy = async (req) => { | const axiosWithProxy = async (req) => { | |||
try { | try { | |||
let proxyReqPayload = { | ||||
...req, | ||||
wantsBinary: true, | ||||
} | ||||
const headers = {} | ||||
if (req.data instanceof FormData) { | ||||
const key = `proxyRequestData-${v4()}` // generate UniqueKey | ||||
headers["multipart-part-key"] = key | ||||
const formData = proxyReqPayload.data | ||||
proxyReqPayload.data = "" // discard formData | ||||
formData.append(key, JSON.stringify(proxyReqPayload)) // append axiosReque | ||||
st to form | ||||
proxyReqPayload = formData | ||||
} | ||||
const { data } = await axios.post( | const { data } = await axios.post( | |||
settingsStore.value.PROXY_URL || "https://proxy.hoppscotch.io", | settingsStore.value.PROXY_URL || "https://proxy.hoppscotch.io", | |||
proxyReqPayload, | ||||
{ | { | |||
...req, | headers, | |||
wantsBinary: true, | ||||
}, | ||||
{ | ||||
cancelToken: cancelSource.token, | cancelToken: cancelSource.token, | |||
} | } | |||
) | ) | |||
if (!data.success) { | if (!data.success) { | |||
throw new Error(data.data.message || "Proxy Error") | throw new Error(data.data.message || "Proxy Error") | |||
} | } | |||
if (data.isBinary) { | if (data.isBinary) { | |||
data.data = decodeB64StringToArrayBuffer(data.data) | data.data = decodeB64StringToArrayBuffer(data.data) | |||
} | } | |||
return data | return data | |||
} catch (e) { | } catch (e) { | |||
// Check if the throw is due to a cancellation | // Check if the throw is due to a cancellation | |||
if (axios.isCancel(e)) { | if (axios.isCancel(e)) { | |||
// eslint-disable-next-line no-throw-literal | // eslint-disable-next-line no-throw-literal | |||
throw "cancellation" | throw "cancellation" | |||
} else { | } else { | |||
console.error(e) | ||||
throw e | throw e | |||
} | } | |||
} | } | |||
} | } | |||
const axiosWithoutProxy = async (req, _store) => { | const axiosWithoutProxy = async (req, _store) => { | |||
try { | try { | |||
const res = await axios({ | const res = await axios({ | |||
...req, | ...req, | |||
cancelToken: (cancelSource && cancelSource.token) || "", | cancelToken: (cancelSource && cancelSource.token) || "", | |||
responseType: "arraybuffer", | responseType: "arraybuffer", | |||
}) | }) | |||
return res | return res | |||
} catch (e) { | } catch (e) { | |||
if (axios.isCancel(e)) { | if (axios.isCancel(e)) { | |||
// eslint-disable-next-line no-throw-literal | // eslint-disable-next-line no-throw-literal | |||
throw "cancellation" | throw "cancellation" | |||
} else if (e.response?.data) { | ||||
throw new JsonFormattedError( | ||||
JSON.parse(Buffer.from(e.response.data, "base64").toString("utf8")) | ||||
) | ||||
} else { | } else { | |||
console.error(e) | ||||
throw e | throw e | |||
} | } | |||
} | } | |||
} | } | |||
const axiosStrategy = (req) => { | const axiosStrategy = (req) => { | |||
if (settingsStore.value.PROXY_ENABLED) { | if (settingsStore.value.PROXY_ENABLED) { | |||
return axiosWithProxy(req) | return axiosWithProxy(req) | |||
} | } | |||
return axiosWithoutProxy(req) | return axiosWithoutProxy(req) | |||
End of changes. 9 change blocks. | ||||
7 lines changed or deleted | 23 lines changed or added |