Voca HTTP
Modern HTTP Client for JavaScript Applications
A lightweight and flexible HTTP client for browser and Node.js environments with support for interceptors, progress tracking, and TypeScript.
- ð GitHub Repository - Open source code
- ð Documentation - Get started guide & examples
Features
- ðĶ Promise-based HTTP client
- ð Request and response interceptors
- âąïļ Automatic request timeout
- ð Built-in request retry
- ð Upload and download progress tracking
- ð Easy file uploads with FormData handling
- ð§Đ TypeScript support
- ðģ Tree-shakable (ESM)
- ðŠķ Lightweight (no dependencies)
- ð§ Works in both browser and Node.js environments
Basic Usage
// Create a configured instance
const api = voca.create(fetch, {
baseUrl: 'https://api.example.com',
timeout: 10000,
onResponse: async (response) => {
if (!response.ok) {
throw new Error(`HTTP Error: ${response.status}`);
}
return response.json();
}
});
// Make requests with your configured API
const posts = await api.get('/posts');
const newPost = await api.post('/posts', { title: 'New Post', body: 'Content' });
File Upload with Progress
// Upload a file with progress tracking
const updateProgress = (percent) => {
console.log(`Upload progress: ${percent.toFixed(1)}%`);
progressBar.style.width = `${percent}%`;
};
const result = await api.uploadFile(
'https://api.example.com/upload',
fileObject,
{ 'Authorization': 'Bearer token' },
updateProgress
);
Preview
Why Voca HTTP?
Most modern JavaScript applications need to communicate with APIs. Voca HTTP simplifies this process by providing:
- Simplified API Interactions: Abstract away the complexities of the Fetch API
- Enhanced Error Handling: Built-in error handling and retry mechanisms
- Progress Monitoring: Real-time tracking for file uploads and downloads
- Customization: Flexible configuration options to meet specific requirements
Advanced Features
Request Interceptors
Modify requests before they are sent, perfect for adding authentication headers:
voca.config.addRequestInterceptor((options) => {
options.headers = {
...options.headers,
'Authorization': `Bearer ${getToken()}`
};
return options;
});
Response Interceptors
Transform responses before they reach your code, simplifying error handling:
voca.config.addResponseInterceptor((response) => {
if (response.status === 401) {
// Handle unauthorized access
redirectToLogin();
}
return response;
});
Use Cases
Scenario | Voca HTTP Solution |
---|---|
Authentication | Add auth headers automatically with request interceptors |
File Uploads | Track progress and handle large files efficiently |
API Error Handling | Centralize error handling with response interceptors |
Slow Networks | Automatic retries with configurable backoff |
Large Data Sets | Progress tracking for long-running requests |
Voca HTTP is designed to simplify HTTP interactions while providing advanced features that help build robust, responsive applications that handle real-world network conditions gracefully.