refactor: update insomnia import

This commit is contained in:
nivedin
2024-02-15 14:48:09 +05:30
committed by Andrew Bastin
parent d44083a380
commit 82d687f665

View File

@@ -8,6 +8,7 @@ import {
knownContentTypes,
makeCollection,
makeRESTRequest,
HoppRESTRequestVariable,
} from "@hoppscotch/data"
import * as A from "fp-ts/Array"
@@ -28,14 +29,27 @@ type UnwrapPromise<T extends Promise<any>> = T extends Promise<infer Y>
type InsomniaDoc = UnwrapPromise<ReturnType<typeof convert>>
type InsomniaResource = ImportRequest
// insomnia-importers v3.6.0 doesn't provide a type for path parameters and they have deprecated the library
type InsomniaPathParameter = {
name: string
value: string
}
type InsomniaFolderResource = ImportRequest & { _type: "request_group" }
type InsomniaRequestResource = ImportRequest & { _type: "request" }
type InsomniaRequestResource = ImportRequest & {
_type: "request"
} & {
pathParameters?: InsomniaPathParameter[]
}
const parseInsomniaDoc = (content: string) =>
TO.tryCatch(() => convert(content))
const replacePathVarTemplating = (expression: string) =>
expression.replaceAll(/:([^/]+)/g, "<<$1>>")
const replaceVarTemplating = (expression: string) =>
replaceInsomniaTemplating(expression)
pipe(expression, replacePathVarTemplating, replaceInsomniaTemplating)
const getFoldersIn = (
folder: InsomniaFolderResource | null,
@@ -177,6 +191,15 @@ const getHoppReqParams = (req: InsomniaRequestResource): HoppRESTParam[] =>
active: !(param.disabled ?? false),
})) ?? []
const getHoppReqVariables = (
req: InsomniaRequestResource
): HoppRESTRequestVariable[] =>
req.pathParameters?.map((variable) => ({
key: replaceVarTemplating(variable.name),
value: replaceVarTemplating(variable.value ?? ""),
active: true,
})) ?? []
const getHoppRequest = (req: InsomniaRequestResource): HoppRESTRequest =>
makeRESTRequest({
name: req.name ?? "Untitled Request",
@@ -189,6 +212,8 @@ const getHoppRequest = (req: InsomniaRequestResource): HoppRESTRequest =>
preRequestScript: "",
testScript: "",
requestVariables: getHoppReqVariables(req),
})
const getHoppFolder = (