Merge branch 'fix/urlencoded'
This commit is contained in:
@@ -39,3 +39,6 @@ export const arrayFlatMap =
|
||||
<T, U>(mapFunc: (value: T, index: number, arr: T[]) => U[]) =>
|
||||
(arr: T[]) =>
|
||||
arr.flatMap(mapFunc)
|
||||
|
||||
export const stringArrayJoin = (separator: string) => (arr: string[]) =>
|
||||
arr.join(separator)
|
||||
|
||||
9
packages/hoppscotch-app/helpers/functional/debug.ts
Normal file
9
packages/hoppscotch-app/helpers/functional/debug.ts
Normal 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
|
||||
}
|
||||
9
packages/hoppscotch-app/helpers/functional/record.ts
Normal file
9
packages/hoppscotch-app/helpers/functional/record.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
export const tupleToRecord = <
|
||||
KeyType extends string | number | symbol,
|
||||
ValueType
|
||||
>(
|
||||
tuples: [KeyType, ValueType][]
|
||||
): Record<KeyType, ValueType> =>
|
||||
tuples.length > 0
|
||||
? (Object.assign as any)(...tuples.map(([key, val]) => ({ [key]: val })))
|
||||
: {}
|
||||
@@ -1,3 +1,6 @@
|
||||
import * as RA from "fp-ts/ReadonlyArray"
|
||||
import * as S from "fp-ts/string"
|
||||
import { pipe, flow } from "fp-ts/function"
|
||||
import * as Har from "har-format"
|
||||
import { HoppRESTRequest } from "@hoppscotch/data"
|
||||
import { FieldEquals, objectFieldIncludes } from "../typeutils"
|
||||
@@ -33,16 +36,24 @@ const buildHarPostParams = (
|
||||
): Har.Param[] => {
|
||||
// URL Encoded strings have a string style of contents
|
||||
if (req.body.contentType === "application/x-www-form-urlencoded") {
|
||||
return req.body.body
|
||||
.split("&") // Split by separators
|
||||
.map((keyValue) => {
|
||||
const [key, value] = keyValue.split("=")
|
||||
return pipe(
|
||||
req.body.body,
|
||||
S.split("\n"),
|
||||
RA.map(
|
||||
flow(
|
||||
// Define how each lines are parsed
|
||||
|
||||
return {
|
||||
name: key,
|
||||
value,
|
||||
}
|
||||
})
|
||||
S.split(":"), // Split by ":"
|
||||
RA.map(S.trim), // Remove trailing spaces in key/value begins and ends
|
||||
([key, value]) => ({
|
||||
// Convert into a proper key value definition
|
||||
name: key,
|
||||
value: value ?? "", // Value can be undefined (if no ":" is present)
|
||||
})
|
||||
)
|
||||
),
|
||||
RA.toArray
|
||||
)
|
||||
} else {
|
||||
// FormData has its own format
|
||||
return req.body.body.flatMap((entry) => {
|
||||
|
||||
40
packages/hoppscotch-app/helpers/rawKeyValue.ts
Normal file
40
packages/hoppscotch-app/helpers/rawKeyValue.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import * as A from "fp-ts/Array"
|
||||
import * as RA from "fp-ts/ReadonlyArray"
|
||||
import * as S from "fp-ts/string"
|
||||
import { pipe, flow } from "fp-ts/function"
|
||||
import { stringArrayJoin } from "./functional/array"
|
||||
|
||||
export type RawKeyValueEntry = {
|
||||
key: string
|
||||
value: string
|
||||
active: boolean
|
||||
}
|
||||
|
||||
const parseRawKeyValueEntry = (str: string): RawKeyValueEntry => {
|
||||
const trimmed = str.trim()
|
||||
const inactive = trimmed.startsWith("#")
|
||||
|
||||
const [key, value] = trimmed.split(":").map(S.trim)
|
||||
|
||||
return {
|
||||
key: inactive ? key.replaceAll(/^#+\s*/g, "") : key, // Remove comment hash and early space
|
||||
value,
|
||||
active: !inactive,
|
||||
}
|
||||
}
|
||||
|
||||
export const parseRawKeyValueEntries = flow(
|
||||
S.split("\n"),
|
||||
RA.filter((x) => x.trim().length > 0), // Remove lines which are empty
|
||||
RA.map(parseRawKeyValueEntry),
|
||||
RA.toArray
|
||||
)
|
||||
|
||||
export const rawKeyValueEntriesToString = (entries: RawKeyValueEntry[]) =>
|
||||
pipe(
|
||||
entries,
|
||||
A.map(({ key, value, active }) =>
|
||||
active ? `${key}: ${value}` : `# ${key}: ${value}`
|
||||
),
|
||||
stringArrayJoin("\n")
|
||||
)
|
||||
194
packages/hoppscotch-app/helpers/rules/BodyTransition.ts
Normal file
194
packages/hoppscotch-app/helpers/rules/BodyTransition.ts
Normal file
@@ -0,0 +1,194 @@
|
||||
/**
|
||||
* Defines how body should be updated for movement between different
|
||||
* content-types
|
||||
*/
|
||||
|
||||
import { pipe } from "fp-ts/function"
|
||||
import * as A from "fp-ts/Array"
|
||||
import {
|
||||
FormDataKeyValue,
|
||||
HoppRESTReqBody,
|
||||
ValidContentTypes,
|
||||
} from "@hoppscotch/data"
|
||||
import {
|
||||
parseRawKeyValueEntries,
|
||||
rawKeyValueEntriesToString,
|
||||
RawKeyValueEntry,
|
||||
} from "../rawKeyValue"
|
||||
|
||||
const ANY_TYPE = Symbol("TRANSITION_RULESET_IGNORE_TYPE")
|
||||
// eslint-disable-next-line no-redeclare
|
||||
type ANY_TYPE = typeof ANY_TYPE
|
||||
|
||||
type BodyType<T extends ValidContentTypes | null | ANY_TYPE> =
|
||||
T extends ValidContentTypes
|
||||
? HoppRESTReqBody & { contentType: T }
|
||||
: HoppRESTReqBody
|
||||
|
||||
type TransitionDefinition<
|
||||
FromType extends ValidContentTypes | null | ANY_TYPE,
|
||||
ToType extends ValidContentTypes | null | ANY_TYPE
|
||||
> = {
|
||||
from: FromType
|
||||
to: ToType
|
||||
definition: (
|
||||
currentBody: BodyType<FromType>,
|
||||
targetType: BodyType<ToType>["contentType"]
|
||||
) => BodyType<ToType>
|
||||
}
|
||||
|
||||
const rule = <
|
||||
FromType extends ValidContentTypes | null | ANY_TYPE,
|
||||
ToType extends ValidContentTypes | null | ANY_TYPE
|
||||
>(
|
||||
input: TransitionDefinition<FromType, ToType>
|
||||
) => input
|
||||
|
||||
// Use ANY_TYPE to ignore from/dest type
|
||||
// Rules apply from top to bottom
|
||||
const transitionRuleset = [
|
||||
rule({
|
||||
from: null,
|
||||
to: "multipart/form-data",
|
||||
definition: () => ({
|
||||
contentType: "multipart/form-data",
|
||||
body: [],
|
||||
}),
|
||||
}),
|
||||
rule({
|
||||
from: ANY_TYPE,
|
||||
to: null,
|
||||
definition: () => ({
|
||||
contentType: null,
|
||||
body: null,
|
||||
}),
|
||||
}),
|
||||
rule({
|
||||
from: null,
|
||||
to: ANY_TYPE,
|
||||
definition: (_, targetType) => ({
|
||||
contentType: targetType as unknown as Exclude<
|
||||
// This is guaranteed by the above rules, we just can't tell TS this
|
||||
ValidContentTypes,
|
||||
"multipart/form-data"
|
||||
>,
|
||||
body: "",
|
||||
}),
|
||||
}),
|
||||
rule({
|
||||
from: "multipart/form-data",
|
||||
to: "application/x-www-form-urlencoded",
|
||||
definition: (currentBody, targetType) => ({
|
||||
contentType: targetType,
|
||||
body: pipe(
|
||||
currentBody.body,
|
||||
A.map(
|
||||
({ key, value, isFile, active }) =>
|
||||
<RawKeyValueEntry>{
|
||||
key,
|
||||
value: isFile ? "" : value,
|
||||
active,
|
||||
}
|
||||
),
|
||||
rawKeyValueEntriesToString
|
||||
),
|
||||
}),
|
||||
}),
|
||||
rule({
|
||||
from: "application/x-www-form-urlencoded",
|
||||
to: "multipart/form-data",
|
||||
definition: (currentBody, targetType) => ({
|
||||
contentType: targetType,
|
||||
body: pipe(
|
||||
currentBody.body,
|
||||
parseRawKeyValueEntries,
|
||||
A.map(
|
||||
({ key, value, active }) =>
|
||||
<FormDataKeyValue>{
|
||||
key,
|
||||
value,
|
||||
active,
|
||||
isFile: false,
|
||||
}
|
||||
)
|
||||
),
|
||||
}),
|
||||
}),
|
||||
rule({
|
||||
from: ANY_TYPE,
|
||||
to: "multipart/form-data",
|
||||
definition: () => ({
|
||||
contentType: "multipart/form-data",
|
||||
body: [],
|
||||
}),
|
||||
}),
|
||||
rule({
|
||||
from: "multipart/form-data",
|
||||
to: ANY_TYPE,
|
||||
definition: (_, target) => ({
|
||||
contentType: target as Exclude<ValidContentTypes, "multipart/form-data">,
|
||||
body: "",
|
||||
}),
|
||||
}),
|
||||
rule({
|
||||
from: "application/x-www-form-urlencoded",
|
||||
to: ANY_TYPE,
|
||||
definition: (_, target) => ({
|
||||
contentType: target as Exclude<ValidContentTypes, "multipart/form-data">,
|
||||
body: "",
|
||||
}),
|
||||
}),
|
||||
rule({
|
||||
from: ANY_TYPE,
|
||||
to: "application/x-www-form-urlencoded",
|
||||
definition: () => ({
|
||||
contentType: "application/x-www-form-urlencoded",
|
||||
body: "",
|
||||
}),
|
||||
}),
|
||||
rule({
|
||||
from: ANY_TYPE,
|
||||
to: ANY_TYPE,
|
||||
definition: (curr, targetType) => ({
|
||||
contentType: targetType as Exclude<
|
||||
// Above rules ensure this will be the case
|
||||
ValidContentTypes,
|
||||
"multipart/form-data" | "application/x-www-form-urlencoded"
|
||||
>,
|
||||
// Again, above rules ensure this will be the case, can't convince TS tho
|
||||
body: (
|
||||
curr as HoppRESTReqBody & {
|
||||
contentType: Exclude<
|
||||
ValidContentTypes,
|
||||
"multipart/form-data" | "application/x-www-form-urlencoded"
|
||||
>
|
||||
}
|
||||
).body,
|
||||
}),
|
||||
}),
|
||||
] as const
|
||||
|
||||
export const applyBodyTransition = <T extends ValidContentTypes | null>(
|
||||
current: HoppRESTReqBody,
|
||||
target: T
|
||||
): HoppRESTReqBody & { contentType: T } => {
|
||||
if (current.contentType === target) {
|
||||
console.warn(
|
||||
`Tried to transition body from and to the same content-type '${target}'`
|
||||
)
|
||||
return current as any
|
||||
}
|
||||
|
||||
const transitioner = transitionRuleset.find(
|
||||
(def) =>
|
||||
(def.from === current.contentType || def.from === ANY_TYPE) &&
|
||||
(def.to === target || def.to === ANY_TYPE)
|
||||
)
|
||||
|
||||
if (!transitioner) {
|
||||
throw new Error("Body Type Transition Ruleset is invalid :(")
|
||||
}
|
||||
|
||||
// TypeScript won't be able to figure this out easily :(
|
||||
return (transitioner.definition as any)(current, target)
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -12,6 +12,9 @@ export const objectFieldIncludes = <
|
||||
field: K,
|
||||
values: V
|
||||
// eslint-disable-next-line
|
||||
): obj is T & { [_x in K]: V[number] } => {
|
||||
return values.includes(obj[field])
|
||||
}
|
||||
): obj is T & { [_x in K]: V[number] } => values.includes(obj[field])
|
||||
|
||||
export const valueIncludes = <T, V extends readonly T[]>(
|
||||
obj: T,
|
||||
values: V
|
||||
): obj is V[number] => values.includes(obj)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { pipe } from "fp-ts/function"
|
||||
import * as A from "fp-ts/Array"
|
||||
import qs from "qs"
|
||||
import { pipe } from "fp-ts/function"
|
||||
import { combineLatest, Observable } from "rxjs"
|
||||
import { map } from "rxjs/operators"
|
||||
import {
|
||||
@@ -10,6 +11,8 @@ import {
|
||||
import { parseTemplateString, parseBodyEnvVariables } from "../templating"
|
||||
import { arrayFlatMap, arraySort } from "../functional/array"
|
||||
import { toFormData } from "../functional/formData"
|
||||
import { tupleToRecord } from "../functional/record"
|
||||
import { parseRawKeyValueEntries } from "../rawKeyValue"
|
||||
import { Environment, getGlobalVariables } from "~/newstore/environments"
|
||||
|
||||
export interface EffectiveHoppRESTRequest extends HoppRESTRequest {
|
||||
@@ -62,6 +65,22 @@ function getFinalBodyFromRequest(
|
||||
return null
|
||||
}
|
||||
|
||||
if (request.body.contentType === "application/x-www-form-urlencoded") {
|
||||
return pipe(
|
||||
request.body.body,
|
||||
parseRawKeyValueEntries,
|
||||
|
||||
// Filter out active
|
||||
A.filter((x) => x.active),
|
||||
// Convert to tuple
|
||||
A.map(({ key, value }) => [key, value] as [string, string]),
|
||||
// Tuple to Record object
|
||||
tupleToRecord,
|
||||
// Stringify
|
||||
qs.stringify
|
||||
)
|
||||
}
|
||||
|
||||
if (request.body.contentType === "multipart/form-data") {
|
||||
return pipe(
|
||||
request.body.body,
|
||||
|
||||
Reference in New Issue
Block a user