DEV Community

Axios vs. Fetch: Which Fetch Wrapper Should I Choose in 2025?

suhaotian on March 10, 2025

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 ...
Collapse
 
webjose profile image
José Pablo Ramírez Vargas

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.

Collapse
 
shash7 profile image
Shash

This is like saying use Date() instead of momentjs(or any other momentjs variations).

Collapse
 
suhaotian profile image
suhaotian • Edited

Yes, for simple use cases, fetch is sufficient, just like using new Date(). However, for date parsing, formatting, and validation, we need Date() wrapper libraries such as dayjs, date-fns, or moment.js.

That's why a fetch wrapper is beneficial—whether from the community or one we develop ourselves.

Thread Thread
 
webjose profile image
José Pablo Ramírez Vargas

Using cloc, xior is over 1,700 LOC:

github.com/AlDanial/cloc v 2.05  T=0.12 s (164.8 files/s, 17136.3 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
TypeScript                      20            190            153           1737
-------------------------------------------------------------------------------
SUM:                            20            190            153           1737
-------------------------------------------------------------------------------
Enter fullscreen mode Exit fullscreen mode

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:

http.interceptors.response.use(
  (result) => {
    const { data, request: config, response: originalResponse } = result;
    return result;
  },
  async (error) => {
    if (error instanceof TypeError) {
      console.log(`Request error:`, error);
    }
    if (error?.response?.status === 401) {
      localStorage.removeItem('REQUEST_TOKEN');
    }
    return Promise.reject(error);
  }
);
Enter fullscreen mode Exit fullscreen mode

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 or response.status.

This is so evident. How come people keep defending what has no defense? This is madness.

Thread Thread
 
suhaotian profile image
suhaotian • Edited

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.

Thread Thread
 
webjose profile image
José Pablo Ramírez Vargas

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.

Collapse
 
drew_e29d5b0152adc2 profile image
Drew Riley

When fetch was introduced it eliminated the need for a library such as axios.

Millions of API calls, oauth, bearer token, various cors requirements, etah, cookies, caching proxies, get, post, patch everything in-between.

Fetch has handled it all.

Collapse
 
decker67 profile image
decker

fetch API works for me, nothing more is needed. The more dependencies the worse the things get. Simply my opinion.

Collapse
 
suhaotian profile image
suhaotian • Edited

Cool! And That's True, The more dependencies the worse the things get. That's the core idea of this lib with unit tests and keep on core functions.

Collapse
 
himanshu_code profile image
Himanshu Sorathiya

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.

Collapse
 
suhaotian profile image
suhaotian

Trust me, you should be proud to share your work with the public, no matter how funny it looks to others.