@rails/request.js
Advanced tools
Comparing version 0.0.2 to 0.0.3
{ | ||
"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 } |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
11148
11
207
101
1