refactor: update insomnia import
This commit is contained in:
@@ -8,6 +8,7 @@ import {
|
|||||||
knownContentTypes,
|
knownContentTypes,
|
||||||
makeCollection,
|
makeCollection,
|
||||||
makeRESTRequest,
|
makeRESTRequest,
|
||||||
|
HoppRESTRequestVariable,
|
||||||
} from "@hoppscotch/data"
|
} from "@hoppscotch/data"
|
||||||
|
|
||||||
import * as A from "fp-ts/Array"
|
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 InsomniaDoc = UnwrapPromise<ReturnType<typeof convert>>
|
||||||
type InsomniaResource = ImportRequest
|
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 InsomniaFolderResource = ImportRequest & { _type: "request_group" }
|
||||||
type InsomniaRequestResource = ImportRequest & { _type: "request" }
|
type InsomniaRequestResource = ImportRequest & {
|
||||||
|
_type: "request"
|
||||||
|
} & {
|
||||||
|
pathParameters?: InsomniaPathParameter[]
|
||||||
|
}
|
||||||
|
|
||||||
const parseInsomniaDoc = (content: string) =>
|
const parseInsomniaDoc = (content: string) =>
|
||||||
TO.tryCatch(() => convert(content))
|
TO.tryCatch(() => convert(content))
|
||||||
|
|
||||||
|
const replacePathVarTemplating = (expression: string) =>
|
||||||
|
expression.replaceAll(/:([^/]+)/g, "<<$1>>")
|
||||||
|
|
||||||
const replaceVarTemplating = (expression: string) =>
|
const replaceVarTemplating = (expression: string) =>
|
||||||
replaceInsomniaTemplating(expression)
|
pipe(expression, replacePathVarTemplating, replaceInsomniaTemplating)
|
||||||
|
|
||||||
const getFoldersIn = (
|
const getFoldersIn = (
|
||||||
folder: InsomniaFolderResource | null,
|
folder: InsomniaFolderResource | null,
|
||||||
@@ -177,6 +191,15 @@ const getHoppReqParams = (req: InsomniaRequestResource): HoppRESTParam[] =>
|
|||||||
active: !(param.disabled ?? false),
|
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 =>
|
const getHoppRequest = (req: InsomniaRequestResource): HoppRESTRequest =>
|
||||||
makeRESTRequest({
|
makeRESTRequest({
|
||||||
name: req.name ?? "Untitled Request",
|
name: req.name ?? "Untitled Request",
|
||||||
@@ -189,6 +212,8 @@ const getHoppRequest = (req: InsomniaRequestResource): HoppRESTRequest =>
|
|||||||
|
|
||||||
preRequestScript: "",
|
preRequestScript: "",
|
||||||
testScript: "",
|
testScript: "",
|
||||||
|
|
||||||
|
requestVariables: getHoppReqVariables(req),
|
||||||
})
|
})
|
||||||
|
|
||||||
const getHoppFolder = (
|
const getHoppFolder = (
|
||||||
|
|||||||
Reference in New Issue
Block a user