AxiosStrategy.ts (hoppscotch-2.2.1) | : | AxiosStrategy.ts (hoppscotch-3.0.0) | ||
---|---|---|---|---|
import axios, { AxiosRequestConfig } from "axios" | import axios, { AxiosRequestConfig } from "axios" | |||
import { v4 } from "uuid" | import { v4 } from "uuid" | |||
import { pipe } from "fp-ts/function" | import { pipe } from "fp-ts/function" | |||
import * as TE from "fp-ts/TaskEither" | import * as TE from "fp-ts/TaskEither" | |||
import { cloneDeep } from "lodash-es" | ||||
import { NetworkResponse, NetworkStrategy } from "../network" | import { NetworkResponse, NetworkStrategy } from "../network" | |||
import { decodeB64StringToArrayBuffer } from "../utils/b64" | import { decodeB64StringToArrayBuffer } from "../utils/b64" | |||
import { settingsStore } from "~/newstore/settings" | import { settingsStore } from "~/newstore/settings" | |||
let cancelSource = axios.CancelToken.source() | let cancelSource = axios.CancelToken.source() | |||
type ProxyHeaders = { | type ProxyHeaders = { | |||
"multipart-part-key"?: string | "multipart-part-key"?: string | |||
} | } | |||
skipping to change at line 43 | skipping to change at line 44 | |||
if (payload.data instanceof FormData) { | if (payload.data instanceof FormData) { | |||
const formData = payload.data | const formData = payload.data | |||
payload.data = "" | payload.data = "" | |||
formData.append(multipartKey!, JSON.stringify(payload)) | formData.append(multipartKey!, JSON.stringify(payload)) | |||
payload = formData | payload = formData | |||
} | } | |||
return payload | return payload | |||
} | } | |||
const preProcessRequest = (req: AxiosRequestConfig): AxiosRequestConfig => { | ||||
const reqClone = cloneDeep(req) | ||||
// If the parameters are URLSearchParams, inject them to URL instead | ||||
// This prevents issues of marshalling the URLSearchParams to the proxy | ||||
if (reqClone.params instanceof URLSearchParams) { | ||||
try { | ||||
const url = new URL(reqClone.url ?? "") | ||||
for (const [key, value] of reqClone.params.entries()) { | ||||
url.searchParams.append(key, value) | ||||
} | ||||
reqClone.url = url.toString() | ||||
} catch (e) { | ||||
// making this a non-empty block, so we can make the linter happy. | ||||
// we should probably use, allowEmptyCatch, or take the time to do somethi | ||||
ng with the caught errors :) | ||||
} | ||||
reqClone.params = {} | ||||
} | ||||
return reqClone | ||||
} | ||||
const axiosWithProxy: NetworkStrategy = (req) => | const axiosWithProxy: NetworkStrategy = (req) => | |||
pipe( | pipe( | |||
TE.Do, | TE.Do, | |||
TE.bind("processedReq", () => TE.of(preProcessRequest(req))), | ||||
// If the request has FormData, the proxy needs a key | // If the request has FormData, the proxy needs a key | |||
TE.bind("multipartKey", () => | TE.bind("multipartKey", ({ processedReq }) => | |||
TE.of(req.data instanceof FormData ? v4() : null) | TE.of(processedReq.data instanceof FormData ? v4() : null) | |||
), | ), | |||
// Build headers to send | // Build headers to send | |||
TE.bind("headers", ({ multipartKey }) => | TE.bind("headers", ({ processedReq, multipartKey }) => | |||
TE.of( | TE.of( | |||
req.data instanceof FormData | processedReq.data instanceof FormData | |||
? <ProxyHeaders>{ | ? <ProxyHeaders>{ | |||
"multipart-part-key": `proxyRequestData-${multipartKey}`, | "multipart-part-key": `proxyRequestData-${multipartKey}`, | |||
} | } | |||
: <ProxyHeaders>{} | : <ProxyHeaders>{} | |||
) | ) | |||
), | ), | |||
// Create payload | // Create payload | |||
TE.bind("payload", ({ multipartKey }) => | TE.bind("payload", ({ processedReq, multipartKey }) => | |||
TE.of(getProxyPayload(req, multipartKey)) | TE.of(getProxyPayload(processedReq, multipartKey)) | |||
), | ), | |||
// Run the proxy request | // Run the proxy request | |||
TE.chain(({ payload, headers }) => | TE.chain(({ payload, headers }) => | |||
TE.tryCatch( | TE.tryCatch( | |||
() => | () => | |||
axios.post( | axios.post( | |||
settingsStore.value.PROXY_URL || "https://proxy.hoppscotch.io", | settingsStore.value.PROXY_URL || "https://proxy.hoppscotch.io", | |||
payload, | payload, | |||
{ | { | |||
End of changes. 7 change blocks. | ||||
6 lines changed or deleted | 35 lines changed or added |