From 170ec158213a34bfef7edf984c6947103a5bde75 Mon Sep 17 00:00:00 2001 From: nivedin Date: Fri, 9 Feb 2024 16:48:31 +0530 Subject: [PATCH] refactor: add v2 request schema --- packages/hoppscotch-data/src/rest/index.ts | 26 +++++++++-- packages/hoppscotch-data/src/rest/v/1.ts | 4 +- packages/hoppscotch-data/src/rest/v/2.ts | 50 ++++++++++++++++++++++ 3 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 packages/hoppscotch-data/src/rest/v/2.ts diff --git a/packages/hoppscotch-data/src/rest/index.ts b/packages/hoppscotch-data/src/rest/index.ts index c0be37354..72390ec5b 100644 --- a/packages/hoppscotch-data/src/rest/index.ts +++ b/packages/hoppscotch-data/src/rest/index.ts @@ -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({ 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: [], } } diff --git a/packages/hoppscotch-data/src/rest/v/1.ts b/packages/hoppscotch-data/src/rest/v/1.ts index 346717754..66a13da41 100644 --- a/packages/hoppscotch-data/src/rest/v/1.ts +++ b/packages/hoppscotch-data/src/rest/v/1.ts @@ -141,7 +141,7 @@ export const HoppRESTHeaders = z.array( export type HoppRESTHeaders = z.infer -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 ): z.infer["body"] { return { diff --git a/packages/hoppscotch-data/src/rest/v/2.ts b/packages/hoppscotch-data/src/rest/v/2.ts new file mode 100644 index 000000000..6c257e2ca --- /dev/null +++ b/packages/hoppscotch-data/src/rest/v/2.ts @@ -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 + +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) { + return { + ...old, + v: "2", + requestVariables: [], + } as z.infer + }, +})