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

fetch-please

v0.4.3

Published

HTTP-transport that supports Promises and cancelable requests (XHR)

Downloads

19

Readme

fetch-please.js Build Status

HTTP-transport that supports Promises and cancelable requests (XHR). Great for React!

Moreover it's extra small (minified+gzipped less than 2.5Kb).

Justification

Handling requests with promises is really easy and convenient. But if you need to abort your request you can't do it with fetch. Promises doesn't cancelable outside of the constructor.

This library provides simple API with cancelable requests and XHR under the hood.

Install

npm install fetch-please --save

Examples

import React from 'react';
import FetchPlease from 'fetch-please';

let api = new FetchPlease('/api/', {
	/* Settings here, see list of available settings below */
});

let SmartComponent = React.createClass({
	getInitialState() {
		return {
			loading: false,
			error: null,
			users: []
		};
	},

	/**
	 * Fetch state (list of users) for this component
	 */
	componentWillMount() {
		api
			.get('users/', {
				limit: 20,
				offset: 10
			})
			.then((json) => {
				// List of users successfully received
				this.setState({
					loading: false,
					users: json.users
				});
			})
			.catch((error) => {
				// Oops, something goes wrong
				this.setState({
					error,
					loading: false
				});
			});

		// Wait response
		this.setState({loading: true});
	},

	/**
	 * Our component was unexpectedly unmounted
	 * Application doesn't need in requested data
	 */
	componentWillUnmount() {
		// Abort all opened requests for this instance of FetchPlease
		api.abort();
	}
});

If you want to abort individual request, you can do this with special API:

// ... same part in the previous example

	componentWillMount() {
		// It's another API, that returns XHR object as a Promise instance
		let {xhr, promise} = api.getRequest('users/');

		promise
			.then((json) => {
				/* Save users in state */
			})
			.catch((error) => {
				/* Save error in state */
			});

		// Save necessary request
		this.xhr = xhr;
		this.setState({loading: true});
	},

	componentWillUnmount() {
		// Abort individual requst with saved XHR object
		this.xhr.abort();
	}

// ... same part in the previous example

Requirements

ES5 compatible besides Promises. Use polyfill for ES6 Promises.

If you need ES3 compatible version use polyfills for JSON, Object.keys, Array.prototype.indexOf, Array.prototype.map, Array.prototype.reduce, Array.prototype.filter, Array.prototype.forEach.

API

Constructor

FetchPlease([String path], [Object settings]);

For example:

let api = new FetchPlease('/api/', {
	timeout: 3000, // 3s
	headers: {
		'Content-Type': 'application/json',
		'X-Custom-Header': 'custom'
	}
});

Settings

timeout = 0

A number of milliseconds a request can take before automatically being terminated. The value of 0 means there is no timeout.

headers = {}

An object with HTTP headers for all requests.

handleResponse

Takes XHR object as a single argument and returns it if response looks like acceptable.

Example of custom response handler:

let api = new FetchPlease('/api/', {
	handleResponse(xhr) {
		if (xhr.status !== 200) {
			throw new Error('Nooooooo!');
		}

		return xhr;
	}
});

handleJson

Takes XHR object as a single argument and returns object corresponding to the given JSON in responseText. Invokes if response header Content-Type: application/json exists.

Methods

get() and getRequest()

Promise get(String url, [Object params,] [Object settings])

Sends GET request with optional parameters and same optional settings (see above for constructor). Returns an instance of Promise.

Object getRequests(String url, [Object params,] [Object settings])

Sends GET request. Returns object with two properties: xhr (instance of XMLHttpRequest) and promise (instance of Promise).

Example:

let api = new FetchPlease('/api/');
api
	.get('users', {limit: 10, offset: 50}) // sends GET request on /api/users?limit=10&offset=50
	.then((data) => {
		console.log(data);
	})
	.catch((error) => {
		console.error(error);
	});

post() and postRequest()

Promise post(String url, [Object data,] [Object settings])
Object postRequest(String url, [Object data,] [Object settings])

Sends data as a body of HTTP request. If a data object is an instance of Blob or FormData or String, it will be sent without transformation. Otherwise, data will be sent as serialized JSON string.

Example:

let api = new FetchPlease('/api/');
api
	.post('users', {name: 'Mary', surname: 'Brown'}) // sends POST request on /api/users
	.then((data) => {
		console.log(data);
	})
	.catch((error) => {
		console.error(error);
	});

put() and putRequest()

Promise put(String url, [Object data,] [Object settings])
Object putRequest(String url, [Object data,] [Object settings])

As well as post() and postRequest().

delete() and deleteRequest()

Promise delete(String url, [Object params,] [Object settings])
Object deleteRequests(String url, [Object params,] [Object settings])

As well as get() and getRequest().

abort()

Aborts all opened requests for appropriate FetchPlease instance. Example:

let api = new FetchPlease('/api/');

api
	.get('users')
	.catch((error) => {
		console.error(error); // Error['Resource has been aborted']
	});

api.abort();