Socket
Socket
Sign inDemoInstall

@rails/request.js

Package Overview
Dependencies
0
Maintainers
12
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.2 to 0.0.3

.github/ISSUE_TEMPLATE/bug_report.md

2

package.json
{
"name": "@rails/request.js",
"version": "0.0.2",
"version": "0.0.3",
"description": "A tiny Fetch API wrapper that allows you to make http requests without need to handle to send the CSRF Token on every request",

@@ -5,0 +5,0 @@ "main": "./src/index.js",

@@ -18,3 +18,3 @@ # Rails Request.JS

Just import the `Request` class from the package and instantiate it passing the request `method`, `url`, `options`, then call `await request.perform()` and do what do you need with the response.
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 do you need with the response.

@@ -24,3 +24,3 @@ Example:

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

@@ -30,3 +30,3 @@ ....

async myMethod () {
const request = new Request('post', 'localhost:3000/my_endpoint', { body: { name: 'Request.JS' }})
const request = new FetchRequest('post', 'localhost:3000/my_endpoint', { body: { name: 'Request.JS' }})
const response = await request.perform()

@@ -41,2 +41,60 @@ if (response.ok) {

#### Shorthand methods
Alternatively, you can use a shorthand version for the main HTTP verbs, `get`, `post`, `put`, `patch` or `destroy`.
Example:
```js
import { get, post, put, patch, destroy } from '@rails/request.js'
...
async myMethod () {
const response = await post('localhost:3000/my_endpoint', { body: { name: 'Request.JS' }})
if (response.ok) {
...
}
}
```
#### Turbo Streams
Request.JS will automatically process Turbo Stream responses. Ensure that your Javascript sets the `window.Turbo` global variable:
```javascript
import { Turbo } from "@hotwired/turbo-rails"
window.Turbo = Turbo
```
#### 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:
```javascript
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()
```
# 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.
```js
const request = new FetchRequest('post', 'localhost:3000/my_endpoint', { body: { name: 'Request.JS' }})
const response = await request.perform()
response.redirected // => will always be false.
```
# License

@@ -43,0 +101,0 @@

@@ -1,4 +0,6 @@

import { Request } from './request'
import { Response } from './response'
import { FetchRequest } from './fetch_request'
import { FetchResponse } from './fetch_response'
import { RequestInterceptor } from './request_interceptor'
import { get, post, put, patch, destroy } from './verbs'
export { Request, Response }
export { FetchRequest, FetchResponse, RequestInterceptor, get, post, put, patch, destroy }
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc