wasqwf

Grab.js - A Lightweight HTTP Client with Advanced Features 🚀

Version License Build Status

Download Latest Release

Table of Contents

Overview

Grab.js is a minimalistic HTTP client designed to make web requests simple and efficient. At just 5KB, it offers a range of features like circuit breakers, ETags, and request deduplication, making it suitable for both small projects and larger applications.

For the latest updates and releases, visit the Releases section.

Features

Installation

You can install Grab.js via npm. Run the following command in your terminal:

npm install grab-js

For a more manual approach, you can download the latest release from the Releases section and execute the file.

Usage

Using Grab.js is straightforward. Below is a simple example of how to make a GET request:

import Grab from 'grab-js';

const client = new Grab();

client.get('https://api.example.com/data')
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.error('Error fetching data:', error);
  });

Making POST Requests

To make a POST request, you can use the post method:

client.post('https://api.example.com/data', {
  key: 'value'
})
.then(response => {
  console.log('Data saved:', response.data);
})
.catch(error => {
  console.error('Error saving data:', error);
});

Examples

Circuit Breaker Example

You can use the circuit breaker feature to prevent your application from sending requests to an unresponsive server:

client.setCircuitBreaker(true);

client.get('https://api.example.com/data')
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.error('Request failed, circuit breaker activated:', error);
  });

ETags Example

Using ETags helps to cache responses efficiently:

client.get('https://api.example.com/data', { headers: { 'If-None-Match': 'your-etag-here' } })
  .then(response => {
    if (response.status === 304) {
      console.log('Data has not changed.');
    } else {
      console.log('Fetched new data:', response.data);
    }
  })
  .catch(error => {
    console.error('Error fetching data:', error);
  });

API Reference

Methods

Options

Contributing

We welcome contributions to Grab.js! To contribute, please follow these steps:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature/YourFeature).
  3. Make your changes.
  4. Commit your changes (git commit -m 'Add some feature').
  5. Push to the branch (git push origin feature/YourFeature).
  6. Open a pull request.

License

Grab.js is licensed under the MIT License. See the LICENSE file for more details.

For the latest updates and releases, visit the Releases section.