1.0.0 • Published 6 months ago
@parthkapoor-dev/better-axios v1.0.0
Axios API Wrapper
A powerful TypeScript axios wrapper that eliminates repetitive API call patterns, provides centralized error handling, and simplifies authentication management.
🚀 Features
- Zero Overhead: No performance impact on your API calls
- Centralized Error Handling: Handle errors globally or per request
- Automatic Authentication: Built-in auth token management
- Type Safety: Full TypeScript support with generics
- Service Pattern: Organized API calls with base service class
- Multiple Clients: Support for different backend services
- Custom Interceptors: Request/response interceptors support
- CLI Tool: Generate boilerplate code quickly
📦 Installation
npm install axios-api-wrapper-cli
# or
yarn add axios-api-wrapper-cli🛠️ CLI Usage
Initialize a new project
npx better-axios init
# or
npx better-axios init --name my-project --examples --framework reactGenerate service files
# Generate a new service
npx better-axios generate service --name user --path /users
# Generate a new API client
npx better-axios generate client --name payment
# Generate configuration file
npx better-axios generate config💻 Quick Start
1. Basic Setup
import { AxiosApi } from 'axios-api-wrapper-cli';
const apiClient = new AxiosApi({
baseURL: 'https://api.example.com',
globalErrorHandler: (error) => {
console.error('API Error:', error.message);
// Show toast notification, etc.
}
});2. Create a Service
import { ApiService } from 'axios-api-wrapper-cli';
interface User {
id: number;
name: string;
email: string;
}
class UserService extends ApiService {
constructor(api: AxiosApi) {
super(api, '/users');
}
async getAllUsers(): Promise<ApiResponse<User[]>> {
return this.get<User[]>('');
}
async getUserById(id: number): Promise<ApiResponse<User>> {
return this.get<User>(`/${id}`);
}
async createUser(userData: Partial<User>): Promise<ApiResponse<User>> {
return this.post<User>('', userData);
}
}
const userService = new UserService(apiClient);3. Use in Your Application
// Simple usage
async function loadUsers() {
try {
const response = await userService.getAllUsers();
if (response.success) {
return response.data; // User[]
}
} catch (error) {
// Error already handled by global handler
return [];
}
}
// With authentication
apiClient.setAuthToken('your-jwt-token');
await userService.createUser({ name: 'John', email: 'john@example.com' });🔧 Configuration Options
const apiClient = new AxiosApi({
baseURL: 'https://api.example.com',
timeout: 15000,
defaultHeaders: {
'Content-Type': 'application/json',
},
authTokenKey: 'Authorization', // Header key for auth token
authTokenPrefix: 'Bearer ', // Token prefix
globalErrorHandler: (error: ApiError) => {
// Handle all errors globally
switch (error.statusCode) {
case 401:
// Redirect to login
break;
case 403:
// Show permission error
break;
default:
// Show generic error
}
},
globalSuccessHandler: (response: ApiResponse) => {
// Optional global success handling
console.log('Request successful');
},
requestInterceptor: (config) => {
// Modify requests before sending
config.headers['X-Custom-Header'] = 'value';
return config;
},
responseInterceptor: (response) => {
// Process responses before handling
return response;
}
});🔐 Authentication
// Set auth token (automatically added to all requests)
apiClient.setAuthToken('your-jwt-token');
// Remove auth token
apiClient.removeAuthToken();
// Skip auth for specific requests
await apiClient.get('/public-endpoint', { useAuth: false });🎯 Advanced Usage
Custom Error Handling per Request
await userService.deleteUser(123, {
customErrorHandler: (error) => {
if (error.statusCode === 403) {
alert('Permission denied');
} else {
alert('Delete failed');
}
},
customSuccessHandler: () => {
alert('User deleted successfully');
}
});Multiple API Clients
const mainApiClient = new AxiosApi({
baseURL: 'https://api.example.com',
globalErrorHandler: handleMainApiError
});
const paymentApiClient = new AxiosApi({
baseURL: 'https://payments.example.com',
globalErrorHandler: handlePaymentApiError
});
const userService = new UserService(mainApiClient);
const paymentService = new PaymentService(paymentApiClient);Direct API Calls
// Without services
const response = await apiClient.get<User[]>('/users');
const newUser = await apiClient.post<User>('/users', userData);
const updatedUser = await apiClient.put<User>(`/users/${id}`, updateData);
await apiClient.delete(`/users/${id}`);📚 API Reference
AxiosApi Class
Constructor Options
baseURL: Base URL for all requeststimeout: Request timeout in millisecondsdefaultHeaders: Default headers for all requestsauthTokenKey: Header key for authentication tokenauthTokenPrefix: Prefix for authentication tokenglobalErrorHandler: Global error handling functionglobalSuccessHandler: Global success handling functionrequestInterceptor: Custom request interceptorresponseInterceptor: Custom response interceptor
Methods
get<T>(url, config?): GET requestpost<T>(url, data?, config?): POST requestput<T>(url, data?, config?): PUT requestpatch<T>(url, data?, config?): PATCH requestdelete<T>(url, config?): DELETE requestsetAuthToken(token): Set authentication tokenremoveAuthToken(): Remove authentication tokensetDefaultHeader(key, value): Set default headerremoveDefaultHeader(key): Remove default header
ApiService Class
Base class for creating organized API services.
Constructor
constructor(api: AxiosApi, basePath: string)Protected Methods
get<T>(endpoint, config?): GET request relative to basePathpost<T>(endpoint, data?, config?): POST request relative to basePathput<T>(endpoint, data?, config?): PUT request relative to basePathpatch<T>(endpoint, data?, config?): PATCH request relative to basePathdelete<T>(endpoint, config?): DELETE request relative to basePathbuildUrl(endpoint): Build full URL from endpoint
Request Configuration
interface RequestConfig {
useAuth?: boolean; // Include auth token (default: true)
customErrorHandler?: (error: ApiError) => void;
customSuccessHandler?: (response: ApiResponse) => void;
skipGlobalHandlers?: boolean; // Skip global handlers
// ... other axios config options
}Response Types
interface ApiResponse<T = any> {
data: T;
success: boolean;
message?: string;
statusCode: number;
}
interface ApiError {
message: string;
statusCode: number;
originalError: any;
}🔍 Examples
Check out the /examples directory in your generated project for complete usage examples including:
- React integration
- Vue integration
- Node.js server usage
- Authentication flows
- Error handling patterns
- Multiple API clients
🤝 Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙋♂️ Support
If you have any questions or need help, please:
- Check the documentation
- Open an issue
- Start a discussion
Made with ❤️ by Your Name
1.0.0
6 months ago