feat: rest revamp (#2918)

Co-authored-by: Liyas Thomas <liyascthomas@gmail.com>
Co-authored-by: Nivedin <53208152+nivedin@users.noreply.github.com>
Co-authored-by: Andrew Bastin <andrewbastin.k@gmail.com>
This commit is contained in:
Anwarul Islam
2023-03-31 01:15:42 +06:00
committed by GitHub
parent dbb45e7253
commit defece95fc
63 changed files with 2262 additions and 1924 deletions

View File

@@ -4,6 +4,7 @@ import { def as envDef } from "./environments"
import { def as collectionsDef } from "./collections"
import { def as settingsDef } from "./settings"
import { def as historyDef } from "./history"
import { def as tabStateDef } from "./tab"
createHoppApp("#app", {
auth: authDef,
@@ -12,5 +13,6 @@ createHoppApp("#app", {
collections: collectionsDef,
settings: settingsDef,
history: historyDef,
tabState: tabStateDef,
},
})

View File

@@ -0,0 +1,48 @@
import { PersistableRESTTabState } from "@hoppscotch/common/helpers/rest/tab"
import { HoppUser } from "@hoppscotch/common/platform/auth"
import { TabStatePlatformDef } from "@hoppscotch/common/platform/tab"
import { doc, getDoc, getFirestore, setDoc } from "firebase/firestore"
import { def as platformAuth } from "./firebase/auth"
/**
* Writes tab state to a user's firestore sync
*
* @param persistableTabState The tab state to write to the request sync
*/
function writeCurrentTabState(
user: HoppUser,
persistableTabState: PersistableRESTTabState
) {
// Remove FormData entries because those can't be stored on Firestore ?
return setDoc(
doc(getFirestore(), "users", user.uid, "requests", "tab-state"),
persistableTabState
)
}
/**
* Loads the synced tab state from the firestore sync
*
* @returns Fetched tab state object if exists else null
*/
async function loadTabStateFromSync(): Promise<PersistableRESTTabState | null> {
const currentUser = platformAuth.getCurrentUser()
if (!currentUser)
throw new Error("Cannot load request from sync without login")
const fbDoc = await getDoc(
doc(getFirestore(), "users", currentUser.uid, "requests", "tab-state")
)
const data = fbDoc.data()
if (!data) return null
else return data as PersistableRESTTabState
}
export const def: TabStatePlatformDef = {
loadTabStateFromSync,
writeCurrentTabState,
}