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 /usersfetch('/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){returnresponse.json()}thrownewError(response.statusText)}).then(function(json){// ...})After
// POST /usersfetchival('/users').post({name: 'Typicode',login: 'typicode'}).then(function(json){// ...}).get(), .put(), .patch() and .delete() methods are also available.
Fetchival is available on Bower and npm
Browser
bower install es6-promise fetch # polyfills
bower install fetchivalnpm install es6-promise whatwg-fetch --save # polyfills
npm install fetchival --save # BrowserifyNode
npm install node-fetch fetchival --savevarposts=fetchival('/posts')//postsposts.get()posts.post({title: 'Fetchival'})//posts?category=javascriptposts.get({category: 'javascript'})//posts/1posts(1).get()posts(1).put({title: 'Fetchival is simple'})posts(1).patch({title: 'Fetchival is simple'})posts(1).delete()varcomments=posts('1/comments')//posts/1/commentscomments.get()//posts/1/comments/1comments(1).get()You can also pass fetch options to fetchival()
varposts=fetchival('/posts',fetchOptions)varcomments=posts('1/comments')// Will inherit fetchOptionsTo catch errors
fetchival('/posts').get().catch(function(err){console.log(err)})To enable CORS
varrequest=fetchival('/',{mode: 'cors'})varposts=request('posts')To fetch plain text (for example, HTML views)
varrequest=fetchival('/',{responseAs: 'text'})varposts=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
varfetchival=require('fetchival')fetchival.fetch=require('node-fetch')![]() | ![]() | ![]() | ![]() | ![]() |
|---|---|---|---|---|
| Latest ✔ | Latest ✔ | 9+ ✔ | Latest ✔ | 6.1+ ✔ |
MIT - Typicode




