Skip to content

Commit a25ca63

Browse files
author
nebarf
committed
Http client doc (followup)
1 parent 808ff9e commit a25ca63

File tree

2 files changed

+111
-12
lines changed

2 files changed

+111
-12
lines changed

CONTRIBUTING.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ Contributing
2121
```sh
2222
$ cd ..
2323
$ create-react-app react-http-fetch-sandbox
24-
$ cd react-http-sandbox
24+
$ cd react-http-fetch-sandbox
2525
$ yarn
2626
```
2727
6. Link the local library to the sanbox:
2828
```sh
29-
$ yarnk link react-http-fetch
29+
$ yarn link react-http-fetch
3030
```
3131
7. Link react peer deps to local library
3232
```sh
3333
$ cd ../react-http-fetch
34-
$ yarnk link ../react-http-fetch-sandbox/node_modules/react
35-
$ yarnk link ../react-http-fetch-sandbox/node_modules/react-dom
34+
$ yarn link ../react-http-fetch-sandbox/node_modules/react
35+
$ yarn link ../react-http-fetch-sandbox/node_modules/react-dom
3636
```
3737
8. Start library build in watch mode:
3838
```sh
@@ -50,4 +50,4 @@ Contributing
5050
Bu sure to cover any changes to the source code by integrating the tests suite.
5151

5252
## **Submit your changes**
53-
Push your changes against your forke repo and make a PR against `nebarf/react-http-fetch:main`.
53+
Push your changes against your forked repo and make a PR against `nebarf/react-http-fetch:main`.

README.md

Lines changed: 106 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030
<hr>
3131
<br>
3232

33-
## Documentation
33+
## Contents
3434

3535
Just follow links below to get an overview of library features.
36-
- [Documentation](#documentation)
36+
- [Contents](#contents)
3737
- [Getting started](#getting-started)
3838
- [Provider](#provider)
3939
- [Http client](#http-client)
@@ -119,7 +119,7 @@ Below the complete set of options you can provide to the `HttpClientConfigProvid
119119
<br>
120120

121121
## Http client
122-
The `useHttpClient` hook return a set of method to perform http requests. The `request` function is the lowest level one, all other exposed functions are just decorators around it. Below a basic example using `request`:
122+
The `useHttpClient` hook return a set of methods to perform http requests. The `request` function is the lowest level one, all other exposed functions are just decorators around it. Below a basic example using `request`:
123123

