feat: request variables (#3825)

Co-authored-by: jamesgeorge007 <jamesgeorge998001@gmail.com>
This commit is contained in:
Nivedin
2024-03-07 12:50:44 +05:30
committed by GitHub
parent 3611cac241
commit 7ec8659381
54 changed files with 1273 additions and 506 deletions

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 = (

View File

@@ -16,6 +16,7 @@ import {
makeRESTRequest,
HoppCollection,
makeCollection,
HoppRESTRequestVariable,
} from "@hoppscotch/data"
import { pipe, flow } from "fp-ts/function"
import * as A from "fp-ts/Array"
@@ -82,6 +83,27 @@ const parseOpenAPIParams = (params: OpenAPIParamsType[]): HoppRESTParam[] =>
)
)
const parseOpenAPIVariables = (
variables: OpenAPIParamsType[]
): HoppRESTRequestVariable[] =>
pipe(
variables,
A.filterMap(
flow(
O.fromPredicate((param) => param.in === "path"),
O.map(
(param) =>
<HoppRESTRequestVariable>{
key: param.name,
value: "", // TODO: Can we do anything more ? (parse default values maybe)
active: true,
}
)
)
)
)
const parseOpenAPIHeaders = (params: OpenAPIParamsType[]): HoppRESTHeader[] =>
pipe(
params,
@@ -577,6 +599,10 @@ const convertPathToHoppReqs = (
preRequestScript: "",
testScript: "",
requestVariables: parseOpenAPIVariables(
(info.parameters as OpenAPIParamsType[] | undefined) ?? []
),
})
}),

View File

@@ -5,6 +5,7 @@ import {
QueryParam,
RequestAuthDefinition,
VariableDefinition,
Variable,
} from "postman-collection"
import {
HoppRESTAuth,
@@ -18,6 +19,7 @@ import {
ValidContentTypes,
knownContentTypes,
FormDataKeyValue,
HoppRESTRequestVariable,
} from "@hoppscotch/data"
import { pipe, flow } from "fp-ts/function"
import * as S from "fp-ts/string"
@@ -91,6 +93,25 @@ const getHoppReqParams = (item: Item): HoppRESTParam[] => {
)
}
const getHoppReqVariables = (item: Item) => {
return pipe(
item.request.url.variables.all(),
A.filter(
(variable): variable is Variable =>
variable.key !== undefined &&
variable.key !== null &&
variable.key.length > 0
),
A.map((variable) => {
return <HoppRESTRequestVariable>{
key: replacePMVarTemplating(variable.key ?? ""),
value: replacePMVarTemplating(variable.value ?? ""),
active: !variable.disabled,
}
})
)
}
type PMRequestAuthDef<
AuthType extends
RequestAuthDefinition["type"] = RequestAuthDefinition["type"],
@@ -280,6 +301,7 @@ const getHoppRequest = (item: Item): HoppRESTRequest => {
params: getHoppReqParams(item),
auth: getHoppReqAuth(item),
body: getHoppReqBody(item),
requestVariables: getHoppReqVariables(item),
// TODO: Decide about this
preRequestScript: "",