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

@degjs/fetch-utils

v3.0.2

Published

A utility library for using the Javascript Fetch API.

Downloads

1

Readme

fetchUtils

Build Status

FetchUtils is a utility module that adds the following features to the Javascript Fetch API:

  • Request timeouts, which the Fetch API currently lacks
  • Helper methods for requesting and parsing a JSON- or HTML-encoded response data from an endpoint

Demo

You can view a demo of fetchUtils here.

Install

As of version 3.0.0, fetchUtils is on NPM and can be installed with

npm install @degjs/fetch-utils

Usage

FetchUtils is a singleton, so it does not need to be instantiated.

import fetchUtils from "@degjs/fetch-utils";

/* Success handler */
function onSuccess(data) {
    console.log("SUCCESS: " + data);
}

/* Error handler */
function onError(error) {
    console.log("ERROR: " + error);
}

/* Perform a GET request with a 30-second timeout and expect a JSON-encoded response */
let fetchParams = {
	method: 'GET'
};

let options = {
	timeout: 30000
};

fetchUtils.getJSON('/endpoint-url', fetchParams, options)
    .then(onSuccess)
    .catch(onError);

Methods

.getHTML(url [, fetchParams [, options]])

Returns: Promise
Perform a request to an endpoint that returns HTML-encoded data. Sets the Accept option on request header to text/html.

.getJSON(url [, fetchParams [, options]])

Returns: Promise
Perform a request to an endpoint that returns JSON-encoded data. Sets the Accept option on request header to application/json.

.fetch(url [, fetchParams [, options]])

Returns: Promise
Perform a request to an endpoint that returns the full, unparsed endpoint response.

The parameters for the .getHTML(), .getJSON() and .fetch() methods are outlined below:

url

Type: String
The URL of the endpoint. This parameter is required.

fetchParams

Type: Object
Request options that are passed through to the fetch() method. These options correspond to those defined in the Fetch API. This parameter is optional.

options

Type: Object
Additional options that fall outside of the Fetch API. This parameter is optional.

options.timeout

Type: Number
The number of milliseconds that the request should wait before timing out. The default timeout value is 10,000 millseconds (10 seconds).

.setCallback(functionName)

Define a callback function that will be executed on fetch success. This is not recommended in most scenarios (use .then() and .catch() from the returned promise instead), but may be useful in specific situations.

.setOptions(options)

Override default plugin options, such as timeout and cachebusting. Must be called before any of the fetch methods above are called.

Browser Support

FetchUtils depends on the following browser APIs:

To support legacy browsers, you'll need to include polyfills for the above APIs.