refactor: pm import body calculation and type corrections

This commit is contained in:
Andrew Bastin
2022-03-18 19:58:03 +05:30
parent d9e1569055
commit bd1780cb6f
2 changed files with 30 additions and 25 deletions

View File

@@ -1,6 +1,5 @@
import { import {
Collection as PMCollection, Collection as PMCollection,
FormParam,
Item, Item,
ItemGroup, ItemGroup,
QueryParam, QueryParam,
@@ -18,6 +17,7 @@ import {
makeCollection, makeCollection,
ValidContentTypes, ValidContentTypes,
knownContentTypes, knownContentTypes,
FormDataKeyValue,
} from "@hoppscotch/data" } from "@hoppscotch/data"
import { pipe, flow } from "fp-ts/function" import { pipe, flow } from "fp-ts/function"
import * as S from "fp-ts/string" import * as S from "fp-ts/string"
@@ -27,6 +27,7 @@ import * as TE from "fp-ts/TaskEither"
import { step } from "../steps" import { step } from "../steps"
import { defineImporter, IMPORTER_INVALID_FILE_FORMAT } from "." import { defineImporter, IMPORTER_INVALID_FILE_FORMAT } from "."
import { PMRawLanguage } from "~/types/pm-coll-exts" import { PMRawLanguage } from "~/types/pm-coll-exts"
import { stringArrayJoin } from "~/helpers/functional/array"
const safeParseJSON = (jsonStr: string) => O.tryCatch(() => JSON.parse(jsonStr)) const safeParseJSON = (jsonStr: string) => O.tryCatch(() => JSON.parse(jsonStr))
@@ -161,42 +162,42 @@ const getHoppReqAuth = (item: Item): HoppRESTAuth => {
return { authType: "none", authActive: true } return { authType: "none", authActive: true }
} }
type PMFormDataParamType = FormParam & {
type: "file" | "text"
}
const getHoppReqBody = (item: Item): HoppRESTReqBody => { const getHoppReqBody = (item: Item): HoppRESTReqBody => {
if (!item.request.body) return { contentType: null, body: null } if (!item.request.body) return { contentType: null, body: null }
// TODO: Implement
const body = item.request.body const body = item.request.body
if (body.mode === "formdata") { if (body.mode === "formdata") {
return { return {
contentType: "multipart/form-data", contentType: "multipart/form-data",
body: body: pipe(
(body.formdata?.all() as PMFormDataParamType[]).map((param) => ({ body.formdata?.all() ?? [],
key: replacePMVarTemplating(param.key), A.map(
value: replacePMVarTemplating( (param) =>
param.type === "text" ? (param.value as string) : "" <FormDataKeyValue>{
), key: replacePMVarTemplating(param.key),
active: !param.disabled, value: replacePMVarTemplating(
isFile: false, // TODO: Preserve isFile state ? param.type === "text" ? (param.value as string) : ""
})) ?? [], ),
active: !param.disabled,
isFile: false, // TODO: Preserve isFile state ?
}
)
),
} }
} else if (body.mode === "urlencoded") { } else if (body.mode === "urlencoded") {
return { return {
contentType: "application/x-www-form-urlencoded", contentType: "application/x-www-form-urlencoded",
body: body: pipe(
body.urlencoded body.urlencoded?.all() ?? [],
?.all() A.map(
.map( (param) =>
(param) => `${replacePMVarTemplating(
`${replacePMVarTemplating( param.key ?? ""
param.key ?? "" )}: ${replacePMVarTemplating(param.value ?? "")}`
)}: ${replacePMVarTemplating(param.value ?? "")}` ),
) stringArrayJoin("\n")
.join("\n") ?? "", ),
} }
} else if (body.mode === "raw") { } else if (body.mode === "raw") {
return pipe( return pipe(

View File

@@ -18,4 +18,8 @@ declare module "postman-collection" {
} }
} }
} }
interface FormParam {
type: "file" | "text"
}
} }