refactor: view implementation to retrieve collections for exporting
This commit is contained in:
@@ -510,7 +510,7 @@ const HoppGistCollectionsExporter: ImporterOrExporter = {
|
||||
const collectionJSON = await getCollectionJSON()
|
||||
const accessToken = currentUser.value?.accessToken
|
||||
|
||||
if (!accessToken) {
|
||||
if (!accessToken || E.isLeft(collectionJSON)) {
|
||||
toast.error(t("error.something_went_wrong"))
|
||||
isHoppGistCollectionExporterInProgress.value = false
|
||||
return
|
||||
@@ -606,6 +606,7 @@ const selectedTeamID = computed(() => {
|
||||
})
|
||||
|
||||
const getCollectionJSON = async () => {
|
||||
// TODO: Implement `getRESTCollectionJSONView` for team workspace
|
||||
if (
|
||||
props.collectionsType.type === "team-collections" &&
|
||||
props.collectionsType.selectedTeam?.teamID
|
||||
@@ -616,11 +617,31 @@ const getCollectionJSON = async () => {
|
||||
|
||||
return E.isRight(res)
|
||||
? E.right(res.right.exportCollectionsToJSON)
|
||||
: E.left(res.left)
|
||||
: E.left(res.left.error.toString())
|
||||
}
|
||||
|
||||
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")
|
||||
|
||||
@@ -14,6 +14,7 @@ import { HandleRef } from "./handle"
|
||||
import { WorkspaceProvider } from "./provider"
|
||||
import {
|
||||
RESTCollectionChildrenView,
|
||||
RESTCollectionJSONView,
|
||||
RESTCollectionLevelAuthHeadersView,
|
||||
RESTSearchResultsView,
|
||||
RootRESTCollectionView,
|
||||
@@ -721,6 +722,35 @@ export class NewWorkspaceService extends Service {
|
||||
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) {
|
||||
if (this.registeredProviders.has(provider.providerID)) {
|
||||
console.warn(
|
||||
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
RESTCollectionChildrenView,
|
||||
RootRESTCollectionView,
|
||||
RESTSearchResultsView,
|
||||
RESTCollectionJSONView,
|
||||
} from "./view"
|
||||
import { HoppCollection, HoppRESTRequest } from "@hoppscotch/data"
|
||||
|
||||
@@ -35,17 +36,20 @@ export interface WorkspaceProvider {
|
||||
|
||||
getRESTRootCollectionView(
|
||||
workspaceHandle: HandleRef<Workspace>
|
||||
): Promise<E.Either<unknown, HandleRef<RootRESTCollectionView>>>
|
||||
): Promise<E.Either<never, HandleRef<RootRESTCollectionView>>>
|
||||
getRESTCollectionChildrenView(
|
||||
collectionHandle: HandleRef<WorkspaceCollection>
|
||||
): Promise<E.Either<unknown, HandleRef<RESTCollectionChildrenView>>>
|
||||
): Promise<E.Either<never, HandleRef<RESTCollectionChildrenView>>>
|
||||
getRESTCollectionLevelAuthHeadersView(
|
||||
collectionHandle: HandleRef<WorkspaceCollection>
|
||||
): Promise<E.Either<unknown, HandleRef<RESTCollectionLevelAuthHeadersView>>>
|
||||
): Promise<E.Either<never, HandleRef<RESTCollectionLevelAuthHeadersView>>>
|
||||
getRESTSearchResultsView(
|
||||
workspaceHandle: HandleRef<Workspace>,
|
||||
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(
|
||||
workspaceHandle: HandleRef<Workspace>,
|
||||
|
||||
@@ -43,6 +43,7 @@ import { HandleRef } from "~/services/new-workspace/handle"
|
||||
import { WorkspaceProvider } from "~/services/new-workspace/provider"
|
||||
import {
|
||||
RESTCollectionChildrenView,
|
||||
RESTCollectionJSONView,
|
||||
RESTCollectionLevelAuthHeadersView,
|
||||
RESTCollectionViewItem,
|
||||
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(
|
||||
workspaceID: string
|
||||
): Promise<E.Either<unknown, HandleRef<Workspace>>> {
|
||||
|
||||
@@ -55,3 +55,10 @@ export interface RESTSearchResultsView {
|
||||
results: Ref<HoppCollection[]>
|
||||
onSessionEnd: () => void
|
||||
}
|
||||
|
||||
export interface RESTCollectionJSONView {
|
||||
providerID: string
|
||||
workspaceID: string
|
||||
|
||||
content: string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user