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

wise-fetch

v0.4.0-1

Published

Feature-rich node-fetch

Downloads

28

Readme

wise-fetch

npm version Build Status codecov

Feature-rich node-fetch:

const wiseFetch = require('wise-fetch');

(async () => {
  const response = await wiseFetch('https://example.org');

  response.status; //=> 200
  response.headers.get('content-length'); //=> '606'

  const text = await response.text();
  //=> '<!doctype html>\n<html>\n<head>\n    <title>Example Domain</title> ...'
})();

Installation

Use npm.

npm install wise-fetch

API

const wiseFetch = require('wise-fetch');

wiseFetch(url [, options])

url: string | URL (HTTP or HTTPS URL)
options: Object
Return: Promise<Response>

The API is very similar to the browser fetch API. It makes an HTTP or HTTPS request and returns a Promise of a node-fetch-npm Response object that works as if DOM Response but has additional methods.

Unlike the fetch API, when the response is unsuccessful, that is, its status code is neither 2xx, 304, it will be rejected with an Error with a response property.

(async () => {
  try {
    await wiseFetch('https://github.com/shinnn/it_does_not_exist');
  } catch (err) {
    err.message; //=> '404 (Not Found) responded by a GET request to https://github.com/shinnn/it_does_not_exist.'
    err.reponse.status; //=> 404
    await err.reponse.arrayBuffer(); //=> ArrayBuffer { ... } (the response body)
  }
})();

The response is cached to the OS's default directory for temporary files in the RFC 7234 compliant way.

options

It supports the all options that make-fetch-happen can receives, except for counter and cacheManager.

When the program is running as an npm script, note that:

  • proxy option defaults to the value of https-proxy or proxy npm config depending on the request protocol.
  • noProxy option defaults to no-proxy npm config.

Additionally, the following wise-fetch specific options are available.

options.baseUrl

Type: string | URL

Set the base URL to resolve against if the request URL is not absolute.

(async () => {
  const response = await wiseFetch('~shinnn', {baseUrl: 'https://www.npmjs.com'});
  response.url; //=> 'https://www.npmjs.com/~shinnn'
})();
options.resolveUnsuccessfulResponse

Type: boolean
Default: false

Return a resolved Promise even if the response is unsuccessful.

(async () => {
  const response = await wiseFetch('https://github.com/shinnn/this_does_not_exist', {
    resolveUnsuccessfulResponse: true
  });

  response.statusText; //=> 'Not Found'
})();
options.signal

Type: AbortSignal

Allow a user to abort the request via the corresponding AbortController. Read the article about abortable fetch for more details.

Currently Node.js doesn't support AbortController, so that users need to substitute the userland implementation for it.

const AbortController = require('abort-controller');

const abortController = new AbortController();

(async () => {
  try {
    await wiseFetch('https://loclahost:5000/very_large_contents', {signal: abortController.signal});
  } catch (err) {
    err.message; //=> '... The GET request to https://loclahost:5000/very_large_contents was aborted'
  }
})();

setTimeout(() => abortController.abort(), 1000);
options.userAgent

Type: string

A shorthand for setting user-agent property of headers option.

wiseFetch.create(baseOptions)

baseOptions: Object
Return: Function

Create a new wiseFetch with the given defaults.

const getGithubUserData = wiseFetch.create({
  baseUrl: 'https://api.github.com/users/',
  headers: {
    accept: 'application/vnd.github.v3+json',
    'user-agent': 'your app name'
  }
});

(async () => {
  await (await getGithubUserData('shinnn')).json();
  //=> {login: 'shinnn', id: 1131567, created_at: '2011-10-16T16:36:43Z', ...}
})();

headers of each function call will be merged to the base headers.

const newWiseFetch = wiseFetch.create({
  headers: {
    'header-A': 'old value'
    'header-B': 'value'
  }
});

newWiseFetch('https://example.org', {
  headers: {
    'header-A': 'updated value',
    'header-C': 'new value'
  }
});
/* The final `header` is {
  'header-A': 'updated value',
  'header-B': 'value'
  'header-C': 'new value'
}. */
baseOptions.frozenOptions

Type: Set<string>

Make given options unconfigurable in each function call.

const alwaysPost = wiseFetch.create({
  method: 'post',
  frozenOptions: new Set(['method'])
});

(async () => {
  try {
    await alwaysPost('https://example.org/api', {method: 'patch'});
  } catch (err) {
    err.toString();
    //=> TypeError: 'method' option is not configurable, but it was tried to be configured.
  }
})();
baseOptions.urlModifier

Type: Function

Update a request URL to the value this function returns, before applying baseUrl option to it.

This function receives the original URL and is expected to return a string or URL.

(async () => {
  const response = await wiseFetch('https://example.org/', {urlModifier: url => `${url}?x=1`});
  response.url; //=> 'https://example.org/?x=1'
})();
baseOptions.additionalOptionValidators

Type: Array<Function>

An array of functions that performs additional option validation. Each functions receive an options Object and if at least one of then throws an error, wiseFetch will be rejected.

const {username} = require('os').userInfo();

const forceYourUA = wiseFetch.create({
  additionalOptionValidators: [
    options => {
      if (options.userAgent && !options.userAgent.includes(username)) {
        throw new Error('user agent must include your name!');
      }
    }
  ]
});

forceYourUA('https://example.org', {userAgent: 'nothing'});
// rejected with an Error: 'user agent must include your name!'

wiseFetch.ABORT_ERROR_CODE

Type: integer

An error code that wiseFetch adds to the error when the request is aborted.

(async () => {
  try {
    await wiseFetch('https://example.org/', {signal: someSignal});
  } catch (err) {
    if (err.code === wiseFetch.ABORT_ERROR_CODE) {
      console.log('Canceled');
    } else {
      throw err;
    }
  }
})();

wiseFetch.CACHE_DIR

Type: string

A path of the directory where wise-fetch saves response cache.

Users can clear cache by deleting this directory.

License

ISC License © 2018 - 2019 Shinnosuke Watanabe