diff --git a/packages/hoppscotch-common/src/helpers/import-export/import/insomnia.ts b/packages/hoppscotch-common/src/helpers/import-export/import/insomnia.ts index 473afb9f9..5cbd4b169 100644 --- a/packages/hoppscotch-common/src/helpers/import-export/import/insomnia.ts +++ b/packages/hoppscotch-common/src/helpers/import-export/import/insomnia.ts @@ -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 type InsomniaDoc = UnwrapPromise> 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 = (