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