124124
```js
125125
import { useHttpClient } from 'react-http-fetch';
@@ -168,20 +168,119 @@ export default App;
168168
The complete *public API* exposed by the hook:
169169
| Method | Description | Params | Return |
170170
| --------------------- | --------------------------------------------------------------------------| --------------------- | --------------------- |
171-
|request | The lowest level method to perform a http request | params| return
172-
| <ul class="httpRequestsList"><li>get</li><li>post</li><li>put</li><li>patch</li><li>delete</li></ul> | Make use of lower level method `request` by just overriding the http method ([example](#example--abortable-request)) | params | return
173-
| abortableRequest | The lowest level method to perform an abortable http request | params | return
174-
| <ul class="httpRequestsList"><li>abortableGet</li><li>abortablePost</li><li>abortablePut</li><li>abortablePatch</li><li>abortableDelete</li></ul> | Make use of lower level method `abortableRequest` by just overriding the http method ([example](#example--abortable-request)) | params | return
171+
|request | The lowest level method to perform a http request | [Request params](#request-params) | [Request return](#request-return)
172+
| <ul class="httpRequestsList"><li>get</li><li>post</li><li>put</li><li>patch</li><li>delete</li></ul> | Make use of lower level method `request` by just overriding the http method ([example](#example--abortable-request)) | [Request params](#request-params) | [Request return](#request-return)
173+
| abortableRequest | The lowest level method to perform an abortable http request ([example](#example--abortable-request)) | [Request params](#request-params) | [Abortable request return](#abortable-request-return)
174+
| <ul class="httpRequestsList"><li>abortableGet</li><li>abortablePost</li><li>abortablePut</li><li>abortablePatch</li><li>abortableDelete</li></ul> | Make use of lower level method `abortableRequest` by just overriding the http method | [Request params](#request-params) | [Abortable request return](#abortable-request-return)
175175

176176
### Request params
177+
| Parameter | Type | Description |
178+
| --------- | ---- | ----------- |
179+
| baseUrlOverride | string | The base url of the request. If provided, it would override the [provider](#provider) base url.
180+
| relativeUrl | string | The url relative to the base one (e.g. posts/1).
181+
| parser | [HttpResponseParser](src/client/types.ts) | An optional response parser that would override the [provider](#provider) global one. |
182+
| requestOptions | [HttpRequestOptions](./src/client/types.ts) | The options carried by the fetch request. |
177183

178184
### Request return
185+
The return value of native JS fetch. If a custom response parser (see [Provider](#provider)) is provided then the return value corresponds to the parsed one.
179186

180187
### Abortable request return
188+
| Value | Type |
189+
| ----- | ---- |
190+
|[request, abortController]|[[RequestReturn](#request-return), [AbortController](https://developer.mozilla.org/en-US/docs/Web/API/AbortController)|
181191

182192
### Example &ndash; Abortable request
193+
```js
194+
import React, { useState, useRef } from 'react';
195+
import { useHttpClient } from 'react-http-fetch';
196+
197+
function App() {
198+
const { abortableRequest } = useHttpClient();
199+
const abortCtrlRef = useRef();
200+
201+
const [todo, setTodo] = useState();
202+
203+
const fetchTodo = async () => {
204+
const [reqPromise, abortController] = abortableRequest({
205+
baseUrlOverride: 'https://jsonplaceholder.typicode.com',
206+
relativeUrl: 'todos/1',
207+
});
208+
abortCtrlRef.current = abortController;
209+
210+
try {
211+
const res = await reqPromise;
212+
setTodo(res);
213+
} catch (error) {
214+
// Abort the request will cause the request promise to be rejected with the following error:
215+
// "DOMException: The user aborted a request."
216+
console.error(error);
217+
} finally {
218+
abortCtrlRef.current = undefined;
219+
}
220+
};
221+
222+
const abortPendingRequest = () => {
223+
if (abortCtrlRef.current) {
224+
abortCtrlRef.current.abort();
225+
}
226+
};
227+
228+
return (
229+
<div style={{ margin: '20px' }}>
230+
<div>{`Todo name: ${todo && todo.title}`}</div>
231+
<button
232+
style={{ marginRight: '10px' }}
233+
type="button"
234+
onClick={fetchTodo}
235+
>
236+
Do request
237+
</button>
238+
<button
239+
type="button"
240+
onClick={abortPendingRequest}
241+
>
242+
Abort
243+
</button>
244+
</div>
245+
);
246+
}
247+
248+
export default App;
249+
```
183250

184251
### Example &ndash; Get request
252+
```js
253+
import React, { useState, useEffect } from 'react';
254+
import { useHttpClient } from 'react-http-fetch';
255+
256+
257+
function App() {
258+
const { get } = useHttpClient();
259+
260+
const [todo, setTodo] = useState();
261+
262+
useEffect(
263+
() => {
264+
const fetchTodo = async () => {
265+
const res = await get({
266+
baseUrlOverride: 'https://jsonplaceholder.typicode.com',
267+
relativeUrl: 'todos/1',
268+
});
269+
setTodo(res);
270+
};
271+
272+
fetchTodo();
273+
},
274+
[get]
275+
);
276+
277+
return (
278+
<div>{`Todo name: ${todo && todo.title}`}</div>
279+
);
280+
}
281+
282+
export default App;
283+
```
185284

186285
<br>
187286

0 commit comments

Comments
 (0)