|
|
|
// TODO: move to `shared/` and distinguish client and server (fix imports)
|
|
|
|
|
|
|
|
export function prependBaseUrl(route: string) {
|
|
|
|
return import.meta.env.BASE_URL + (route.startsWith('/') ? route.substring(1) : route)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const server = {
|
|
|
|
async get(url: string) {
|
|
|
|
const res = await fetch(prependBaseUrl(url), { credentials: 'include' })
|
|
|
|
return await res.json()
|
|
|
|
},
|
|
|
|
async post<T>(url: string, body?: T) {
|
|
|
|
const res = await fetch(prependBaseUrl(url), {
|
|
|
|
method: 'POST',
|
|
|
|
credentials: 'include',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
})
|
|
|
|
|
|
|
|
return await res.json()
|
|
|
|
},
|
|
|
|
async patch<T>(url: string, body?: T) {
|
|
|
|
const res = await fetch(prependBaseUrl(url), {
|
|
|
|
method: 'PATCH',
|
|
|
|
credentials: 'include',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
})
|
|
|
|
|
|
|
|
return await res.json()
|
|
|
|
},
|
|
|
|
async delete(url: string) {
|
|
|
|
const res = await fetch(prependBaseUrl(url), {
|
|
|
|
method: 'DELETE',
|
|
|
|
credentials: 'include',
|
|
|
|
})
|
|
|
|
|
|
|
|
return await res.json()
|
|
|
|
},
|
|
|
|
}
|