Skip to content
This repository was archived by the owner on Aug 3, 2023. It is now read-only.

Commit 515312b

Browse files
authored
Merge pull request #130 from jmeas/2.0.0
2.0.0
2 parents cf188ac + 83d297c commit 515312b

File tree

9 files changed

+399
-87
lines changed

9 files changed

+399
-87
lines changed

.eslintrc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99
}
1010
},
1111
"rules": {
12-
"no-unused-vars": "error"
12+
"no-unused-vars": "error",
13+
"react/jsx-uses-react": "error",
14+
"react/jsx-uses-vars": "error"
1315
},
1416
"env": {
1517
"browser": true,
1618
"node": true
1719
},
1820
"globals": {
1921
"Promise": true
20-
}
22+
},
23+
"plugins": ["react"]
2124
}

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
# Changelog
22

3+
### v2.0.0 (2018/2/17)
4+
5+
**Breaking**
6+
7+
* `transformResponse` has been renamed to be `transformData`
8+
* `fetchPolicy` is now determined by the method that you pass in. This change was made to support using
9+
POST methods for read requests, and is unlikely to break your code.
10+
* A new prop, `cacheResponse`, is used to determine if a response is added to the cache or
11+
not. This is to support using POST methods for read requests, and is unlikely to break your code.
12+
13+
**New Features**
14+
15+
* A new `failed` property is passed to you in the render prop callback. This allows you to
16+
quickly determine if a request failed for any reason (be it network errors or "error" status
17+
codes).
18+
319
### v1.1.0 (2018/2/7)
420

521
**New Features**

