axios
Replacing Axios axios.get()
Vanilla JavaScript
const axios = {
get: async (url, config = {}) => {
const res = await fetch(url, { ...config, method: 'GET' });
if (!res.ok) throw new Error(`HTTP Error: ${res.status}`);
return { data: await res.json(), status: res.status, headers: res.headers };
},
post: async (url, data, config = {}) => {
const res = await fetch(url, {
...config,
method: 'POST',
body: JSON.stringify(data),
headers: { 'Content-Type': 'application/json', ...config.headers }
});
if (!res.ok) throw new Error(`HTTP Error: ${res.status}`);
return { data: await res.json(), status: res.status, headers: res.headers };
}
};
// Usage:
// const { data } = await axios.get('/api/users');
Why Use This?
The native `fetch` API is built into all modern browsers and handles 99% of HTTP requests seamlessly. While Axios provides conveniences like automatic JSON transformation and request cancellation, native `fetch` combined with `AbortController` handles complex use cases beautifully without adding 11kB to your bundle.