nuxt-open-fetch icon indicating copy to clipboard operation
nuxt-open-fetch copied to clipboard

Add $clientFetch.raw

Open IlyaSemenov opened this issue 2 years ago • 4 comments

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?

IlyaSemenov avatar Dec 06 '23 07:12 IlyaSemenov

Similarly, $fetch.create will be very useful, such as for creating authenticated fetcher.

IlyaSemenov avatar Dec 11 '23 04:12 IlyaSemenov

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.

enkot avatar Dec 11 '23 20:12 enkot

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
      })
    ]))
  }
})

enkot avatar Dec 11 '23 20:12 enkot

Hey! I also found myself wanting to use a $fetch.raw equivalent today.

jonkri avatar Jul 26 '24 15:07 jonkri