request.ts (hoppscotch-2.0.0) | : | request.ts (hoppscotch-2.1.0) | ||
---|---|---|---|---|
import firebase from "firebase/app" | ||||
import "firebase/firestore" | ||||
import { | import { | |||
audit, | audit, | |||
combineLatest, | combineLatest, | |||
distinctUntilChanged, | distinctUntilChanged, | |||
EMPTY, | EMPTY, | |||
from, | from, | |||
map, | map, | |||
Subscription, | Subscription, | |||
} from "rxjs" | } from "rxjs" | |||
import { doc, getDoc, getFirestore, setDoc } from "firebase/firestore" | ||||
import { | import { | |||
HoppRESTRequest, | HoppRESTRequest, | |||
translateToNewRequest, | translateToNewRequest, | |||
} from "../types/HoppRESTRequest" | } from "../types/HoppRESTRequest" | |||
import { currentUser$, HoppUser } from "./auth" | import { currentUser$, HoppUser } from "./auth" | |||
import { restRequest$ } from "~/newstore/RESTSession" | import { restRequest$ } from "~/newstore/RESTSession" | |||
/** | /** | |||
* Writes a request to a user's firestore sync | * Writes a request to a user's firestore sync | |||
* | * | |||
* @param user The user to write to | * @param user The user to write to | |||
* @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 firebase | return setDoc( | |||
.firestore() | doc(getFirestore(), "users", user.uid, "requests", "rest"), | |||
.collection("users") | request | |||
.doc(user.uid) | ) | |||
.collection("requests") | ||||
.doc("rest") | ||||
.set(request) | ||||
} | } | |||
/** | /** | |||
* Loads the synced request from the firestore sync | * Loads the synced request from the firestore sync | |||
* | * | |||
* @returns Fetched request object if exists else null | * @returns Fetched request object if exists else null | |||
*/ | */ | |||
export async function loadRequestFromSync(): Promise<HoppRESTRequest | null> { | export async function loadRequestFromSync(): Promise<HoppRESTRequest | null> { | |||
const currentUser = currentUser$.value | const currentUser = currentUser$.value | |||
if (!currentUser) | if (!currentUser) | |||
throw new Error("Cannot load request from sync without login") | throw new Error("Cannot load request from sync without login") | |||
const doc = await firebase | const fbDoc = await getDoc( | |||
.firestore() | doc(getFirestore(), "users", currentUser.uid, "requests", "rest") | |||
.collection("users") | ) | |||
.doc(currentUser.uid) | ||||
.collection("requests") | ||||
.doc("rest") | ||||
.get() | ||||
const data = doc.data() | const data = fbDoc.data() | |||
if (!data) return null | if (!data) return null | |||
else return translateToNewRequest(data) | else return translateToNewRequest(data) | |||
} | } | |||
/** | /** | |||
* Performs sync of the REST Request session with Firestore. | * Performs sync of the REST Request session with Firestore. | |||
* | * | |||
* @returns A subscription to the sync observable stream. | * @returns A subscription to the sync observable stream. | |||
* Unsubscribe to stop syncing. | * Unsubscribe to stop syncing. | |||
End of changes. 5 change blocks. | ||||
17 lines changed or deleted | 9 lines changed or added |