GithubHelp home page GithubHelp logo

fetchival's Introduction

fetchival.js Travis

Makes writing JSON requests with fetch easier

Fetchival is a tiny (0.5kb min/gz) fetch wrapper that can be used in the browser (IE9+) and Node.

Before

// POST /users
fetch('/users', {
  method: 'post',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    name: 'Typicode',
    login: 'typicode',
  })
})
.then(function(response) {
  if (response.status >= 200 && response.status < 300) {
    return response.json()
  }
  throw new Error(response.statusText)
})
.then(function(json) {
  // ...
})

After

// POST /users
fetchival('/users').post({
  name: 'Typicode',
  login: 'typicode'
})
.then(function(json) {
  // ...
})

.get(), .put(), .patch() and .delete() methods are also available.

Installation

Fetchival is available on Bower and npm

Browser

bower install es6-promise fetch # polyfills
bower install fetchival
npm install es6-promise whatwg-fetch --save # polyfills
npm install fetchival --save # Browserify

Node

npm install node-fetch fetchival --save

Usage examples

var posts = fetchival('/posts')

//posts
posts.get()
posts.post({ title: 'Fetchival' })

//posts?category=javascript
posts.get({ category: 'javascript' })

//posts/1
posts(1).get()
posts(1).put({ title: 'Fetchival is simple' })
posts(1).patch({ title: 'Fetchival is simple' })
posts(1).delete()

var comments = posts('1/comments')

//posts/1/comments
comments.get()

//posts/1/comments/1
comments(1).get()

You can also pass fetch options to fetchival()

var posts = fetchival('/posts', fetchOptions)
var comments = posts('1/comments') // Will inherit fetchOptions

To catch errors

fetchival('/posts')
  .get()
  .catch(function(err) {
    console.log(err)
  })

To enable CORS

var request = fetchival('/', { mode: 'cors' })
var posts = request('posts')

To fetch plain text (for example, HTML views)

var request = fetchival('/', { responseAs: 'text' })
var posts = request('posts')

responseAs can be response, text or json (default)

To use fetchival in Node, you need to install node-fetch and configure fetchival to use it

var fetchival = require('fetchival')
fetchival.fetch = require('node-fetch')

Browser Support

Chrome Firefox IE Opera Safari
Latest ✔ Latest ✔ 9+ ✔ Latest ✔ 6.1+ ✔

License

MIT - Typicode

fetchival's People

Contributors

typicode avatar gfelizola avatar pedropinheiro avatar klardotsh avatar gustavobigardi avatar termina1 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.