Registry / security / fetch-rails

fetch-rails

JSON →
library0.8.3jsnpmunverified

A lightweight (4kB) wrapper around the GitHub fetch polyfill that automatically includes Rails CSRF tokens in requests. Current version 0.8.3 (last release 2019, low activity). Simplifies JSON, HTML, and text requests for Rails apps by handling CSRF token extraction, parameter encoding, and HTTP status checks. Compared to plain fetch or axios, it provides Rails-specific defaults like JSON headers and credential inclusion.

security
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ESM default export; CJS require gives the default object, not a named export.

import Fetch from 'fetch-rails'

Methods are on the default export, not separate named exports.

import Fetch from 'fetch-rails' Fetch.json(url, options)

The method expects data as second argument, not a body option.

import Fetch from 'fetch-rails' Fetch.postJSON(url, data)

Shows basic GET and POST JSON requests including CSRF token handling for Rails backends.

import Fetch from 'fetch-rails'; // GET JSON Fetch.json('https://jsonplaceholder.typicode.com/posts/1') .then(post => console.log(post)) .catch(err => console.error(err)); // POST JSON Fetch.postJSON('https://jsonplaceholder.typicode.com/posts', { title: 'foo', body: 'bar', userId: 1 }).then(post => console.log(post)).catch(err => console.error(err)); // With Rails CSRF auto-inclusion Fetch.json('/api/resource', { search: { q: 'term' } }) .then(data => console.log(data));
Debug
Known footguns
deprecatedThe package is no longer actively maintained; last release was in 2019. Consider using modern alternatives or maintain a fork.
gotchaThe package does not include a fetch polyfill; you must provide it separately (e.g., whatwg-fetch) for older browsers like IE 9.
gotchaCSRF token extraction relies on a meta tag named 'csrf-token' in the DOM. If the tag is missing or named differently, requests may fail.
breakingThe package uses ES modules (import/export) but does not provide a CommonJS build. Direct require() in Node.js will fail.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
18 hits · last 30 days
gptbot
3
amazonbot
3
bytedance
3
ahrefsbot
1
Resources