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

regrest

v4.0.1

Published

tiny http client - features rich - modern promise base

Downloads

83

Readme

🚀 Regrest - Micro HTTP client

License: MIT npm version install size code style: prettier

Micro Promise based HTTP client for the browser and node.js

✨ Features

👍🏻 Browser Support

| Chrome | Firefox | Safari | Opera | Edge | IE | | ---------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- | | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | 11 ✔ |

NOTE

If you intend to support Internet Explorer, be sure to have a poly-fill that adds a global Promise object

🏗 Installing

Using npm:

$ npm install regrest

Using cdn:

<script src="https://cdn.jsdelivr.net/npm/regrest/lib/index.umd.min.js"></script>

🎬 Example

Regrest is designed to be the simplest way possible to make http calls

Performing a GET request

// Import using NodeJS or CommonJS module
const regrest = require("regrest").default;
// Or using ES6 module
import regrest from "regrest";

// Use Promise
regrest
  .get("/man/bear/pig")
  // Print the raw response string
  .then((response) => console.log(response.text))
  // Print any error if occurred
  .catch((error) => console.log(`*** Error: ${error}`));

// Or use the new async/await keywords
const getGood = async () => {
  try {
    // Store the response in a variable
    const response = await regrest.get("/foo/bar.json");
    // print out the parsed response
    console.log(response.json);
  } catch (error) {
    // Print any error if occurred
    console.log(`*** Error: ${error}`);
  }
};

getGood();

// Or use callbacks
// WE DON'T DO THAT HERE

Performing a POST request

regrest
  .post("/comment", JSON.stringify({ name: "Foo", comment: "Bar" }))
  .then((response) => console.log(response.status, response.statusText))
  .catch((error) => console.log(error));

📚 Documentation

Convenience methods

regrest.request(options)
regrest.get(url[, options])
regrest.head(url[, options])
regrest.post(url[, data[, options]])
regrest.put(url[, data[, options]])
regrest.delete(url[, options])
regrest.options(url[, options])
regrest.patch(url[, data[, options]])

Options options

// Default options are marked with *
const options = {
  method: "GET", // *GET, POST, PUT, DELETE, etc.
  url: "https://some-domain.com/api/",
  headers: { "Content-Type": "application/json; charset=utf-8" }, // *{}
  params: { UID: 9873 },
  data: JSON.stringify(data), // *null
  maxRedirects: 10, // *5
  withCredentials: true, // *false, true
};

Response object attributes

{
  // Contains the status code of the response, e.g. 404 for a not found resource, 200 for a success
  status: 200,
  // A message related to the status attribute, e.g. OK for a status 200
  statusText: "OK",
  // The headers that the server responded with
  headers: {},
  // Response content as a string
  text: "",
  // Response content as JSON
  json: {},
  // Response content as Blob on browser and Buffer on Node js
  arrayBuffer: instance of Blob || instance of Buffer,
  // Reponse content as Blob
  blob: instance of Blob
};

Errors handling

regrest.get("/McNullington").catch((error) => {
  if (error.response) {
    /**
     * A request was made but server responded
     * with status code out of the 2XX range
     * `error.response` is an instance of the response object
     */
    console.log(error.response.status);
    console.log(error.response.statusText);
    console.log(error.response.headers);
    // ...
  } else if (error.request) {
    /**
     * A request was made, but no response was received
     * `error.request` is an instance of XMLHttpRequest on browser and an instance of
     * http.ClientRequest on Node js
     */
    console.log(error.request);
  } else {
    // Something else happened
    console.log(error.message);
  }
});