AxiosX is a tiny React hook library for smart Axios requests with built-in:
- Token auth & request interceptors
- Retry logic & cancellation
- Clean React state:
status
,isLoading
,data
,error
- Supports GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS
npm install axiosx-dev axios react
import { init, useGet, usePost } from 'axiosx-dev';
// 1️⃣ Initialize once (e.g. in App.tsx)
init({
baseURL: 'https://api.example.com',
withAuth: true,
tokenKey: 'token',
maxRetries: 2,
retryDelay: 500,
on401: (err) => console.warn('Not authorized!', err),
});
// 2️⃣ Use inside a component
const { get, isLoading, data, error, cancelRequest } = useGet();
get('/users');
if (isLoading) console.log('Loading...');
if (error) console.log(error);
Hook | HTTP Method | Description |
---|---|---|
useGet |
GET |
Performs a GET request. |
usePost |
POST |
Performs a POST request. |
usePut |
PUT |
Performs a PUT request. |
useDelete |
DELETE |
Performs a DELETE request. |
usePatch |
PATCH |
Performs a PATCH request. |
useHead |
HEAD |
Performs a HEAD request. |
useOptions |
OPTIONS |
Performs an OPTIONS request. |
const { get, data, isLoading, status } = useGet();
useEffect(() => {
get('/users');
}, []);
const { post, data } = usePost();
post('/users', { name: 'John' });
Option | Type | Description |
---|---|---|
baseURL |
string |
Base URL for the Axios instance. |
timeout |
number |
Request timeout in milliseconds. |
headers |
Record<string, string> |
Custom headers to attach to every request. |
withAuth |
boolean |
Whether to add an Authorization header with a Bearer token. |
tokenKey |
string |
Key name in localStorage to read the token from. |
getToken |
`() => string | null` |
on401 |
(error: any) => void |
Callback for handling 401 Unauthorized errors. |
onError |
(error: any) => void |
Global error handler callback for failed requests. |
maxRetries |
number |
Number of retry attempts for failed requests. |
retryDelay |
number |
Delay in ms between retry attempts. |
👉 Full Docs: https://axiosx-dev-docs.vercel.app (free Vercel subdomain)
MIT License.