fix: urlencoded form evaluation issues

This commit is contained in:
Andrew Bastin
2022-01-19 23:11:26 +05:30
parent 0efbc58b26
commit 09269b3cec
6 changed files with 58 additions and 18 deletions

View File

@@ -0,0 +1,9 @@
/**
* Logs the current value and returns the same value
* @param x The value to log
* @returns The parameter `x` passed to this
*/
export const trace = <T>(x: T) => {
console.log(x)
return x
}

View File

@@ -0,0 +1,7 @@
export const tupleToRecord = <
KeyType extends string | number | symbol,
ValueType
>(
tuples: [KeyType, ValueType][]
): Record<KeyType, ValueType> =>
(Object.assign as any)(...tuples.map(([key, val]) => ({ [key]: val })))

View File

@@ -122,8 +122,9 @@ const axiosWithoutProxy: NetworkStrategy = (req) =>
)
const axiosStrategy: NetworkStrategy = (req) =>
settingsStore.value.PROXY_ENABLED
? axiosWithProxy(req)
: axiosWithoutProxy(req)
pipe(
req,
settingsStore.value.PROXY_ENABLED ? axiosWithProxy : axiosWithoutProxy
)
export default axiosStrategy

View File

@@ -1,3 +1,7 @@
import * as RA from "fp-ts/ReadonlyArray"
import * as S from "fp-ts/string"
import qs from "qs"
import { pipe, flow } from "fp-ts/function"
import { combineLatest, Observable } from "rxjs"
import { map } from "rxjs/operators"
import {
@@ -6,6 +10,7 @@ import {
HoppRESTRequest,
} from "@hoppscotch/data"
import { parseTemplateString, parseBodyEnvVariables } from "../templating"
import { tupleToRecord } from "../functional/record"
import { Environment, getGlobalVariables } from "~/newstore/environments"
export interface EffectiveHoppRESTRequest extends HoppRESTRequest {
@@ -58,6 +63,25 @@ function getFinalBodyFromRequest(
return null
}
if (request.body.contentType === "application/x-www-form-urlencoded") {
return pipe(
request.body.body,
S.split("\n"),
RA.map(
flow(
// Define how each lines are parsed
S.split(":"), // Split by ":"
RA.map(S.trim), // Remove trailing spaces in key/value begins and ends
([key, value]) => [key, value ?? ""] as [string, string] // Add a default empty by default
)
),
RA.toArray,
tupleToRecord, // Convert the tuple to a record
qs.stringify
)
}
if (request.body.contentType === "multipart/form-data") {
const formData = new FormData()

View File

@@ -94,6 +94,7 @@
"openapi-types": "^10.0.0",
"paho-mqtt": "^1.1.0",
"postman-collection": "^4.1.1",
"qs": "^6.10.3",
"rxjs": "^7.5.2",
"socket.io-client-v2": "npm:socket.io-client@^2.4.0",
"socket.io-client-v3": "npm:socket.io-client@^3.1.3",