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

@@ -3,6 +3,7 @@ import * as S from "fp-ts/string"
import cloneDeep from "lodash/cloneDeep"
import V0_VERSION from "./v/0"
import V1_VERSION from "./v/1"
import V2_VERSION from "./v/2"
import { createVersionedEntity, InferredEntity } from "verzod"
import { lodashIsEqualEq, mapThenEq, undefinedEq } from "../utils/eq"
import {
@@ -11,6 +12,7 @@ import {
HoppRESTHeaders,
HoppRESTParams,
} from "./v/1"
import { HoppRESTRequestVariables } from "./v/2"
import { z } from "zod"
export * from "./content-types"
@@ -28,16 +30,19 @@ export {
HoppRESTHeaders,
} from "./v/1"
export { HoppRESTRequestVariables } from "./v/2"
const versionedObject = z.object({
// v is a stringified number
v: z.string().regex(/^\d+$/).transform(Number),
})
export const HoppRESTRequest = createVersionedEntity({
latestVersion: 1,
latestVersion: 2,
versionMap: {
0: V0_VERSION,
1: V1_VERSION,
2: V2_VERSION,
},
getVersion(data) {
// For V1 onwards we have the v string storing the number
@@ -73,12 +78,18 @@ const HoppRESTRequestEq = Eq.struct<HoppRESTRequest>({
name: S.Eq,
preRequestScript: S.Eq,
testScript: S.Eq,
requestVariables: mapThenEq(
(arr) => arr.filter((v: any) => v.key !== "" && v.value !== ""),
lodashIsEqualEq
),
})
export const RESTReqSchemaVersion = "1"
export const RESTReqSchemaVersion = "2"
export type HoppRESTParam = HoppRESTRequest["params"][number]
export type HoppRESTHeader = HoppRESTRequest["headers"][number]
export type HoppRESTRequestVariable =
HoppRESTRequest["requestVariables"][number]
export const isEqualHoppRESTRequest = HoppRESTRequestEq.equals
@@ -144,6 +155,14 @@ export function safelyExtractRESTRequest(
req.headers = result.data
}
}
if ("requestVariables" in x) {
const result = HoppRESTRequestVariables.safeParse(x.requestVariables)
if (result.success) {
req.requestVariables = result.data
}
}
}
return req
@@ -160,7 +179,7 @@ export function makeRESTRequest(
export function getDefaultRESTRequest(): HoppRESTRequest {
return {
v: "1",
v: "2",
endpoint: "https://echo.hoppscotch.io",
name: "Untitled",
params: [],
@@ -176,6 +195,7 @@ export function getDefaultRESTRequest(): HoppRESTRequest {
contentType: null,
body: null,
},
requestVariables: [],
}
}

View File

@@ -141,7 +141,7 @@ export const HoppRESTHeaders = z.array(
export type HoppRESTHeaders = z.infer<typeof HoppRESTHeaders>
const V1_SCHEMA = z.object({
export const V1_SCHEMA = z.object({
v: z.literal("1"),
id: z.optional(z.string()), // Firebase Firestore ID
@@ -158,7 +158,7 @@ const V1_SCHEMA = z.object({
body: HoppRESTReqBody,
})
function parseRequestBody(
export function parseRequestBody(
x: z.infer<typeof V0_SCHEMA>
): z.infer<typeof V1_SCHEMA>["body"] {
return {

View File

@@ -0,0 +1,50 @@
import { defineVersion } from "verzod"
import { z } from "zod"
import {
HoppRESTAuth,
HoppRESTHeaders,
HoppRESTParams,
HoppRESTReqBody,
} from "./1"
import { V1_SCHEMA } from "./1"
export const HoppRESTRequestVariables = z.array(
z.object({
key: z.string().catch(""),
value: z.string().catch(""),
active: z.boolean().catch(true),
})
)
export type HoppRESTRequestVariables = z.infer<typeof HoppRESTRequestVariables>
const V2_SCHEMA = z.object({
v: z.literal("2"),
id: z.optional(z.string()), // Firebase Firestore ID
name: z.string(),
method: z.string(),
endpoint: z.string(),
params: HoppRESTParams,
headers: HoppRESTHeaders,
preRequestScript: z.string().catch(""),
testScript: z.string().catch(""),
auth: HoppRESTAuth,
body: HoppRESTReqBody,
requestVariables: HoppRESTRequestVariables,
})
export default defineVersion({
initial: false,
schema: V2_SCHEMA,
up(old: z.infer<typeof V1_SCHEMA>) {
return {
...old,
v: "2",
requestVariables: [],
} as z.infer<typeof V2_SCHEMA>
},
})