forked from MixMatch-Inc/Discoverly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.ts
More file actions
69 lines (58 loc) · 1.67 KB
/
api.ts
File metadata and controls
69 lines (58 loc) · 1.67 KB
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
import { useAuthStore } from "../store/useAuthStore"
const API_BASE_URL = process.env.EXPO_PUBLIC_API_BASE_URL ?? "http://localhost:5000"
const DEMO_USER_ID = process.env.EXPO_PUBLIC_DEMO_USER_ID ?? "660000000000000000000001"
export type DiscoverItem = {
id: string
restaurantId: string
name: string
description: string
price: number
imageUrl: string
restaurantName: string
distanceMeters: number
}
type DiscoverResponse = {
items: DiscoverItem[]
cursor: string | null
}
function getAuthHeaders() {
const token = useAuthStore.getState().token
return token ? { Authorization: `Bearer ${token}` } : {}
}
export async function fetchDiscoverFeed(params: {
longitude: number
latitude: number
cursor?: string | null
}) {
const query = new URLSearchParams({
longitude: String(params.longitude),
latitude: String(params.latitude),
})
if (params.cursor) {
query.set("cursor", params.cursor)
}
const response = await fetch(`${API_BASE_URL}/api/foods/discover?${query.toString()}`, {
headers: {
...getAuthHeaders(),
"Content-Type": "application/json",
},
})
if (!response.ok) {
throw new Error(`Failed to fetch discover feed: ${response.status}`)
}
return (await response.json()) as DiscoverResponse
}
export async function sendSwipe(params: { foodId: string; action: "like" | "pass" }) {
const response = await fetch(`${API_BASE_URL}/api/swipe`, {
method: "POST",
headers: {
...getAuthHeaders(),
"Content-Type": "application/json",
"x-user-id": DEMO_USER_ID,
},
body: JSON.stringify(params),
})
if (!response.ok) {
throw new Error(`Failed to send swipe: ${response.status}`)
}
}