refactor: add v2 request schema
This commit is contained in:
@@ -3,6 +3,7 @@ import * as S from "fp-ts/string"
|
|||||||
import cloneDeep from "lodash/cloneDeep"
|
import cloneDeep from "lodash/cloneDeep"
|
||||||
import V0_VERSION from "./v/0"
|
import V0_VERSION from "./v/0"
|
||||||
import V1_VERSION from "./v/1"
|
import V1_VERSION from "./v/1"
|
||||||
|
import V2_VERSION from "./v/2"
|
||||||
import { createVersionedEntity, InferredEntity } from "verzod"
|
import { createVersionedEntity, InferredEntity } from "verzod"
|
||||||
import { lodashIsEqualEq, mapThenEq, undefinedEq } from "../utils/eq"
|
import { lodashIsEqualEq, mapThenEq, undefinedEq } from "../utils/eq"
|
||||||
import {
|
import {
|
||||||
@@ -11,6 +12,7 @@ import {
|
|||||||
HoppRESTHeaders,
|
HoppRESTHeaders,
|
||||||
HoppRESTParams,
|
HoppRESTParams,
|
||||||
} from "./v/1"
|
} from "./v/1"
|
||||||
|
import { HoppRESTRequestVariables } from "./v/2"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
|
|
||||||
export * from "./content-types"
|
export * from "./content-types"
|
||||||
@@ -28,16 +30,19 @@ export {
|
|||||||
HoppRESTHeaders,
|
HoppRESTHeaders,
|
||||||
} from "./v/1"
|
} from "./v/1"
|
||||||
|
|
||||||
|
export { HoppRESTRequestVariables } from "./v/2"
|
||||||
|
|
||||||
const versionedObject = z.object({
|
const versionedObject = z.object({
|
||||||
// v is a stringified number
|
// v is a stringified number
|
||||||
v: z.string().regex(/^\d+$/).transform(Number),
|
v: z.string().regex(/^\d+$/).transform(Number),
|
||||||
})
|
})
|
||||||
|
|
||||||
export const HoppRESTRequest = createVersionedEntity({
|
export const HoppRESTRequest = createVersionedEntity({
|
||||||
latestVersion: 1,
|
latestVersion: 2,
|
||||||
versionMap: {
|
versionMap: {
|
||||||
0: V0_VERSION,
|
0: V0_VERSION,
|
||||||
1: V1_VERSION,
|
1: V1_VERSION,
|
||||||
|
2: V2_VERSION,
|
||||||
},
|
},
|
||||||
getVersion(data) {
|
getVersion(data) {
|
||||||
// For V1 onwards we have the v string storing the number
|
// For V1 onwards we have the v string storing the number
|
||||||
@@ -73,12 +78,18 @@ const HoppRESTRequestEq = Eq.struct<HoppRESTRequest>({
|
|||||||
name: S.Eq,
|
name: S.Eq,
|
||||||
preRequestScript: S.Eq,
|
preRequestScript: S.Eq,
|
||||||
testScript: 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 HoppRESTParam = HoppRESTRequest["params"][number]
|
||||||
export type HoppRESTHeader = HoppRESTRequest["headers"][number]
|
export type HoppRESTHeader = HoppRESTRequest["headers"][number]
|
||||||
|
export type HoppRESTRequestVariable =
|
||||||
|
HoppRESTRequest["requestVariables"][number]
|
||||||
|
|
||||||
export const isEqualHoppRESTRequest = HoppRESTRequestEq.equals
|
export const isEqualHoppRESTRequest = HoppRESTRequestEq.equals
|
||||||
|
|
||||||
@@ -144,6 +155,14 @@ export function safelyExtractRESTRequest(
|
|||||||
req.headers = result.data
|
req.headers = result.data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ("requestVariables" in x) {
|
||||||
|
const result = HoppRESTRequestVariables.safeParse(x.requestVariables)
|
||||||
|
|
||||||
|
if (result.success) {
|
||||||
|
req.requestVariables = result.data
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return req
|
return req
|
||||||
@@ -160,7 +179,7 @@ export function makeRESTRequest(
|
|||||||
|
|
||||||
export function getDefaultRESTRequest(): HoppRESTRequest {
|
export function getDefaultRESTRequest(): HoppRESTRequest {
|
||||||
return {
|
return {
|
||||||
v: "1",
|
v: "2",
|
||||||
endpoint: "https://echo.hoppscotch.io",
|
endpoint: "https://echo.hoppscotch.io",
|
||||||
name: "Untitled",
|
name: "Untitled",
|
||||||
params: [],
|
params: [],
|
||||||
@@ -176,6 +195,7 @@ export function getDefaultRESTRequest(): HoppRESTRequest {
|
|||||||
contentType: null,
|
contentType: null,
|
||||||
body: null,
|
body: null,
|
||||||
},
|
},
|
||||||
|
requestVariables: [],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -141,7 +141,7 @@ export const HoppRESTHeaders = z.array(
|
|||||||
|
|
||||||
export type HoppRESTHeaders = z.infer<typeof HoppRESTHeaders>
|
export type HoppRESTHeaders = z.infer<typeof HoppRESTHeaders>
|
||||||
|
|
||||||
const V1_SCHEMA = z.object({
|
export const V1_SCHEMA = z.object({
|
||||||
v: z.literal("1"),
|
v: z.literal("1"),
|
||||||
id: z.optional(z.string()), // Firebase Firestore ID
|
id: z.optional(z.string()), // Firebase Firestore ID
|
||||||
|
|
||||||
@@ -158,7 +158,7 @@ const V1_SCHEMA = z.object({
|
|||||||
body: HoppRESTReqBody,
|
body: HoppRESTReqBody,
|
||||||
})
|
})
|
||||||
|
|
||||||
function parseRequestBody(
|
export function parseRequestBody(
|
||||||
x: z.infer<typeof V0_SCHEMA>
|
x: z.infer<typeof V0_SCHEMA>
|
||||||
): z.infer<typeof V1_SCHEMA>["body"] {
|
): z.infer<typeof V1_SCHEMA>["body"] {
|
||||||
return {
|
return {
|
||||||
|
|||||||
50
packages/hoppscotch-data/src/rest/v/2.ts
Normal file
50
packages/hoppscotch-data/src/rest/v/2.ts
Normal 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>
|
||||||
|
},
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user