unjs / ofetch

😱 A better fetch API. Works on node, browser and workers.
MIT License
4k stars 125 forks source link

stateful fetch client #435

Open pi0 opened 1 month ago

pi0 commented 1 month ago

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:

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

YoungKamilson commented 1 week ago

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

guyaumetremblay commented 1 day ago

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

pi0 commented 1 day ago

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