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

better-cross-fetch

v0.2.0-dev.3

Published

A cross-platform (Browser and NodeJS) better "fetch" API. It's better because I made it.

Downloads

18

Readme

better-cross-fetch

Adding yet another more request making library to the pile. Works on NodeJS and browsers.

It's "better" because I made it.

...why? There are so many...

While I would love to use the native fetch API on browsers and use one of the many pre-existing polyfills for NodeJS, my issue is that sometimes I want to show progress bars for larger file uploads, and so building on top of good ol' XMLHttpRequest was the answer. And if I was going to make a web-request library anyway, might as well make it cross-platform.

Oh and it also supports content compression on the NodeJS side, isn't that neat?

Fine, how do I use this?

Like this.

const {betterCrossFetch} = require("better-cross-fetch")

const url = "https://aritzcracker.ca";
const options = {
	headers: {
		"some-header": "some-value";
	},
	responseType: "text",
	throwOnErrorStatus: true,
	onUploadProgress: (current, total) => {
		console.log("Uploaded", current, "out of", total, "bytes!")
	},
	onDownloadProgress: (current, total) => {
		console.log("Downloaded", current, "out of", total, "bytes!")
	},
}

await betterCrossFetch(url, options);

That will return something like this:

{
	status: 200,
	statusText: "OK",
	response: "<!DOCTYPE html>...",
	headers: {
		server: "nginx/xxx"
	},
	url: "https://www.aritzcracker.ca/" // The final URL after any redirects
}

What's the responseType property?

It chooses how to interpret the server response. Here are the possible values and their effects

  • "head" HEAD request, only gets the headers.
  • "text" The result's response property will be a string.
  • "json" The response will be parsed as JSON, the result's response property will be the resulting object. This also sets the accept request header to application/json.
  • "buffer" The result's response will be a Buffer.
    • If this option is used on browsers, a global Buffer class is expected. Consider setting your bundler to use buffer-lite!
  • "blob" The result's response property will be a Blob.
  • "dom" The result's response property will be a Document (Browser only).
  • "stream" The result's response property will be a Readable (readable stream) (NodeJS only).

Can I easily attach querystring data to the URL?

Yep! In fact, there's multiple ways. You can either use the URL object as your URL, or use the getData property of the options, which can be an object representing a string -> string map, an array of string pairs, or a URLSearchParams object. Note that using options.getData will over-write any pre-existing query data in the url argument.

How can I send POST data?

Take a look at the post property in your options and let your intellisense guide you. Use {type: "uri", data: {...}} for simple key-value pairs, or {type: "multipart", data: {...}} if you want to include binary data or files. There's also {type: "json", data: {...}} for API's that like that sorda thing. You're also free to put a FormData there if that's more convenient for you.

Any pitfalls?

There are 2 http statuses, 307 and 308, that require forms to be re-submitted at the redirect. POSTing NodeJS streams will break things when those redirects occur, because the stream has already been consumed on the first request, and this library doesn't provide a way to restart streams when this happens.