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 🙏

© 2026 – Pkg Stats / Ryan Hefner

js-easy-fetch

v2.0.1

Published

Makes fetch a little easier to use, especially if you do a lot of fetches with the same headers or a custom http agent

Readme

js-easy-fetch

The goals of this packages are...

  1. ... to reduce boilerplate code
  2. ... make it easier to add the same (e.g. Authorization) headers on every request
  3. ... use a custom http agent, e.g. if you need to use company CAs
  4. ... make debugging easier by using a logging mechanism
  5. ... stay compatible with window.fetch API like (https://www.npmjs.com/package/node-fetch)[node-fetch]

Usage examples

In every case, you need to require your favourite fetch function first and assign it to the global scope, then require or import js-easy-fetch:

global.fetch = require('node-fetch')
const fetch = require('js-easy-fetch')()

If you need multiple instances of js-easy-fetch (e.g. because you need different authentication headers), you can create them by first requiring the constructor and then instantiate the different fetch instances:

const EasyFetch = require('js-easy-fetch')
const twitterFetch = new EasyFetch()
twitterFetch.addDefaultHeader('Authorization', 'Bearer ' + twitterToken)
const facebookFetch = new EasyFetch()
facebookFetch.addDefaultHeader('Authorization', 'Bearer ' + facebookToken)

Doing fetches

try {
  const result = await fetch('https://httpbin.org/anything')
  console.log(result.headers['User-Agent'])
} catch (error) {
  console.error('' + error)
}

In case fetch failed, the error is an object containing the HTTP response, the parsed content, the http error message (a string combined of the status code and the status text) and a toString() function to make it easy to print it like shown in the example above, which appends the object to an empty string (which implicitly calls toString()).

The toString() function returns either the content if it is not empty or the message. This makes it possible to use more complex error information provided by the server.

Use authentication

If you have a authentication token and want to add it in every request you do, just add a call prior to all fetches:

fetch.addDefaultHeader('Authorization', 'Bearer ' + JsonWebToken)

Likewise you can add more headers if you need them.

Accept known CA certificates

Put all accepted CA certificates into a folder and use the following code:

const https = require('https')
const fs = require('fs')

const ca = []
const certDir = __dirname + '/certs/'
fs.readdirSync(certDir).forEach(file => ca.push(fs.readFileSync(certDir + '/' + file, {encoding: 'utf-8'})))
fetch.setAgent(new https.Agent({ca}))

fetch('https://my-server-with-custom-ca.com/anything')

Log errors or calls

Use the logger of your choice:

const winston = require('winston')
fetch.setLogger(winston)

Or even create your own (it just needs functions debug() and error()):

const logger = {
  debug: message => console.log('DEBUG: ' + message),
  error: message => console.error('ERROR: ' + message)
}
fetch.setLogger(logger)

js-easy-fetch logs failing request with error() and info about requests and request performance with debug():

DEBUG: GET https://httpbin.org/status/404 (898435.3980102539µs)
ERROR: 404 NOT FOUND

In case of a failing request, in addition to the error, the content of the request is also logged (with debug()) which might help to make it easier to find the source of the problem.

Upgrading from version 1.x

The main difference between versions 1.x and 2.x is that js-easy-fetch needs to be instantiated now. This helps using multiple fetch contexts (e.g. using different default headers for different servers). But on the other hand, it requires to modify your code. Instead of

const fetch = require('js-easy-fetch')

you need

const EasyFetch = require('js-easy-fetch')
const fetch = new EasyFetch()

or, if you dont need multiple contexts, but like shortness:

const fetch = require('js-easy-fetch')()