ifms-printer-manager/web/src/api.ts
Douglas Barone 72ad91ff00 Fix env
2023-06-29 14:14:20 -04:00

28 lines
569 B
TypeScript

const BASE_URL = import.meta.env.VITE_BASE_URL || 'http://localhost:8000/api/'
export async function api<T>(endpoint: string, options: any): Promise<T> {
const token = localStorage.getItem('token')
if (token) {
options.headers = {
...options.headers,
Authorization: token
}
}
options.headers = {
...options.headers,
'Content-Type': 'application/json'
}
const response = await fetch(BASE_URL + endpoint, options)
const json = await response.json()
if (response.ok) {
return json
}
throw new Error(json.error)
}