|
30 | 30 | <hr> |
31 | 31 | <br> |
32 | 32 |
|
33 | | -## Documentation |
| 33 | +## Contents |
34 | 34 |
|
35 | 35 | Just follow links below to get an overview of library features. |
36 | | -- [Documentation](#documentation) |
| 36 | +- [Contents](#contents) |
37 | 37 | - [Getting started](#getting-started) |
38 | 38 | - [Provider](#provider) |
39 | 39 | - [Http client](#http-client) |
@@ -119,7 +119,7 @@ Below the complete set of options you can provide to the `HttpClientConfigProvid |
119 | 119 | <br> |
120 | 120 |
|
121 | 121 | ## 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`: |
123 | 123 |
|
124 | 124 | ```js |
125 | 125 | import { useHttpClient } from 'react-http-fetch'; |
@@ -168,20 +168,119 @@ export default App; |
168 | 168 | The complete *public API* exposed by the hook: |
169 | 169 | | Method | Description | Params | Return | |
170 | 170 | | --------------------- | --------------------------------------------------------------------------| --------------------- | --------------------- | |
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) |
175 | 175 |
|
176 | 176 | ### 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. | |
177 | 183 |
|
178 | 184 | ### 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. |
179 | 186 |
|
180 | 187 | ### Abortable request return |
| 188 | +| Value | Type | |
| 189 | +| ----- | ---- | |
| 190 | +|[request, abortController]|[[RequestReturn](#request-return), [AbortController](https://developer.mozilla.org/en-US/docs/Web/API/AbortController)| |
181 | 191 |
|
182 | 192 | ### Example – 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 | +``` |
183 | 250 |
|
184 | 251 | ### Example – 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 | +``` |
185 | 284 |
|
186 | 285 | <br> |
187 | 286 |
|
|
0 commit comments