Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

fix(common): fetch full team collection tree on workspace import (#5792)

Chhavi Goyalf012c31ba24f
diff --git a/packages/hoppscotch-common/src/components/importExport/ImportExportSteps/AllCollectionImport.vue b/packages/hoppscotch-common/src/components/importExport/ImportExportSteps/AllCollectionImport.vue
index 310fd1f43..bbf8adf95 100644
--- a/packages/hoppscotch-common/src/components/importExport/ImportExportSteps/AllCollectionImport.vue
+++ b/packages/hoppscotch-common/src/components/importExport/ImportExportSteps/AllCollectionImport.vue
@@ -91,32 +91,18 @@
</template>
<script setup lang="ts">
-import {
- GQLHeader,
- HoppCollection,
- HoppCollectionVariable,
- HoppGQLAuth,
- HoppGQLRequest,
- HoppRESTAuth,
- HoppRESTHeader,
- HoppRESTRequest,
- makeCollection,
-} from "@hoppscotch/data"
+import { HoppCollection } from "@hoppscotch/data"
import { useService } from "dioc/vue"
import { computed, onMounted, ref, watch } from "vue"
import { useI18n } from "~/composables/i18n"
import { useReadonlyStream } from "~/composables/stream"
import { runGQLQuery } from "~/helpers/backend/GQLClient"
import { RootCollectionsOfTeamDocument } from "~/helpers/backend/graphql"
+import { getTeamCollectionObject } from "~/helpers/backend/helpers"
import { TEAMS_BACKEND_PAGE_SIZE } from "~/helpers/teams/TeamCollectionAdapter"
import { getRESTCollection, restCollections$ } from "~/newstore/collections"
import { WorkspaceService } from "~/services/workspace.service"
import * as E from "fp-ts/Either"
-import {
- getCollectionChildCollections,
- getSingleCollection,
-} from "~/helpers/teams/TeamCollection"
-import { getCollectionChildRequests } from "~/helpers/teams/TeamRequest"
import { useToast } from "~/composables/toast"
const workspaceService = useService(WorkspaceService)
@@ -257,99 +243,6 @@ const getWorkspaceRootCollections = async (workspaceID: string) => {
return E.right(totalCollections)
}
-const convertToInheritedProperties = (
- data?: string | null
-): {
- auth: HoppRESTAuth | HoppGQLAuth
- headers: Array<HoppRESTHeader | GQLHeader>
- variables: HoppCollectionVariable[]
-} => {
- const collectionLevelAuthAndHeaders = data
- ? (JSON.parse(data) as {
- auth: HoppRESTAuth | HoppGQLAuth
- headers: Array<HoppRESTHeader | GQLHeader>
- variables: HoppCollectionVariable[]
- })
- : null
-
- const headers = collectionLevelAuthAndHeaders?.headers ?? []
-
- const auth = collectionLevelAuthAndHeaders?.auth ?? {
- authType: "none",
- authActive: true,
- }
-
- const variables = collectionLevelAuthAndHeaders?.variables ?? []
-
- return {
- auth,
- headers,
- variables,
- }
-}
-
-const getTeamCollection = async (
- collectionID: string
-): Promise<E.Either<any, HoppCollection>> => {
- const rootCollectionRes = await getSingleCollection(collectionID)
-
- if (E.isLeft(rootCollectionRes)) {
- return E.left(rootCollectionRes.left)
- }
-
- const rootCollection = rootCollectionRes.right.collection
-
- if (!rootCollection) {
- return E.left("ROOT_COLLECTION_NOT_FOUND")
- }
-
- const childRequests = await getCollectionChildRequests(collectionID)
-
- if (E.isLeft(childRequests)) {
- return E.left(childRequests.left)
- }
-
- const childCollectionsRes = await getCollectionChildCollections(collectionID)
-
- if (E.isLeft(childCollectionsRes)) {
- return E.left(childCollectionsRes.left)
- }
-
- if (!childCollectionsRes.right.collection) {
- return E.left("CHILD_COLLECTIONS_NOT_FOUND")
- }
-
- const childCollectionExpandedPromises =
- childCollectionsRes.right.collection.children.map((col) =>
- getTeamCollection(col.id)
- )
-
- const childCollectionPromiseRes = await Promise.all(
- childCollectionExpandedPromises
- )
-
- const hasAnyError = childCollectionPromiseRes.some((res) => E.isLeft(res))
-
- if (hasAnyError) {
- return E.left("CHILD_COLLECTIONS_NOT_FOUND")
- }
-
- const unwrappedChildCollections = childCollectionPromiseRes.map(
- (res) => (res as E.Right<HoppCollection>).right
- )
-
- const collectionInHoppFormat: HoppCollection = makeCollection({
- name: rootCollection.title,
- ...convertToInheritedProperties(rootCollection.data),
- folders: unwrappedChildCollections,
- requests: childRequests.right.requestsInCollection.map((req) => {
- return JSON.parse(req.request) as HoppRESTRequest | HoppGQLRequest
- }),
- })
-
- return E.right(collectionInHoppFormat)
-}
-
const getCollectionDetailsAndImport = async () => {
if (!selectedCollectionID.value) {
return
@@ -360,7 +253,10 @@ const getCollectionDetailsAndImport = async () => {
if (selectedWorkspaceID.value === "personal") {
collectionToImport = getRESTCollection(parseInt(selectedCollectionID.value))
} else {
- const res = await getTeamCollection(selectedCollectionID.value)
+ const res = await getTeamCollectionObject(
+ selectedWorkspaceID.value!,
+ selectedCollectionID.value
+ )
if (E.isLeft(res)) {
toast.error(t("import.failed"))
diff --git a/packages/hoppscotch-common/src/helpers/backend/helpers.ts b/packages/hoppscotch-common/src/helpers/backend/helpers.ts
index fe5c7050a..a7a2848e3 100644
--- a/packages/hoppscotch-common/src/helpers/backend/helpers.ts
+++ b/packages/hoppscotch-common/src/helpers/backend/helpers.ts
@@ -291,14 +291,14 @@ export const getTeamCollectionJSON = async (teamID: string) => {
}
/**
- * Get the JSON string of a single collection of the specified team
+ * Fetch a single team collection and return it as a HoppCollection.
* @param teamID - ID of the team
* @param collectionID - ID of the collection
*/
-export const getSingleTeamCollectionJSON = async (
+export const getTeamCollectionObject = async (
teamID: string,
collectionID: string
-) => {
+): Promise<E.Either<GQLError<string> | string, HoppCollection>> => {
const data = await runGQLQuery({
query: ExportCollectionToJsonDocument,
variables: {
@@ -308,17 +308,38 @@ export const getSingleTeamCollectionJSON = async (
})
if (E.isLeft(data)) {
- return E.left(data.left.error.toString())
+ return E.left(data.left)
}
- const collection = JSON.parse(data.right.exportCollectionToJSON)
-
- if (!collection) {
- const t = getI18n()
+ try {
+ const collection = JSON.parse(data.right.exportCollectionToJSON)
+ if (!collection) {
+ return E.left("Collection not found")
+ }
+ return E.right(teamCollectionJSONToHoppRESTColl(collection))
+ } catch {
+ return E.left("Failed to parse collection data")
+ }
+}
- return E.left(t("error.no_collections_to_export"))
+/**
+ * Get the JSON string of a single collection of the specified team
+ * @param teamID - ID of the team
+ * @param collectionID - ID of the collection
+ */
+export const getSingleTeamCollectionJSON = async (
+ teamID: string,
+ collectionID: string
+) => {
+ const result = await getTeamCollectionObject(teamID, collectionID)
+
+ if (E.isLeft(result)) {
+ const errorMsg =
+ typeof result.left === "string"
+ ? result.left
+ : result.left.error.toString()
+ return E.left(errorMsg)
}
- const hoppCollection = teamCollectionJSONToHoppRESTColl(collection)
- return E.right(JSON.stringify(hoppCollection, null, 2))
+ return E.right(JSON.stringify(result.right, null, 2))
}