From d6a8e60239f9bf6c244284147d3aff3045d40ebf Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Thu, 8 Feb 2024 20:58:54 +0530 Subject: [PATCH] refactor: finalize API methods --- .../components/collections/SaveRequest.vue | 175 +++++++------- .../src/components/http/Request.vue | 8 +- .../components/new-collections/rest/index.vue | 96 ++------ .../src/services/new-workspace/index.ts | 65 ++--- .../src/services/new-workspace/provider.ts | 34 +-- .../providers/personal.workspace.ts | 225 +++++------------- .../src/services/new-workspace/view.ts | 2 +- 7 files changed, 209 insertions(+), 396 deletions(-) diff --git a/packages/hoppscotch-common/src/components/collections/SaveRequest.vue b/packages/hoppscotch-common/src/components/collections/SaveRequest.vue index 45e666995..58227014f 100644 --- a/packages/hoppscotch-common/src/components/collections/SaveRequest.vue +++ b/packages/hoppscotch-common/src/components/collections/SaveRequest.vue @@ -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({ diff --git a/packages/hoppscotch-common/src/components/http/Request.vue b/packages/hoppscotch-common/src/components/http/Request.vue index dacba5b3c..8c8dd7770 100644 --- a/packages/hoppscotch-common/src/components/http/Request.vue +++ b/packages/hoppscotch-common/src/components/http/Request.vue @@ -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 ) diff --git a/packages/hoppscotch-common/src/components/new-collections/rest/index.vue b/packages/hoppscotch-common/src/components/new-collections/rest/index.vue index d87ce082b..a4dd4c197 100644 --- a/packages/hoppscotch-common/src/components/new-collections/rest/index.vue +++ b/packages/hoppscotch-common/src/components/new-collections/rest/index.vue @@ -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, diff --git a/packages/hoppscotch-common/src/services/new-workspace/index.ts b/packages/hoppscotch-common/src/services/new-workspace/index.ts index a9bcf0705..5258f902f 100644 --- a/packages/hoppscotch-common/src/services/new-workspace/index.ts +++ b/packages/hoppscotch-common/src/services/new-workspace/index.ts @@ -139,7 +139,7 @@ export class NewWorkspaceService extends Service { } public async getRequestHandle( - parentCollHandle: HandleRef, + workspaceHandle: HandleRef, requestID: string ): Promise< E.Either< @@ -147,19 +147,19 @@ export class NewWorkspaceService extends Service { HandleRef > > { - 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 + HandleRef > > { if (collHandle.value.type === "invalid") { @@ -273,7 +273,7 @@ export class NewWorkspaceService extends Service { ): Promise< E.Either< WorkspaceError<"INVALID_HANDLE" | "INVALID_PROVIDER">, - HandleRef + HandleRef > > { if (collHandle.value.type === "invalid") { @@ -306,7 +306,7 @@ export class NewWorkspaceService extends Service { ): Promise< E.Either< WorkspaceError<"INVALID_HANDLE" | "INVALID_PROVIDER">, - HandleRef + HandleRef > > { if (collHandle.value.type === "invalid") { @@ -338,7 +338,7 @@ export class NewWorkspaceService extends Service { ): Promise< E.Either< WorkspaceError<"INVALID_HANDLE" | "INVALID_PROVIDER">, - HandleRef + HandleRef > > { if (collHandle.value.type === "invalid") { @@ -367,7 +367,7 @@ export class NewWorkspaceService extends Service { ): Promise< E.Either< WorkspaceError<"INVALID_HANDLE" | "INVALID_PROVIDER">, - HandleRef + HandleRef > > { if (parentCollHandle.value.type === "invalid") { @@ -393,7 +393,8 @@ export class NewWorkspaceService extends Service { public async createRESTRequest( parentCollHandle: HandleRef, - 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 + HandleRef > > { if (requestHandle.value.type === "invalid") { @@ -453,43 +455,13 @@ export class NewWorkspaceService extends Service { return E.right(result.right) } - public async editRESTRequest( - requestHandle: HandleRef, - newRequestName: string - ): Promise< - E.Either< - WorkspaceError<"INVALID_HANDLE" | "INVALID_PROVIDER">, - HandleRef - > - > { - 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, updatedRequest: HoppRESTRequest ): Promise< E.Either< WorkspaceError<"INVALID_HANDLE" | "INVALID_PROVIDER">, - HandleRef + HandleRef > > { 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 }) diff --git a/packages/hoppscotch-common/src/services/new-workspace/provider.ts b/packages/hoppscotch-common/src/services/new-workspace/provider.ts index 309c1d923..635df7148 100644 --- a/packages/hoppscotch-common/src/services/new-workspace/provider.ts +++ b/packages/hoppscotch-common/src/services/new-workspace/provider.ts @@ -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>> getRequestHandle( - parentCollHandle: HandleRef, + workspaceHandle: HandleRef, requestID: string ): Promise>> @@ -38,6 +42,9 @@ export interface WorkspaceProvider { getRESTCollectionChildrenView( collectionHandle: HandleRef ): Promise>> + // getRESTCollectionAuthHeaders( + // collectionHandle: HandleRef + // ): Promise>> createRESTRootCollection( workspaceHandle: HandleRef, @@ -50,34 +57,31 @@ export interface WorkspaceProvider { editRESTRootCollection( collHandle: HandleRef, newCollectionName: string - ): Promise>> + ): Promise>> editRESTChildCollection( parentCollHandle: HandleRef, newCollectionName: string - ): Promise>> + ): Promise>> editRESTCollectionProperties( parentCollHandle: HandleRef, updatedCollProps: UpdatedCollectionProperties - ): Promise>> + ): Promise>> removeRESTRootCollection( collHandle: HandleRef - ): Promise>> + ): Promise>> removeRESTChildCollection( parentCollHandle: HandleRef - ): Promise>> + ): Promise>> createRESTRequest( parentCollHandle: HandleRef, - requestName: string + requestName: string, + openInNewTab: boolean ): Promise>> removeRESTRequest( requestHandle: HandleRef - ): Promise>> - editRESTRequest( - requestHandle: HandleRef, - newRequestName: string - ): Promise>> - saveRESTRequest( + ): Promise>> + updateRESTRequest( requestHandle: HandleRef, updatedRequest: HoppRESTRequest - ): Promise>> + ): Promise>> } diff --git a/packages/hoppscotch-common/src/services/new-workspace/providers/personal.workspace.ts b/packages/hoppscotch-common/src/services/new-workspace/providers/personal.workspace.ts index 31379a230..04a0398ae 100644 --- a/packages/hoppscotch-common/src/services/new-workspace/providers/personal.workspace.ts +++ b/packages/hoppscotch-common/src/services/new-workspace/providers/personal.workspace.ts @@ -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, newCollectionName: string - ): Promise>> { + ): Promise>> { 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, newCollectionName: string - ): Promise>> { + ): Promise>> { 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, updatedCollProps: UpdatedCollectionProperties - ): Promise>> { + ): Promise>> { 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 - ): Promise>> { + ): Promise>> { 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 - ): Promise>> { + ): Promise>> { 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, - requestName: string + requestName: string, + openInNewTab: boolean ): Promise>> { 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 - ): Promise>> { + ): Promise>> { 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, - newRequestName: string - ): Promise>> { - 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, updatedRequest: HoppRESTRequest - ): Promise>> { + ): Promise>> { 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, + workspaceHandle: HandleRef, requestID: string ): Promise>> { 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, diff --git a/packages/hoppscotch-common/src/services/new-workspace/view.ts b/packages/hoppscotch-common/src/services/new-workspace/view.ts index d54ca7f8e..d4d476a49 100644 --- a/packages/hoppscotch-common/src/services/new-workspace/view.ts +++ b/packages/hoppscotch-common/src/services/new-workspace/view.ts @@ -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 = {