Skip to content

Commit 7ddd9ee

Browse files
authored
Merge branch 'main' into shorthand-verbs
2 parents c7a6374 + 72bb933 commit 7ddd9ee

File tree

6 files changed

+80
-3
lines changed

6 files changed

+80
-3
lines changed

.github/workflows/ci.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: CI
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
qa:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v2
10+
- uses: actions/setup-node@v2
11+
with:
12+
node-version: '12'
13+
- uses: actions/cache@v2
14+
with:
15+
path: node_modules
16+
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
17+
- run: yarn install
18+
- name: Lint
19+
run: yarn lint

README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Just import the `FetchRequest` class from the package and instantiate it passing
2020
Example:
2121

2222
```js
23-
import { Request } from '@rails/request.js'
23+
import { FetchRequest } from '@rails/request.js'
2424

2525
....
2626

@@ -44,6 +44,35 @@ import { Turbo } from "@hotwired/turbo-rails"
4444
window.Turbo = Turbo
4545
```
4646

47+
#### Request Interceptor
48+
49+
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:
50+
51+
```javascript
52+
import { RequestInterceptor } from '@rails/request.js'
53+
// ...
54+
55+
// Set interceptor
56+
RequestInterceptor.register(async (request) => {
57+
const token = await getSessionToken(window.app)
58+
request.addHeader('Authorization', `Bearer ${token}`)
59+
})
60+
61+
// Reset interceptor
62+
RequestInterceptor.reset()
63+
```
64+
65+
# Known Issues
66+
67+
`FetchRequest` sets a `"X-Requested-With": "XmlHttpRequest"` header. If you have not upgraded to Turbo and still use `Turbolinks` in your Gemfile, this means
68+
you will not be able to check if the request was redirected.
69+
70+
```js
71+
const request = new FetchRequest('post', 'localhost:3000/my_endpoint', { body: { name: 'Request.JS' }})
72+
const response = await request.perform()
73+
response.redirected // => will always be false.
74+
```
75+
4776
# License
4877

4978
Rails Request.JS is released under the [MIT License](LICENSE).

src/fetch_request.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { FetchResponse } from './fetch_response'
2+
import { RequestInterceptor } from './request_interceptor'
23
import { getCookie } from './lib/cookie'
34

45
export class FetchRequest {
@@ -9,6 +10,14 @@ export class FetchRequest {
910
}
1011

1112
async perform () {
13+
try {
14+
const requestInterceptor = RequestInterceptor.get()
15+
if (requestInterceptor) {
16+
await requestInterceptor(this)
17+
}
18+
} catch (error) {
19+
console.error(error)
20+
}
1221
const response = new FetchResponse(await window.fetch(this.url, this.fetchOptions))
1322
if (response.unauthenticated && response.authenticationURL) {
1423
return Promise.reject(window.location.href = response.authenticationURL)
@@ -18,6 +27,12 @@ export class FetchRequest {
1827
}
1928
}
2029

30+
addHeader (key, value) {
31+
const headers = this.additionalHeaders
32+
headers[key] = value
33+
this.options.headers = headers
34+
}
35+
2136
get fetchOptions () {
2237
return {
2338
method: this.method.toUpperCase(),

src/fetch_response.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class FetchResponse {
5555
async renderTurboStream () {
5656
if (this.isTurboStream) {
5757
if (window.Turbo) {
58-
Turbo.renderStreamMessage(await this.text)
58+
window.Turbo.renderStreamMessage(await this.text)
5959
} else {
6060
console.warn('You must set `window.Turbo = Turbo` to automatically process Turbo Stream events with request.js')
6161
}

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { FetchRequest } from './fetch_request'
22
import { FetchResponse } from './fetch_response'
3+
import { RequestInterceptor } from './request_interceptor'
34
import { get, post, put, patch, destroy } from './verbs'
45

5-
export { FetchRequest, FetchResponse, get, post, put, patch, destroy }
6+
export { FetchRequest, FetchResponse, RequestInterceptor, get, post, put, patch, destroy }

src/request_interceptor.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export class RequestInterceptor {
2+
static register (interceptor) {
3+
this.interceptor = interceptor
4+
}
5+
6+
static get () {
7+
return this.interceptor
8+
}
9+
10+
static reset () {
11+
this.interceptor = undefined
12+
}
13+
}

0 commit comments

Comments
 (0)