nuxt-open-fetch
nuxt-open-fetch copied to clipboard
Add $clientFetch.raw
The original $fetch has $fetch.raw which can be used to simplify handling errors.
https://github.com/unjs/ofetch#-access-to-raw-response
The $clientFetch doesn't have the corresponding .raw. Can we have it?
Similarly, $fetch.create will be very useful, such as for creating authenticated fetcher.
Unfortunately, currently we can't use $fetch.create, because we need to replace path placeholders with values. We can do this in the onRequest interceptor, but if the user also uses the onRequest interceptor, our interceptor will be lost.
There is an issue to improve the ofetch API - https://github.com/unjs/ofetch/issues/319.
But you can use custom plugin and $fetch.create if overriding onRequest is not a problem:
export default defineNuxtPlugin(() => {
const { public: { openFetch: clients }} = useRuntimeConfig()
return {
provide: Object.fromEntries(Object.entries(clients).map(([name, options]) => [
`${name}Fetch`,
$fetch.create({
...options,
onRequest: openFetchRequestInterceptor
})
]))
}
})
Hey! I also found myself wanting to use a $fetch.raw equivalent today.