A production-ready Axios interceptor module providing robust API communication features including authentication, request/response interceptors, error handling, and token management.
- π Token-based Authentication: Easily manage authentication tokens.
- π Automatic Retry Mechanism: Retries failed requests based on configuration.
- π― Standardized Request/Response Handling: Consistent API interactions.
- π¨ Comprehensive Error Handling: Graceful error management and callbacks.
- βοΈ Configurable Settings: Flexible setup to suit various project needs.
First, install the package:
npm install github:mandula-abhilash/visdak-aim
Since this library uses axios
as a peer dependency, ensure it's installed:
npm install axios
import { createAxiosInstance } from 'visdak-aim';
const api = createAxiosInstance({
baseURL: 'https://api.example.com',
timeout: 5000,
retryCount: 3,
retryDelay: 1000,
onUnauthorized: () => {
// Handle 401 errors (e.g., redirect to login)
window.location.href = '/login';
},
defaultHeaders: {
'Content-Type': 'application/json',
},
});
import { tokenManager } from 'visdak-aim';
// Set token after login
tokenManager.setToken('your-jwt-token');
// Get current token
const token = tokenManager.getToken();
// Remove token on logout
tokenManager.removeToken();
// GET request
try {
const response = await api.get('/users');
console.log(response.data);
} catch (error) {
console.error(error);
}
// POST request
try {
const response = await api.post('/users', {
name: 'John Doe',
email: 'john@example.com',
});
console.log(response.data);
} catch (error) {
console.error(error);
}
const api = createAxiosInstance({
baseURL: 'https://api.example.com',
retryCount: 3,
retryDelay: 2000,
onRetry: (retryCount) => {
console.log(`Retry attempt: ${retryCount}`);
},
});
{
status: 'success',
data: {
// Response data
},
message: 'Request successful',
}
{
status: 'error',
message: 'An error occurred',
error: {
code: 400,
details: 'Invalid request parameters',
},
}
Status Code | Description | Usage |
---|---|---|
200 | OK | Successful request |
201 | Created | Resource created successfully |
204 | No Content | Successful request with no content |
400 | Bad Request | Invalid request parameters |
401 | Unauthorized | Missing or invalid authentication |
403 | Forbidden | Insufficient permissions |
404 | Not Found | Resource not found |
409 | Conflict | Resource conflict |
500 | Internal Server Error | Server-side error |
502 | Bad Gateway | Upstream server error |
Option | Type | Description | Default |
---|---|---|---|
baseURL |
string |
Base URL for API requests | - |
timeout |
number |
Request timeout in milliseconds | 10000 |
retryCount |
number |
Number of retry attempts | 3 |
retryDelay |
number |
Delay between retries in milliseconds | 1000 |
onUnauthorized |
function |
Callback for 401 Unauthorized errors | - |
onForbidden |
function |
Callback for 403 Forbidden errors | - |
onRetry |
function |
Callback for each retry attempt | - |
defaultHeaders |
object |
Default headers for all requests | {} |
const api = createAxiosInstance({
baseURL: 'https://api.example.com',
onUnauthorized: () => {
// Clear local storage
localStorage.clear();
// Redirect to login
window.location.href = '/login';
},
onForbidden: () => {
// Handle forbidden access
window.location.href = '/forbidden';
},
});
If you're using the module in a non-browser environment (e.g., Node.js), localStorage
won't be available. You can provide a custom token manager.
import { setCustomTokenManager } from 'visdak-aim';
const customTokenManager = {
getToken: () => {
// Implement your token retrieval logic
return myCustomStorage.get('authToken');
},
setToken: (token) => {
// Implement your token storage logic
myCustomStorage.set('authToken', token);
},
removeToken: () => {
// Implement your token removal logic
myCustomStorage.remove('authToken');
},
};
setCustomTokenManager(customTokenManager);
const onUnauthorized = async () => {
try {
const refreshToken = tokenManager.getToken(); // Assume this stores the refresh token
const response = await api.post('/refresh-token', { refreshToken });
const newAccessToken = response.data.data.accessToken;
tokenManager.setToken(newAccessToken);
console.log('Token refreshed successfully');
} catch (error) {
console.error('Token refresh failed:', error);
// Handle failed token refresh (e.g., redirect to login)
window.location.href = '/login';
}
};
const api = createAxiosInstance({
baseURL: 'https://api.example.com',
onUnauthorized,
});
Since localStorage
is not available in Node.js, you must set up a custom token manager when using the module in a Node.js environment.
import { setCustomTokenManager } from 'visdak-aim';
const customTokenManager = {
getToken: () => {
return myNodeStorage.get('authToken');
},
setToken: (token) => {
myNodeStorage.set('authToken', token);
},
removeToken: () => {
myNodeStorage.remove('authToken');
},
};
setCustomTokenManager(customTokenManager);
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
MIT License - see the LICENSE file for details
Feel free to reach out if you have any questions or need further assistance!