1.1.0 • Published 7 years ago

ifetch v1.1.0

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

ifetch

A tiny fetch lib to use ajax based on promise

Install

  $ npm install ifetch

Usage

Extending some common configurations based on ifetch:

// Usage
const request = require('ifetch');

function errHandler(err) {
  if (err.status === 401) {
    window.location = '/auth/logout';
  }
  return new Promise(function(resolve, reject) {
    reject(err);
  });
}

let fetch = {};

['get', 'post', 'put', 'delete'].forEach((m) => {
  fetch[m] = function(options) {
    var opt = Object.assign({
      dataType: 'json',
      contentType: 'application/json',
      headers: {
        'X-REGION': 'ext'
      }
    }, options);

    return request[m](opt).catch(errHandler);
  };
});

module.exports = fetch;

License

ifetch is available under the terms of the MIT license.