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

rd-fetch

v1.0.7

Published

A developer friendly fetch wrapper class

Downloads

16

Readme

rd-fetch

A developer friendly fetch wrapper class.

Build Status

What this do?

When trying to migrate across from ajax, I found the way fetch handled HTTP response codes to be a bit confusing, especially for what would usually be considered errors. I found it doubly confusing trying to handle json response payloads for errors. So I made this wrapper to make things a little more convenient.

Currently this only really supports JSON, but there's no reason it couldn't support other data/content types as well.

Installation

npm i -S rd-fetch

Tests

npm run test

Usage

rd-fetch exports a class Fetch which contains static methods for making different kinds of requests. So all you need to do is

import Fetch from 'rd-fetch';

Then call the static method you need passing the required arguments to send your request.

Methods

Fetch.json

Fetch.json is a static method that will handle communicating with a resource via json. If the request status is ok, (ie., response.ok) you will be able to do what you want with the response in your then. If the request status is not ok, a rejected promise will be returned allowing you to catch the response payload in your catch. In both instances, the response will have a json property attached to it.

import Fetch from 'rd-fetch';

Fetch.json('https://example.com/api')
  .then((response) => {
	console.log(response.json);
  })
  .catch((error) => {
	console.log(error.json);
  });
Arguments
url

The URL to fetch

options

The options to use with the fetch.

options.method

The method to use, defaults to GET.

options.headers

The headers to use, defaults to

{ 'Accept': 'application/json', 'Content-Type': 'application/json', }
options.body

The body to send, defaults to null. Fetch.json will auto JSON.stringify this option so you should pass a standard JavaScript object for this option.

Advanced Usage

Since fetch calls return promises, you can handle default and custom behaviour pretty nicely by wrapping a call to a Fetch static method in a function that returns it like so

const log = { responses: [], errors: [] };
let user;

/**
 * General use request method. Pushes responses/errors into a log.
 * Always returns a promise.
 *
 * @param {string} url - the url to request
 * @param {object} [options] - the options to pass to Fetch.json
 * @return {object} Promise
 */
function request(url, options) {
  return Fetch.json(url, options)
    .then((response) => {
      log.responses.push(response);
      Promise.resolve(response);
    })
    .catch((error) => {
      log.errors.push(error);
      Promise.reject(error);
	});
}

/**
 * Logs the user in. Sets user data.
 *
 * @param {object} creds - user credentials
 * @return void
 */
function login(creds) {
  request('https://example.com/api/login', {
    method: 'POST',
    body: creds
  })
    .then((response) => {
      user = response.json;
    })
    .catch((error) => {
      console.log(error);
    });
}

License

MIT

Credits

rd-fetch was built by rohan-deshpande with