refactor: finalize API methods

This commit is contained in:
jamesgeorge007
2024-02-08 20:58:54 +05:30
parent 89bcc58de6
commit d6a8e60239
7 changed files with 209 additions and 396 deletions

View File

@@ -65,6 +65,7 @@ import {
} from "@hoppscotch/data"
import { computedWithControl } from "@vueuse/core"
import { useService } from "dioc/vue"
import * as E from "fp-ts/Either"
import * as TE from "fp-ts/TaskEither"
import { pipe } from "fp-ts/function"
import { cloneDeep } from "lodash-es"
@@ -78,11 +79,10 @@ import { Picked } from "~/helpers/types/HoppPicked"
import {
cascadeParentCollectionForHeaderAuth,
editGraphqlRequest,
editRESTRequest,
saveGraphqlRequestAs,
saveRESTRequestAs,
} from "~/newstore/collections"
import { platform } from "~/platform"
import { NewWorkspaceService } from "~/services/new-workspace"
import { GQLTabService } from "~/services/tab/graphql"
import { RESTTabService } from "~/services/tab/rest"
import { TeamWorkspace } from "~/services/workspace.service"
@@ -92,6 +92,7 @@ const toast = useToast()
const RESTTabs = useService(RESTTabService)
const GQLTabs = useService(GQLTabService)
const workspaceService = useService(NewWorkspaceService)
type CollectionType =
| {
@@ -212,99 +213,106 @@ const saveRequestAs = async () => {
return
}
const requestUpdated =
const updatedRequest =
props.mode === "rest"
? cloneDeep(RESTTabs.currentActiveTab.value.document.request)
: cloneDeep(GQLTabs.currentActiveTab.value.document.request)
requestUpdated.name = requestName.value
updatedRequest.name = requestName.value
if (picked.value.pickedType === "my-collection") {
if (!isHoppRESTRequest(requestUpdated))
if (!workspaceService.activeWorkspaceHandle.value) {
return
}
if (
picked.value.pickedType === "my-collection" ||
picked.value.pickedType === "my-folder"
) {
if (!isHoppRESTRequest(updatedRequest))
throw new Error("requestUpdated is not a REST Request")
const insertionIndex = saveRESTRequestAs(
`${picked.value.collectionIndex}`,
requestUpdated
const collPathIndex =
picked.value.pickedType === "my-collection"
? picked.value.collectionIndex.toString()
: picked.value.folderPath
const collHandleResult = await workspaceService.getCollectionHandle(
workspaceService.activeWorkspaceHandle.value,
collPathIndex
)
RESTTabs.currentActiveTab.value.document = {
request: requestUpdated,
isDirty: false,
saveContext: {
originLocation: "user-collection",
folderPath: `${picked.value.collectionIndex}`,
requestIndex: insertionIndex,
},
if (E.isLeft(collHandleResult)) {
// INVALID_WORKSPACE_HANDLE
return
}
const { auth, headers } = cascadeParentCollectionForHeaderAuth(
`${picked.value.collectionIndex}`,
"rest"
const collHandle = collHandleResult.right
if (collHandle.value.type === "invalid") {
// WORKSPACE_INVALIDATED | INVALID_COLLECTION_HANDLE
return
}
const resultHandle = await workspaceService.createRESTRequest(
collHandle,
updatedRequest.name,
false
)
RESTTabs.currentActiveTab.value.document.inheritedProperties = {
auth,
headers,
if (E.isLeft(resultHandle)) {
// WORKSPACE_INVALIDATED | INVALID_COLLECTION_HANDLE
return
}
platform.analytics?.logEvent({
type: "HOPP_SAVE_REQUEST",
createdNow: true,
platform: "rest",
workspaceType: "personal",
})
const result = resultHandle.right
requestSaved()
} else if (picked.value.pickedType === "my-folder") {
if (!isHoppRESTRequest(requestUpdated))
throw new Error("requestUpdated is not a REST Request")
const insertionIndex = saveRESTRequestAs(
picked.value.folderPath,
requestUpdated
)
RESTTabs.currentActiveTab.value.document = {
request: requestUpdated,
isDirty: false,
saveContext: {
originLocation: "user-collection",
folderPath: picked.value.folderPath,
requestIndex: insertionIndex,
},
if (result.value.type === "invalid") {
// WORKSPACE_INVALIDATED | INVALID_COLLECTION_HANDLE
return
}
const { auth, headers } = cascadeParentCollectionForHeaderAuth(
picked.value.folderPath,
"rest"
)
RESTTabs.currentActiveTab.value.document.inheritedProperties = {
auth,
headers,
}
platform.analytics?.logEvent({
type: "HOPP_SAVE_REQUEST",
createdNow: true,
platform: "rest",
workspaceType: "personal",
})
requestSaved()
} else if (picked.value.pickedType === "my-request") {
if (!isHoppRESTRequest(requestUpdated))
if (!isHoppRESTRequest(updatedRequest))
throw new Error("requestUpdated is not a REST Request")
editRESTRequest(
picked.value.folderPath,
picked.value.requestIndex,
requestUpdated
const requestHandleResult = await workspaceService.getRequestHandle(
workspaceService.activeWorkspaceHandle.value,
picked.value.folderPath
)
if (E.isLeft(requestHandleResult)) {
// INVALID_WORKSPACE_HANDLE
return
}
const requestHandle = requestHandleResult.right
if (requestHandle.value.type === "invalid") {
// WORKSPACE_INVALIDATED | INVALID_REQUEST_HANDLE
return
}
const resultHandle = await workspaceService.updateRESTRequest(
requestHandle,
updatedRequest
)
if (E.isLeft(resultHandle)) {
// WORKSPACE_INVALIDATED | INVALID_REQUEST_HANDLE
return
}
const result = resultHandle.right
if (result.value.type === "invalid") {
// WORKSPACE_INVALIDATED | INVALID_REQUEST_HANDLE
return
}
// These remain here in the component
RESTTabs.currentActiveTab.value.document = {
request: requestUpdated,
request: updatedRequest,
isDirty: false,
saveContext: {
originLocation: "user-collection",
@@ -323,19 +331,12 @@ const saveRequestAs = async () => {
headers,
}
platform.analytics?.logEvent({
type: "HOPP_SAVE_REQUEST",
createdNow: false,
platform: "rest",
workspaceType: "personal",
})
requestSaved()
} else if (picked.value.pickedType === "teams-collection") {
if (!isHoppRESTRequest(requestUpdated))
if (!isHoppRESTRequest(updatedRequest))
throw new Error("requestUpdated is not a REST Request")
updateTeamCollectionOrFolder(picked.value.collectionID, requestUpdated)
updateTeamCollectionOrFolder(picked.value.collectionID, updatedRequest)
platform.analytics?.logEvent({
type: "HOPP_SAVE_REQUEST",
@@ -344,10 +345,10 @@ const saveRequestAs = async () => {
workspaceType: "team",
})
} else if (picked.value.pickedType === "teams-folder") {
if (!isHoppRESTRequest(requestUpdated))
if (!isHoppRESTRequest(updatedRequest))
throw new Error("requestUpdated is not a REST Request")
updateTeamCollectionOrFolder(picked.value.folderID, requestUpdated)
updateTeamCollectionOrFolder(picked.value.folderID, updatedRequest)
platform.analytics?.logEvent({
type: "HOPP_SAVE_REQUEST",
@@ -356,7 +357,7 @@ const saveRequestAs = async () => {
workspaceType: "team",
})
} else if (picked.value.pickedType === "teams-request") {
if (!isHoppRESTRequest(requestUpdated))
if (!isHoppRESTRequest(updatedRequest))
throw new Error("requestUpdated is not a REST Request")
if (
@@ -368,8 +369,8 @@ const saveRequestAs = async () => {
modalLoadingState.value = true
const data = {
request: JSON.stringify(requestUpdated),
title: requestUpdated.name,
request: JSON.stringify(updatedRequest),
title: updatedRequest.name,
}
platform.analytics?.logEvent({
@@ -397,7 +398,7 @@ const saveRequestAs = async () => {
editGraphqlRequest(
picked.value.folderPath,
picked.value.requestIndex,
requestUpdated as HoppGQLRequest
updatedRequest as HoppGQLRequest
)
platform.analytics?.logEvent({
@@ -422,7 +423,7 @@ const saveRequestAs = async () => {
// TODO: Check for GQL request ?
saveGraphqlRequestAs(
picked.value.folderPath,
requestUpdated as HoppGQLRequest
updatedRequest as HoppGQLRequest
)
platform.analytics?.logEvent({
@@ -447,7 +448,7 @@ const saveRequestAs = async () => {
// TODO: Check for GQL request ?
saveGraphqlRequestAs(
`${picked.value.collectionIndex}`,
requestUpdated as HoppGQLRequest
updatedRequest as HoppGQLRequest
)
platform.analytics?.logEvent({

View File

@@ -35,7 +35,7 @@
:key="`method-${index}`"
:label="method"
:style="{
color: getMethodLabelColorClassOf({ method }),
color: getMethodLabelColor(method),
}"
@click="
() => {
@@ -262,7 +262,7 @@ import { InterceptorService } from "~/services/interceptor.service"
import { HoppTab } from "~/services/tab"
import { HoppRESTDocument } from "~/helpers/rest/document"
import { RESTTabService } from "~/services/tab/rest"
import { getMethodLabelColorClassOf } from "~/helpers/rest/labelColoring"
import { getMethodLabelColor } from "~/helpers/rest/labelColoring"
import { WorkspaceService } from "~/services/workspace.service"
import { NewWorkspaceService } from "~/services/new-workspace"
@@ -539,7 +539,7 @@ const saveRequest = async () => {
}
const requestHandleResult = await newWorkspaceService.getRequestHandle(
collHandle,
newWorkspaceService.activeWorkspaceHandle.value,
`${saveCtx.folderPath}/${saveCtx.requestIndex.toString()}`
)
@@ -555,7 +555,7 @@ const saveRequest = async () => {
return
}
const updatedRequestResult = await newWorkspaceService.saveRESTRequest(
const updatedRequestResult = await newWorkspaceService.updateRESTRequest(
requestHandle,
updatedRequest
)

View File

@@ -335,16 +335,17 @@ const onAddRequest = async (requestName: string) => {
const result = await workspaceService.createRESTRequest(
collHandle,
requestName
requestName,
true
)
if (E.isLeft(result)) {
// INVALID_WORKSPACE_HANDLE
// INVALID_COLLECTION_HANDLE
return
}
if (result.right.value.type === "invalid") {
// WORKSPACE_INVALIDATED
// COLLECTION_INVALIDATED
return
}
@@ -551,28 +552,10 @@ const removeRequest = (requestIndexPath: string) => {
}
const onRemoveRequest = async () => {
const parentCollIndexPath = editingCollIndexPath.value
const requestIndexPath = editingRequestIndexPath.value
const parentCollHandleResult = await workspaceService.getCollectionHandle(
props.workspaceHandle,
parentCollIndexPath
)
if (E.isLeft(parentCollHandleResult)) {
// INVALID_WORKSPACE_HANDLE
return
}
const parentCollHandle = parentCollHandleResult.right
if (parentCollHandle.value.type === "invalid") {
// WORKSPACE_INVALIDATED | INVALID_COLLECTION_HANDLE
return
}
const requestHandleResult = await workspaceService.getRequestHandle(
parentCollHandle,
props.workspaceHandle,
requestIndexPath
)
@@ -607,25 +590,8 @@ const onRemoveRequest = async () => {
const selectRequest = async (requestIndexPath: string) => {
const collIndexPath = requestIndexPath.split("/").slice(0, -1).join("/")
const collHandleResult = await workspaceService.getCollectionHandle(
props.workspaceHandle,
collIndexPath
)
if (E.isLeft(collHandleResult)) {
// INVALID_WORKSPACE_HANDLE
return
}
const collHandle = collHandleResult.right
if (collHandle.value.type === "invalid") {
// WORKSPACE_INVALIDATED | INVALID_COLLECTION_HANDLE
return
}
const requestHandleResult = await workspaceService.getRequestHandle(
collHandle,
props.workspaceHandle,
requestIndexPath
)
@@ -679,25 +645,8 @@ const selectRequest = async (requestIndexPath: string) => {
const duplicateRequest = async (requestIndexPath: string) => {
const collPath = requestIndexPath.split("/").slice(0, -1).join("/")
const collHandleResult = await workspaceService.getCollectionHandle(
props.workspaceHandle,
collPath
)
if (E.isLeft(collHandleResult)) {
// INVALID_WORKSPACE_HANDLE
return
}
const collHandle = collHandleResult.right
if (collHandle.value.type === "invalid") {
// WORKSPACE_INVALIDATED | INVALID_COLLECTION_HANDLE
return
}
const requestHandleResult = await workspaceService.getRequestHandle(
collHandle,
props.workspaceHandle,
requestIndexPath
)
@@ -742,28 +691,10 @@ const editRequest = (payload: {
}
const onEditRequest = async (newReqName: string) => {
const parentCollID = editingCollIndexPath.value
const requestID = editingRequestIndexPath.value
const parentCollHandleResult = await workspaceService.getCollectionHandle(
props.workspaceHandle,
parentCollID
)
if (E.isLeft(parentCollHandleResult)) {
// INVALID_WORKSPACE_HANDLE
return
}
const parentCollHandle = parentCollHandleResult.right
if (parentCollHandle.value.type === "invalid") {
// WORKSPACE_INVALIDATED | INVALID_COLLECTION_HANDLE
return
}
const requestHandleResult = await workspaceService.getRequestHandle(
parentCollHandle,
props.workspaceHandle,
requestID
)
@@ -779,9 +710,14 @@ const onEditRequest = async (newReqName: string) => {
return
}
const result = await workspaceService.editRESTRequest(
const updatedRequest = {
...requestHandle.value.data.request,
name: newReqName,
} as HoppRESTRequest
const result = await workspaceService.updateRESTRequest(
requestHandle,
newReqName
updatedRequest
)
if (E.isLeft(result)) {
@@ -818,7 +754,7 @@ const editCollectionProperties = async (collIndexPath: string) => {
},
],
} as HoppInheritedProperty
// Have a provider level implementation that returns a view that says what the headesd and auth are
if (parentIndex) {
const { auth, headers } = cascadeParentCollectionForHeaderAuth(
parentIndex,

View File

@@ -139,7 +139,7 @@ export class NewWorkspaceService extends Service {
}
public async getRequestHandle(
parentCollHandle: HandleRef<WorkspaceCollection>,
workspaceHandle: HandleRef<Workspace>,
requestID: string
): Promise<
E.Either<
@@ -147,19 +147,19 @@ export class NewWorkspaceService extends Service {
HandleRef<WorkspaceRequest>
>
> {
if (parentCollHandle.value.type === "invalid") {
if (workspaceHandle.value.type === "invalid") {
return E.left({ type: "SERVICE_ERROR", error: "INVALID_HANDLE" })
}
const provider = this.registeredProviders.get(
parentCollHandle.value.data.providerID
workspaceHandle.value.data.providerID
)
if (!provider) {
return E.left({ type: "SERVICE_ERROR", error: "INVALID_PROVIDER" })
}
const result = await provider.getRequestHandle(parentCollHandle, requestID)
const result = await provider.getRequestHandle(workspaceHandle, requestID)
if (E.isLeft(result)) {
return E.left({ type: "PROVIDER_ERROR", error: result.left })
@@ -240,7 +240,7 @@ export class NewWorkspaceService extends Service {
): Promise<
E.Either<
WorkspaceError<"INVALID_HANDLE" | "INVALID_PROVIDER">,
HandleRef<WorkspaceCollection>
HandleRef<boolean>
>
> {
if (collHandle.value.type === "invalid") {
@@ -273,7 +273,7 @@ export class NewWorkspaceService extends Service {
): Promise<
E.Either<
WorkspaceError<"INVALID_HANDLE" | "INVALID_PROVIDER">,
HandleRef<WorkspaceCollection>
HandleRef<boolean>
>
> {
if (collHandle.value.type === "invalid") {
@@ -306,7 +306,7 @@ export class NewWorkspaceService extends Service {
): Promise<
E.Either<
WorkspaceError<"INVALID_HANDLE" | "INVALID_PROVIDER">,
HandleRef<WorkspaceCollection>
HandleRef<boolean>
>
> {
if (collHandle.value.type === "invalid") {
@@ -338,7 +338,7 @@ export class NewWorkspaceService extends Service {
): Promise<
E.Either<
WorkspaceError<"INVALID_HANDLE" | "INVALID_PROVIDER">,
HandleRef<WorkspaceCollection>
HandleRef<boolean>
>
> {
if (collHandle.value.type === "invalid") {
@@ -367,7 +367,7 @@ export class NewWorkspaceService extends Service {
): Promise<
E.Either<
WorkspaceError<"INVALID_HANDLE" | "INVALID_PROVIDER">,
HandleRef<WorkspaceCollection>
HandleRef<boolean>
>
> {
if (parentCollHandle.value.type === "invalid") {
@@ -393,7 +393,8 @@ export class NewWorkspaceService extends Service {
public async createRESTRequest(
parentCollHandle: HandleRef<WorkspaceCollection>,
requestName: string
requestName: string,
openInNewTab: boolean
): Promise<
E.Either<
WorkspaceError<"INVALID_HANDLE" | "INVALID_PROVIDER">,
@@ -414,7 +415,8 @@ export class NewWorkspaceService extends Service {
const result = await provider.createRESTRequest(
parentCollHandle,
requestName
requestName,
openInNewTab
)
if (E.isLeft(result)) {
@@ -429,7 +431,7 @@ export class NewWorkspaceService extends Service {
): Promise<
E.Either<
WorkspaceError<"INVALID_HANDLE" | "INVALID_PROVIDER">,
HandleRef<WorkspaceRequest>
HandleRef<boolean>
>
> {
if (requestHandle.value.type === "invalid") {
@@ -453,43 +455,13 @@ export class NewWorkspaceService extends Service {
return E.right(result.right)
}
public async editRESTRequest(
requestHandle: HandleRef<WorkspaceRequest>,
newRequestName: string
): Promise<
E.Either<
WorkspaceError<"INVALID_HANDLE" | "INVALID_PROVIDER">,
HandleRef<WorkspaceRequest>
>
> {
if (requestHandle.value.type === "invalid") {
return E.left({ type: "SERVICE_ERROR", error: "INVALID_HANDLE" })
}
const provider = this.registeredProviders.get(
requestHandle.value.data.providerID
)
if (!provider) {
return E.left({ type: "SERVICE_ERROR", error: "INVALID_PROVIDER" })
}
const result = await provider.editRESTRequest(requestHandle, newRequestName)
if (E.isLeft(result)) {
return E.left({ type: "PROVIDER_ERROR", error: result.left })
}
return E.right(result.right)
}
public async saveRESTRequest(
public async updateRESTRequest(
requestHandle: HandleRef<WorkspaceRequest>,
updatedRequest: HoppRESTRequest
): Promise<
E.Either<
WorkspaceError<"INVALID_HANDLE" | "INVALID_PROVIDER">,
HandleRef<WorkspaceRequest>
HandleRef<boolean>
>
> {
if (requestHandle.value.type === "invalid") {
@@ -504,7 +476,10 @@ export class NewWorkspaceService extends Service {
return E.left({ type: "SERVICE_ERROR", error: "INVALID_PROVIDER" })
}
const result = await provider.saveRESTRequest(requestHandle, updatedRequest)
const result = await provider.updateRESTRequest(
requestHandle,
updatedRequest
)
if (E.isLeft(result)) {
return E.left({ type: "PROVIDER_ERROR", error: result.left })

View File

@@ -8,7 +8,11 @@ import {
WorkspaceRequest,
} from "./workspace"
import { RESTCollectionChildrenView, RootRESTCollectionView } from "./view"
import { HoppRESTAuth, HoppRESTHeaders, HoppRESTRequest } from "@hoppscotch/data"
import {
HoppRESTAuth,
HoppRESTHeaders,
HoppRESTRequest,
} from "@hoppscotch/data"
export type UpdatedCollectionProperties = {
auth: HoppRESTAuth
@@ -28,7 +32,7 @@ export interface WorkspaceProvider {
collectionID: string
): Promise<E.Either<unknown, HandleRef<WorkspaceCollection>>>
getRequestHandle(
parentCollHandle: HandleRef<WorkspaceCollection>,
workspaceHandle: HandleRef<Workspace>,
requestID: string
): Promise<E.Either<unknown, HandleRef<WorkspaceRequest>>>
@@ -38,6 +42,9 @@ export interface WorkspaceProvider {
getRESTCollectionChildrenView(
collectionHandle: HandleRef<WorkspaceCollection>
): Promise<E.Either<unknown, HandleRef<RESTCollectionChildrenView>>>
// getRESTCollectionAuthHeaders(
// collectionHandle: HandleRef<WorkspaceCollection>
// ): Promise<E.Either<unknown, HandleRef<RESTCollectionAuthHeadersView>>>
createRESTRootCollection(
workspaceHandle: HandleRef<Workspace>,
@@ -50,34 +57,31 @@ export interface WorkspaceProvider {
editRESTRootCollection(
collHandle: HandleRef<WorkspaceCollection>,
newCollectionName: string
): Promise<E.Either<unknown, HandleRef<WorkspaceCollection>>>
): Promise<E.Either<unknown, HandleRef<boolean>>>
editRESTChildCollection(
parentCollHandle: HandleRef<WorkspaceCollection>,
newCollectionName: string
): Promise<E.Either<unknown, HandleRef<WorkspaceCollection>>>
): Promise<E.Either<unknown, HandleRef<boolean>>>
editRESTCollectionProperties(
parentCollHandle: HandleRef<WorkspaceCollection>,
updatedCollProps: UpdatedCollectionProperties
): Promise<E.Either<unknown, HandleRef<WorkspaceCollection>>>
): Promise<E.Either<unknown, HandleRef<boolean>>>
removeRESTRootCollection(
collHandle: HandleRef<WorkspaceCollection>
): Promise<E.Either<unknown, HandleRef<WorkspaceCollection>>>
): Promise<E.Either<unknown, HandleRef<boolean>>>
removeRESTChildCollection(
parentCollHandle: HandleRef<WorkspaceCollection>
): Promise<E.Either<unknown, HandleRef<WorkspaceCollection>>>
): Promise<E.Either<unknown, HandleRef<boolean>>>
createRESTRequest(
parentCollHandle: HandleRef<WorkspaceCollection>,
requestName: string
requestName: string,
openInNewTab: boolean
): Promise<E.Either<unknown, HandleRef<WorkspaceCollection>>>
removeRESTRequest(
requestHandle: HandleRef<WorkspaceRequest>
): Promise<E.Either<unknown, HandleRef<WorkspaceRequest>>>
editRESTRequest(
requestHandle: HandleRef<WorkspaceRequest>,
newRequestName: string
): Promise<E.Either<unknown, HandleRef<WorkspaceRequest>>>
saveRESTRequest(
): Promise<E.Either<unknown, HandleRef<boolean>>>
updateRESTRequest(
requestHandle: HandleRef<WorkspaceRequest>,
updatedRequest: HoppRESTRequest
): Promise<E.Either<unknown, HandleRef<WorkspaceRequest>>>
): Promise<E.Either<unknown, HandleRef<boolean>>>
}

View File

@@ -1,4 +1,8 @@
import { HoppCollection, makeCollection } from "@hoppscotch/data"
import {
HoppCollection,
isHoppRESTRequest,
makeCollection,
} from "@hoppscotch/data"
import { Service } from "dioc"
import * as E from "fp-ts/Either"
import { Ref, computed, markRaw, nextTick, ref, shallowRef } from "vue"
@@ -148,7 +152,7 @@ export class PersonalWorkspaceProviderService
data: {
providerID: this.providerID,
workspaceID: workspaceHandle.value.data.workspaceID,
collectionID: "",
collectionID: "", // Compute this and supply
collection: newRootCollection,
},
}
@@ -217,7 +221,7 @@ export class PersonalWorkspaceProviderService
public editRESTRootCollection(
collHandle: HandleRef<WorkspaceCollection>,
newCollectionName: string
): Promise<E.Either<unknown, HandleRef<WorkspaceCollection>>> {
): Promise<E.Either<unknown, HandleRef<boolean>>> {
if (
collHandle.value.type !== "ok" ||
collHandle.value.data.providerID !== this.providerID ||
@@ -240,8 +244,7 @@ export class PersonalWorkspaceProviderService
}
}
const { collection, collectionID, providerID, workspaceID } =
collHandle.value.data
const { collection, collectionID } = collHandle.value.data
const updatedCollection = {
...(collection as HoppCollection),
@@ -253,12 +256,7 @@ export class PersonalWorkspaceProviderService
return {
type: "ok",
data: {
providerID,
workspaceID,
collectionID,
collection: updatedCollection,
},
data: true,
}
})
)
@@ -268,7 +266,7 @@ export class PersonalWorkspaceProviderService
public editRESTChildCollection(
collHandle: HandleRef<WorkspaceCollection>,
newCollectionName: string
): Promise<E.Either<unknown, HandleRef<WorkspaceCollection>>> {
): Promise<E.Either<unknown, HandleRef<boolean>>> {
if (
collHandle.value.type !== "ok" ||
collHandle.value.data.providerID !== this.providerID ||
@@ -291,8 +289,7 @@ export class PersonalWorkspaceProviderService
}
}
const { collection, collectionID, providerID, workspaceID } =
collHandle.value.data
const { collection, collectionID } = collHandle.value.data
const updatedCollection = {
...(collection as HoppCollection),
@@ -303,12 +300,7 @@ export class PersonalWorkspaceProviderService
return {
type: "ok",
data: {
providerID,
workspaceID,
collectionID,
collection: updatedCollection,
},
data: true,
}
})
)
@@ -318,7 +310,7 @@ export class PersonalWorkspaceProviderService
public editRESTCollectionProperties(
collHandle: HandleRef<WorkspaceCollection>,
updatedCollProps: UpdatedCollectionProperties
): Promise<E.Either<unknown, HandleRef<WorkspaceCollection>>> {
): Promise<E.Either<unknown, HandleRef<boolean>>> {
if (
collHandle.value.type !== "ok" ||
collHandle.value.data.providerID !== this.providerID ||
@@ -341,8 +333,7 @@ export class PersonalWorkspaceProviderService
}
}
const { collection, collectionID, providerID, workspaceID } =
collHandle.value.data
const { collection, collectionID } = collHandle.value.data
const { auth, headers } = updatedCollProps
@@ -376,12 +367,7 @@ export class PersonalWorkspaceProviderService
return {
type: "ok",
data: {
providerID,
workspaceID,
collectionID,
collection: updatedCollection,
},
data: true,
}
})
)
@@ -390,7 +376,7 @@ export class PersonalWorkspaceProviderService
public removeRESTRootCollection(
collHandle: HandleRef<WorkspaceCollection>
): Promise<E.Either<unknown, HandleRef<WorkspaceCollection>>> {
): Promise<E.Either<unknown, HandleRef<boolean>>> {
if (
collHandle.value.type !== "ok" ||
collHandle.value.data.providerID !== this.providerID ||
@@ -413,8 +399,7 @@ export class PersonalWorkspaceProviderService
}
}
const { collectionID, providerID, workspaceID } =
collHandle.value.data
const { collectionID } = collHandle.value.data
const collectionIndex = parseInt(collectionID)
@@ -437,12 +422,7 @@ export class PersonalWorkspaceProviderService
return {
type: "ok",
data: {
providerID,
workspaceID,
collectionID,
collection: null,
},
data: true,
}
})
)
@@ -451,7 +431,7 @@ export class PersonalWorkspaceProviderService
public removeRESTChildCollection(
parentCollHandle: HandleRef<WorkspaceCollection>
): Promise<E.Either<unknown, HandleRef<WorkspaceCollection>>> {
): Promise<E.Either<unknown, HandleRef<boolean>>> {
if (
parentCollHandle.value.type !== "ok" ||
parentCollHandle.value.data.providerID !== this.providerID ||
@@ -474,8 +454,7 @@ export class PersonalWorkspaceProviderService
}
}
const { collectionID, providerID, workspaceID } =
parentCollHandle.value.data
const { collectionID } = parentCollHandle.value.data
const folderToRemove = path
? navigateToFolderWithIndexPath(
@@ -502,12 +481,7 @@ export class PersonalWorkspaceProviderService
return {
type: "ok",
data: {
providerID,
workspaceID,
collectionID,
collection: null,
},
data: true,
}
})
)
@@ -516,14 +490,15 @@ export class PersonalWorkspaceProviderService
public createRESTRequest(
parentCollHandle: HandleRef<WorkspaceCollection>,
requestName: string
requestName: string,
openInNewTab: boolean
): Promise<E.Either<unknown, HandleRef<WorkspaceCollection>>> {
if (
parentCollHandle.value.type !== "ok" ||
parentCollHandle.value.data.providerID !== this.providerID ||
parentCollHandle.value.data.workspaceID !== "personal"
) {
return Promise.resolve(E.left("INVALID_WORKSPACE_HANDLE" as const))
return Promise.resolve(E.left("INVALID_COLLECTION_HANDLE" as const))
}
return Promise.resolve(
@@ -536,7 +511,7 @@ export class PersonalWorkspaceProviderService
) {
return {
type: "invalid" as const,
reason: "WORKSPACE_INVALIDATED" as const,
reason: "COLLECTION_INVALIDATED" as const,
}
}
@@ -550,24 +525,26 @@ export class PersonalWorkspaceProviderService
const insertionIndex = saveRESTRequestAs(collectionID, newRequest)
const { auth, headers } = cascadeParentCollectionForHeaderAuth(
collectionID,
"rest"
)
if (openInNewTab) {
const { auth, headers } = cascadeParentCollectionForHeaderAuth(
collectionID,
"rest"
)
this.tabs.createNewTab({
request: newRequest,
isDirty: false,
saveContext: {
originLocation: "user-collection",
folderPath: collectionID,
requestIndex: insertionIndex,
},
inheritedProperties: {
auth,
headers,
},
})
this.tabs.createNewTab({
request: newRequest,
isDirty: false,
saveContext: {
originLocation: "user-collection",
folderPath: collectionID,
requestIndex: insertionIndex,
},
inheritedProperties: {
auth,
headers,
},
})
}
platform.analytics?.logEvent({
type: "HOPP_SAVE_REQUEST",
@@ -597,7 +574,7 @@ export class PersonalWorkspaceProviderService
public removeRESTRequest(
requestHandle: HandleRef<WorkspaceRequest>
): Promise<E.Either<unknown, HandleRef<WorkspaceRequest>>> {
): Promise<E.Either<unknown, HandleRef<boolean>>> {
if (
requestHandle.value.type !== "ok" ||
requestHandle.value.data.providerID !== this.providerID ||
@@ -620,8 +597,7 @@ export class PersonalWorkspaceProviderService
}
}
const { collectionID, providerID, requestID, workspaceID } =
requestHandle.value.data
const { collectionID, requestID } = requestHandle.value.data
const requestIndex = parseInt(requestID.split("/").slice(-1)[0])
const possibleTab = this.tabs.getTabRefWithSaveContext({
@@ -656,90 +632,17 @@ export class PersonalWorkspaceProviderService
return {
type: "ok",
data: {
providerID,
workspaceID,
collectionID,
requestID,
request: null,
},
data: true,
}
})
)
)
}
public editRESTRequest(
requestHandle: HandleRef<WorkspaceRequest>,
newRequestName: string
): Promise<E.Either<unknown, HandleRef<WorkspaceRequest>>> {
if (
requestHandle.value.type !== "ok" ||
requestHandle.value.data.providerID !== this.providerID ||
requestHandle.value.data.workspaceID !== "personal"
) {
return Promise.resolve(E.left("INVALID_REQUEST_HANDLE" as const))
}
return Promise.resolve(
E.right(
computed(() => {
if (
requestHandle.value.type !== "ok" ||
requestHandle.value.data.providerID !== this.providerID ||
requestHandle.value.data.workspaceID !== "personal"
) {
return {
type: "invalid" as const,
reason: "REQUEST_INVALIDATED" as const,
}
}
const { collectionID, providerID, request, requestID, workspaceID } =
requestHandle.value.data
const requestIndexPath = requestID.split("/").slice(-1).join("")
const requestIndex = parseInt(requestIndexPath)
const updatedRequest = {
...request,
name: newRequestName || request?.name,
} as HoppRESTRequest
const possibleActiveTab = this.tabs.getTabRefWithSaveContext({
originLocation: "user-collection",
requestIndex,
folderPath: collectionID,
})
editRESTRequest(collectionID, requestIndex, updatedRequest)
if (possibleActiveTab) {
possibleActiveTab.value.document.request.name = updatedRequest.name
nextTick(() => {
possibleActiveTab.value.document.isDirty = false
})
}
return {
type: "ok",
data: {
providerID,
workspaceID,
collectionID,
requestID,
request: updatedRequest,
},
}
})
)
)
}
public saveRESTRequest(
public updateRESTRequest(
requestHandle: HandleRef<WorkspaceRequest>,
updatedRequest: HoppRESTRequest
): Promise<E.Either<unknown, HandleRef<WorkspaceRequest>>> {
): Promise<E.Either<unknown, HandleRef<boolean>>> {
if (
requestHandle.value.type !== "ok" ||
requestHandle.value.data.providerID !== this.providerID ||
@@ -762,8 +665,7 @@ export class PersonalWorkspaceProviderService
}
}
const { collectionID, providerID, requestID, workspaceID } =
requestHandle.value.data
const { collectionID, requestID } = requestHandle.value.data
try {
const requestIndex = parseInt(requestID)
@@ -784,13 +686,7 @@ export class PersonalWorkspaceProviderService
return {
type: "ok",
data: {
providerID,
workspaceID,
collectionID,
requestID,
request: updatedRequest,
},
data: true,
}
})
)
@@ -859,13 +755,13 @@ export class PersonalWorkspaceProviderService
}
public getRequestHandle(
parentCollHandle: HandleRef<WorkspaceCollection>,
workspaceHandle: HandleRef<Workspace>,
requestID: string
): Promise<E.Either<unknown, HandleRef<WorkspaceRequest>>> {
if (
parentCollHandle.value.type !== "ok" ||
parentCollHandle.value.data.providerID !== this.providerID ||
parentCollHandle.value.data.workspaceID !== "personal"
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))
}
@@ -874,9 +770,9 @@ export class PersonalWorkspaceProviderService
E.right(
computed(() => {
if (
parentCollHandle.value.type !== "ok" ||
parentCollHandle.value.data.providerID !== this.providerID ||
parentCollHandle.value.data.workspaceID !== "personal"
workspaceHandle.value.type !== "ok" ||
workspaceHandle.value.data.providerID !== this.providerID ||
workspaceHandle.value.data.workspaceID !== "personal"
) {
return {
type: "invalid" as const,
@@ -891,11 +787,12 @@ export class PersonalWorkspaceProviderService
}
}
const { collectionID, providerID, workspaceID } = parentCollHandle.value.data
const { providerID, workspaceID } = workspaceHandle.value.data
const collectionID = requestID.split("/").slice(0, -1).join("/")
const requestIndexPath = requestID.split("/").slice(-1)[0]
if (!collectionID || !requestIndexPath) {
if (!requestIndexPath) {
return {
type: "invalid" as const,
reason: "INVALID_REQUEST_HANDLE" as const,

View File

@@ -4,7 +4,7 @@ import { Ref } from "vue"
export type RESTCollectionViewCollection = {
collectionID: string
collection: HoppCollection
collection: HoppCollection // Should not store children including folders and requests
}
export type RESTCollectionViewRequest = {