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

@sunney/requests

v0.2.7

Published

A minimal, but powerful HTTP Client for Node.js

Downloads

16

Readme

requests

Introducing requests, an incredibly lightweight (1.2kb) and powerful HTTP client for Node.js, built on top of the Fetch API.

Motivation

Driven by the limitations of existing JavaScript HTTP libraries, I developed requests with inspiration from the versatile Python requests library. My goal was to create a user-friendly and flexible solution for working with HTTP requests in JavaScript, filling the gaps left by other alternatives.

Installation

npm install @sunney/requests

Usage

To use @sunney/requests, follow the steps below:

1. Import the library

import { requests } from "@sunney/requests";

2. Create an HTTP Client

Creating a custom client allows you to set a base URL, default headers, manage cookies, and intercept requests/responses. By default, cookies are persisted.

const client = requests.client({
  baseUrl: "https://jsonplaceholder.typicode.com",
  userAgent: "Custom User-Agent",
  headers: {
    "Custom-Header": "CustomHeaderValue",
  },
  cookies: {
    "Custom-Cookie": "CustomCookieValue",
  },
  persistCookies: true,
  interceptors: {
    onRequest: (url, init) => {
      // Modify request before it is sent
    },
    onResponse: (url, init, response) => {
      // Process response after it is received
    },
  },
});

3. Make a GET request

const response = await client.get("/todos/1");

// Retrieve the response data (no need to call `response.json()`)
console.log(response.data);

Features

Automatically save cookies from responses

By default, cookies are persisted. You can change this behavior by setting the persistCookies option when creating a client.

const client = requests.client({
  baseUrl: "https://jsonplaceholder.typicode.com",
  persistCookies: false,
});

Intercepting requests and responses

You can intercept requests and responses to modify them before they are sent or after they are received.

const client = requests.client({
  baseUrl: "https://jsonplaceholder.typicode.com",
  interceptors: {
    onRequest: (url, init) => {
      return { ...init, headers: { ...init.headers, foo: "bar" } };
    },
    onResponse: (url, init, response) => {
      // Process response after it is received
    },
  },
});

Modify default headers and cookies

Easily modify the default headers and cookies for your client.

// Modifying headers
client.headers.set("foo", "bar");

// Modifying cookies
client.cookies.set("foo", "bar");

Make POST, PUT, DELETE, and PATCH requests

// POST request
const postResponse = await client.post("/todos", {
  body: { title: "New todo", completed: false },
});

// PUT request
const putResponse = await client.put("/todos/1", {
  body: { title: "Updated todo", completed: true },
});

// DELETE request
const deleteResponse = await client.delete("/todos/1");

// PATCH request
const patchResponse = await client.patch("/todos/1", {
  body: { title: "Patched todo" },
});