README.md

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,10 @@ The [render prop](https://cdb.reacttraining.com/use-a-render-prop-50de598f11ce)
198198
It is called with one argument, `result`, an object with the following keys:
199199

200200
* `fetching`: A Boolean representing whether or not a request is currently in flight for this component
201-
* `error`: A Boolean representing if a network error occurred. Note that HTTP "error" status codes do not
202-
cause `error` to be `true`; only failed or aborted network requests do. For more, see the
201+
* `failed`: A Boolean representing whether or not the request failed for any reason. This includes network
202+
errors and status codes that are greater than or equal to`400`.
203+
* `error`: An error object representing a network error occurred. Note that HTTP "error" status codes do not
204+
cause errors; only failed or aborted network requests do. For more, see the
203205
["Using Fetch" MDN guide](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Checking_that_the_fetch_was_successful).
204206
* `response`: An instance of [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response). The
205207
[`body`](https://developer.mozilla.org/en-US/docs/Web/API/Body) will already be read, and made
@@ -299,15 +301,15 @@ response is from the cache or from a network request. Receives two arguments:
299301
</Fetch>
300302
```
301303

302-
##### `transformResponse`
304+
##### `transformData`
303305

304306
A function that is called with the data returned from the response. You can use this
305307
hook to transform the data before it is passed into `children`.
306308

307309
```jsx
308310
<Fetch
309311
url="/posts/2"
310-
transformResponse={data => data.post>
312+
transformData={data => data.post>
311313
{({ fetching, error, response, data }) => {
312314
<div>
313315
{fetching && ('Loading...')}
@@ -323,7 +325,7 @@ hook to transform the data before it is passed into `children`.
323325
</Fetch>
324326
```
325327
326-
> Note: `transformResponse` does not modify the value of `response.data`. The transformed data is
328+
> Note: `transformData` does not modify the value of `response.data`. The transformed data is
327329
> made available to you in the render prop argument under the `data` key.
328330
329331
##### `responseType`
@@ -395,11 +397,30 @@ This determines how the request interacts with the cache. Valid options are:
395397
* `"network-only"`
396398
* `"cache-only"`
397399
398-
For documentation on this prop, refer to the [response caching guide](./docs/guides/response-caching.md).
400+
For documentation on what each of these values do, refer to the [response caching guide](./docs/guides/response-caching.md).
401+
402+
The default value of this prop is based on the value of the `method` prop that you pass to `<Fetch/>`.
403+
404+
| Method | Default value |
405+
| ------------------------ | ---------------- |
406+
| GET, HEAD, OPTIONS | `"cache-first"` |
407+
| POST, PUT, PATCH, DELETE | `"network-only"` |
399408
400409
> This prop behaves identically to the Apollo prop
401410
> [with the same name](https://www.apollographql.com/docs/react/basics/queries.html#graphql-config-options-fetchPolicy).
402411
412+
##### `cacheResponse`
413+
414+
Whether or not the response will be cached. The default value is based on the value of the `method` prop that you pass
415+
to `<Fetch/>`.
416+
417+
| Method | Default value |
418+
| ------------------------ | ------------- |
419+
| GET, HEAD, OPTIONS | `true` |
420+
| POST, PUT, PATCH, DELETE | `false` |
421+
422+
For documentation on this prop, refer to the [response caching guide](./docs/guides/response-caching.md).
423+
403424
##### `dedupe`
404425
405426
A Boolean value representing whether or not the request should be

docs/guides/best-practices.md

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,56 @@ Here are some tips that may help you when using React Request.
44

55
### Handling errors
66

7-
It may seem like handling errors is as simple as checking for an `error` object
8-
within the render callback, but the `error` object is only included as an
9-
argument in the following situations:
7+
The `failed` Boolean is passed to the `children` callback, which
8+
represents whether _any_ error occurred with the request. This includes
9+
network requests or status codes greater than or equal to 400.
1010

11-
1. A network error occurred, such as a timeout or a loss of network connection
12-
2. A new request "aborted" the previous one (meaning that the component
13-
will ignore the response of the earlier request)
11+
This Boolean is convenient for a coarse understanding that the network
12+
failed, but using this Boolean alone is typically not enough to provide
13+
a great user experience. A user may want to know why the request
14+
failed. Was the resource not found? Did the user submit bad information?
15+
Was there a network error? Was the user logged out?
1416

15-
There are other situations when most developers typically the component
16-
to be in an error state, such as when a 404 is returned.
17+
We encourage you to dig into the `error` and `response` objects
18+
to provide your users with a more detailed explanation of what went wrong,
19+
rather than displaying a generic "There was an error" message.
1720

18-
The best way to cover all situations is by _also_ looking at the `response.ok` value.
19-
This is a Boolean that is `false` anytime that the `status` of the response
20-
is `>= 400`. So this will catch other errors such as Not Found errors, Unauthorized errors,
21-
and other client and server errors.
22-
23-
Together, checking for `error` and `response.ok` should cover all possible
24-
situations when a request is "unsuccessful." The following example demonstrates
25-
this.
21+
Here is an example that shows the different kinds of ways that a response
22+
can error.
2623

2724
```js
2825
<Fetch {...fetchProps}>
29-
{({ error, response }) => {
30-
if (error || (response && !response.ok)) {
31-
console.log('There was some kind of error.');
32-
} else {
33-
console.log('The request is either loading or it succeeded');
26+
{({ failed, error, response }) => {
27+
if (failed) {
28+
console.log('There was _some_ kind of error. What happened?');
29+
}
30+
31+
if (error) {
32+
console.log('There was a network error');
33+
34+
if (navigation.onLine) {
35+
console.log('The user lost internet connection.');
36+
} else {
37+
// You can look at the Error to learn more.
38+
console.log('The request was aborted, or it timed out');
39+
}
40+
}
41+
42+
const status = response && response.status;
43+
44+
if (status === 404) {
45+
console.log('The resource was not found.');
46+
} else if (status === 401) {
47+
console.log('You user have been logged out.');
48+
} else if (status === 400) {
49+
console.log('Invalid data was submitted');
50+
} else if (status === 500) {
51+
console.log('Something went wrong on the server.');
3452
}
3553
}}
3654
</Fetch>
3755
```
3856

39-
> Note: you can explain what has gone wrong to the user in greater detail by looking
40-
> at properties on the `error` object or the `response` object.
41-
4257
### Making "fetch components"
4358

4459
HTTP requests require a lot of configuration, which can make your

docs/guides/response-caching.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
# Response Caching
22

33
React Request has a built-in response caching system. Interactions with the
4-
cache are configurable with the `fetchPolicy` prop.
4+
cache are configurable with the `fetchPolicy` and `cacheResponse` prop.
55

66
The way the cache works is like this: any time a response from the server is received,
77
it will be cached using the request's [request key](./request-keys.md). Subsequent
88
requests are matched with existing cached server responses using _their_ request key.
99

10+
The `cacheResponse` prop determines if server responses will be cached. Typically,
11+
you only want to cache responses for "read" requests. Accordingly, the default
12+
value is based on the value of the `method` prop:
13+
14+
| Method | Default value |
15+
| ------------------------ | ------------- |
16+
| GET, HEAD, OPTIONS | `true` |
17+
| POST, PUT, PATCH, DELETE | `false` |
18+
1019
There are four ways that a `<Fetch/>` component can interact with the
11-
cache.
20+
cached responses, which are configurable with the `fetchPolicy` prop:
1221

1322
### `cache-first`
1423

@@ -34,3 +43,31 @@ The cache is ignored, and a network request is always made.
3443

3544
If a response exists in the cache, then it will be returned. If no response
3645
exists in the cache, then an error will be passed into the render prop function.
46+
47+
---
48+
49+
Like `cacheResponse`, the default value of `fetchPolicy` is based on the method that you pass.
50+
51+
| Method | Default value |
52+
| ------------------------ | ---------------- |
53+
| GET, HEAD, OPTIONS | `"cache-first"` |
54+
| POST, PUT, PATCH, DELETE | `"network-only"` |
55+
56+
### Using `POST` for read requests
57+
58+
Some APIs use the `POST` method for read requests. React Request supports this, but you will
59+
need to manually configure the cache. This may look something like this:
60+
61+
```jsx
62+
<Fetch
63+
method="post"
64+
url="/books/2"
65+
cacheResponse
66+
fetchPolicy="cache-first"
67+
lazy={false}
68+
/>
69+
```
70+
71+
With the above configuration, responses will be stored in the cache, and requests will
72+
only be made when the cache is empty. Also note that the [lazy prop](./using-the-lazy-prop.md)
73+
is set to `false` so that the request fires when the component mounts.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"name": "react-request",
3-
"version": "1.1.0",
3+
"version": "2.0.0",
44
"description": "Declarative HTTP requests with React.",
55
"main": "lib/index.js",
66
"module": "es/index.js",
77
"scripts": {
88
"clean": "rimraf dist es tmp lib",
99
"test": "npm run lint && npm run test:unit",
1010
"test:unit": "jest",
11-
"lint": "eslint src",
11+
"lint": "eslint src test",
1212
"prepublish": "in-publish && npm run build || not-in-publish",
1313
"build": "npm run clean && npm run build:umd && npm run build:umd:min && npm run build:es && npm run build:commonjs",
1414
"build:commonjs": "cross-env BABEL_ENV=commonjs babel src --out-dir lib",
@@ -66,6 +66,7 @@
6666
"enzyme": "^3.3.0",
6767
"enzyme-adapter-react-16": "^1.1.1",
6868
"eslint": "^4.17.0",
69+
"eslint-plugin-react": "^7.6.1",
6970
"fetch-mock": "^6.0.0",
7071
"in-publish": "^2.0.0",
7172
"jest": "^22.1.4",

0 commit comments

Comments
 (0)