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

View File

@@ -439,8 +439,7 @@ export class NewWorkspaceService extends Service {
} }
public async exportRESTCollections( public async exportRESTCollections(
workspaceHandle: Handle<Workspace>, workspaceHandle: Handle<Workspace>
collections: HoppCollection[]
): Promise< ): Promise<
E.Either<WorkspaceError<"INVALID_HANDLE" | "INVALID_PROVIDER">, void> 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" }) return E.left({ type: "SERVICE_ERROR", error: "INVALID_PROVIDER" })
} }
const result = await provider.exportRESTCollections( const result = await provider.exportRESTCollections(workspaceHandle)
workspaceHandle,
collections
)
if (E.isLeft(result)) { if (E.isLeft(result)) {
return E.left({ type: "PROVIDER_ERROR", error: result.left }) 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 * as E from "fp-ts/Either"
import { Ref } from "vue"
import { HoppCollection, HoppRESTRequest } from "@hoppscotch/data"
import { Handle } from "./handle" import { Handle } from "./handle"
import {
RESTCollectionChildrenView,
RESTCollectionJSONView,
RESTCollectionLevelAuthHeadersView,
RESTSearchResultsView,
RootRESTCollectionView,
} from "./view"
import { import {
Workspace, Workspace,
WorkspaceCollection, WorkspaceCollection,
WorkspaceDecor, WorkspaceDecor,
WorkspaceRequest, WorkspaceRequest,
} from "./workspace" } from "./workspace"
import {
RESTCollectionLevelAuthHeadersView,
RESTCollectionChildrenView,
RootRESTCollectionView,
RESTSearchResultsView,
RESTCollectionJSONView,
} from "./view"
import { HoppCollection, HoppRESTRequest } from "@hoppscotch/data"
export interface WorkspaceProvider { export interface WorkspaceProvider {
providerID: string providerID: string
@@ -83,8 +83,7 @@ export interface WorkspaceProvider {
collections: HoppCollection[] collections: HoppCollection[]
): Promise<E.Either<unknown, Handle<WorkspaceCollection>>> ): Promise<E.Either<unknown, Handle<WorkspaceCollection>>>
exportRESTCollections( exportRESTCollections(
workspaceHandle: Handle<Workspace>, workspaceHandle: Handle<Workspace>
collections: HoppCollection[]
): Promise<E.Either<unknown, void>> ): Promise<E.Either<unknown, void>>
exportRESTCollection( exportRESTCollection(
collectionHandle: Handle<WorkspaceCollection>, collectionHandle: Handle<WorkspaceCollection>,

View File

@@ -713,18 +713,26 @@ export class PersonalWorkspaceProviderService
} }
public exportRESTCollections( public exportRESTCollections(
workspaceHandle: Handle<WorkspaceCollection>, workspaceHandle: Handle<WorkspaceCollection>
collections: HoppCollection[]
): Promise<E.Either<unknown, void>> { ): Promise<E.Either<unknown, void>> {
const workspaceHandleRef = workspaceHandle.get() const workspaceHandleRef = workspaceHandle.get()
if ( if (
!isValidWorkspaceHandle(workspaceHandleRef, this.providerID, "personal") !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)) return Promise.resolve(E.right(undefined))
} }
@@ -1362,7 +1370,7 @@ export class PersonalWorkspaceProviderService
if ( if (
!isValidWorkspaceHandle(workspaceHandleRef, this.providerID, "personal") !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 === "") { if (requestID === "") {