axios-api-plus
v1.0.2
Published
A simple, production-ready Axios wrapper for streamlined API data fetching and CRUD operations. Supports all standard HTTP methods, interceptors, error normalization, token-based authentication, and more.
Readme
Axios-api-plus
A simple, production-ready Axios wrapper for streamlined API data fetching and CRUD operations. Supports all standard HTTP methods, interceptors, error normalization, token-based authentication, and more.
Features
- Base URL configuration
- Request and response interceptors
- Centralized error handling and response normalization
- Token-based authentication (optional)
- Supports async/await and Promises
- Customizable headers and timeout
- TypeScript support
Installation
npm install axios-api-plusUsage
import { AxiosApi } from 'axios-api-plus';
const api = new AxiosApi({
baseURL: 'https://api.example.com',
timeout: 8000,
headers: { 'X-Custom-Header': 'foobar' },
token: 'your-jwt-token', // optional
});
// GET request
const data = await api.get('/users');
// POST request
const created = await api.post('/users', { name: 'Alice' });
// PUT request
await api.put('/users/1', { name: 'Bob' });
// PATCH request
await api.patch('/users/1', { name: 'Charlie' });
// DELETE request
await api.delete('/users/1');
// Set or remove token dynamically
api.setToken('new-token');
api.removeToken();API Reference
new AxiosApi(config)
baseURL(string): Base URL for all requeststimeout(number): Request timeout in ms (default: 10000)headers(object): Default headerstoken(string, optional): Bearer token for Authorization header
Methods
get<T>(url, config?)post<T>(url, data?, config?)put<T>(url, data?, config?)patch<T>(url, data?, config?)delete<T>(url, config?)setToken(token: string)removeToken()
All methods return a Promise of the response data.
Error Handling
All errors are normalized to:
{
message: string;
status?: number;
data?: any;
isAxiosError: boolean;
}Interceptors
You can customize interceptors by editing the setupInterceptors method in the source.
License
MIT
👤 Author
Akshay Bhalala
Happy API fetching! 🎉
🏷️ Keywords
axios, api, wrapper, http, fetch, rest, typescript, request, response, interceptor, token, authentication, error-handling, promise, async, client, crud, javascript, nodejs
