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

@financial-times/ip-wxp-fetch-wrapper

v1.0.2

Published

A small wrapper around fetch that throws custom http errors

Downloads

24

Readme

ip-wxp-fetch-wrapper

CircleCI npm version

A small wrapper around fetch that throws custom HTTP errors.

setup

Install via npm:

  npm i -S @financial-times/ip-wxp-fetch-wrapper

usage

A common pattern for fetch with async await looks like the below:

try{
  const { AMAZING_KEY: API_KEY } = process.env
  const response = await fetch('http://www.amazing.com/awesome-json', {
    method: 'POST',
    headers: {  API_KEY, 'content-type': 'application/json' },
    body: { awesomeness: 9000 }
  })

  if(response.status === 200) {
    console.log( await response.json())
  } else {
    // deal with the fetch error somehow
    throw Error('fetch failed for some reason')
  }
} catch(err) {
  // handle the URL/fetch error somehow e.g pass it to an error handler middleware in express
  next(err);
}

This module wraps much of that pattern and throws custom HTTP Errors if fetch fails.

const { create } = require('@financial-times/ip-wxp-fetch-wrapper')
const { AMAZING_KEY: API_KEY } = process.env
const api = create('http://www.amazing.com', { headers: { 'content-type': 'application/json', 'API_KEY': 'AWESOME-KEY' }}, { bodyParser: 'text', errorParser: 'json', fetch: window.fetch})

try{
  const result = await api.post('/awesome-json', { body: { awesomeness: 9000 } }
  console.log(result) // receives a JSON object
} catch(err) {
  // will receive an HTTP Error with a status and name e.g. status: 404, name: NotFound
  next(err)
}

methods

createMethod

Returns a function with a canned configuration for querying an api.

method

The HTTP Verb to be used for the request e.g GET

baseUrl

The base url for requests, should be parsable by URL. This should include the protocol, hostname and port used by the request, it can also contain a partial pathname.

opts

Additional options:

  • fetch - the fetch implementation to use (default: isomorphic-fetch)
  • bodyParser - the body mixin function to use (default: json)
  • errorParser - the body mixin function to use for error responses (default: bodyParser setting)

Returns a method used to make an http request taking following arguments:

  • pathname - this will be concatenated onto the base url
  • options - an object of fetch options, this is merged with the config options passed to create the init argument for the request

config = {}

Base init for fetch requests, use for things like api key headers which need to be the same on every request.

createAll

Maps all provided verbs to the method returned by createMethod, returns an object of schema { [verb]: method }.

baseUrl

Same as above

opts = {}

Same as above

config

Same as above

verbs

The verbs to create methods for, defaults to the list in http.json, which is also the list provided by the node http module.

create

Same as createAll above except the returned object also contains the HTTPErrors object.

HTTP Errors

The HttpErrors object contains a set of custom errors based on HTTP status codes, they extend the usual JS Error type but also include a status code and name e.g. status 400 name BadRequest.