Skip to content

Commit

Permalink
fix(common): ensure requests are translated to the latest version dur…
Browse files Browse the repository at this point in the history
…ing import and search actions (#3931)
  • Loading branch information
jamesgeorge007 committed Mar 25, 2024
1 parent c326f54 commit 0a71783
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 20 deletions.
59 changes: 47 additions & 12 deletions 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(
Expand All @@ -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))
}
Expand Down Expand Up @@ -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 (collectionSchemaParsedResult.type === "ok") {
const requests = collectionSchemaParsedResult.value.requests.map(
(request) => {
const requestSchemaParsedResult = HoppGQLRequest.safeParse(request)

return requestSchemaParsedResult.type === "ok"
? requestSchemaParsedResult.value
: getDefaultGQLRequest()
}
)

if (result.success) return O.some(result.data)
return O.some({
...collectionSchemaParsedResult.value,
requests,
})
}

return O.some(translateToNewGQLCollection(collection))
}
@@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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,
})
}
})
Expand Down

0 comments on commit 0a71783

Please sign in to comment.