From a0bf684fc985f6c19ef21c344dc7c3e08bdbbb3e Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Fri, 22 Mar 2024 22:55:27 +0530 Subject: [PATCH 1/3] fix: ensure requests within imported collections are translated to the latest version --- .../src/helpers/import-export/import/hopp.ts | 53 ++++++++++++++----- 1 file changed, 41 insertions(+), 12 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 2e144ce28e..125c2147e5 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 { safeParseJSON } from "~/helpers/functional/json" import { translateToNewGQLCollection } from "@hoppscotch/data" -import { entityReference } from "verzod" -import { z } from "zod" +import { safeParseJSON } from "~/helpers/functional/json" +import { IMPORTER_INVALID_FILE_FORMAT } from "." export const hoppRESTImporter = (content: string[]) => pipe( @@ -32,8 +36,23 @@ 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 +83,19 @@ 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 result = HoppCollection.safeParse(collection) + + if (result.type === "ok") + return O.some({ + ...result.value, + requests: result.value.requests.map((request) => { + const requestSchemaParsedResult = HoppRESTRequest.safeParse(request) - if (result.success) return O.some(result.data) + return requestSchemaParsedResult.type === "ok" + ? requestSchemaParsedResult.value + : getDefaultGQLRequest() + }), + }) return O.some(translateToNewGQLCollection(collection)) } From c8017aa7a3fa17217deff346084aed4bca556558 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Fri, 22 Mar 2024 22:56:37 +0530 Subject: [PATCH 2/3] fix: ensure requests within search results are translated to the latest version --- .../src/helpers/import-export/import/hopp.ts | 18 +++++++---- .../src/helpers/teams/TeamsSearch.service.ts | 30 ++++++++++++++----- 2 files changed, 34 insertions(+), 14 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 125c2147e5..ea501fe0b3 100644 --- a/packages/hoppscotch-common/src/helpers/import-export/import/hopp.ts +++ b/packages/hoppscotch-common/src/helpers/import-export/import/hopp.ts @@ -37,6 +37,7 @@ export const hoppRESTImporter = (content: string[]) => */ const validateCollection = (collection: unknown) => { const collectionSchemaParsedResult = HoppCollection.safeParse(collection) + if (collectionSchemaParsedResult.type === "ok") { const requests = collectionSchemaParsedResult.value.requests.map( (request) => { @@ -83,19 +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 = HoppCollection.safeParse(collection) + const collectionSchemaParsedResult = HoppCollection.safeParse(collection) - if (result.type === "ok") - return O.some({ - ...result.value, - requests: result.value.requests.map((request) => { + if (collectionSchemaParsedResult.type === "ok") { + const requests = collectionSchemaParsedResult.value.requests.map( + (request) => { const requestSchemaParsedResult = HoppRESTRequest.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 015a611d55..00f6bfaf75 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, }) } }) From 1592147005db36bd02de2f54fc7994f84bc37cfc Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Sat, 23 Mar 2024 13:12:06 +0530 Subject: [PATCH 3/3] fix: use the correct versioned entity for validating GQL requests --- .../src/helpers/import-export/import/hopp.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 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 ea501fe0b3..82f2f26f6d 100644 --- a/packages/hoppscotch-common/src/helpers/import-export/import/hopp.ts +++ b/packages/hoppscotch-common/src/helpers/import-export/import/hopp.ts @@ -11,7 +11,7 @@ import * as RA from "fp-ts/ReadonlyArray" import * as TE from "fp-ts/TaskEither" import { flow, pipe } from "fp-ts/function" -import { translateToNewGQLCollection } from "@hoppscotch/data" +import { HoppGQLRequest, translateToNewGQLCollection } from "@hoppscotch/data" import { safeParseJSON } from "~/helpers/functional/json" import { IMPORTER_INVALID_FILE_FORMAT } from "." @@ -89,7 +89,7 @@ export const validateGQLCollection = (collection: unknown) => { if (collectionSchemaParsedResult.type === "ok") { const requests = collectionSchemaParsedResult.value.requests.map( (request) => { - const requestSchemaParsedResult = HoppRESTRequest.safeParse(request) + const requestSchemaParsedResult = HoppGQLRequest.safeParse(request) return requestSchemaParsedResult.type === "ok" ? requestSchemaParsedResult.value