npm package: @imperion/fetch
Description: High-performance fetch module.
Author: Rex
Latest version: 0.5.0

# @imperion/fetch

![npm](https://img.shields.io/npm/v/@imperion/fetch)
![license](https://img.shields.io/npm/l/@imperion/fetch)
![npm downloads](https://img.shields.io/npm/dw/@imperion/fetch)

`@imperion/fetch` is a powerful, fast, and flexible HTTP/HTTPS request management library built with Rust and available as an npm module. It supports advanced features such as proxy handling, retry logic, interceptors, and more.

## Features

- 🌐 **HTTP and HTTPS Support**: Easily manage both HTTP and HTTPS requests.
- 🛠️ **Request Shortcuts**: Supports GET, POST, PUT, DELETE, PATCH, and HEAD methods.
- 🌍 **Proxy Support**: Send requests through a proxy server.
- 🔄 **Retry Mechanism**: Configure retries for failed requests.
- 🧩 **Interceptors**: Modify request and response data.
- 🔁 **Cancelable Requests**: Supports canceling long-running requests.
- 🧵 **Multithreaded Downloads**: Speed up download tasks using multiple threads.
- 🛡️ **Advanced Error Handling**: Customize error handling with your own logic.
- 💾 **Cache Support**: Cache responses for reuse.
- 🚨 **Fallback URLs**: Define backup URLs in case the primary request fails.

## Installation

You can install `@imperion/fetch` via npm:

```bash
npm install @imperion/fetch

USAGE
Simple get request:

```
const Imperion = require('@imperion/fetch');

const client = new Imperion();

client.fetch('https://example.com')
  .then(response => console.log(response))
  .catch(error => console.error('Error:', error));
```

Cancelable request:

```
const Imperion = require('@imperion/fetch');

const client = new Imperion();

client.cancelable_request()
  .fetch('https://example.com')
  .then(response => console.log(response))
  .catch(error => console.error('Error:', error));

// To cancel the request:
client.cancel_tx.send();
```

Multithreaded Download:

```
const Imperion = require('@imperion/fetch');

const client = new Imperion();

client.download_multithreaded('https://example.com/largefile.zip', 4)
  .then(() => console.log('Download completed'))
  .catch(error => console.error('Error:', error));
```

Advanced Error Handling with Fallback URLs:

```
const Imperion = require('@imperion/fetch');

const client = new Imperion();

client.add_fallback_url('https://example.com')
  .set_custom_error_handler((error) => {
    console.error('Custom Error:', error);
  })
  .fetch('https://example.com')
  .then(response => console.log(response))
  .catch(error => console.error('Final Error:', error));
```

API

fetch(url: string): Promise<string>
// Sends a GET request to the specified URL.

post(url: string, body: string): Promise<string>
// Sends a POST request to the specified URL with the given body.

set_proxy(proxy: string): Imperion
// Sets a proxy server for requests.

cancelable_request(): Imperion
// Makes the next request cancelable.

download_multithreaded(url: string, num_threads: number): Promise<void>
// Downloads a file using multiple threads.

add_fallback_url(url: string): Imperion
// Adds a fallback URL for the request.

set_custom_error_handler(handler: (error: string) => void): Imperion
// Sets a custom error handler function.