refactor: update exportRESTCollections method signature

- Drop the `collections` parameter since the it is already available in the `PersonalWorkspaceProviderService` context.
- Make the above method return a left error of `NO_COLLECTIONS_TO_EXPORT` when the collections list is empty.
- Error handling updates.
This commit is contained in:
jamesgeorge007
2024-05-27 11:33:34 +05:30
parent 17169e1c46
commit 2374ceb808
4 changed files with 46 additions and 44 deletions

View File

@@ -19,29 +19,28 @@ import { UrlSource } from "~/helpers/import-export/import/import-sources/UrlSour
import IconFile from "~icons/lucide/file"
import {
hoppRESTImporter,
hoppInsomniaImporter,
hoppPostmanImporter,
toTeamsImporter,
hoppOpenAPIImporter,
hoppPostmanImporter,
hoppRESTImporter,
toTeamsImporter,
} from "~/helpers/import-export/import/importers"
import { defineStep } from "~/composables/step-components"
import MyCollectionImport from "~/components/importExport/ImportExportSteps/MyCollectionImport.vue"
import { useI18n } from "~/composables/i18n"
import { useToast } from "~/composables/toast"
import { restCollections$ } from "~/newstore/collections"
import MyCollectionImport from "~/components/importExport/ImportExportSteps/MyCollectionImport.vue"
import IconFolderPlus from "~icons/lucide/folder-plus"
import IconOpenAPI from "~icons/lucide/file"
import IconPostman from "~icons/hopp/postman"
import IconInsomnia from "~icons/hopp/insomnia"
import IconPostman from "~icons/hopp/postman"
import IconOpenAPI from "~icons/lucide/file"
import IconFolderPlus from "~icons/lucide/folder-plus"
import IconGithub from "~icons/lucide/github"
import IconLink from "~icons/lucide/link"
import IconUser from "~icons/lucide/user"
import { useReadonlyStream } from "~/composables/stream"
import IconUser from "~icons/lucide/user"
import { getTeamCollectionJSON } from "~/helpers/backend/helpers"
@@ -51,9 +50,9 @@ import { initializeDownloadFile } from "~/helpers/import-export/export"
import { gistExporter } from "~/helpers/import-export/export/gist"
import { teamCollectionsExporter } from "~/helpers/import-export/export/teamCollections"
import { GistSource } from "~/helpers/import-export/import/import-sources/GistSource"
import { ImporterOrExporter } from "~/components/importExport/types"
import { useService } from "dioc/vue"
import { ImporterOrExporter } from "~/components/importExport/types"
import { GistSource } from "~/helpers/import-export/import/import-sources/GistSource"
import { NewWorkspaceService } from "~/services/new-workspace"
import { TeamWorkspace } from "~/services/workspace.service"
@@ -83,8 +82,6 @@ const currentUser = useReadonlyStream(
platform.auth.getCurrentUser()
)
const myCollections = useReadonlyStream(restCollections$, [])
const workspaceService = useService(NewWorkspaceService)
const activeWorkspaceHandle = workspaceService.activeWorkspaceHandle
@@ -96,7 +93,7 @@ const showImportFailedError = () => {
const handleImportToStore = async (collections: HoppCollection[]) => {
if (props.collectionsType.type === "my-collections") {
if (!activeWorkspaceHandle.value) {
return
return E.left("INVALID_WORKSPACE_HANDLE")
}
const collectionHandleResult = await workspaceService.importRESTCollections(
@@ -413,23 +410,25 @@ const HoppMyCollectionsExporter: ImporterOrExporter = {
isLoading: isHoppMyCollectionExporterInProgress,
},
action: async () => {
if (!myCollections.value.length) {
return toast.error(t("error.no_collections_to_export"))
}
if (!activeWorkspaceHandle.value) {
return
return toast.error("error.something_went_wrong")
}
isHoppMyCollectionExporterInProgress.value = true
const result = await workspaceService.exportRESTCollections(
activeWorkspaceHandle.value,
myCollections.value
activeWorkspaceHandle.value
)
// INVALID_COLLECTION_HANDLE | NO_COLLECTIONS_TO_EXPORT
if (E.isLeft(result)) {
// INVALID_WORKSPACE_HANDLE
isHoppMyCollectionExporterInProgress.value = false
if (result.left.error === "NO_COLLECTIONS_TO_EXPORT") {
return toast.error(t("error.no_collections_to_export"))
}
return toast.error(t("error.something_went_wrong"))
}
toast.success(t("state.download_started"))

View File

@@ -439,8 +439,7 @@ export class NewWorkspaceService extends Service {
}
public async exportRESTCollections(
workspaceHandle: Handle<Workspace>,
collections: HoppCollection[]
workspaceHandle: Handle<Workspace>
): Promise<
E.Either<WorkspaceError<"INVALID_HANDLE" | "INVALID_PROVIDER">, void>
> {
@@ -458,10 +457,7 @@ export class NewWorkspaceService extends Service {
return E.left({ type: "SERVICE_ERROR", error: "INVALID_PROVIDER" })
}
const result = await provider.exportRESTCollections(
workspaceHandle,
collections
)
const result = await provider.exportRESTCollections(workspaceHandle)
if (E.isLeft(result)) {
return E.left({ type: "PROVIDER_ERROR", error: result.left })

View File

@@ -1,21 +1,21 @@
import { Ref } from "vue"
import * as E from "fp-ts/Either"
import { Ref } from "vue"
import { HoppCollection, HoppRESTRequest } from "@hoppscotch/data"
import { Handle } from "./handle"
import {
RESTCollectionChildrenView,
RESTCollectionJSONView,
RESTCollectionLevelAuthHeadersView,
RESTSearchResultsView,
RootRESTCollectionView,
} from "./view"
import {
Workspace,
WorkspaceCollection,
WorkspaceDecor,
WorkspaceRequest,
} from "./workspace"
import {
RESTCollectionLevelAuthHeadersView,
RESTCollectionChildrenView,
RootRESTCollectionView,
RESTSearchResultsView,
RESTCollectionJSONView,
} from "./view"
import { HoppCollection, HoppRESTRequest } from "@hoppscotch/data"
export interface WorkspaceProvider {
providerID: string
@@ -83,8 +83,7 @@ export interface WorkspaceProvider {
collections: HoppCollection[]
): Promise<E.Either<unknown, Handle<WorkspaceCollection>>>
exportRESTCollections(
workspaceHandle: Handle<Workspace>,
collections: HoppCollection[]
workspaceHandle: Handle<Workspace>
): Promise<E.Either<unknown, void>>
exportRESTCollection(
collectionHandle: Handle<WorkspaceCollection>,

View File

@@ -713,18 +713,26 @@ export class PersonalWorkspaceProviderService
}
public exportRESTCollections(
workspaceHandle: Handle<WorkspaceCollection>,
collections: HoppCollection[]
workspaceHandle: Handle<WorkspaceCollection>
): Promise<E.Either<unknown, void>> {
const workspaceHandleRef = workspaceHandle.get()
if (
!isValidWorkspaceHandle(workspaceHandleRef, this.providerID, "personal")
) {
return Promise.resolve(E.left("INVALID_COLLECTION_HANDLE" as const))
return Promise.resolve(E.left("INVALID_WORKSPACE_HANDLE" as const))
}
initializeDownloadFile(JSON.stringify(collections, null, 2), "Collections")
const collectionsToExport = this.restCollectionState.value.state
if (collectionsToExport.length === 0) {
return Promise.resolve(E.left("NO_COLLECTIONS_TO_EXPORT" as const))
}
initializeDownloadFile(
JSON.stringify(collectionsToExport, null, 2),
`${workspaceHandleRef.value.data.workspaceID}-collections`
)
return Promise.resolve(E.right(undefined))
}
@@ -1362,7 +1370,7 @@ export class PersonalWorkspaceProviderService
if (
!isValidWorkspaceHandle(workspaceHandleRef, this.providerID, "personal")
) {
return Promise.resolve(E.left("INVALID_COLLECTION_HANDLE" as const))
return Promise.resolve(E.left("INVALID_WORKSPACE_HANDLE" as const))
}
if (requestID === "") {