npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hetch-api

v5.0.0

Published

HTTP Request managing library

Downloads

5

Readme

Hetch-API

Hetch is a fetch wrapper that provides you with a convenient way to make HTTP requests. It simplifies the process of making API calls and offers several useful features to enhance your development experience.

Table of Contents

Features

  • Direct access to methods like get, put, post, and delete, allowing you to make different types of HTTP requests easily.
  • A flexible any method that lets you perform an HTTP request with a custom method.
  • Configuration set for the library, including options such as timeout, custom headers, and settings for handling network errors.
  • Directly use the response data without having to manually convert it to JSON.
  • Interceptors to execute functions before the request is sent and after the response is received, enabling you to modify request and response data.

Installation

You can install Hetch-API using npm:

npm install hetch-api

Getting Started

To use Hetch-API in your project, import it and create an instance of the Hetch class:

import { Hetch } from "hetch-api";

const hetch = new Hetch();

Making HTTP requests

Hetch-API provides easy-to-use methods to make HTTP requests. For example, to make a GET request:

const url = "[url]";
const getResponse = await hetch.get(url);
console.log("GET Response Data");
console.log(getResponse.data);

Similarly, you can use the post, put, and delete methods for corresponding HTTP methods.

Custom Method

If you need to make an HTTP request with a custom method, you can use the any method:

const customMethod = "PATCH";
const customMethodData = {
  id: 1,
  title: "Custom Method Request",
  completed: false,
  userId: 1,
};

const customMethodResponse = await hetch.any(
  customMethod,
  "[url]",
  {
    body: JSON.stringify(customMethodData),
  }
);
console.log("Custom Method Response Data");
console.log(customMethodResponse.data);

Configuration

You can modify default configuration values while creating the Hetch instance:

const customConfig = {
  timeout: 5000,
  maxRetries: 5,
  retryDelay: 2000,
};

const hetch = new Hetch(customConfig);

The customConfig object allows you to set the timeout for requests, specify the number of maxRetries for failed requests, and set a retryDelay between retries.

Interceptors

Hetch-API supports request and response interceptors. You can add functions to be executed before the request is sent or after the response is received:

// Request Interceptor
hetch.useRequestInterceptor(async (config) => {
  // Modify request config if needed
  return config;
});

// Response Interceptor
hetch.useResponseInterceptor(async (responseData, response) => {
  // Modify response data if needed
  return responseData;
});

Error Handling

Hetch-API provides built-in error handling for network errors and server responses. When making HTTP requests, the library handles different types of errors and provides appropriate error classes for each scenario.

Network Errors

When a network error occurs (e.g., the server is down or there is no internet connection), Hetch-API throws a NetworkError. This error indicates that the network request failed and provides information about the error.

Server Errors

If the server returns an error status code (e.g., 4xx or 5xx), Hetch-API throws a ServerError. This error includes the status code returned by the server, allowing you to handle specific server errors in your code.

Client Errors

For all other errors that do not fall under network errors or server errors, Hetch-API throws a ClientError. This error is a generic client-side error and can be used to handle any other unexpected errors.

Here's an example of how you could implement error handling in your project using Hetch-API:

import { Hetch, NetworkError, ServerError, ClientError } from "hetch-api";

const hetch = new Hetch();

const url = "[url]";

try {
  const response = await hetch.get(url);
  console.log("Response Data:", response.data);
} catch (error) {
  if (error instanceof NetworkError) {
    console.error("Network Error:", error.message);
  } else if (error instanceof ServerError) {
    console.error("Server Error:", error.message);
    console.error("Status Code:", error.statusCode);
  } else if (error instanceof ClientError) {
    console.error("Client Error:", error.message);
  } else {
    console.error("Unexpected Error:", error.message);
  }
}

Example of Use

Here's an example of use for the Hetch library:

import { Hetch } from "hetch-api";

const customConfig = {
  timeout: 5000,
  maxRetries: 5,
  retryDelay: 2000,
};

const hetch = new Hetch(customConfig);

const url = 'https://jsonplaceholder.typicode.com/todos/1';
const getResponse = await hetch.get(url);
console.log("GET Response Data");
console.log(getResponse.data);

Output:

GET Response Data
{
  "userId": 1,
  "id": 1,
  "title": "delectus aut autem",
  "completed": false
}

Contributions

Contributions to Hetch-API are welcome! Feel free to create issues or submit pull requests for improvements or bug fixes.

License

Hetch-API is released under the ISC License