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

@switch-company/fetcher

v2.0.1

Published

Wrap the Fetch API with convenience methods.

Downloads

3

Readme

Switch - Fetcher

Wrap the Fetch API with convenience methods.


Methods get, send, form returns a promise that will resolve or reject with an object containing the data of the response when possible.

The structure of the response's object uses the same structure the response interface properties of a fetch request.

Structure of a fetch response object:

{
    data,
    headers,
    ok,
    redirected,
    status,
    statusText,
    type,
    url
  }

The headers entry returns an object containing the headers received by the server. You can access them by using their names like headers[ 'content-type' ]. Headers are in lowercase.

Depending of the response's Content-Type header the data structure of returned value will be different:

  • application/json - a JSON object
  • multipart/form-data - a FormData object
  • application/octet-stream - a Blob

Any other Content-Type will be returned as a USVString

Unlike the Fetch API, when Response.ok is falsy the returned promise is rejected, allowing you to catch and treat differently the error cases.

Example of 404 rejection:

{
  headers: {
    "content-type": "text/plain"
  },
  redirected: false,
  ok : false,
  status: 404,
  statusText: "Not Found",
  type: "basic",
  url: "https://localhost:3000/fetch"
}

If the fetch request times out or the fetched url is unavailable the returned object will only contain status, statusText and url since the server is not involved in the request.

When the request times out the status code will be 599 otherwise it will be 0.

Parameters

Default parameters can be changed using fetch.config = {};

Default parameters

  • headers - default headers to send. Any headers set when making a request will overide the default headers
  • params - standard fetch api request properties minus the header entry - defaults to { credentials: 'same-origin' }
  • timeout - duration in milliseconds before the request is considered timed out - defaults to 30000

Methods

.get( url, parameters, fetchOptions )

Execute a GET request to the url using the parameters provided.

Parameters

Parameters are any parameter provided by the fetch API.

A data object is avalaible to pass the query string.

fetchOptions

Fetcher specific options for the current request (optional).

  • parse - if false, the request will not be parsed by fetcher and will return the fetch response object (defaults to true)
  • timeout - specific timeout for the current request (in milliseconds)

Creating a GET fetch

// create a 'GET' request to "/search?term=fetch"
fetchy.get( '/search', {
  data: {
    term: 'fetch'
  }
});

.send( url, parameters, fetchOptions )

Send data to the url using the parameters provided. Method defaults to POST.

Parameters

Parameters are any parameter provided by the fetch API.

A data object is avalaible to pass a JSON, a Blob or a FormData object.

In order to POST a FormData object, the request's Content-Type must be set to multipart/form-data.

fetchOptions

Fetcher specific options for the current request (optional).

  • parse - if false, the request will not be parsed by fetcher and will return the fetch response object (defaults to true)
  • timeout - specific timeout for the current request (in milliseconds)

.form( HTMLFormElement, parameters, fetchOptions )

Shortcut to create a fetch request based on a form element. The request endpoint and method are based on the form's action and method attributes. Entries in the parameters object overwrite the form's attribute when fetching.

Non-strings fields value will be ignored when the from's method is set to GET.

A FormData object will be sent only when a non-string field value is found. By default the form's data object is a JSON object.

Parameters

Parameters are any parameter provided by the fetch API.

fetchOptions

Fetcher specific options for the current request (optional).

  • parse - if false, the request will not be parsed by fetcher and will return the fetch response object (defaults to true)
  • timeout - specific timeout for the current request (in milliseconds)