created frontend with login, room listing and creation, and message page with all features
This commit is contained in:
27
src/api/client.ts
Normal file
27
src/api/client.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { useAuthStore } from '../stores/auth'
|
||||
|
||||
const BASE_URL = 'http://localhost:8080'
|
||||
|
||||
export async function apiFetch<T>(
|
||||
path: string,
|
||||
options: RequestInit = {}
|
||||
): Promise<T> {
|
||||
const auth = useAuthStore()
|
||||
|
||||
const res = await fetch(`${BASE_URL}${path}`, {
|
||||
...options,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...(auth.token ? { Authorization: `Bearer ${auth.token}` } : {}),
|
||||
...options.headers,
|
||||
},
|
||||
})
|
||||
|
||||
if (!res.ok) {
|
||||
const text = await res.text()
|
||||
throw new Error(text || res.statusText)
|
||||
}
|
||||
|
||||
return res.json()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user