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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@isabroch/fetch-fallback

v1.1.6

Published

Fetch functionality with fallbacks for node.js environments and environments that don't support fetch but support XMLHttpRequest. Only accepts JSON responses from API.

Downloads

36

Readme

Fetch (Fallback)

Fetch functionality with fallbacks for node.js environments and environments that don't support fetch but support XMLHttpRequest. Only accepts JSON responses from API.

Installation

Node.js

npm i @isabroch/fetch-fallback

Web

Add the script before other scripts in your HTML to get the latest version.

If you need an earlier version, change 1.1.6 to the relevant version number. You can find previous versions of Fetch (Fallback) in the Versions tab on npmjs.com

<script src="https://unpkg.com/@isabroch/[email protected]/umd.js"></script>

Usage

If you're using Node.js

The following code works in all environments - so you do not have to write multiple scripts for different environments. If you are using only Node.js, the if wrapper is not necessary.

if (typeof exports === "object") {
  var xfetch = require("@isabroch/fetch-fallback");
}

Initializing

apiLink should be the base URL of the API you are trying to access. For example: https://reqres.in/api/

apiAuth should be the key used to access the API, if there is one.

xfetch.init({
  address: apiLink,
  key: apiAuth
});

Functionality

Available functions include: get(), post(), put(), del()

resource is what you want from the API. For example, if you're trying to access https://reqres.in/api/users/, resource would be users. Not providing resource defaults to trying to access just the apiLink: https://reqres.in/api/.

input is specific to post() and put(). When creating/updating content, input is the new content to be pushed to the API.

result is the response from the API as parsed JSON, usually an object or array.

In the case of del(), result returns the status code - i.e. 204.

Fetch Fallback is an async function, so accessing results should be done via async/await or then().

xfetch.get(resource)
.then(result => console.log(result))

xfetch.post(resource, input)
.then(result => console.log(result))

xfetch.put(resource, input)
.then(result => console.log(result))

xfetch.del(resource)
.then(result => console.log(result))

Testing with Reqres

Reqres is a free REST-API great for testing functionality!
You can run the code below to test if Fetch Fallback is working properly.

if (typeof exports === "object") {
  var xfetch = require("@isabroch/fetch-fallback");
}

xfetch.init({
  address: "https://reqres.in/api/",
  key: ""
});

xfetch.get("users/1")
.then(result => console.log(result))
/* EXPECTED CONSOLE LOG:
  { data:
    { id: 1,
      email: '[email protected]',
      first_name: 'George',
      last_name: 'Bluth',
      avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg' }
  } */


xfetch.post("users", {name: "morpheus", job: "leader"})
.then(result => console.log(result))
/* EXPECTED CONSOLE LOG:
  { name: 'morpheus',
    job: 'leader',
    id: '537',
    createdAt: '2019-12-20T09:06:36.482Z' } */

xfetch.put("users/2", {name: "morpheus", job: "zion resident"})
.then(result => console.log(result))
/* EXPECTED CONSOLE LOG:
  { name: 'morpheus',
    job: 'zion resident',
    updatedAt: '2019-12-20T09:07:31.314Z' } */

xfetch.del("users/2")
.then(result => console.log(result))
/* EXPECTED CONSOLE LOG:
  204 */

Need Help?

Create an issue at this project's issue tracker! Please include the following details when creating an issue:

  • Context of the error; what are you trying to achieve?
  • Error message you are receiving from the console
  • Environment(s) you are running Fetch (Fallback) in
    e.g. Node.js, Chrome v77, Firefox...

The more details you are able to provide from the beginning, the faster I can help you.

License

Fetch (Fallback) uses MIT License.