ofetch icon indicating copy to clipboard operation
ofetch copied to clipboard

stateful fetch client

Open pi0 opened this issue 1 year ago • 4 comments

Describe the feature

ofetch/$fetch same as native fetch is stateless by-design. This has safety advantages when ofetch is deeply integrated into tools such as Nitro and Nuxt and prevents global side effects and potential header leaks.

However, there are also good use cases of having stateful fetch instances:

  • Sharable headers
  • Cookie jar (#382)
  • Global interceptors such as auth

I think $fetch.withState() could be useful.

pi0 avatar Aug 28 '24 10:08 pi0

It's very good idea especially useful for testing API's

kamil-kubiczek avatar Sep 14 '24 18:09 kamil-kubiczek

Hi @pi0 What's the difference between this and ofetch.create ?

guyaumetremblay avatar Sep 26 '24 13:09 guyaumetremblay

@guyaumetremblay ofetch.create creates instance with immutable defaults. this version has mutable state (and most importantly working cookies, just like a browser tab)

pi0 avatar Sep 26 '24 14:09 pi0

Here from #456 and honestly would really love to have some way to merge interceptors instead of overriding them

voidtask avatar Mar 24 '25 07:03 voidtask