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

fetchutils

v0.0.1

Published

Make the `fetch` API great again! This library is a very simple interface on top of the `fetch` API - so that you don't need to parse stuff or deal with issues.

Downloads

13

Readme

fetchutils

Make the fetch API great again! This library is a very simple interface on top of the fetch API - so that you don't need to parse stuff or deal with issues.

This library doesn't include a polyfill - if you need one, make sure to import it as well! isomorphic-fetch is a great alternative.

Usage

First, install it:

$ yarn add fetchutils # or npm install --save fetchutils

Then, use it!

import { get, post, put, del } from 'fetchutils';

function getUsers() {
    return get('/api/users');
}

function saveUser(user) {
    return post('/api/users', user);
}

function updateUser(user), {
    return put(`/api/users/${user.id}`, user);
}

function deleteUser(user) {
    return del(`/api/users/${user.id}`);
}

Each of the methods returns a promise, with the first argument being the parsed result (either text or JSON, based on content type). The method will throw if the response code is >= 400, also then with the parsed result.

The error thrown is of the type FetchError, with the following properties:

{
    message,    // The status code description
    status,     // The status code
    details,    // The parsed response from the server
}

Send data

You can send data with your request as the second argument to each function. If the method is get, the argument will be turned into get-parameters. Otherwise, the second argument is run through JSON.stringify() and sent as the body.

Example:

import { get, post } from 'fetchutils';

// Produces a get-request for /api/users?search=stuff&limit=10
function searchUsers(query, limit = 10) {
    return get('/api/users', { search: query, limit });
}

// Produces a post-request with {"name": "Scott", "age": 42} as a body
function addUser(name, age) {
    return post('/api/users', { name, age });
}

But...

Yep, this is pretty opinionated. So if you want to override or add headers, or send some more parameters to the fetch function - send all of those in as a third parameter. Here's an example:

import { get } from 'fetchutils';

function getUsersFromMyApp() {
    return get('http://www.some-api.io/api/users', {
        headers: { 'X-Requested-By': 'my-application' },
        mode: 'cors',
    });
}

That's it!

This library is small on purpose. If you need a feature I haven't thought of, create a new issue, and - if you're cool - submit a pull request.