refactor: port import/export functionality

This commit is contained in:
jamesgeorge007
2024-02-19 13:27:26 +05:30
parent 8483339005
commit 6ed9c09f06
11 changed files with 331 additions and 72 deletions

View File

@@ -383,6 +383,99 @@ export class NewWorkspaceService extends Service {
return E.right(result.right)
}
public async importRESTCollections(
workspaceHandle: HandleRef<Workspace>,
collections: HoppCollection[]
): Promise<
E.Either<
WorkspaceError<"INVALID_HANDLE" | "INVALID_PROVIDER">,
HandleRef<WorkspaceCollection>
>
> {
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.importRESTCollections(
workspaceHandle,
collections
)
if (E.isLeft(result)) {
return E.left({ type: "PROVIDER_ERROR", error: result.left })
}
return E.right(result.right)
}
public async exportRESTCollections(
workspaceHandle: HandleRef<Workspace>,
collections: HoppCollection[]
): Promise<
E.Either<WorkspaceError<"INVALID_HANDLE" | "INVALID_PROVIDER">, void>
> {
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.exportRESTCollections(
workspaceHandle,
collections
)
if (E.isLeft(result)) {
return E.left({ type: "PROVIDER_ERROR", error: result.left })
}
return E.right(result.right)
}
public async exportRESTCollection(
collectionHandle: HandleRef<WorkspaceCollection>,
collection: HoppCollection
): Promise<
E.Either<WorkspaceError<"INVALID_HANDLE" | "INVALID_PROVIDER">, void>
> {
if (collectionHandle.value.type === "invalid") {
return E.left({ type: "SERVICE_ERROR", error: "INVALID_HANDLE" })
}
const provider = this.registeredProviders.get(
collectionHandle.value.data.providerID
)
if (!provider) {
return E.left({ type: "SERVICE_ERROR", error: "INVALID_PROVIDER" })
}
const result = await provider.exportRESTCollection(
collectionHandle,
collection
)
if (E.isLeft(result)) {
return E.left({ type: "PROVIDER_ERROR", error: result.left })
}
return E.right(result.right)
}
public async getRESTCollectionChildrenView(
collectionHandle: HandleRef<WorkspaceCollection>
): Promise<

View File

@@ -67,4 +67,17 @@ export interface WorkspaceProvider {
removeRESTRequest(
requestHandle: HandleRef<WorkspaceRequest>
): Promise<E.Either<unknown, void>>
importRESTCollections(
workspaceHandle: HandleRef<Workspace>,
collections: HoppCollection[]
): Promise<E.Either<unknown, HandleRef<WorkspaceCollection>>>
exportRESTCollections(
workspaceHandle: HandleRef<Workspace>,
collections: HoppCollection[]
): Promise<E.Either<unknown, void>>
exportRESTCollection(
collectionHandle: HandleRef<WorkspaceCollection>,
collection: HoppCollection
): Promise<E.Either<unknown, void>>
}

View File

@@ -15,6 +15,7 @@ import { useStreamStatic } from "~/composables/stream"
import {
addRESTCollection,
addRESTFolder,
appendRESTCollections,
editRESTCollection,
editRESTFolder,
editRESTRequest,
@@ -49,6 +50,7 @@ import { HoppGQLHeader } from "~/helpers/graphql"
import { HoppInheritedProperty } from "~/helpers/types/HoppInheritedProperties"
import IconUser from "~icons/lucide/user"
import { NewWorkspaceService } from ".."
import { initializeDownloadFile } from "~/helpers/import-export/export"
export class PersonalWorkspaceProviderService
extends Service
@@ -217,7 +219,7 @@ export class PersonalWorkspaceProviderService
collectionHandle.value.data.providerID !== this.providerID ||
collectionHandle.value.data.workspaceID !== "personal"
) {
return Promise.resolve(E.left("INVALID_WORKSPACE_HANDLE" as const))
return Promise.resolve(E.left("INVALID_COLLECTION_HANDLE" as const))
}
const { collectionID } = collectionHandle.value.data
@@ -393,6 +395,86 @@ export class PersonalWorkspaceProviderService
return Promise.resolve(E.right(undefined))
}
public importRESTCollections(
workspaceHandle: HandleRef<Workspace>,
collections: HoppCollection[]
): Promise<E.Either<unknown, HandleRef<WorkspaceCollection>>> {
if (
workspaceHandle.value.type !== "ok" ||
workspaceHandle.value.data.providerID !== this.providerID ||
workspaceHandle.value.data.workspaceID !== "personal"
) {
return Promise.resolve(E.left("INVALID_WORKSPACE_HANDLE" as const))
}
appendRESTCollections(collections)
const newCollectionName = collections[0].name
const newCollectionID =
this.restCollectionState.value.state.length.toString()
return Promise.resolve(
E.right(
computed(() => {
if (
workspaceHandle.value.type !== "ok" ||
workspaceHandle.value.data.providerID !== this.providerID ||
workspaceHandle.value.data.workspaceID !== "personal"
) {
return {
type: "invalid" as const,
reason: "WORKSPACE_INVALIDATED" as const,
}
}
return {
type: "ok",
data: {
providerID: this.providerID,
workspaceID: workspaceHandle.value.data.workspaceID,
collectionID: newCollectionID,
name: newCollectionName,
},
}
})
)
)
}
public exportRESTCollections(
workspaceHandle: HandleRef<WorkspaceCollection>,
collections: HoppCollection[]
): Promise<E.Either<unknown, void>> {
if (
workspaceHandle.value.type !== "ok" ||
workspaceHandle.value.data.providerID !== this.providerID ||
workspaceHandle.value.data.workspaceID !== "personal"
) {
return Promise.resolve(E.left("INVALID_COLLECTION_HANDLE" as const))
}
initializeDownloadFile(JSON.stringify(collections, null, 2), "Collections")
return Promise.resolve(E.right(undefined))
}
public exportRESTCollection(
collectionHandle: HandleRef<WorkspaceCollection>,
collection: HoppCollection
): Promise<E.Either<unknown, void>> {
if (
collectionHandle.value.type !== "ok" ||
collectionHandle.value.data.providerID !== this.providerID ||
collectionHandle.value.data.workspaceID !== "personal"
) {
return Promise.resolve(E.left("INVALID_COLLECTION_HANDLE" as const))
}
initializeDownloadFile(JSON.stringify(collection, null, 2), collection.name)
return Promise.resolve(E.right(undefined))
}
public getCollectionHandle(
workspaceHandle: HandleRef<Workspace>,
collectionID: string