refactor: network strategy rewrite
This commit is contained in:
@@ -1,94 +0,0 @@
|
||||
import axios from "axios"
|
||||
import { v4 } from "uuid"
|
||||
import { decodeB64StringToArrayBuffer } from "../utils/b64"
|
||||
import { settingsStore } from "~/newstore/settings"
|
||||
import { JsonFormattedError } from "~/helpers/utils/JsonFormattedError"
|
||||
|
||||
let cancelSource = axios.CancelToken.source()
|
||||
|
||||
export const cancelRunningAxiosRequest = () => {
|
||||
cancelSource.cancel()
|
||||
|
||||
// Create a new cancel token
|
||||
cancelSource = axios.CancelToken.source()
|
||||
}
|
||||
|
||||
const axiosWithProxy = async (req) => {
|
||||
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 axiosRequest to form
|
||||
proxyReqPayload = formData
|
||||
}
|
||||
const { data } = await axios.post(
|
||||
settingsStore.value.PROXY_URL || "https://proxy.hoppscotch.io",
|
||||
proxyReqPayload,
|
||||
{
|
||||
headers,
|
||||
cancelToken: cancelSource.token,
|
||||
}
|
||||
)
|
||||
|
||||
if (!data.success) {
|
||||
throw new Error(data.data.message || "Proxy Error")
|
||||
}
|
||||
|
||||
if (data.isBinary) {
|
||||
data.data = decodeB64StringToArrayBuffer(data.data)
|
||||
}
|
||||
|
||||
return data
|
||||
} catch (e) {
|
||||
// Check if the throw is due to a cancellation
|
||||
if (axios.isCancel(e)) {
|
||||
// eslint-disable-next-line no-throw-literal
|
||||
throw "cancellation"
|
||||
} else {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const axiosWithoutProxy = async (req, _store) => {
|
||||
try {
|
||||
const res = await axios({
|
||||
...req,
|
||||
cancelToken: (cancelSource && cancelSource.token) || "",
|
||||
responseType: "arraybuffer",
|
||||
})
|
||||
return res
|
||||
} catch (e) {
|
||||
if (axios.isCancel(e)) {
|
||||
// eslint-disable-next-line no-throw-literal
|
||||
throw "cancellation"
|
||||
} else if (e.response?.data) {
|
||||
throw new JsonFormattedError(
|
||||
JSON.parse(Buffer.from(e.response.data, "base64").toString("utf8"))
|
||||
)
|
||||
} else {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const axiosStrategy = (req) => {
|
||||
if (settingsStore.value.PROXY_ENABLED) {
|
||||
return axiosWithProxy(req)
|
||||
}
|
||||
return axiosWithoutProxy(req)
|
||||
}
|
||||
|
||||
export const testables = {
|
||||
cancelSource,
|
||||
}
|
||||
|
||||
export default axiosStrategy
|
||||
129
packages/hoppscotch-app/helpers/strategies/AxiosStrategy.ts
Normal file
129
packages/hoppscotch-app/helpers/strategies/AxiosStrategy.ts
Normal file
@@ -0,0 +1,129 @@
|
||||
import axios, { AxiosRequestConfig } from "axios"
|
||||
import { v4 } from "uuid"
|
||||
import { pipe } from "fp-ts/function"
|
||||
import * as TE from "fp-ts/TaskEither"
|
||||
import { NetworkResponse, NetworkStrategy } from "../network"
|
||||
import { decodeB64StringToArrayBuffer } from "../utils/b64"
|
||||
import { settingsStore } from "~/newstore/settings"
|
||||
|
||||
let cancelSource = axios.CancelToken.source()
|
||||
|
||||
type ProxyHeaders = {
|
||||
"multipart-part-key"?: string
|
||||
}
|
||||
|
||||
type ProxyPayloadType = FormData | (AxiosRequestConfig & { wantsBinary: true })
|
||||
|
||||
export const cancelRunningAxiosRequest = () => {
|
||||
cancelSource.cancel()
|
||||
|
||||
// Create a new cancel token
|
||||
cancelSource = axios.CancelToken.source()
|
||||
}
|
||||
|
||||
const getProxyPayload = (
|
||||
req: AxiosRequestConfig,
|
||||
multipartKey: string | null
|
||||
) => {
|
||||
let payload: ProxyPayloadType = {
|
||||
...req,
|
||||
wantsBinary: true,
|
||||
}
|
||||
|
||||
if (payload.data instanceof FormData) {
|
||||
const formData = payload.data
|
||||
payload.data = ""
|
||||
formData.append(multipartKey!, JSON.stringify(payload))
|
||||
payload = formData
|
||||
}
|
||||
|
||||
return payload
|
||||
}
|
||||
|
||||
const axiosWithProxy: NetworkStrategy = (req) =>
|
||||
pipe(
|
||||
TE.Do,
|
||||
|
||||
// If the request has FormData, the proxy needs a key
|
||||
TE.bind("multipartKey", () =>
|
||||
TE.of(req.data instanceof FormData ? v4() : null)
|
||||
),
|
||||
|
||||
// Build headers to send
|
||||
TE.bind("headers", ({ multipartKey }) =>
|
||||
TE.of(
|
||||
req.data instanceof FormData
|
||||
? <ProxyHeaders>{
|
||||
"multipart-part-key": `proxyRequestData-${multipartKey}`,
|
||||
}
|
||||
: <ProxyHeaders>{}
|
||||
)
|
||||
),
|
||||
|
||||
// Create payload
|
||||
TE.bind("payload", ({ multipartKey }) =>
|
||||
TE.of(getProxyPayload(req, multipartKey))
|
||||
),
|
||||
|
||||
// Run the proxy request
|
||||
TE.chain(({ payload, headers }) =>
|
||||
TE.tryCatch(
|
||||
() =>
|
||||
axios.post(
|
||||
settingsStore.value.PROXY_URL || "https://proxy.hoppscotch.io",
|
||||
payload,
|
||||
{
|
||||
headers,
|
||||
cancelToken: cancelSource.token,
|
||||
}
|
||||
),
|
||||
(reason) =>
|
||||
axios.isCancel(reason)
|
||||
? "cancellation" // Convert cancellation errors into cancellation strings
|
||||
: reason
|
||||
)
|
||||
),
|
||||
|
||||
// Check success predicate
|
||||
TE.chain(
|
||||
TE.fromPredicate(
|
||||
({ data }) => data.success,
|
||||
({ data }) => data.data.message || "Proxy Error"
|
||||
)
|
||||
),
|
||||
|
||||
// Process Base64
|
||||
TE.chain(({ data }) => {
|
||||
if (data.isBinary) {
|
||||
data.data = decodeB64StringToArrayBuffer(data.data)
|
||||
}
|
||||
|
||||
return TE.of(data)
|
||||
})
|
||||
)
|
||||
|
||||
const axiosWithoutProxy: NetworkStrategy = (req) =>
|
||||
pipe(
|
||||
TE.tryCatch(
|
||||
() =>
|
||||
axios({
|
||||
...req,
|
||||
cancelToken: (cancelSource && cancelSource.token) || "",
|
||||
responseType: "arraybuffer",
|
||||
}),
|
||||
(e) => (axios.isCancel(e) ? "cancellation" : (e as any))
|
||||
),
|
||||
|
||||
TE.orElse((e) =>
|
||||
e !== "cancellation" && e.response
|
||||
? TE.right(e.response as NetworkResponse)
|
||||
: TE.left(e)
|
||||
)
|
||||
)
|
||||
|
||||
const axiosStrategy: NetworkStrategy = (req) =>
|
||||
settingsStore.value.PROXY_ENABLED
|
||||
? axiosWithProxy(req)
|
||||
: axiosWithoutProxy(req)
|
||||
|
||||
export default axiosStrategy
|
||||
@@ -1,90 +0,0 @@
|
||||
import { decodeB64StringToArrayBuffer } from "../utils/b64"
|
||||
import { settingsStore } from "~/newstore/settings"
|
||||
|
||||
export const hasExtensionInstalled = () =>
|
||||
typeof window.__POSTWOMAN_EXTENSION_HOOK__ !== "undefined"
|
||||
|
||||
export const hasChromeExtensionInstalled = () =>
|
||||
hasExtensionInstalled() &&
|
||||
/Chrome/i.test(navigator.userAgent) &&
|
||||
/Google/i.test(navigator.vendor)
|
||||
|
||||
export const hasFirefoxExtensionInstalled = () =>
|
||||
hasExtensionInstalled() && /Firefox/i.test(navigator.userAgent)
|
||||
|
||||
export const cancelRunningExtensionRequest = () => {
|
||||
if (
|
||||
hasExtensionInstalled() &&
|
||||
window.__POSTWOMAN_EXTENSION_HOOK__.cancelRunningRequest
|
||||
) {
|
||||
window.__POSTWOMAN_EXTENSION_HOOK__.cancelRunningRequest()
|
||||
}
|
||||
}
|
||||
|
||||
const extensionWithProxy = async (req) => {
|
||||
const backupTimeDataStart = new Date().getTime()
|
||||
|
||||
const res = await window.__POSTWOMAN_EXTENSION_HOOK__.sendRequest({
|
||||
method: "post",
|
||||
url: settingsStore.value.PROXY_URL || "https://proxy.hoppscotch.io/",
|
||||
data: {
|
||||
...req,
|
||||
wantsBinary: true,
|
||||
},
|
||||
})
|
||||
|
||||
const backupTimeDataEnd = new Date().getTime()
|
||||
|
||||
const parsedData = JSON.parse(res.data)
|
||||
|
||||
if (!parsedData.success) {
|
||||
throw new Error(parsedData.data.message || "Proxy Error")
|
||||
}
|
||||
|
||||
if (parsedData.isBinary) {
|
||||
parsedData.data = decodeB64StringToArrayBuffer(parsedData.data)
|
||||
}
|
||||
|
||||
if (!(res && res.config && res.config.timeData)) {
|
||||
res.config = {
|
||||
timeData: {
|
||||
startTime: backupTimeDataStart,
|
||||
endTime: backupTimeDataEnd,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
parsedData.config = res.config
|
||||
|
||||
return parsedData
|
||||
}
|
||||
|
||||
const extensionWithoutProxy = async (req) => {
|
||||
const backupTimeDataStart = new Date().getTime()
|
||||
|
||||
const res = await window.__POSTWOMAN_EXTENSION_HOOK__.sendRequest({
|
||||
...req,
|
||||
wantsBinary: true,
|
||||
})
|
||||
|
||||
const backupTimeDataEnd = new Date().getTime()
|
||||
|
||||
if (!(res && res.config && res.config.timeData)) {
|
||||
res.config = {
|
||||
timeData: {
|
||||
startTime: backupTimeDataStart,
|
||||
endTime: backupTimeDataEnd,
|
||||
},
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
const extensionStrategy = (req) => {
|
||||
if (settingsStore.value.PROXY_ENABLED) {
|
||||
return extensionWithProxy(req)
|
||||
}
|
||||
return extensionWithoutProxy(req)
|
||||
}
|
||||
|
||||
export default extensionStrategy
|
||||
@@ -0,0 +1,62 @@
|
||||
import * as TE from "fp-ts/TaskEither"
|
||||
import { pipe } from "fp-ts/function"
|
||||
import { NetworkResponse, NetworkStrategy } from "../network"
|
||||
|
||||
export const hasExtensionInstalled = () =>
|
||||
typeof window.__POSTWOMAN_EXTENSION_HOOK__ !== "undefined"
|
||||
|
||||
export const hasChromeExtensionInstalled = () =>
|
||||
hasExtensionInstalled() &&
|
||||
/Chrome/i.test(navigator.userAgent) &&
|
||||
/Google/i.test(navigator.vendor)
|
||||
|
||||
export const hasFirefoxExtensionInstalled = () =>
|
||||
hasExtensionInstalled() && /Firefox/i.test(navigator.userAgent)
|
||||
|
||||
export const cancelRunningExtensionRequest = () => {
|
||||
if (
|
||||
hasExtensionInstalled() &&
|
||||
window.__POSTWOMAN_EXTENSION_HOOK__.cancelRunningRequest
|
||||
) {
|
||||
window.__POSTWOMAN_EXTENSION_HOOK__.cancelRunningRequest()
|
||||
}
|
||||
}
|
||||
|
||||
const extensionStrategy: NetworkStrategy = (req) =>
|
||||
pipe(
|
||||
TE.Do,
|
||||
|
||||
// Storeing backup timing data in case the extension does not have that info
|
||||
TE.bind("backupTimeDataStart", () => TE.of(new Date().getTime())),
|
||||
|
||||
// Run the request
|
||||
TE.bind("response", () =>
|
||||
TE.tryCatch(
|
||||
() =>
|
||||
window.__POSTWOMAN_EXTENSION_HOOK__.sendRequest({
|
||||
...req,
|
||||
wantsBinary: true,
|
||||
}) as Promise<NetworkResponse>,
|
||||
(err) => err as any
|
||||
)
|
||||
),
|
||||
|
||||
// Inject backup time data if not present
|
||||
TE.map(({ backupTimeDataStart, response }) => ({
|
||||
...response,
|
||||
config: {
|
||||
timeData: {
|
||||
startTime: backupTimeDataStart,
|
||||
endTime: new Date().getTime(),
|
||||
},
|
||||
...response.config,
|
||||
},
|
||||
})),
|
||||
TE.orElse((e) =>
|
||||
e !== "cancellation" && e.response
|
||||
? TE.right(e.response as NetworkResponse)
|
||||
: TE.left(e)
|
||||
)
|
||||
)
|
||||
|
||||
export default extensionStrategy
|
||||
Reference in New Issue
Block a user