
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
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.
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
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')
![]() | ![]() | ![]() | ![]() | ![]() |
---|---|---|---|---|
Latest ✔ | Latest ✔ | 9+ ✔ | Latest ✔ | 6.1+ ✔ |
MIT - Typicode
FAQs
Makes JSON requests with fetch easier
We found that fetchival demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.