109 lines
2.8 KiB
TypeScript
109 lines
2.8 KiB
TypeScript
export const useApi = () => {
|
|
const config = useRuntimeConfig()
|
|
const router = useRouter()
|
|
const { toast } = useToast()
|
|
const { isLoggedIn, logout } = useAuth()
|
|
|
|
// Use current domain for API calls (same subdomain routing)
|
|
const getApiBaseUrl = () => {
|
|
if (import.meta.client) {
|
|
// In browser, use current hostname but with port 3000 for API
|
|
const currentHost = window.location.hostname
|
|
const protocol = window.location.protocol
|
|
return `${protocol}//${currentHost}:3000`
|
|
}
|
|
// Fallback for SSR
|
|
return config.public.apiBaseUrl
|
|
}
|
|
|
|
const getHeaders = () => {
|
|
const headers: Record<string, string> = {
|
|
'Content-Type': 'application/json',
|
|
}
|
|
|
|
// Add tenant ID from localStorage or state
|
|
if (import.meta.client) {
|
|
const tenantId = localStorage.getItem('tenantId')
|
|
if (tenantId) {
|
|
headers['x-tenant-id'] = tenantId
|
|
}
|
|
|
|
const token = localStorage.getItem('token')
|
|
if (token) {
|
|
headers['Authorization'] = `Bearer ${token}`
|
|
}
|
|
}
|
|
|
|
return headers
|
|
}
|
|
|
|
const handleResponse = async (response: Response) => {
|
|
if (response.status === 401) {
|
|
// Unauthorized - not authenticated
|
|
if (import.meta.client) {
|
|
logout()
|
|
toast.error('Your session has expired. Please login again.')
|
|
router.push('/login')
|
|
}
|
|
throw new Error('Unauthorized')
|
|
}
|
|
|
|
if (response.status === 403) {
|
|
// Forbidden - not authorized
|
|
if (import.meta.client) {
|
|
toast.error('You do not have permission to perform this action.')
|
|
// Redirect to home if logged in, otherwise to login
|
|
if (isLoggedIn()) {
|
|
router.push('/')
|
|
} else {
|
|
router.push('/login')
|
|
}
|
|
}
|
|
throw new Error('Forbidden')
|
|
}
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP error! status: ${response.status}`)
|
|
}
|
|
|
|
return response.json()
|
|
}
|
|
|
|
const api = {
|
|
async get(path: string) {
|
|
const response = await fetch(`${getApiBaseUrl()}/api${path}`, {
|
|
headers: getHeaders(),
|
|
})
|
|
return handleResponse(response)
|
|
},
|
|
|
|
async post(path: string, data: any) {
|
|
const response = await fetch(`${getApiBaseUrl()}/api${path}`, {
|
|
method: 'POST',
|
|
headers: getHeaders(),
|
|
body: JSON.stringify(data),
|
|
})
|
|
return handleResponse(response)
|
|
},
|
|
|
|
async put(path: string, data: any) {
|
|
const response = await fetch(`${getApiBaseUrl()}/api${path}`, {
|
|
method: 'PUT',
|
|
headers: getHeaders(),
|
|
body: JSON.stringify(data),
|
|
})
|
|
return handleResponse(response)
|
|
},
|
|
|
|
async delete(path: string) {
|
|
const response = await fetch(`${getApiBaseUrl()}/api${path}`, {
|
|
method: 'DELETE',
|
|
headers: getHeaders(),
|
|
})
|
|
return handleResponse(response)
|
|
},
|
|
}
|
|
|
|
return { api }
|
|
}
|