Skip to content

Commit 5c74920

Browse files
committed
Merge branch 'shorthand-verbs' of https://github.com/adrienpoly/request.js into shorthand-verbs
2 parents 0c6a469 + 7ddd9ee commit 5c74920

File tree

6 files changed

+89
-12
lines changed

6 files changed

+89
-12
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: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ yarn add @rails/request.js
1515

1616
# How to use
1717

18-
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.
18+
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.
1919

2020
Example:
2121

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

2525
....
2626

2727
async myMethod () {
28-
const request = new Request('post', 'localhost:3000/my_endpoint', { body: { name: 'Request.JS' }})
28+
const request = new FetchRequest('post', 'localhost:3000/my_endpoint', { body: { name: 'Request.JS' }})
2929
const response = await request.perform()
3030
if (response.ok) {
3131
const body = await response.text
@@ -64,6 +64,35 @@ import { Turbo } from "@hotwired/turbo-rails"
6464
window.Turbo = Turbo
6565
```
6666

67+
#### Request Interceptor
68+
69+
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:
70+
71+
```javascript
72+
import { RequestInterceptor } from '@rails/request.js'
73+
// ...
74+
75+
// Set interceptor
76+
RequestInterceptor.register(async (request) => {
77+
const token = await getSessionToken(window.app)
78+
request.addHeader('Authorization', `Bearer ${token}`)
79+
})
80+
81+
// Reset interceptor
82+
RequestInterceptor.reset()
83+
```
84+
85+
# Known Issues
86+
87+
`FetchRequest` sets a `"X-Requested-With": "XmlHttpRequest"` header. If you have not upgraded to Turbo and still use `Turbolinks` in your Gemfile, this means
88+
you will not be able to check if the request was redirected.
89+
90+
```js
91+
const request = new FetchRequest('post', 'localhost:3000/my_endpoint', { body: { name: 'Request.JS' }})
92+
const response = await request.perform()
93+
response.redirected // => will always be false.
94+
```
95+
6796
# License
6897

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

src/request.js renamed to src/fetch_request.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
1-
import { Response } from './response'
1+
import { FetchResponse } from './fetch_response'
2+
import { RequestInterceptor } from './request_interceptor'
23
import { getCookie } from './lib/cookie'
34

4-
export class Request {
5+
export class FetchRequest {
56
constructor (method, url, options = {}) {
67
this.method = method
78
this.url = url
89
this.options = options
910
}
1011

1112
async perform () {
12-
const response = new Response(await window.fetch(this.url, this.fetchOptions))
13+
try {
14+
const requestInterceptor = RequestInterceptor.get()
15+
if (requestInterceptor) {
16+
await requestInterceptor(this)
17+
}
18+
} catch (error) {
19+
console.error(error)
20+
}
21+
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)
1524
} else {
@@ -18,6 +27,12 @@ export class Request {
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/response.js renamed to src/fetch_response.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export class Response {
1+
export class FetchResponse {
22
constructor (response) {
33
this.response = response
44
}
@@ -55,7 +55,7 @@ export class Response {
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: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { Request } from './request'
2-
import { Response } from './response'
3-
import {get, post, put, patch, destroy} from './verbs'
1+
import { FetchRequest } from './fetch_request'
2+
import { FetchResponse } from './fetch_response'
3+
import { RequestInterceptor } from './request_interceptor'
4+
import { get, post, put, patch, destroy } from './verbs'
45

5-
export { Request, Response, 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)