fix: same key params are not overwritten to the last defined (#2299)

Co-authored-by: Andrew Bastin <andrewbastin.k@gmail.com>
This commit is contained in:
Anwarul Islam
2022-04-30 19:53:43 +06:00
committed by GitHub
parent 07735994e1
commit 50744136d0
3 changed files with 66 additions and 15 deletions

View File

@@ -132,13 +132,13 @@ export function createRESTNetworkRequestStream(
), ),
// Assembling params object // Assembling params object
TE.bind("params", ({ req }) => TE.bind("params", ({ req }) => {
TE.of( const params = new URLSearchParams()
req.effectiveFinalParams.reduce((acc, { key, value }) => { req.effectiveFinalParams.forEach((x) => {
return Object.assign(acc, { [key]: value }) params.append(x.key, x.value)
}, {}) })
) return TE.of(params)
), }),
// Keeping the backup start time // Keeping the backup start time
TE.bind("backupTimeStart", () => TE.of(Date.now())), TE.bind("backupTimeStart", () => TE.of(Date.now())),

View File

@@ -2,6 +2,7 @@ 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/cloneDeep"
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"
@@ -40,19 +41,43 @@ const getProxyPayload = (
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) {}
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}`,
} }
@@ -61,8 +86,8 @@ const axiosWithProxy: NetworkStrategy = (req) =>
), ),
// 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

View File

@@ -1,5 +1,7 @@
import * as TE from "fp-ts/TaskEither" import * as TE from "fp-ts/TaskEither"
import { pipe } from "fp-ts/function" import { pipe } from "fp-ts/function"
import { AxiosRequestConfig } from "axios"
import cloneDeep from "lodash/cloneDeep"
import { NetworkResponse, NetworkStrategy } from "../network" import { NetworkResponse, NetworkStrategy } from "../network"
import { browserIsChrome, browserIsFirefox } from "../utils/userAgent" import { browserIsChrome, browserIsFirefox } from "../utils/userAgent"
@@ -21,19 +23,43 @@ export const cancelRunningExtensionRequest = () => {
} }
} }
const preProcessRequest = (req: AxiosRequestConfig): AxiosRequestConfig => {
const reqClone = cloneDeep(req)
// If the parameters are URLSearchParams, inject them to URL instead
// This prevents marshalling issues with structured cloning of URLSearchParams
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) {}
reqClone.params = {}
}
return reqClone
}
const extensionStrategy: NetworkStrategy = (req) => const extensionStrategy: NetworkStrategy = (req) =>
pipe( pipe(
TE.Do, TE.Do,
TE.bind("processedReq", () => TE.of(preProcessRequest(req))),
// Storeing backup timing data in case the extension does not have that info // Storeing backup timing data in case the extension does not have that info
TE.bind("backupTimeDataStart", () => TE.of(new Date().getTime())), TE.bind("backupTimeDataStart", () => TE.of(new Date().getTime())),
// Run the request // Run the request
TE.bind("response", () => TE.bind("response", ({ processedReq }) =>
TE.tryCatch( TE.tryCatch(
() => () =>
window.__POSTWOMAN_EXTENSION_HOOK__.sendRequest({ window.__POSTWOMAN_EXTENSION_HOOK__.sendRequest({
...req, ...processedReq,
wantsBinary: true, wantsBinary: true,
}) as Promise<NetworkResponse>, }) as Promise<NetworkResponse>,
(err) => err as any (err) => err as any