fix: fix history not writing when formdata is present

This commit is contained in:
Andrew Bastin
2021-11-11 18:36:26 +05:30
parent 521a96bffb
commit 25878b9bb1
3 changed files with 61 additions and 9 deletions

View File

@@ -11,6 +11,7 @@ import {
query, query,
updateDoc, updateDoc,
} from "firebase/firestore" } from "firebase/firestore"
import { FormDataKeyValue } from "../types/HoppRESTRequest"
import { currentUser$ } from "./auth" import { currentUser$ } from "./auth"
import { settingsStore } from "~/newstore/settings" import { settingsStore } from "~/newstore/settings"
import { import {
@@ -47,12 +48,36 @@ let loadedRESTHistory = false
*/ */
let loadedGraphqlHistory = 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) if (currentUser$.value == null)
throw new Error("User not logged in to sync history") throw new Error("User not logged in to sync history")
const hs = { const hs = {
...entry, ...processedEntry,
updatedOn: new Date(), 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) if (currentUser$.value == null)
throw new Error("User not logged in to delete history") 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) 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) { async function toggleStar(entry: any, col: HistoryFBCollections) {

View File

@@ -8,6 +8,7 @@ import {
Subscription, Subscription,
} from "rxjs" } from "rxjs"
import { doc, getDoc, getFirestore, setDoc } from "firebase/firestore" import { doc, getDoc, getFirestore, setDoc } from "firebase/firestore"
import cloneDeep from "lodash/cloneDeep"
import { import {
HoppRESTRequest, HoppRESTRequest,
translateToNewRequest, translateToNewRequest,
@@ -22,10 +23,22 @@ import { restRequest$ } from "~/newstore/RESTSession"
* @param request The request to write to the request sync * @param request The request to write to the request sync
*/ */
function writeCurrentRequest(user: HoppUser, request: HoppRESTRequest) { function writeCurrentRequest(user: HoppUser, request: HoppRESTRequest) {
return setDoc( const req = cloneDeep(request)
doc(getFirestore(), "users", user.uid, "requests", "rest"),
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)
} }
/** /**

View File

@@ -293,7 +293,18 @@ completedRESTResponse$.subscribe((res) => {
addRESTHistoryEntry( addRESTHistoryEntry(
makeRESTHistoryEntry({ 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: { responseMeta: {
duration: res.meta.responseDuration, duration: res.meta.responseDuration,
statusCode: res.statusCode, statusCode: res.statusCode,