refactor: urlencoded key value pair system

This commit is contained in:
Andrew Bastin
2022-01-21 19:32:05 +05:30
parent 09269b3cec
commit 73cccf73df
6 changed files with 448 additions and 14 deletions

View File

@@ -0,0 +1,2 @@
export const stringArrayJoin = (separator: string) => (arr: string[]) =>
arr.join(separator)

View File

@@ -0,0 +1,39 @@
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.map(parseRawKeyValueEntry),
RA.toArray
)
export const rawKeyValueEntriesToString = (entries: RawKeyValueEntry[]) =>
pipe(
entries,
A.map(({ key, value, active }) =>
active ? `${key}: ${value}` : `# ${key}: ${value}`
),
stringArrayJoin("\n")
)

View File

@@ -1,7 +1,6 @@
import * as RA from "fp-ts/ReadonlyArray"
import * as S from "fp-ts/string"
import * as A from "fp-ts/Array"
import qs from "qs"
import { pipe, flow } from "fp-ts/function"
import { pipe } from "fp-ts/function"
import { combineLatest, Observable } from "rxjs"
import { map } from "rxjs/operators"
import {
@@ -11,6 +10,7 @@ import {
} from "@hoppscotch/data"
import { parseTemplateString, parseBodyEnvVariables } from "../templating"
import { tupleToRecord } from "../functional/record"
import { parseRawKeyValueEntries } from "../rawKeyValue"
import { Environment, getGlobalVariables } from "~/newstore/environments"
export interface EffectiveHoppRESTRequest extends HoppRESTRequest {
@@ -66,18 +66,15 @@ function getFinalBodyFromRequest(
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
parseRawKeyValueEntries,
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
// 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
)
}