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

real-fetch

v0.3.0

Published

Native fetch + interceptors + timeouts + more...

Downloads

2

Readme

Real Fetch

Decorates (native) fetch with support for interceptors and timeouts.

Installation

npm install real-fetch --save

Usage

import { create, addInterceptor, removeAllInterceptors } from "real-fetch";
// create decorated fetch
// see the note for other than browser environments and glob replacement
const fetch = create(window.fetch);

// register interceptor
const remove = addInterceptor({
	request(url, config) {
		console.log("request", url, config);
		return [url, config];
	},
	requestError(error, url, config) {
		console.log("requestError", error, url, config);
		// return [url, config] to recover from error or throw something
		return [url, config];
	},
	response(response, url, config) {},
	responseError(error, url, config) {
		// return value to recover, or throw something
		return false;
	},
});

// Fetch with custom timeout
fetch("http://www.example.org", { timeout: 5000 })
	.then(response => {
		console.log(response.text());
	})
	.catch(error => {
		if (error instanceof AbortError) {
			console.log("Timeout!");
		}
	});

// remove previously added interceptor
remove();

// remove all
removeAllInterceptors();

Note If you need to intercept or timeout native fetch() in other components, you can define function which replaces native global:

import { create } from "real-fetch";
export default function attach(env) {
	env.fetch = create(env.fetch);
}

and then call it before:

import attach from "./attach";
attach(window);

Description

You'll quickly notice if you try using native fetch, that some parts required for real use are missing:

  • interceptors (to make error handling global)
  • custom connection timeout (to fail fast instead of waiting forever)
  • upload and download progress observation

The library decorates (native) fetch with support for interceptors and timeouts.

Upload progress observation issue is not solvable at the moment.

API

create

Creates decorated fetch() with support for interceptors and timeouts.

Syntax

create(native_fetch);

Parameters

Returns

Decorated fetch() with interceptors and timeouts.

Returned function signature:

fetch(url, config);

Parameters:

  • url - URL the user should be redirected to after successful authentication.
  • config - Same as in fetch() + additional property:
    • timeout - custom connection timeout in ms. Defaults to browser timeout.

Returns:

Promise as in Fetch API. Example

const fetch = create(window.fetch);
fetch("http://www.example.org", { timeout: 5000 })
	.then(response => {
		console.log(response.text());
	})
	.catch(error => {
		if (error instanceof AbortError) {
			console.log("Timeout!");
		}
	});

addInterceptor

Adds interceptor to the end of (promise) chain. The interceptors are invoked in order of addition.

Syntax

addInterceptor(interceptor);

Parameters

  • interceptor - Object with following callback methods:
    • request(url, config) - enable url/config preprocessing. Returns [url, config]
    • requestError(error, url, config) - called when processing of request() callback result in error.
    • response(response, url, config) - called after resolved fetch. Should either return response, or throw an error.
    • responseError(error, url, config)- called after rejected fetch (or previous interceptor). Should either return Response to recover, or (re)throw an error.

Returns

Function used to remove the interceptor.

Example

// register interceptor
const remove = addInterceptor({
	request(url, config) {
		console.log("request", url, config);
		return [url, config];
	},
	requestError(error, url, config) {
		console.log("requestError", error, url, config);
		// return [url, config] to recover from error or throw something
		return [url, config];
	},
	response(response, url, config) {},
	responseError(error, url, config) {
		// return value to recover, or throw something
		return false;
	},
});

// Fetch with custom timeout
fetch("http://www.example.org");

// remove previously added interceptor
remove();

removeAllInterceptors

Removes all previously added interceptors.

Syntax

removeAllInterceptors();

Example

addInterceptor({
	response(response, url, config) {
		//...
	},
});

removeAllInterceptors();

Acknowledgement

Based on the ideas from:

License

Apache License, Version 2.0