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

fetch-utils

v4.0.3

Published

Modern client request sending method - Fetch, both work for node and browser.

Downloads

26

Readme

fetch-utils

Join the chat at https://gitter.im/zslucky/fetch-utils Build Status Coverage Status Inline docs

This is a configurabled lib for isomorphic-fetch that we can add a global default configuration for every requests, aslo provide some packed methods that we can do request more easily.

We are followed SamVer 2.0, this started from version 2.0.0, then we will try our best to avoide the dependency hell.


Install

npm i fetch-utils --save

Or use yarn

yarn add fetch-utils

Usage

For configruation: (Every options please refer to fetch)

/*
 *  // the default setting, override it if necessary
 *  headers: {
 *      'Accept': 'application/json',
 *      'Content-Type': 'application/json'
 *  }
 */
import { setConfig } from 'fetch-utils';

class NotFoundError extends Error {}

const config = {
    /*
     * Can set the response type, default is json.
     * You can config it in any single request to override it.
     * Response type can be follows:
     *   json, text, formData, blob, arrayBuffer
     */
    responseType: 'json',
    errorHandlers: {
      404: NotFoundError
      // Other error.
    }
};

// This setting will reflect to every requests, it's a global setting.
setConfig(config);

For methods:

import { doGet, doPut, doPost, doDelete } from 'fetch-utils';

// Every method will reture a Promise instance
const promise = doGet(param);
const promise = doPut(param);
const promise = doPost(param);
const promise = doDelete(param);

e.g.
/*
 * data type is defined by attribute `responseType`
 * err is instance of Error, can be all of child class which super class is Error
 */
promise
  .then(function(data) {
    //... the body data
  })
  .catch(function(error) {
    //... error object
  });

error is an instance of Error, can be pass any child Error class which extends Error, if you are using babel to suppoert builtin extend, you should add transform-builtin-extend plugin for babel add config in .babelrc to add Error in global.

param can be string or object.

  1. string: the request url.

  2. object: the request option that contain the url and other settings.

// e.g.
// Get a user which id is 1.
doGet('http://www.yourdomain.com/api/v1/user/1?base=true&show=false');
// Delete a user which id is 1.
doDelete('http://www.yourdomain.com/api/v1/user1');

// the same
doGet({
    url: 'http://www.yourdomain.com/api/v1/user/1?base=true&show=false'
});

doDelete({
    url: 'http://www.yourdomain.com/api/v1/user/1'
});

doPut({
    url: 'http://www.yourdomain.com/api/v1/user/1',
    body: new FormData();
});

doPost({
    url: 'http://www.yourdomain.com/api/v1/user/1',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    }
    body: JSON.stringfy({
        name: 'newName'
    });
});

/*
 * multipart/form-data
 * single file example, multi-file is the same.
 */
var dataBean = new Blob(
  [JSON.stringify({user: 'user1'})],
  {type : 'application/json'}
);

var file = $('#file')[0].files[0];

var formData = new FormData();

formData.append('bean', dataBean);
formData.append('file', file);

// content-type can be ignored.
doPost({
    url: 'http://www.yourdomain.com/api/v1/user/1/upload',
    body: formData
});