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.

npm install fetch-rails
INSTALL
IMPORT
SIG · FETCH-RAILS
F
fetch-rails
securityjavascriptv0.8.3
harness data pending
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.

default (Fetch)
✓ import Fetch from 'fetch-rails'
✗ const { Fetch } = require('fetch-rails')
ESM default export; CJS require gives the default object, not a named export.
Fetch.json
✓ import Fetch from 'fetch-rails' Fetch.json(url, options)
✗ import { json } from 'fetch-rails'
Methods are on the default export, not separate named exports.
Fetch.postJSON
✓ import Fetch from 'fetch-rails' Fetch.postJSON(url, data)
✗ Fetch.postJSON(url, { body: data })
The method expects data as second argument, not a body option.

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 issues
deprecatedThe package is no longer actively maintained; last release was in 2019. Consider using modern alternatives or maintain a fork.
fix
Use native fetch with rails-ujs CSRF tokens, or switch to axios with csrf interceptors.
affects: >=0.8.3
gotchaThe package does not include a fetch polyfill; you must provide it separately (e.g., whatwg-fetch) for older browsers like IE 9.
fix
Install whatwg-fetch and import it before fetch-rails in your entry file.
affects: *
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.
fix
Ensure your Rails layout includes <%= csrf_meta_tags %> and that the meta tag name is 'csrf-token'.
affects: *
breakingThe package uses ES modules (import/export) but does not provide a CommonJS build. Direct require() in Node.js will fail.
fix
Use ESM (import) or transpile with Babel/TypeScript.
affects: >=0.8.0
Errors
Common errors & fixes
Uncaught TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
fetch-rails relies on the global fetch function, which may be overwritten or unavailable in certain environments (e.g., using a polyfill that isn't properly bound).
fix
Ensure a global fetch polyfill (like whatwg-fetch) is loaded and not shadowed by a local variable named fetch.
Uncaught TypeError: Cannot read property 'getAttribute' of null
The meta tag with name 'csrf-token' is not present in the DOM when fetch-rails tries to read the CSRF token.
fix
Insert <%= csrf_meta_tags %> in your Rails layout file (app/views/layouts/application.html.erb).
Uncaught ReferenceError: require is not defined
Using require('fetch-rails') in a browser environment that doesn't support CommonJS modules.
fix
Use ES module import: import Fetch from 'fetch-rails'. If needed, use a bundler like Webpack or Browserify.
Uncaught TypeError: Fetch.json is not a function
Trying to call Fetch.json before the import completes (e.g., due to circular dependencies or incorrect import).
fix
Ensure proper import order; import Fetch from 'fetch-rails' before using Fetch methods.
Upgrade
Version history
0.8.3latest on npm
Audit
Dependencies
whatwg-fetchoptionalpolyfill for fetch API in older browsers (IE 9+) – intended as peer dependency
Agent activity
28 hits · last 30 days
node
26
OpenAI (training)
1
Resources
fetch-rails — npm install fetch-rails · libregistry