From 0a71783eaaba4eac47977fc1d4da9a9a8f6080dd Mon Sep 17 00:00:00 2001 From: James George Date: Mon, 25 Mar 2024 17:09:54 +0530 Subject: [PATCH] fix(common): ensure requests are translated to the latest version during import and search actions (#3931) --- .../src/helpers/import-export/import/hopp.ts | 59 +++++++++++++++---- .../src/helpers/teams/TeamsSearch.service.ts | 30 +++++++--- 2 files changed, 69 insertions(+), 20 deletions(-) diff --git a/packages/hoppscotch-common/src/helpers/import-export/import/hopp.ts b/packages/hoppscotch-common/src/helpers/import-export/import/hopp.ts index 2e144ce28..82f2f26f6 100644 --- a/packages/hoppscotch-common/src/helpers/import-export/import/hopp.ts +++ b/packages/hoppscotch-common/src/helpers/import-export/import/hopp.ts @@ -1,15 +1,19 @@ -import { pipe, flow } from "fp-ts/function" -import * as TE from "fp-ts/TaskEither" +import { + HoppCollection, + HoppRESTRequest, + getDefaultGQLRequest, + getDefaultRESTRequest, + translateToNewRESTCollection, +} from "@hoppscotch/data" +import * as A from "fp-ts/Array" import * as O from "fp-ts/Option" import * as RA from "fp-ts/ReadonlyArray" -import * as A from "fp-ts/Array" -import { translateToNewRESTCollection, HoppCollection } from "@hoppscotch/data" +import * as TE from "fp-ts/TaskEither" +import { flow, pipe } from "fp-ts/function" -import { IMPORTER_INVALID_FILE_FORMAT } from "." +import { HoppGQLRequest, translateToNewGQLCollection } from "@hoppscotch/data" import { safeParseJSON } from "~/helpers/functional/json" -import { translateToNewGQLCollection } from "@hoppscotch/data" -import { entityReference } from "verzod" -import { z } from "zod" +import { IMPORTER_INVALID_FILE_FORMAT } from "." export const hoppRESTImporter = (content: string[]) => pipe( @@ -32,8 +36,24 @@ export const hoppRESTImporter = (content: string[]) => * else translate it into one. */ const validateCollection = (collection: unknown) => { - const result = entityReference(HoppCollection).safeParse(collection) - if (result.success) return O.some(result.data) + const collectionSchemaParsedResult = HoppCollection.safeParse(collection) + + if (collectionSchemaParsedResult.type === "ok") { + const requests = collectionSchemaParsedResult.value.requests.map( + (request) => { + const requestSchemaParsedResult = HoppRESTRequest.safeParse(request) + + return requestSchemaParsedResult.type === "ok" + ? requestSchemaParsedResult.value + : getDefaultRESTRequest() + } + ) + + return O.some({ + ...collectionSchemaParsedResult.value, + requests, + }) + } return O.some(translateToNewRESTCollection(collection)) } @@ -64,9 +84,24 @@ export const hoppGQLImporter = (content: string) => * @returns the collection if it is valid, else a translated version of the collection */ export const validateGQLCollection = (collection: unknown) => { - const result = z.array(entityReference(HoppCollection)).safeParse(collection) + const collectionSchemaParsedResult = HoppCollection.safeParse(collection) - if (result.success) return O.some(result.data) + if (collectionSchemaParsedResult.type === "ok") { + const requests = collectionSchemaParsedResult.value.requests.map( + (request) => { + const requestSchemaParsedResult = HoppGQLRequest.safeParse(request) + + return requestSchemaParsedResult.type === "ok" + ? requestSchemaParsedResult.value + : getDefaultGQLRequest() + } + ) + + return O.some({ + ...collectionSchemaParsedResult.value, + requests, + }) + } return O.some(translateToNewGQLCollection(collection)) } diff --git a/packages/hoppscotch-common/src/helpers/teams/TeamsSearch.service.ts b/packages/hoppscotch-common/src/helpers/teams/TeamsSearch.service.ts index 015a611d5..00f6bfaf7 100644 --- a/packages/hoppscotch-common/src/helpers/teams/TeamsSearch.service.ts +++ b/packages/hoppscotch-common/src/helpers/teams/TeamsSearch.service.ts @@ -1,4 +1,14 @@ -import { ref } from "vue" +import { + HoppRESTAuth, + HoppRESTHeader, + HoppRESTRequest, + getDefaultRESTRequest, +} from "@hoppscotch/data" +import axios from "axios" +import { Service } from "dioc" +import * as E from "fp-ts/Either" +import { Ref, ref } from "vue" + import { runGQLQuery } from "../backend/GQLClient" import { GetCollectionChildrenDocument, @@ -7,15 +17,10 @@ import { GetSingleRequestDocument, } from "../backend/graphql" import { TeamCollection } from "./TeamCollection" -import { HoppRESTAuth, HoppRESTHeader } from "@hoppscotch/data" -import * as E from "fp-ts/Either" +import { platform } from "~/platform" import { HoppInheritedProperty } from "../types/HoppInheritedProperties" import { TeamRequest } from "./TeamRequest" -import { Service } from "dioc" -import axios from "axios" -import { Ref } from "vue" -import { platform } from "~/platform" type CollectionSearchMeta = { isSearchResult?: boolean @@ -150,12 +155,21 @@ function convertToTeamTree( if (isAlreadyInserted) return if (parentCollection) { + const requestSchemaParsedResult = HoppRESTRequest.safeParse( + request.request + ) + + const effectiveRequest = + requestSchemaParsedResult.type === "ok" + ? requestSchemaParsedResult.value + : getDefaultRESTRequest() + parentCollection.requests = parentCollection.requests || [] parentCollection.requests.push({ id: request.id, collectionID: request.collectionID, title: request.title, - request: request.request, + request: effectiveRequest, }) } })