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-railsNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Shows basic GET and POST JSON requests including CSRF token handling for Rails backends.
Use native fetch with rails-ujs CSRF tokens, or switch to axios with csrf interceptors.
Install whatwg-fetch and import it before fetch-rails in your entry file.
Ensure your Rails layout includes <%= csrf_meta_tags %> and that the meta tag name is 'csrf-token'.
Use ESM (import) or transpile with Babel/TypeScript.
Ensure a global fetch polyfill (like whatwg-fetch) is loaded and not shadowed by a local variable named fetch.
Insert <%= csrf_meta_tags %> in your Rails layout file (app/views/layouts/application.html.erb).
Use ES module import: import Fetch from 'fetch-rails'. If needed, use a bundler like Webpack or Browserify.
Ensure proper import order; import Fetch from 'fetch-rails' before using Fetch methods.