mini-skamja/composables/api.ts

30 lines
896 B
TypeScript

import { useRuntimeConfig, useFetch, createError } from '#imports'
export function useApiFetch() {
const config = useRuntimeConfig()
const apiBase = config.public.apiBase
const prefix = config.public.apiPrefix
const fetchData = <T>(path: string, global = false) => {
const headers = new Headers()
headers.set('Referer', config.public.baseUrl)
return useFetch<T>(`${apiBase}/${global ? 'gb' : prefix}/${path}`, {
baseURL: config.public.baseUrl,
headers,
onResponseError({ response }) {
console.log(response.status)
console.log(response.url)
window.location.pathname = '/404'
throw createError({
statusCode: 404,
fatal: true
})
},
})
}
return {
fetchData
}
}