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 🙏

© 2026 – Pkg Stats / Ryan Hefner

universal-common-net-http

v1.0.2

Published

A lightweight HTTP library based on the fetch API.

Readme

universal-common-net-http

A lightweight HTTP library based on the fetch API that simplifies making HTTP requests.

Installation

npm install universal-common-net-http

Overview

This library provides three main classes to simplify HTTP requests:

  • HttpClient - Handles HTTP requests with a clean, fluent interface.
  • HttpContent - Represents HTTP content bodies with associated headers (the derived type JsonContent is more commonly used with JSON APIs).
  • HttpError - Wraps HTTP error responses with easy access to status and body.

Usage

Importing

// Commonly used types.
import { HttpClient, HttpContent, HttpError, JsonContent } from "universal-common-net-http";

Basic GET Request

const client = new HttpClient();

const response = await client.getAsync("https://api.example.com/users");
const users = await response.json();
console.log(users);

Handling Query Parameters

const client = new HttpClient();

const response = await client.getAsync("https://api.example.com/search?query=test&page=1");
const results = await response.json();

POST Request with JSON Data

const client = new HttpClient();

import { JsonContent } from "universal-common-net-http";

const userData = {
  name: "John Doe",
  email: "[email protected]"
};

const jsonContent = new JsonContent(userData);
const response = await client.postAsync("https://api.example.com/users", jsonContent);

const newUser = await response.json();
console.log(`Created user with ID: ${newUser.id}`);

Using Raw HttpContent

const client = new HttpClient();

const content = new HttpContent();
content.body = JSON.stringify({ name: "Jane Doe" });
content.headers = {
  "Content-Type": "application/json",
  "X-Custom-Header": "CustomValue"
};

const response = await client.postAsync("https://api.example.com/users", content);

Working with FormData

const client = new HttpClient();

const formData = new FormData();
formData.append("username", "johndoe");
formData.append("avatar", fileInput.files[0]);

const content = new HttpContent();
content.body = formData;

const response = await client.postAsync("https://api.example.com/profile", content);

Error Handling

const client = new HttpClient();

try {
  const response = await client.getAsync("https://api.example.com/restricted");
  const data = await response.json();
  console.log(data);
} catch (error) {
  if (error instanceof HttpError) {
    console.error(`HTTP Error: ${error.status} - ${error.statusText}`);
    
    try {
      const errorBody = await error.json();
      console.error("Error details:", errorBody);
    } catch (e) {
      console.error("Could not parse error response as JSON");
    }
  } else {
    console.error("Network or other error:", error);
  }
}

Custom HttpClient

class ApiClient extends HttpClient {
  constructor(baseUrl, apiKey) {
    super();
    this.baseUrl = baseUrl;
    this.apiKey = apiKey;
  }
  
  preConfigureRequestOptions(uri, requestOptions) {
    if (!requestOptions.headers) {
      requestOptions.headers = {};
    }
    
    requestOptions.headers["Authorization"] = `Bearer ${this.apiKey}`;
  }
  
  handleNonSuccess(response) {
    throw new HttpError(response);
  }
  
  async getUsersAsync() {
    const response = await this.getAsync(`${this.baseUrl}/users`);
    return await response.json();
  }
}

const apiClient = new ApiClient("https://api.example.com/v1", "your-api-key");
const users = await apiClient.getUsersAsync();

Aborting Requests

const client = new HttpClient();
const controller = new AbortController();

const timeoutId = setTimeout(() => controller.abort(), 5000);

try {
  const response = await client.getAsync(
    "https://api.example.com/slow-endpoint", 
    controller.signal
  );
  
  clearTimeout(timeoutId);
  const data = await response.json();
  console.log(data);
} catch (error) {
  if (error.name === "AbortError") {
    console.log("Request was aborted due to timeout");
  } else {
    console.error("Request failed:", error);
  }
}

PUT and DELETE Requests

const client = new HttpClient();

const updateData = new JsonContent({ name: "Updated Name" });
const updateResponse = await client.putAsync("https://api.example.com/users/123", updateData);

const deleteResponse = await client.deleteAsync("https://api.example.com/users/123");

Advanced Configuration

Custom Request Options

const client = new HttpClient();

const requestOptions = {
  credentials: "include",
  cache: "no-cache",
  redirect: "follow",
  referrerPolicy: "no-referrer"
};

const response = await client.getAsync(
  "https://api.example.com/data",
  null,  // No abort signal
  requestOptions
);