Skip to content

Commit 0f0dbe4

Browse files
authored
adds 5 shorthand functions for request verbs get, post, put, patch, destroy (#10)
close #9 This PR adds shorthand functions to directly call one specific HTTP request verb. 5 new functions are available : get, post, put, patch, 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) { const body = await response.text ... } } ``` ### DELETE -> `destroy` `delete` is a javascript reserved word so for this shorthand function I used `destroy`
1 parent 72bb933 commit 0f0dbe4

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,26 @@ async myMethod () {
3535
}
3636
```
3737

38+
#### Shorthand methods
39+
40+
Alternatively, you can use a shorthand version for the main HTTP verbs, `get`, `post`, `put`, `patch` or `destroy`.
41+
42+
Example:
43+
44+
```js
45+
import { get, post, put, patch, destroy } from '@rails/request.js'
46+
47+
...
48+
49+
async myMethod () {
50+
const response = await post('localhost:3000/my_endpoint', { body: { name: 'Request.JS' }})
51+
if (response.ok) {
52+
...
53+
}
54+
}
55+
```
56+
57+
3858
#### Turbo Streams
3959

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

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'
33
import { RequestInterceptor } from './request_interceptor'
4+
import { get, post, put, patch, destroy } from './verbs'
45

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

src/verbs.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { FetchRequest } from './fetchRequest'
2+
3+
async function get (url, options) {
4+
const response = new FetchRequest('get', url, options)
5+
return response.perform()
6+
}
7+
8+
async function post (url, options) {
9+
const response = new FetchRequest('post', url, options)
10+
return response.perform()
11+
}
12+
13+
async function put (url, options) {
14+
const response = new FetchRequest('put', url, options)
15+
return response.perform()
16+
}
17+
18+
async function patch (url, options) {
19+
const response = new FetchRequest('patch', url, options)
20+
return response.perform()
21+
}
22+
23+
async function destroy (url, options) {
24+
const response = new FetchRequest('delete', url, options)
25+
return response.perform()
26+
}
27+
28+
export { get, post, put, patch, destroy }

0 commit comments

Comments
 (0)