refactor: finalize API methods
This commit is contained in:
@@ -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({
|
||||
|
||||
@@ -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
|
||||
)
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user