first commit
This commit is contained in:
27
src/shared/api/base.ts
Normal file
27
src/shared/api/base.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { API_URL } from '@shared/config/env'
|
||||
import { getCsrfToken } from './csrf'
|
||||
|
||||
async function request<T>(path: string, options: RequestInit = {}): Promise<T> {
|
||||
const token = await getCsrfToken()
|
||||
|
||||
const res = await fetch(`${API_URL}${path}`, {
|
||||
...options,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-CSRF-Token': token,
|
||||
...options.headers,
|
||||
},
|
||||
})
|
||||
|
||||
const data = await res.json()
|
||||
|
||||
if (!res.ok) throw data
|
||||
|
||||
return data as T
|
||||
}
|
||||
|
||||
export const api = {
|
||||
get: <T>(path: string) => request<T>(path),
|
||||
post: <T>(path: string, body: unknown) =>
|
||||
request<T>(path, { method: 'POST', body: JSON.stringify(body) }),
|
||||
}
|
||||
Reference in New Issue
Block a user