2023-04-26 11:28:19 +00:00
|
|
|
import type { UseFetchOptions } from 'nuxt/app'
|
|
|
|
import { defu } from 'defu'
|
|
|
|
|
|
|
|
export function useCustomFetch<T> (url: string, options: UseFetchOptions<T> = {}) {
|
|
|
|
const userAuth = useCookie('token')
|
|
|
|
const config = useRuntimeConfig()
|
|
|
|
|
|
|
|
const defaults: UseFetchOptions<T> = {
|
|
|
|
baseURL: config.baseUrl ?? 'https://api.nuxtjs.dev',
|
|
|
|
// cache request
|
|
|
|
key: url,
|
|
|
|
|
|
|
|
// set user token if connected
|
|
|
|
headers: userAuth.value
|
|
|
|
? { Authorization: `Bearer ${userAuth.value}` }
|
|
|
|
: {},
|
|
|
|
|
2023-05-01 12:41:17 +00:00
|
|
|
onResponse (_ctx) {
|
|
|
|
// _ctx.response._data = new myBusinessResponse(_ctx.response._data)
|
2023-04-26 11:28:19 +00:00
|
|
|
},
|
|
|
|
|
2023-05-01 12:41:17 +00:00
|
|
|
onResponseError (_ctx) {
|
|
|
|
// throw new myBusinessError()
|
2023-04-26 11:28:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// for nice deep defaults, please use unjs/defu
|
2023-05-16 22:06:28 +00:00
|
|
|
const params = defu(options, defaults)
|
2023-04-26 11:28:19 +00:00
|
|
|
|
|
|
|
return useFetch(url, params)
|
|
|
|
}
|