
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
fetch-rails
Advanced tools
Use GitHub's fetch library with Ruby on Rails. Based heavily on this wrapper to encapsulate some of the callback handling of HTTP status codes.
npm install fetch-rails --save
fetch(url, options).then((response) => response.json()).catch((response) => response.json())
// params = { q: { name: "Jhon" } }
fetch("apiUrl?q%5Bname%5D=Jhon", options).then((response) => response.json()).catch((response) => response.json())
fetch(url, {
method: 'POST',
body: JSON.stringify(body),
}).then((response) => response.json()).catch((response) => response.json())
With fetch-rails, it's more simple and you can getCSRF, encodeParams, checkStatus, set default json headers, set default credentials, and you can override all of this.
global override
import Fetch from "fetch-rails"
Fetch.defaultHeaders = () => ({
"X-Requested-With": 'XMLHttpRequest',
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer 1234',
})
or override by request
import Fetch from "fetch-rails"
Fetch.json("apiUrl", {}, {headers: { ...Fetch.defaultHeadersJSON, "Authorization": 'Bearer 1234'} })
Fetch.json('https://jsonplaceholder.typicode.com/posts')
.then( function( posts ){
console.log( posts ) // response in json
});
Send params without encoding
Fetch.json('https://jsonplaceholder.typicode.com/posts', { search: { name: "Jhon" }})
.then( function( posts ){
console.log( posts ) // response in json
});
Fetch.postJSON('https://jsonplaceholder.typicode.com/posts', {
title: 'foo',
body: 'bar',
userId: 1
})
.then( function( response ){
console.log(response) // response in json
}).catch( function( error ){
console.log(error) // error in json
});
Fetch.putJSON('https://jsonplaceholder.typicode.com/posts', {
title: 'foo',
body: 'bar',
userId: 1
})
.then( function( response ){
console.log(response) // response in json
}).catch( function( error ){
console.log( error ) // error in json
});
Fetch.deleteJSON('https://jsonplaceholder.typicode.com/posts')
.then( function( response ){
console.log(response) // response in json
}).catch( function( error ){
console.log(error) // error in json
});
Fetch.html('/api/get-html')
.then( function( response ){
document.body.innerHTML = response.data;
});
Fetch.text('/api/get-text')
.then( function( text ){
document.querySelector('.item').innerText = text;
});
The checkStatus function return a Promise and parse the error in json.
import Fetch from 'fetch-rails'
Fetch.postJSON('/comment', comment)
.then( (comment) => {
console.log(comment) // { text: "Hi" }
})
.catch( (errors) => {
console.log(errors) // { text: ["can't be blank] }
})
function checkStatus(response) {
return new Promise( (resolve, reject) => {
if(response.status >= 200 && response.status < 300) {
resolve(response)
}else {
response.json().then( (response_json) => {
reject(response_json)
})
}
})
}
import Fetch from 'fetch-rails'
Fetch.checkStatus = myFunction
FAQs
wrapper fetch with rails CSRF token.
We found that fetch-rails 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
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.