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

stipulate

v0.2.0

Published

A module extending the Fetch API with some useful default error handling and hooks.

Downloads

99

Readme

stipulate

A module extending the Fetch API with some useful default error handling and data extraction.

Build Status Coverage Status Dependency Status

Stipulate assumes the presence of a global fetch, in accordance with the Fetch API spec.

Usage

Simplest use-case would be fetching json data from an endpoint.

import stipulate from 'stipulate';

stipulate('/some/resource.json')
  .then(console.log.bind(console));

// => { "some": "data" }

If you want to do something else, you send options.

import stipulate from 'stipulate';

const options = {
  credentials: 'same-origin',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json'
  },
  method: 'POST',
  body: JSON.stringify(someDataObject)
};

const postResult = stipulate('/some/endpoint', options);

If you need to extend options, you can use the buildOptions export.

import { buildOptions } from 'stipulate';

const opts = {
  credentials: 'same-origin',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json'
  }
};

const moreOpts = {
  headers: {
    'Cache-Control': 'no-cache'
  }
};

const mergedOpts = buildOptions(opts, moreOpts);
//  {
//    credentials: 'same-origin',
//    headers: {
//      'Accept': 'application/json',
//      'Cache-Control': 'no-cache',
//      'Content-Type': 'application/json'
//    }
//  }

Null or empty string headers will not be written to merged options. If you pass buildOptions two options objects with the same header, one set to null or "" and the other with a value, order matters.

const nullAcceptHeaderOpts = {
  headers: {
    'Accept': null
  }
};

const  = mergedOpts = buildOptions(nullAcceptHeaderOpts, opts);
// buildOpts gives priority to first option set seen, so this would
// produce:
//  {
//    credentials: 'same-origin',
//    headers: {
//      'Content-Type': 'application/json'
//    }
//  }

buildOptions(opts, nullAcceptHeadersOpts);
// this would produce:
// {
//   credentials: 'same-origin',
//   headers: {
//    'Accept': 'application/json',
//    'Content-Type': 'application/json'
//   }
// }

resolveUrl is a function for adding query parameters from an object to a url string. Duplicate keys will get resolved with priority going to values found on the query object. Like headers, you can override query params from the url by passing null or '' values for those params in the query object.

const query = {
  foo: '',
  zip: 'zap'
};

resolveUrl('http://some.domain/some/endpoint?foo=bar&fizz=buzz', query);
// returns: "http://some.domain/some/endpoint?fizz=buzz&zip=zap"

Errors

By default, Stipulate rejects responses with non 2XX status codes. There are two ways to modify this behavior:

  1. pass an okCodes array of status codes that are acceptable (beyond 2XX; this array extends that range) as part of your options.
const result = stipulate('/foobar', { okCodes: [401, 403] });
// will not reject 2XX, 401, or 403 responses
  1. pass a test function with your options.
// e.g., to get fetch's normal behavior of fulfilling every request, regardless of success:
// (unless there's a network error)
const neverReject = function(response) {
  return response;
};

const result = stipulate('/foo', { test: neverReject });

Data Extraction

By default, after checking for errors Stipulate will try to extract json from the response to return in a promise. If you want another data type (text and blob are some examples of other supported response data types) you can pass a third argument to stipulate, which is the data type's extraction method name (as a string) that you want. Data extraction methods can be found in the Fetch API spec under the Body section.

const textResponse = stipulate('/foo', options, 'text');

If you don't want any data extraction and just want the error handling, just import enforceOk and use it with fetch.

import { enforceOk } from 'stipulate';

const errorFreeResponse = fetch('/foobar', options).then(enforceOk(options));
// enforceOk looks at your options for the "okCodes" or "test" keys