It's 2025, and Axios has supported fetch since v1.7, making it just another fetch wrapper.
The Axios package is 35.6KB/Gzip 13.5KB—too big as a ...
For further actions, you may consider blocking this person and/or reporting abuse
Why people complicate themselves? The
fetch()
function is all you need 99% of the time. 1% might need upload/download progress, and that's about it. Interceptors are the worst monster ever. I immediately reject any wrapper with this concept when I can simply do a data-fetching function of my own in a couple lines of code.Thank you for the comment!
Fetch for a simple use case is okay.
But there are different use cases, which is why people choose a fetch wrapper or build their own.
Because A fetch wrapper is used for:
Customization
Yet somehow people convince themselves that interceptors are "the way to go". Compare this code with the code require for an interceptor.
Error handling
Rookie mistake. Error handling is only done at the point where the user can be informed. Middle-layer error handling is a rookie mistake.
Reusability
The
myFetch
function is reusable.Abstraction
All packages abstract the response incorrectly because they all do:
This anit-pattern is exchanging branching for catching.
Extra Features
The minority of the people require extra features. I agree, they are sometimes needed.
So all in all, they are all superflous and badly done.
Wow, indeed it's simple!
I did this before—I have my own fetch wrapper in the project codebase.
After I tried Axios, it was so good that I never used my own fetch wrapper like
myFetch(url, options)
, you need addqs
module to support nested query encoding which is another ~11KB in size.But one year ago, I encountered some issues running Axios in Next.js middleware and Cloudflare Worker runtime. that's why I wrote my axios-like API fetch wrapper, and share it with people, and fixing issues, add unit testing, make it stronger and stable.
This is like saying use Date() instead of momentjs(or any other momentjs variations).
Yes, for simple use cases,
fetch
is sufficient, just like usingnew Date()
. However, for date parsing, formatting, and validation, we needDate()
wrapper libraries such asdayjs
,date-fns
, ormoment.js
.That's why a fetch wrapper is beneficial—whether from the community or one we develop ourselves.
Using
cloc
,xior
is over 1,700 LOC:How much of this is needed on average?
This is the same story for
axios
,ky
, etc.Yes, a wrapper is beneficial. Just not these wrappers.
Have a look at this code from xior's README:
This is a response interceptor. Look how evident the anti-pattern is here: The NPM package exchanged, using an IF statement, a non-OK response status for a thrown error. This has forced the above example, which is the consumer's code to invert this: The consumer now has to use the callback, examin the error using another IF, just to do what you should have done in the first place: Branch with
response.ok
orresponse.status
.This is so evident. How come people keep defending what has no defense? This is madness.
Yes, Xior includes typescript types, plugins and many other features, which is why there are so many lines of code. And even more lines with the unit tests if you count the tests/ folder.
The response interceptor example code shows people what properties they can get from the function.
I appreciate your comment on this post and the advice. Sorry that the fetch wrapper is not working for you.
I just counted the LOC in /src using the command
perl ..\cloc-dev.pl .\src
.The best NPM packages don't add unnecessary overhead and are focused on solving one problem. The main problem with all popular fetch wrappers is the throw-instead-of-branching anti-pattern, which creates overhead where none should exist. The next big problem is the interceptors, where consumers of the package end up writing more code than if no wrapper was used.
What's meaning of
throw-instead-of-branching anti-pattern
, can you give examples?For interceptors, it's idea from Axios which I think is nice, you can use many request or response interceptors:
And custom our own plugin with
http.plugins.use
:I explained it in another response: The package uses an IF statement to throw an error. Then the consumer is forced to revert what the package did, either within a callback, or in a catch block and then use an extra IF to determine things like in the example I took from
xior
's README, which was to test for a 401 response to delete the token.A good wrapper would instead forward the
ok
andstatus
properties and let the user decide what is wanted in special cases, with just one IF statement, not 2.Also, just because
axios
does it doesn't mean it is OK. It is my professional opinion thataxios
,ky
andxior
all do this incorrectly, no matter how many years this has gone unnoticed.You can actually access
ok
andstatus
from the response:For the "if throw" problem, good catch! Do you think adding an option like a
checkResponse
function to let the user determine whether an error should be thrown is a good choice? Or you talking something similar like JavaScript's Safe Assignment Operator Proposal?I don't propose anything except discarding all these packages as their flaws are plural, not singular. I created my own wrapper library that does the job properly, and for the cherry on top, allows you to type the response body for all possible HTTP status codes. You can call it "doctor fetch". [dr-fetch](github.com/WJSoftware/dr-fetch]. This wrapper takes a completely different approach to the problem.
Would you like to give any reasons with logical argumentation? Because I don't follow.
Axios existed before fetch was a thing, now that fetch is a thing why load a bunch of extra code to do the same thing?
When there is a new spec that can do all the things moment can do devs should stop using moment. There is already DateTimeFormat and RelativeTimeFormat which get you part way there.
It's 2025 and still people thinking of using something else and doing comparison 🙄.
I'm a newbie ( 1 year old ), and still knows that no matter of others fetch is all you need.