Skip to content

Commit 8c8937d

Browse files
authored
1st argument changes, remove "path" and "url" options, bug fixes (#251)
* first go * cleanup * cleanup * updating documentation * added docs for conditional auto-managed state * cleanup * docs cleanup * cleanup * link to contributions.md * bettering docs * added `skip` option, `skip` tests, fixed bug with `responseType` * forgot to hit save * tests for not overwriting every instance of useFetch global options * tests for path and route without a / * cache cleanup * updating docs * removing conditional auto-managed state from this PR * forgot to remove skip from types.ts
1 parent f7cc214 commit 8c8937d

11 files changed

+265
-441
lines changed

README.md

+20-71
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<br />
1010

1111
<p align="center">
12-
<a href="https://github.com/ava/use-http/pulls">
12+
<a href="https://github.com/ava/use-http/blob/master/.github/contributing.md">
1313
<img src="https://camo.githubusercontent.com/d4e0f63e9613ee474a7dfdc23c240b9795712c96/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5052732d77656c636f6d652d627269676874677265656e2e737667" />
1414
</a>
1515
<a href="https://circleci.com/gh/ava/use-http">
@@ -89,7 +89,6 @@ Usage
8989
### Examples + Videos
9090

9191
- useFetch - managed state, request, response, etc. [![](https://img.shields.io/badge/example-blue.svg)](https://codesandbox.io/s/usefetch-request-response-managed-state-ruyi3?file=/src/index.js) [![](https://img.shields.io/badge/video-red.svg)](https://www.youtube.com/watch?v=_-GujYZFCKI&list=PLZIwrWkE9rCdUybd8t3tY-mUMvXkCdenW&index=6)
92-
- useFetch - route, path, Provider, etc. [![](https://img.shields.io/badge/example-blue.svg)](https://codesandbox.io/s/usefetch-with-provider-c78w2) [![](https://img.shields.io/badge/video-red.svg)](https://www.youtube.com/watch?v=JWDL_AVOYT0&list=PLZIwrWkE9rCdUybd8t3tY-mUMvXkCdenW&index=10)
9392
- useFetch - request/response interceptors [![](https://img.shields.io/badge/example-blue.svg)](https://codesandbox.io/s/usefetch-provider-requestresponse-interceptors-s1lex) [![](https://img.shields.io/badge/video-red.svg)](https://www.youtube.com/watch?v=3HauoWh0Jts&list=PLZIwrWkE9rCdUybd8t3tY-mUMvXkCdenW&index=8)
9493
- useFetch - retries, retryOn, retryDelay [![](https://img.shields.io/badge/example-blue.svg)](https://codesandbox.io/s/usefetch-retryon-retrydelay-s74q9) [![](https://img.shields.io/badge/video-red.svg)](https://www.youtube.com/watch?v=grE3AX-Q9ss&list=PLZIwrWkE9rCdUybd8t3tY-mUMvXkCdenW&index=9)
9594
- useFetch - abort, timeout, onAbort, onTimeout [![](https://img.shields.io/badge/video-red.svg)](https://www.youtube.com/watch?v=7SuD3ZOfu7E&list=PLZIwrWkE9rCdUybd8t3tY-mUMvXkCdenW&index=4)
@@ -101,7 +100,7 @@ Usage
101100
- useFetch - Next.js [![](https://img.shields.io/badge/example-blue.svg)](https://codesandbox.io/s/usefetch-in-nextjs-nn9fm)
102101
- useFetch - create-react-app [![](https://img.shields.io/badge/example-blue.svg)](https://codesandbox.io/embed/km04k9k9x5)
103102

104-
<details open><summary><b>Basic Usage (managed state) <code>useFetch</code></b></summary>
103+
<details open><summary><b>Basic Usage Managed State <code>useFetch</code></b></summary>
105104

106105
If the last argument of `useFetch` is not a dependency array `[]`, then it will not fire until you call one of the http methods like `get`, `post`, etc.
107106

@@ -147,20 +146,18 @@ function Todos() {
147146
148147
</details>
149148
150-
<details><summary><b>Basic Usage (auto managed state) <code>useFetch</code></b></summary>
149+
<details open><summary><b>Basic Usage Auto-Managed State <code>useFetch</code></b></summary>
151150
152-
This fetch is run `onMount/componentDidMount`. The last argument `[]` means it will run `onMount`. If you pass it a variable like `[someVariable]`, it will run `onMount` and again whenever `someVariable` changes values (aka `onUpdate`). **If no method is specified, GET is the default**
151+
This fetch is run `onMount/componentDidMount`. The last argument `[]` means it will run `onMount`. If you pass it a variable like `[someVariable]`, it will run `onMount` and again whenever `someVariable` changes values (aka `onUpdate`). If no method is specified, GET is the default.
153152
154153
```js
155154
import useFetch from 'use-http'
156155

157156
function Todos() {
158-
// accepts all `fetch` options
159-
const options = {
160-
data: [], // setting default for `data` as array instead of undefined
161-
}
162-
163-
const { loading, error, data } = useFetch('https://example.com/todos', options, []) // onMount (GET by default)
157+
const { loading, error, data } = useFetch('https://example.com/todos', {
158+
// these options accept all native `fetch` options
159+
data: [] // defaults the `data` to an array instead of `undefined`
160+
}, []) // <- this [] means it will fire onMount (GET by default)
164161

165162
return (
166163
<>
@@ -179,52 +176,15 @@ function Todos() {
179176
180177
</details>
181178
182-
183-
<details open><summary><b>Basic Usage (auto managed state) with <code>Provider</code></b></summary>
184-
185-
```js
186-
import useFetch, { Provider } from 'use-http'
187-
188-
function Todos() {
189-
const { loading, error, data } = useFetch({
190-
path: '/todos',
191-
data: []
192-
}, []) // onMount
193-
194-
return (
195-
<>
196-
{error && 'Error!'}
197-
{loading && 'Loading...'}
198-
{data.map(todo => (
199-
<div key={todo.id}>{todo.title}</div>
200-
)}
201-
</>
202-
)
203-
}
204-
205-
const App = () => (
206-
<Provider url='https://example.com'>
207-
<Todos />
208-
</Provider>
209-
)
210-
```
211-
212-
<!-- TODO: youtube -->
213-
<a target="_blank" rel="noopener noreferrer" href='https://codesandbox.io/s/usefetch-provider-requestresponse-interceptors-s1lex?file=/src/index.js'><img width='150px' height='30px' src='https://codesandbox.io/static/img/play-codesandbox.svg' /></a>
214-
<!-- <a target="_blank" rel="noopener noreferrer" href='XXXXXXX'><img width='150px' height='30px' src='https://github.com/ava/use-http/raw/master/public/watch-youtube-video.png' /></a> -->
215-
216-
</details>
217-
218-
<details open><summary><b>Suspense Mode (auto managed state)</b></summary>
179+
<details open><summary><b>Suspense Mode<sup>(experimental)</sup> Auto-Managed State</b></summary>
219180
220181
Can put `suspense` in 2 places. Either `useFetch` (A) or `Provider` (B).
221182
222183
```js
223184
import useFetch, { Provider } from 'use-http'
224185

225186
function Todos() {
226-
const { data: todos } = useFetch({
227-
path: '/todos',
187+
const { data: todos } = useFetch('/todos', {
228188
data: [],
229189
suspense: true // A. can put `suspense: true` here
230190
}, []) // onMount
@@ -250,9 +210,9 @@ function App() {
250210
251211
</details>
252212
253-
<details><summary><b>Suspense Mode (managed state)</b></summary>
213+
<details><summary><b>Suspense Mode<sup>(experimental)</sup> Managed State</b></summary>
254214
255-
Can put `suspense` in 2 places. Either `useFetch` (A) or `Provider` (B).
215+
Can put `suspense` in 2 places. Either `useFetch` (A) or `Provider` (B). Suspense mode via managed state is very experimental.
256216
257217
```js
258218
import useFetch, { Provider } from 'use-http'
@@ -328,8 +288,7 @@ import useFetch, { Provider } from 'use-http'
328288
const Todos = () => {
329289
const [page, setPage] = useState(1)
330290

331-
const { data, loading } = useFetch({
332-
path: `/todos?page=${page}&amountPerPage=15`,
291+
const { data, loading } = useFetch(`/todos?page=${page}&amountPerPage=15`, {
333292
onNewData: (currTodos, newTodos) => [...currTodos, ...newTodos], // appends newly fetched todos
334293
perPage: 15, // stops making more requests if last todos fetched < 15
335294
data: []
@@ -430,10 +389,7 @@ var {
430389
431390
<details><summary><b>Relative routes <code>useFetch</code></b></summary>
432391
433-
⚠️ `baseUrl` is no longer supported, it is now only `url`
434392
```jsx
435-
var request = useFetch({ url: 'https://example.com' })
436-
// OR
437393
var request = useFetch('https://example.com')
438394

439395
request.post('/todos', {
@@ -451,17 +407,15 @@ request.post('/todos', {
451407
452408
453409
```jsx
454-
const githubRepos = useFetch({
455-
url: `https://api.github.com/search/repositories?q=`
456-
})
410+
const { get, abort, loading, data: repos } = useFetch('https://api.github.com/search/repositories?q=')
457411

458412
// the line below is not isomorphic, but for simplicity we're using the browsers `encodeURI`
459-
const searchGithubRepos = e => githubRepos.get(encodeURI(e.target.value))
413+
const searchGithubRepos = e => get(encodeURI(e.target.value))
460414

461415
<>
462416
<input onChange={searchGithubRepos} />
463-
<button onClick={githubRepos.abort}>Abort</button>
464-
{githubRepos.loading ? 'Loading...' : githubRepos.data.items.map(repo => (
417+
<button onClick={abort}>Abort</button>
418+
{loading ? 'Loading...' : repos.data.items.map(repo => (
465419
<div key={repo.id}>{repo.name}</div>
466420
))}
467421
</>
@@ -853,7 +807,6 @@ This is exactly what you would pass to the normal js `fetch`, with a little extr
853807
| `onError` | Runs when the request get's an error. If retrying, it is only called on the last retry attempt. | empty function |
854808
| `onNewData` | Merges the current data with the incoming data. Great for pagination. | `(curr, new) => new` |
855809
| `onTimeout` | Called when the request times out. | empty function |
856-
| `path` | When using a global `url` set in the `Provider`, this is useful for adding onto it | `''` |
857810
| `persist` | Persists data for the duration of `cacheLife`. If `cacheLife` is not set it defaults to 24h. Currently only available in Browser. | `false` |
858811
| `perPage` | Stops making more requests if there is no more data to fetch. (i.e. if we have 25 todos, and the perPage is 10, after fetching 2 times, we will have 20 todos. The last 5 tells us we don't have any more to fetch because it's less than 10) For pagination. | `0` |
859812
| `responseType` | This will determine how the `data` field is set. If you put `json` then it will try to parse it as JSON. If you set it as an array, it will attempt to parse the `response` in the order of the types you put in the array. Read about why we don't put `formData` in the defaults [in the yellow Note part here](https://developer.mozilla.org/en-US/docs/Web/API/Body/formData). | `['json', 'text', 'blob', 'readableStream']` |
@@ -862,7 +815,6 @@ This is exactly what you would pass to the normal js `fetch`, with a little extr
862815
| `retryOn` | You can retry on certain http status codes or have custom logic to decide whether to retry or not via a function. Make sure `retries > 0` otherwise it won't retry. | `[]` |
863816
| `suspense` | Enables Experimental React Suspense mode. [example](https://codesandbox.io/s/usefetch-suspense-i22wv) | `false` |
864817
| `timeout` | The request will be aborted/cancelled after this amount of time. This is also the interval at which `retries` will be made at. **in milliseconds**. If set to `0`, it will not timeout except for browser defaults. | `0` |
865-
| `url` | Allows you to set a base path so relative paths can be used for each request :) | empty string |
866818
867819
```jsx
868820
const options = {
@@ -906,9 +858,6 @@ const options = {
906858
// called when the request times out
907859
onTimeout: () => {},
908860

909-
// if you have a global `url` set up, this is how you can add to it
910-
path: '/path/to/your/api',
911-
912861
// this will tell useFetch not to run the request if the list doesn't haveMore. (pagination)
913862
// i.e. if the last page fetched was < 15, don't run the request again
914863
perPage: 15,
@@ -957,9 +906,6 @@ const options = {
957906

958907
// amount of time before the request get's canceled/aborted
959908
timeout: 10000,
960-
961-
// used to be `baseUrl`. You can set your URL this way instead of as the 1st argument
962-
url: 'https://example.com',
963909
}
964910

965911
useFetch(options)
@@ -1001,6 +947,9 @@ If you have feature requests, [submit an issue][1] to let us know what you would
1001947
Todos
1002948
------
1003949
950+
- [ ] prefetching
951+
- [ ] global cache state management
952+
- [ ] optimistic updates
1004953
- [ ] `persist` support for React Native
1005954
- [ ] better loading state management. When using only 1 useFetch in a component and we use
1006955
`Promise.all([get('/todos/1'), get('/todos/2')])` then don't have a loading true,

0 commit comments

Comments
 (0)