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

@workablehr/request

v1.0.4

Published

Request

Downloads

2,376

Readme

request

HTTP client that uses the native fetch. Request is based on middlewares in order to extend the default fetch functionality.

Installing

Using npm:

$ npm install @workablehr/request

Basic usage

import request from "@workablehr/request";

const promise = request("resource.com"); // submits a get request to 'resource.com'

promise
  .then(reponse => {
    console.log("reponse", reponse);
  })
  .catch(error => {
    console.error("error", error);
  });

Request will resolve the promise if the response status is between 200 and 299. Otherwise, it will reject the promise. If the promise resolved successfully, it will return the JSON content of the response. If the promise rejected, it will return an error containing the response and an XHRHandler.

XHRHandler

XHRHandler triggers callbacks according to http response rules.

Example

XHRHandler.init(response).catch(error =>
  error.asyncHandler.then(handler =>
    handler
      .when(400, () => "doSomething")
      .whenNot(401, () => "doSomething")
      .otherwise(() => "doSomething")
      .always(() => "doSomething")
      .handle()
  )
);

Middlewares

  • withAbort
  • withCache
  • withShortcuts
  • withBgSync

Middleware is an easy way to extend the basic functionality of the request. For example, if you would like to create a request that can be aborted, you could merely do:

import basicRequest, { withAbort } from "@workablehr/request";
const request = withAbort(basicRequest);
const promise = request("resource.com", {
  method: "POST",
  body: JSON.stringify({ data })
});
promise.abort();

Or if you would like to use the shortcut request.post(), you could:

import basicRequest, { withAbort, withShortcuts } from "@workablehr/request";
const request = withShortcuts(withAbort(basicRequest));
const promise = request.post("resource.com", { data });
promise.abort();

You can find more information for each middleware on the corresponding file.

withAbort

Injects an abort function in the request promise.

withCache

Caches a request in the indexed DB.

withShortcuts

Extends the request method with post/put/del methods.

withBgSync

Sends the request to the service worker in order to perform it when the device is back online.

⚠️ It needs an extra handling in the service worker side, in order to cache the request with the 'bgSync: 1' header.

Custom middleware

A middleware is nothing more than a simple function that accepts request method and returns the extended request.

import basicRequest from "@workablehr/request";

const withRequestLogger = request => {
  return (url, params) => {
    console.log("start request", url);
    request(url, params).then(response => {
      console.log("successfully request", url);
      return response;
    });
  };
};

const request = withRequestLogger(basicRequest);