Skip to content

Commit b13b284

Browse files
committed
Update readme
1 parent 775697f commit b13b284

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

README.md

+41-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,44 @@
1-
# rails-fetch
1+
# Rails Fetch
2+
3+
Rails Fetch encapsulates the logic to send by default some headers that are required by rails applications like the `X-CSRF-Token`.
4+
5+
# Install
6+
7+
### npm
8+
```
9+
npm i rails-fetch
10+
```
11+
### yarn
12+
```shell
13+
yarn add rails-fetch
14+
```
15+
16+
# How to use
17+
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.
19+
20+
Example:
21+
22+
```js
23+
import { Request } from 'rails-fetch'
24+
25+
....
26+
27+
async myMethod () {
28+
const request = new Request('post', 'localhost:3000/my_endpoint', { body: { name: 'Rails Fetch' }})
29+
const response = await request.perform()
30+
if (response.ok) {
31+
const body = await response.text
32+
// Do whatever do you want with the response body
33+
// You also are able to call `response.html` or `response.json`, be aware that if you call `response.json` and the response contentType isn't `application/json` there will be raised an error.
34+
}
35+
}
36+
```
237

338
# Credits
439

5-
[Basecamp](https://github.com/basecamp), since this piece of code has been extracted from hey.com.
40+
[Basecamp](https://github.com/basecamp), since this piece of code has been extracted from hey.com.
41+
42+
# License
43+
44+
Rails Fetch is released under the [MIT License](LICENSE).

0 commit comments

Comments
 (0)