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

fetch-json-simple

v0.1.3

Published

Configurable fetch wrapper for retrieving JSON

Downloads

31

Readme

fetch-json-simple

Configurable wrapper around fetch to retrieve JSON

Main features:

  • Attaches relevant headers for JSON:
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    }
  • Stringifies request { body } (but can also handle already stringified body)
  • Text responses converted to JSON { body }
  • Options are merged rather than assigned.
  • Auto-specifies {method: 'post'} if omitted and a body was passed
  • Shortcut methods: fetch.get, fetch.post, fetch.put, ...

Additional features:

  • Configurable fetch - use either native or polyfill
  • Configurable host - prefixed to paths before sending request
  • ... see #config

Other similar libraries: json-fetch, fetch-json.

Install

npm install fetch-json-simple --save

Usage

Example

fetch('/data-path')
  .then(json => {
    if (json.body) {
      // text body
    } else {
      // json
    }
  }).catch(error => {

  })

API

fetch(path, options)
  • path [string](required) Path to make request to.

  • options [object] Options object containing headers, body etc. body can be plain JS object.

    Merged with configured options object (see #config) and the following json-related default options object:

    defaults = {
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
      },
    }
      merge({},
        defaults, // above
        {method: body ? 'post' : 'get'},
        fetch.options, // configured
        opts, // passed as argument
        {body} // after stringifying
      )

Shortcut Methods

fetch.⟪method⟫(...)
  • get Equivalent of: fetch(path, {method: 'get'})
  • post Equivalent of: fetch(path, {method: 'post'})
  • ... put, patch, delete

Config

fetch.⟪config⟫ = ...
  • host [string](default:none) Use this host to add all paths to before making the fetch request

    fetch.host = 'http://server.com'
  • options [object](default:none) Default set of options used in every fetch request. Merged with actual (json-related) options.

    fetch.options = {
      headers: {
        'Cache-Control': 'no-cache'
        // 'Accept': 'application/json',  < will still be present in final request
        'Content-Type': 'json' // < overridden default json-related option
    
      }
    }
  • fetch [function](default:none(uses native)) Underlying fetch function to use. Use this to polyfill if needed.

    fetch.fetch = require('isomorphic-fetch')

    You may also use isomorphic-fetch to globally polyfill the underlying fetch in which case the above won't be needed.