refactor: view implementation to retrieve collections for exporting

This commit is contained in:
jamesgeorge007
2024-02-29 22:58:07 +05:30
parent 01573cc51c
commit 3fd85df84b
5 changed files with 106 additions and 7 deletions

View File

@@ -510,7 +510,7 @@ const HoppGistCollectionsExporter: ImporterOrExporter = {
const collectionJSON = await getCollectionJSON() const collectionJSON = await getCollectionJSON()
const accessToken = currentUser.value?.accessToken const accessToken = currentUser.value?.accessToken
if (!accessToken) { if (!accessToken || E.isLeft(collectionJSON)) {
toast.error(t("error.something_went_wrong")) toast.error(t("error.something_went_wrong"))
isHoppGistCollectionExporterInProgress.value = false isHoppGistCollectionExporterInProgress.value = false
return return
@@ -606,6 +606,7 @@ const selectedTeamID = computed(() => {
}) })
const getCollectionJSON = async () => { const getCollectionJSON = async () => {
// TODO: Implement `getRESTCollectionJSONView` for team workspace
if ( if (
props.collectionsType.type === "team-collections" && props.collectionsType.type === "team-collections" &&
props.collectionsType.selectedTeam?.teamID props.collectionsType.selectedTeam?.teamID
@@ -616,11 +617,31 @@ const getCollectionJSON = async () => {
return E.isRight(res) return E.isRight(res)
? E.right(res.right.exportCollectionsToJSON) ? E.right(res.right.exportCollectionsToJSON)
: E.left(res.left) : E.left(res.left.error.toString())
} }
if (props.collectionsType.type === "my-collections") { if (props.collectionsType.type === "my-collections") {
return E.right(JSON.stringify(myCollections.value, null, 2)) if (!activeWorkspaceHandle.value) {
return E.left("INVALID_WORKSPACE_HANDLE")
}
const collectionJSONHandleResult =
await workspaceService.getRESTCollectionJSONView(
activeWorkspaceHandle.value
)
if (E.isLeft(collectionJSONHandleResult)) {
return E.left(collectionJSONHandleResult.left.error)
}
const collectionJSONHandle = collectionJSONHandleResult.right
if (collectionJSONHandle.value.type === "invalid") {
// WORKSPACE_INVALIDATED
return E.left("WORKSPACE_INVALIDATED")
}
return E.right(collectionJSONHandle.value.data.content)
} }
return E.left("INVALID_SELECTED_TEAM_OR_INVALID_COLLECTION_TYPE") return E.left("INVALID_SELECTED_TEAM_OR_INVALID_COLLECTION_TYPE")

View File

@@ -14,6 +14,7 @@ import { HandleRef } from "./handle"
import { WorkspaceProvider } from "./provider" import { WorkspaceProvider } from "./provider"
import { import {
RESTCollectionChildrenView, RESTCollectionChildrenView,
RESTCollectionJSONView,
RESTCollectionLevelAuthHeadersView, RESTCollectionLevelAuthHeadersView,
RESTSearchResultsView, RESTSearchResultsView,
RootRESTCollectionView, RootRESTCollectionView,
@@ -721,6 +722,35 @@ export class NewWorkspaceService extends Service {
return E.right(result.right) return E.right(result.right)
} }
public async getRESTCollectionJSONView(
workspaceHandle: HandleRef<Workspace>
): Promise<
E.Either<
WorkspaceError<"INVALID_HANDLE" | "INVALID_PROVIDER">,
HandleRef<RESTCollectionJSONView>
>
> {
if (workspaceHandle.value.type === "invalid") {
return E.left({ type: "SERVICE_ERROR", error: "INVALID_HANDLE" })
}
const provider = this.registeredProviders.get(
workspaceHandle.value.data.providerID
)
if (!provider) {
return E.left({ type: "SERVICE_ERROR", error: "INVALID_PROVIDER" })
}
const result = await provider.getRESTCollectionJSONView(workspaceHandle)
if (E.isLeft(result)) {
return E.left({ type: "PROVIDER_ERROR", error: result.left })
}
return E.right(result.right)
}
public registerWorkspaceProvider(provider: WorkspaceProvider) { public registerWorkspaceProvider(provider: WorkspaceProvider) {
if (this.registeredProviders.has(provider.providerID)) { if (this.registeredProviders.has(provider.providerID)) {
console.warn( console.warn(

View File

@@ -13,6 +13,7 @@ import {
RESTCollectionChildrenView, RESTCollectionChildrenView,
RootRESTCollectionView, RootRESTCollectionView,
RESTSearchResultsView, RESTSearchResultsView,
RESTCollectionJSONView,
} from "./view" } from "./view"
import { HoppCollection, HoppRESTRequest } from "@hoppscotch/data" import { HoppCollection, HoppRESTRequest } from "@hoppscotch/data"
@@ -35,17 +36,20 @@ export interface WorkspaceProvider {
getRESTRootCollectionView( getRESTRootCollectionView(
workspaceHandle: HandleRef<Workspace> workspaceHandle: HandleRef<Workspace>
): Promise<E.Either<unknown, HandleRef<RootRESTCollectionView>>> ): Promise<E.Either<never, HandleRef<RootRESTCollectionView>>>
getRESTCollectionChildrenView( getRESTCollectionChildrenView(
collectionHandle: HandleRef<WorkspaceCollection> collectionHandle: HandleRef<WorkspaceCollection>
): Promise<E.Either<unknown, HandleRef<RESTCollectionChildrenView>>> ): Promise<E.Either<never, HandleRef<RESTCollectionChildrenView>>>
getRESTCollectionLevelAuthHeadersView( getRESTCollectionLevelAuthHeadersView(
collectionHandle: HandleRef<WorkspaceCollection> collectionHandle: HandleRef<WorkspaceCollection>
): Promise<E.Either<unknown, HandleRef<RESTCollectionLevelAuthHeadersView>>> ): Promise<E.Either<never, HandleRef<RESTCollectionLevelAuthHeadersView>>>
getRESTSearchResultsView( getRESTSearchResultsView(
workspaceHandle: HandleRef<Workspace>, workspaceHandle: HandleRef<Workspace>,
searchQuery: Ref<string> searchQuery: Ref<string>
): Promise<E.Either<unknown, HandleRef<RESTSearchResultsView>>> ): Promise<E.Either<never, HandleRef<RESTSearchResultsView>>>
getRESTCollectionJSONView(
workspaceHandle: HandleRef<Workspace>
): Promise<E.Either<never, HandleRef<RESTCollectionJSONView>>>
createRESTRootCollection( createRESTRootCollection(
workspaceHandle: HandleRef<Workspace>, workspaceHandle: HandleRef<Workspace>,

View File

@@ -43,6 +43,7 @@ import { HandleRef } from "~/services/new-workspace/handle"
import { WorkspaceProvider } from "~/services/new-workspace/provider" import { WorkspaceProvider } from "~/services/new-workspace/provider"
import { import {
RESTCollectionChildrenView, RESTCollectionChildrenView,
RESTCollectionJSONView,
RESTCollectionLevelAuthHeadersView, RESTCollectionLevelAuthHeadersView,
RESTCollectionViewItem, RESTCollectionViewItem,
RESTSearchResultsView, RESTSearchResultsView,
@@ -1037,6 +1038,42 @@ export class PersonalWorkspaceProviderService
) )
} }
public getRESTCollectionJSONView(
workspaceHandle: HandleRef<Workspace>
): Promise<E.Either<never, HandleRef<RESTCollectionJSONView>>> {
return Promise.resolve(
E.right(
computed(() => {
if (
!isValidWorkspaceHandle(
workspaceHandle,
this.providerID,
"personal"
)
) {
return {
type: "invalid" as const,
reason: "INVALID_WORKSPACE_HANDLE" as const,
}
}
return markRaw({
type: "ok" as const,
data: {
providerID: this.providerID,
workspaceID: workspaceHandle.value.data.workspaceID,
content: JSON.stringify(
this.restCollectionState.value.state,
null,
2
),
},
})
})
)
)
}
public getWorkspaceHandle( public getWorkspaceHandle(
workspaceID: string workspaceID: string
): Promise<E.Either<unknown, HandleRef<Workspace>>> { ): Promise<E.Either<unknown, HandleRef<Workspace>>> {

View File

@@ -55,3 +55,10 @@ export interface RESTSearchResultsView {
results: Ref<HoppCollection[]> results: Ref<HoppCollection[]>
onSessionEnd: () => void onSessionEnd: () => void
} }
export interface RESTCollectionJSONView {
providerID: string
workspaceID: string
content: string
}