npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@rails/request.js

v0.0.9

Published

A tiny Fetch API wrapper that allows you to make http requests without need to handle to send the CSRF Token on every request

Downloads

370,588

Readme

Rails Request.JS

Rails Request.JS encapsulates the logic to send by default some headers that are required by rails applications like the X-CSRF-Token.

Install

Asset Pipeline

Install the requestjs-rails gem and follow the step described there.

Webpacker/Esbuild

npm

npm i @rails/request.js

yarn

yarn add @rails/request.js

How to use

Just import the FetchRequest class from the package and instantiate it passing the request method, url, options, then call await request.perform() and do what you need with the response.

Example:

import { FetchRequest } from '@rails/request.js'

....

async myMethod () {
  const request = new FetchRequest('post', 'localhost:3000/my_endpoint', { body: JSON.stringify({ name: 'Request.JS' }) })
  const response = await request.perform()
  if (response.ok) {
    const body = await response.text
    // Do whatever do you want with the response body
    // You also are able to call `response.html` or `response.json`, be aware that if you call `response.json` and the response contentType isn't `application/json` there will be raised an error.
  }
}

Shorthand methods

Alternatively, you can use a shorthand version for the main HTTP verbs, get, post, put, patch or destroy.

Example:

import { get, post, put, patch, destroy } from '@rails/request.js'

...

async myMethod () {
  const response = await post('localhost:3000/my_endpoint', { body: JSON.stringify({ name: 'Request.JS' }) })
  if (response.ok) {
    ...
  }
}

Request Options

You can pass options to a request as the last argument. For example:

post("/my_endpoint", {
  body: {},
  contentType: "application/json",
  headers: {},
  query: {},
  responseKind: "html"
})
body

This is the body for POST requests. You can pass in a Javascript object, FormData, Files, strings, etc.

Request.js will automatically JSON stringify the body if the content type is application/json.

contentType

When provided this value will be sent in the Content-Type header. When not provided Request.JS will send nothing when the body of the request is null or an instance of FormData, when the body is an instance of a File then the type of the file will be sent and application/json will be sent if none of the prior conditions matches.

headers

Adds additional headers to the request. X-CSRF-Token and Content-Type are automatically included.

credentials

Specifies the credentials option. Default is same-origin.

query

Appends query parameters to the URL. Query params in the URL are preserved and merged with the query options.

Accepts Object, FormData or URLSearchParams.

responseKind

Specifies which response format will be accepted. Default is html.

Options are html, turbo-stream, json.

Turbo Streams

Request.JS will automatically process Turbo Stream responses. Ensure that your Javascript sets the window.Turbo global variable:

import { Turbo } from "@hotwired/turbo-rails"
window.Turbo = Turbo

Since v7.0.0-beta.6 Turbo sets window.Turbo automatically.

Request Interceptor

To authenticate fetch requests (eg. with Bearer token) you can use request interceptor. It allows pausing request invocation for fetching token and then adding it to headers:

import { RequestInterceptor } from '@rails/request.js'
// ...

// Set interceptor
RequestInterceptor.register(async (request) => {
  const token = await getSessionToken(window.app)
  request.addHeader('Authorization', `Bearer ${token}`)
})

// Reset interceptor
RequestInterceptor.reset()

Before and after hooks

Wrap the request Promise with your own code. Just pure and simple JavaScript like this:

import { FetchRequest } from "@rails/request.js"
import { navigator } from "@hotwired/turbo"

function showProgressBar() {
  navigator.delegate.adapter.progressBar.setValue(0)
  navigator.delegate.adapter.progressBar.show()
}

function hideProgressBar() {
  navigator.delegate.adapter.progressBar.setValue(1)
  navigator.delegate.adapter.progressBar.hide()
}

export function withProgress(request) {
  showProgressBar()

  return request.then((response) => {
    hideProgressBar()
    return response
  })
}

export function get(url, options) {
  const request = new FetchRequest("get", url, options)
  return withProgress(request.perform())
}

Response

statusCode

Returns the response status.

ok

Returns true if the response was successful.

unauthenticated

Returns true if the response has a 401 status code.

authenticationURL

Returns the value contained in the WWW-Authenticate header.

contentType

Returns the response content-type.

html

Returns the html body, if the content type of the response isn't html then will be returned a rejected promise.

json

Returns the json body, if the content type of the response isn't json then will be returned a rejected promise.

headers

Returns the response headers.

Known Issues

FetchRequest sets a "X-Requested-With": "XmlHttpRequest" header. If you have not upgraded to Turbo and still use Turbolinks in your Gemfile, this means you will not be able to check if the request was redirected.

  const request = new FetchRequest('post', 'localhost:3000/my_endpoint', { body: JSON.stringify({ name: 'Request.JS' }) })
  const response = await request.perform()
  response.redirected // => will always be false.

License

Rails Request.JS is released under the MIT License.

© 2021 Basecamp, LLC.