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

exfetch

v1.0.0

Published

Enhanced unfetch API.

Downloads

4,181

Readme

exfetch

Build Status BrowserStack Status

Enhanced unfetch API.

Features:

Install

npm install exfetch --save

Usage

Custom abort implementation

import exfetch from 'exfetch';

(async () => {
	const { request, abort, isAborted, onEvent } = exfetch('https://becky.com');
	let downloaded = 0;
	let uploaded = 0;

	onEvent('download', (e) => {
		if (e.lengthComputable) {
			downloaded = e.loaded / e.total;
		}
	});

	onEvent('upload', (e) => {
		if (e.lengthComputable) {
			uploaded = e.loaded / e.total;
		}
	});

	setTimeout(() => {
		// Will abort request after 2 seconds
		abort();
	}, 2000);

	const response = await request();

	if (isAborted()) {
		// Request aborted!
		return;
	}

	// Parse response as JSON
	const data = await response.json();
})();

"Abortable Fetch" approach

Using abort and isAborted export properties will throw error which instructs to use AbortController.abort() method and AbortSignal.aborted property respectively.

import exfetch from 'exfetch';

(async () => {
	const controller = new AbortController();
	const signal = controller.signal;
	const { request, onEvent } = exfetch('https://becky.com', { signal });
	let downloaded = 0;
	let uploaded = 0;

	onEvent('download', (e) => {
		if (e.lengthComputable) {
			downloaded = e.loaded / e.total;
		}
	});

	onEvent('upload', (e) => {
		if (e.lengthComputable) {
			uploaded = e.loaded / e.total;
		}
	});

	setTimeout(() => {
		// Will abort request after 2 seconds
		controller.abort();
	}, 2000);

	try {
		const response = await request();
		// Parse response as JSON
		const data = await response.json();
	} catch (error) {
		if (error.name === 'AbortError') {
			// Request aborted!
			return;
		}
	}
})();

API

exfetch(url, [options])

Returns: Object

See unfetch API documentation for arguments.

Returns API object with following properties:

request

Type: Function
Returns: Promise

Returns request Promise.

onEvent(eventName, handler)

Type: Function
Returns: Function

Wrapper around progress event.

| Event name | Original handler | | ---------- | ----------------------- | | download | xhr.onprogress | | upload | xhr.upload.onprogress |

Handler receives one argument which is original Event.

Returns function for unlistening event.

abort

Type: Function
Returns: undefined

Aborts request.

This has effect only with custom abort implementation. If you use this method with "abortable Fetch" approach, it will throw error telling you to use AbortController.abort() instead.

isAborted

Type: Function
Returns: boolean

Check if current request is aborted.

This has effect only with custom abort implementation. If you use this method with "abortable Fetch" approach, it will throw error telling you to use AbortSignal.aborted instead.

Questions

Why two different abort implementations?

Original GitHub issue on aborting Fetch is rather long and it culminated with generic AbortController approach not connected only with Fetch, which is great for any kind of abortable operations.

But XHR already has simple solution for aborting requests and it would be shame not to use that.

I wanted to support both approaches, but they have differences in how they are resolved.

For "abortable Fetch" approach, request Promise is rejected with AbortError following standard implementation.

For custom abort approach, request Promise is resolved/fulfilled to response object following XHR abort operation sequence with ok set to false and status set to 0.

Browser support

Tested in IE11+ and all modern browsers, assuming Promise is available (polyfill).

If you want to use "abortable Fetch" approach, you also need to have AbortController available (polyfill).

Test

For automated tests, run npm run test:automated (append :watch for watcher support).

License

MIT © Ivan Nikolić