refactor: unify edit collection API methods and ensure consistent naming convention

This commit is contained in:
jamesgeorge007
2024-02-09 11:11:41 +05:30
parent f0dab55c99
commit f0f504d10e
6 changed files with 205 additions and 383 deletions

View File

@@ -8,12 +8,12 @@ import {
shallowRef,
watch,
} from "vue"
import { UpdatedCollectionProperties, WorkspaceProvider } from "./provider"
import { WorkspaceProvider } from "./provider"
import { HandleRef } from "./handle"
import * as E from "fp-ts/Either"
import { Workspace, WorkspaceCollection, WorkspaceRequest } from "./workspace"
import { RESTCollectionChildrenView, RootRESTCollectionView } from "./view"
import { HoppRESTRequest } from "@hoppscotch/data"
import { HoppCollection, HoppRESTRequest } from "@hoppscotch/data"
export type WorkspaceError<ServiceErr> =
| { type: "SERVICE_ERROR"; error: ServiceErr }
@@ -202,7 +202,7 @@ export class NewWorkspaceService extends Service {
}
public async createRESTChildCollection(
parentCollHandle: HandleRef<WorkspaceCollection>,
parentCollectionHandle: HandleRef<WorkspaceCollection>,
collectionName: string
): Promise<
E.Either<
@@ -210,12 +210,12 @@ export class NewWorkspaceService extends Service {
HandleRef<WorkspaceCollection>
>
> {
if (parentCollHandle.value.type === "invalid") {
if (parentCollectionHandle.value.type === "invalid") {
return E.left({ type: "SERVICE_ERROR", error: "INVALID_HANDLE" })
}
const provider = this.registeredProviders.get(
parentCollHandle.value.data.providerID
parentCollectionHandle.value.data.providerID
)
if (!provider) {
@@ -223,7 +223,7 @@ export class NewWorkspaceService extends Service {
}
const result = await provider.createRESTChildCollection(
parentCollHandle,
parentCollectionHandle,
collectionName
)
@@ -234,96 +234,30 @@ export class NewWorkspaceService extends Service {
return E.right(result.right)
}
public async editRESTRootCollection(
collHandle: HandleRef<WorkspaceCollection>,
newCollectionName: string
public async editRESTCollection(
collectionHandle: HandleRef<WorkspaceCollection>,
updatedCollection: HoppCollection
): Promise<
E.Either<
WorkspaceError<"INVALID_HANDLE" | "INVALID_PROVIDER">,
HandleRef<boolean>
>
> {
if (collHandle.value.type === "invalid") {
if (collectionHandle.value.type === "invalid") {
return E.left({ type: "SERVICE_ERROR", error: "INVALID_HANDLE" })
}
const provider = this.registeredProviders.get(
collHandle.value.data.providerID
collectionHandle.value.data.providerID
)
if (!provider) {
return E.left({ type: "SERVICE_ERROR", error: "INVALID_PROVIDER" })
}
const result = await provider.editRESTRootCollection(
collHandle,
newCollectionName
)
if (E.isLeft(result)) {
return E.left({ type: "PROVIDER_ERROR", error: result.left })
}
return E.right(result.right)
}
public async editRESTChildCollection(
collHandle: HandleRef<WorkspaceCollection>,
newCollectionName: string
): Promise<
E.Either<
WorkspaceError<"INVALID_HANDLE" | "INVALID_PROVIDER">,
HandleRef<boolean>
>
> {
if (collHandle.value.type === "invalid") {
return E.left({ type: "SERVICE_ERROR", error: "INVALID_HANDLE" })
}
const provider = this.registeredProviders.get(
collHandle.value.data.providerID
)
if (!provider) {
return E.left({ type: "SERVICE_ERROR", error: "INVALID_PROVIDER" })
}
const result = await provider.editRESTChildCollection(
collHandle,
newCollectionName
)
if (E.isLeft(result)) {
return E.left({ type: "PROVIDER_ERROR", error: result.left })
}
return E.right(result.right)
}
public async editRESTCollectionProperties(
collHandle: HandleRef<WorkspaceCollection>,
updatedCollProps: UpdatedCollectionProperties
): Promise<
E.Either<
WorkspaceError<"INVALID_HANDLE" | "INVALID_PROVIDER">,
HandleRef<boolean>
>
> {
if (collHandle.value.type === "invalid") {
return E.left({ type: "SERVICE_ERROR", error: "INVALID_HANDLE" })
}
const provider = this.registeredProviders.get(
collHandle.value.data.providerID
)
if (!provider) {
return E.left({ type: "SERVICE_ERROR", error: "INVALID_PROVIDER" })
}
const result = await provider.editRESTCollectionProperties(
collHandle,
updatedCollProps
const result = await provider.editRESTCollection(
collectionHandle,
updatedCollection
)
if (E.isLeft(result)) {
@@ -334,26 +268,26 @@ export class NewWorkspaceService extends Service {
}
public async removeRESTRootCollection(
collHandle: HandleRef<WorkspaceCollection>
collectionHandle: HandleRef<WorkspaceCollection>
): Promise<
E.Either<
WorkspaceError<"INVALID_HANDLE" | "INVALID_PROVIDER">,
HandleRef<boolean>
>
> {
if (collHandle.value.type === "invalid") {
if (collectionHandle.value.type === "invalid") {
return E.left({ type: "SERVICE_ERROR", error: "INVALID_HANDLE" })
}
const provider = this.registeredProviders.get(
collHandle.value.data.providerID
collectionHandle.value.data.providerID
)
if (!provider) {
return E.left({ type: "SERVICE_ERROR", error: "INVALID_PROVIDER" })
}
const result = await provider.removeRESTRootCollection(collHandle)
const result = await provider.removeRESTRootCollection(collectionHandle)
if (E.isLeft(result)) {
return E.left({ type: "PROVIDER_ERROR", error: result.left })
@@ -363,26 +297,28 @@ export class NewWorkspaceService extends Service {
}
public async removeRESTChildCollection(
parentCollHandle: HandleRef<WorkspaceCollection>
parentCollectionHandle: HandleRef<WorkspaceCollection>
): Promise<
E.Either<
WorkspaceError<"INVALID_HANDLE" | "INVALID_PROVIDER">,
HandleRef<boolean>
>
> {
if (parentCollHandle.value.type === "invalid") {
if (parentCollectionHandle.value.type === "invalid") {
return E.left({ type: "SERVICE_ERROR", error: "INVALID_HANDLE" })
}
const provider = this.registeredProviders.get(
parentCollHandle.value.data.providerID
parentCollectionHandle.value.data.providerID
)
if (!provider) {
return E.left({ type: "SERVICE_ERROR", error: "INVALID_PROVIDER" })
}
const result = await provider.removeRESTChildCollection(parentCollHandle)
const result = await provider.removeRESTChildCollection(
parentCollectionHandle
)
if (E.isLeft(result)) {
return E.left({ type: "PROVIDER_ERROR", error: result.left })
@@ -392,7 +328,7 @@ export class NewWorkspaceService extends Service {
}
public async createRESTRequest(
parentCollHandle: HandleRef<WorkspaceCollection>,
parentCollectionHandle: HandleRef<WorkspaceCollection>,
requestName: string,
openInNewTab: boolean
): Promise<
@@ -401,12 +337,12 @@ export class NewWorkspaceService extends Service {
HandleRef<WorkspaceCollection>
>
> {
if (parentCollHandle.value.type === "invalid") {
if (parentCollectionHandle.value.type === "invalid") {
return E.left({ type: "SERVICE_ERROR", error: "INVALID_HANDLE" })
}
const provider = this.registeredProviders.get(
parentCollHandle.value.data.providerID
parentCollectionHandle.value.data.providerID
)
if (!provider) {
@@ -414,7 +350,7 @@ export class NewWorkspaceService extends Service {
}
const result = await provider.createRESTRequest(
parentCollHandle,
parentCollectionHandle,
requestName,
openInNewTab
)

View File

@@ -8,16 +8,7 @@ import {
WorkspaceRequest,
} from "./workspace"
import { RESTCollectionChildrenView, RootRESTCollectionView } from "./view"
import {
HoppRESTAuth,
HoppRESTHeaders,
HoppRESTRequest,
} from "@hoppscotch/data"
export type UpdatedCollectionProperties = {
auth: HoppRESTAuth
headers: HoppRESTHeaders
}
import { HoppCollection, HoppRESTRequest } from "@hoppscotch/data"
export interface WorkspaceProvider {
providerID: string
@@ -51,37 +42,29 @@ export interface WorkspaceProvider {
collectionName: string
): Promise<E.Either<unknown, HandleRef<WorkspaceCollection>>>
createRESTChildCollection(
parentCollHandle: HandleRef<WorkspaceCollection>,
parentCollectionHandle: HandleRef<WorkspaceCollection>,
collectionName: string
): Promise<E.Either<unknown, HandleRef<WorkspaceCollection>>>
editRESTRootCollection(
collHandle: HandleRef<WorkspaceCollection>,
newCollectionName: string
): Promise<E.Either<unknown, HandleRef<boolean>>>
editRESTChildCollection(
parentCollHandle: HandleRef<WorkspaceCollection>,
newCollectionName: string
): Promise<E.Either<unknown, HandleRef<boolean>>>
editRESTCollectionProperties(
parentCollHandle: HandleRef<WorkspaceCollection>,
updatedCollProps: UpdatedCollectionProperties
editRESTCollection(
collectionHandle: HandleRef<WorkspaceCollection>,
updatedCollection: HoppCollection
): Promise<E.Either<unknown, HandleRef<boolean>>>
removeRESTRootCollection(
collHandle: HandleRef<WorkspaceCollection>
collectionHandle: HandleRef<WorkspaceCollection>
): Promise<E.Either<unknown, HandleRef<boolean>>>
removeRESTChildCollection(
parentCollHandle: HandleRef<WorkspaceCollection>
parentCollectionHandle: HandleRef<WorkspaceCollection>
): Promise<E.Either<unknown, HandleRef<boolean>>>
createRESTRequest(
parentCollHandle: HandleRef<WorkspaceCollection>,
parentCollectionHandle: HandleRef<WorkspaceCollection>,
requestName: string,
openInNewTab: boolean
): Promise<E.Either<unknown, HandleRef<WorkspaceCollection>>>
removeRESTRequest(
requestHandle: HandleRef<WorkspaceRequest>
): Promise<E.Either<unknown, HandleRef<boolean>>>
updateRESTRequest(
requestHandle: HandleRef<WorkspaceRequest>,
updatedRequest: HoppRESTRequest
): Promise<E.Either<unknown, HandleRef<boolean>>>
removeRESTRequest(
requestHandle: HandleRef<WorkspaceRequest>
): Promise<E.Either<unknown, HandleRef<boolean>>>
}

View File

@@ -1,7 +1,7 @@
import { HoppCollection, makeCollection } from "@hoppscotch/data"
import { Service } from "dioc"
import * as E from "fp-ts/Either"
import { Ref, computed, markRaw, nextTick, ref, shallowRef } from "vue"
import { Ref, computed, markRaw, ref, shallowRef } from "vue"
import PersonalWorkspaceSelector from "~/components/workspace/PersonalWorkspaceSelector.vue"
import { useStreamStatic } from "~/composables/stream"
@@ -23,10 +23,7 @@ import {
import { platform } from "~/platform"
import { HandleRef } from "~/services/new-workspace/handle"
import {
UpdatedCollectionProperties,
WorkspaceProvider,
} from "~/services/new-workspace/provider"
import { WorkspaceProvider } from "~/services/new-workspace/provider"
import {
RESTCollectionChildrenView,
RESTCollectionViewItem,
@@ -52,7 +49,6 @@ import path from "path"
import {
resolveSaveContextOnCollectionReorder,
getFoldersByPath,
updateInheritedPropertiesForAffectedRequests,
} from "~/helpers/collection/collection"
export class PersonalWorkspaceProviderService
@@ -209,9 +205,9 @@ export class PersonalWorkspaceProviderService
)
}
public editRESTRootCollection(
public editRESTCollection(
collHandle: HandleRef<WorkspaceCollection>,
newCollectionName: string
updatedCollection: HoppCollection
): Promise<E.Either<unknown, HandleRef<boolean>>> {
if (
collHandle.value.type !== "ok" ||
@@ -250,129 +246,6 @@ export class PersonalWorkspaceProviderService
}
}
const updatedCollection = {
...collection,
name: newCollectionName,
}
const collectionIndex = parseInt(collectionID)
editRESTCollection(collectionIndex, updatedCollection)
return {
type: "ok",
data: true,
}
})
)
)
}
public editRESTChildCollection(
collHandle: HandleRef<WorkspaceCollection>,
newCollectionName: string
): Promise<E.Either<unknown, HandleRef<boolean>>> {
if (
collHandle.value.type !== "ok" ||
collHandle.value.data.providerID !== this.providerID ||
collHandle.value.data.workspaceID !== "personal"
) {
return Promise.resolve(E.left("INVALID_WORKSPACE_HANDLE" as const))
}
return Promise.resolve(
E.right(
computed(() => {
if (
collHandle.value.type !== "ok" ||
collHandle.value.data.providerID !== this.providerID ||
collHandle.value.data.workspaceID !== "personal"
) {
return {
type: "invalid" as const,
reason: "WORKSPACE_INVALIDATED" as const,
}
}
const { collectionID } = collHandle.value.data
const collection: HoppCollection | null =
navigateToFolderWithIndexPath(
this.restCollectionState.value.state,
collectionID.split("/").map((id) => parseInt(id))
)
if (!collection) {
return {
type: "invalid" as const,
reason: "COLLECTION_NOT_FOUND" as const,
}
}
const updatedCollection = {
...collection,
name: newCollectionName,
}
editRESTFolder(collectionID, updatedCollection)
return {
type: "ok",
data: true,
}
})
)
)
}
public editRESTCollectionProperties(
collHandle: HandleRef<WorkspaceCollection>,
updatedCollProps: UpdatedCollectionProperties
): Promise<E.Either<unknown, HandleRef<boolean>>> {
if (
collHandle.value.type !== "ok" ||
collHandle.value.data.providerID !== this.providerID ||
collHandle.value.data.workspaceID !== "personal"
) {
return Promise.resolve(E.left("INVALID_WORKSPACE_HANDLE" as const))
}
return Promise.resolve(
E.right(
computed(() => {
if (
collHandle.value.type !== "ok" ||
collHandle.value.data.providerID !== this.providerID ||
collHandle.value.data.workspaceID !== "personal"
) {
return {
type: "invalid" as const,
reason: "WORKSPACE_INVALIDATED" as const,
}
}
const { collectionID } = collHandle.value.data
const { auth, headers } = updatedCollProps
const collection: HoppCollection | null =
navigateToFolderWithIndexPath(
this.restCollectionState.value.state,
collectionID.split("/").map((id) => parseInt(id))
)
if (!collection) {
return {
type: "invalid" as const,
reason: "COLLECTION_NOT_FOUND" as const,
}
}
const updatedCollection = {
...collection,
auth,
headers,
} as HoppCollection
const isRootCollection = collectionID.split("/").length === 1
if (isRootCollection) {
@@ -381,20 +254,6 @@ export class PersonalWorkspaceProviderService
editRESTFolder(collectionID, updatedCollection)
}
const { auth: cascadedAuth, headers: cascadedHeaders } =
cascadeParentCollectionForHeaderAuth(collectionID, "rest")
nextTick(() => {
updateInheritedPropertiesForAffectedRequests(
collectionID,
{
auth: cascadedAuth,
headers: cascadedHeaders,
},
"rest"
)
})
return {
type: "ok",
data: true,