File size: 1,347 Bytes
5378afe | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | import { useAuth } from '@/composables/useAuth'
interface FetchOptions extends RequestInit {
params?: Record<string, string | number | boolean | undefined>;
}
export async function http(url: string, options: FetchOptions = {}) {
const { logout } = useAuth()
const headers = new Headers(options.headers)
// Handle Query Params
let fullUrl = url
if (options.params) {
const searchParams = new URLSearchParams()
Object.entries(options.params).forEach(([key, value]) => {
if (value !== undefined && value !== null) {
searchParams.append(key, String(value))
}
})
const queryString = searchParams.toString()
if (queryString) {
fullUrl += (url.includes('?') ? '&' : '?') + queryString
}
}
const config: RequestInit = {
...options,
headers,
}
const response = await fetch(fullUrl, config)
if (response.status === 401) {
// Basic Auth failed or session expired
logout()
// Optional: Redirect to login handled by router or state change
throw new Error('Unauthorized')
}
if (!response.ok) {
const errorData = await response.json().catch(() => ({}))
throw new Error(errorData.detail || `HTTP error! status: ${response.status}`)
}
// Handle 204 No Content
if (response.status === 204) {
return null
}
return response.json()
}
|