-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
80 lines (64 loc) · 1.7 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { initializeApp } from "firebase/app";
import {
getFirestore,
collection,
doc,
getDocs,
getDoc,
query,
where,
documentId
} from "firebase/firestore/lite"
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyCLrhaXF8c2qmn8YR8SO7Ht2EnZ0T9OY0U",
authDomain: "vanlife-5aae0.firebaseapp.com",
projectId: "vanlife-5aae0",
storageBucket: "vanlife-5aae0.appspot.com",
messagingSenderId: "890782266497",
appId: "1:890782266497:web:8aea40991f5397e6c88980"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig)
const db = getFirestore(app)
// Refactoring the fetching functions below
const vansCollectionRef = collection(db, "vans")
export async function getVans() {
const snapshot = await getDocs(vansCollectionRef)
const vans = snapshot.docs.map(doc => ({
...doc.data(),
id: doc.id
}))
return vans
}
export async function getVan(id) {
const docRef = doc(db, "vans", id)
const snapshot = await getDoc(docRef)
return {
...snapshot.data(),
id: snapshot.id
}
}
export async function getHostVans() {
const q = query(vansCollectionRef, where("hostId", "==", 123))
const snapshot = await getDocs(q)
const vans = snapshot.docs.map(doc => ({
...doc.data(),
id: doc.id
}))
return vans
}
export async function loginUser(creds) {
const res = await fetch("/api/login",
{ method: "post", body: JSON.stringify(creds) }
)
const data = await res.json()
if (!res.ok) {
throw {
message: data.message,
statusText: res.statusText,
status: res.status
}
}
return data
}