Voca HTTP

Modern HTTP Client for JavaScript Applications

NPM Version

A lightweight and flexible HTTP client for browser and Node.js environments with support for interceptors, progress tracking, and TypeScript.

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

ScenarioVoca HTTP Solution
AuthenticationAdd auth headers automatically with request interceptors
File UploadsTrack progress and handle large files efficiently
API Error HandlingCentralize error handling with response interceptors
Slow NetworksAutomatic retries with configurable backoff
Large Data SetsProgress 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.