fix: fix history not writing when formdata is present
This commit is contained in:
@@ -11,6 +11,7 @@ import {
|
||||
query,
|
||||
updateDoc,
|
||||
} from "firebase/firestore"
|
||||
import { FormDataKeyValue } from "../types/HoppRESTRequest"
|
||||
import { currentUser$ } from "./auth"
|
||||
import { settingsStore } from "~/newstore/settings"
|
||||
import {
|
||||
@@ -47,12 +48,36 @@ let loadedRESTHistory = false
|
||||
*/
|
||||
let loadedGraphqlHistory = false
|
||||
|
||||
async function writeHistory(entry: any, col: HistoryFBCollections) {
|
||||
const purgeFormDataFromRequest = (req: RESTHistoryEntry): RESTHistoryEntry => {
|
||||
if (req.request.body.contentType !== "multipart/form-data") return req
|
||||
|
||||
req.request.body.body = req.request.body.body.map<FormDataKeyValue>(
|
||||
(formData) => {
|
||||
if (!formData.isFile) return formData
|
||||
|
||||
return {
|
||||
active: formData.active,
|
||||
isFile: false, // Something we can do to keep the status ?
|
||||
key: formData.key,
|
||||
value: "",
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
return req
|
||||
}
|
||||
|
||||
async function writeHistory(
|
||||
entry: RESTHistoryEntry,
|
||||
col: HistoryFBCollections
|
||||
) {
|
||||
const processedEntry = purgeFormDataFromRequest(entry)
|
||||
|
||||
if (currentUser$.value == null)
|
||||
throw new Error("User not logged in to sync history")
|
||||
|
||||
const hs = {
|
||||
...entry,
|
||||
...processedEntry,
|
||||
updatedOn: new Date(),
|
||||
}
|
||||
|
||||
@@ -67,7 +92,10 @@ async function writeHistory(entry: any, col: HistoryFBCollections) {
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteHistory(entry: any, col: HistoryFBCollections) {
|
||||
async function deleteHistory(
|
||||
entry: RESTHistoryEntry & { id: string },
|
||||
col: HistoryFBCollections
|
||||
) {
|
||||
if (currentUser$.value == null)
|
||||
throw new Error("User not logged in to delete history")
|
||||
|
||||
@@ -89,7 +117,7 @@ async function clearHistory(col: HistoryFBCollections) {
|
||||
collection(getFirestore(), "users", currentUser$.value.uid, col)
|
||||
)
|
||||
|
||||
await Promise.all(docs.map((e) => deleteHistory(e, col)))
|
||||
await Promise.all(docs.map((e) => deleteHistory(e as any, col)))
|
||||
}
|
||||
|
||||
async function toggleStar(entry: any, col: HistoryFBCollections) {
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
Subscription,
|
||||
} from "rxjs"
|
||||
import { doc, getDoc, getFirestore, setDoc } from "firebase/firestore"
|
||||
import cloneDeep from "lodash/cloneDeep"
|
||||
import {
|
||||
HoppRESTRequest,
|
||||
translateToNewRequest,
|
||||
@@ -22,10 +23,22 @@ import { restRequest$ } from "~/newstore/RESTSession"
|
||||
* @param request The request to write to the request sync
|
||||
*/
|
||||
function writeCurrentRequest(user: HoppUser, request: HoppRESTRequest) {
|
||||
return setDoc(
|
||||
doc(getFirestore(), "users", user.uid, "requests", "rest"),
|
||||
request
|
||||
)
|
||||
const req = cloneDeep(request)
|
||||
|
||||
// Remove FormData entries because those can't be stored on Firestore
|
||||
if (req.body.contentType === "multipart/form-data") {
|
||||
req.body.body = req.body.body.map((formData) => {
|
||||
if (!formData.isFile) return formData
|
||||
|
||||
return {
|
||||
active: formData.active,
|
||||
isFile: false,
|
||||
key: formData.key,
|
||||
value: "",
|
||||
}
|
||||
})
|
||||
}
|
||||
return setDoc(doc(getFirestore(), "users", user.uid, "requests", "rest"), req)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -293,7 +293,18 @@ completedRESTResponse$.subscribe((res) => {
|
||||
|
||||
addRESTHistoryEntry(
|
||||
makeRESTHistoryEntry({
|
||||
request: res.req,
|
||||
request: {
|
||||
auth: res.req.auth,
|
||||
body: res.req.body,
|
||||
endpoint: res.req.endpoint,
|
||||
headers: res.req.headers,
|
||||
method: res.req.method,
|
||||
name: res.req.name,
|
||||
params: res.req.params,
|
||||
preRequestScript: res.req.preRequestScript,
|
||||
testScript: res.req.testScript,
|
||||
v: res.req.v,
|
||||
},
|
||||
responseMeta: {
|
||||
duration: res.meta.responseDuration,
|
||||
statusCode: res.statusCode,
|
||||
|
||||
Reference in New Issue
Block a user